2 #include <Audio/AudioInterface.hpp>
3 #include <Audio/PortAudioInterface.hpp>
4 #include <Audio/Settings/Model.hpp>
5 #include <Audio/Settings/View.hpp>
7 #include <score/command/Dispatchers/SettingsCommandDispatcher.hpp>
8 #include <score/tools/Bind.hpp>
9 #include <score/widgets/SignalUtils.hpp>
12 #include <QFormLayout>
13 #if __has_include(<pa_mac_core.h>)
14 #include <pa_mac_core.h>
19 #if __has_include(<pa_mac_core.h>)
20 class CoreAudioFactory final
24 SCORE_CONCRETE(
"e75cb711-613f-4f15-834f-398ab1807470")
26 std::vector<PortAudioCard> devices;
28 CoreAudioFactory() { rescan(); }
30 ~CoreAudioFactory()
override { }
31 bool available() const noexcept
override {
return true; }
35 auto device_in = ossia::find_if(devices, [&](
const PortAudioCard& dev) {
36 return dev.raw_name == set.getCardIn() && dev.hostapi != paInDevelopment;
38 auto device_out = ossia::find_if(devices, [&](
const PortAudioCard& dev) {
39 return dev.raw_name == set.getCardOut() && dev.hostapi != paInDevelopment;
42 if(device_in == devices.end() || device_out == devices.end())
44 auto default_device_in = ossia::find_if(devices, [&](
const PortAudioCard& dev) {
45 return dev.inputChan > 0 && dev.defaultDevice;
47 auto default_device_out = ossia::find_if(devices, [&](
const PortAudioCard& dev) {
48 return dev.outputChan > 0 && dev.defaultDevice;
51 if(default_device_in != devices.end())
53 set.setCardIn(default_device_in->raw_name);
54 set.setDefaultIn(default_device_in->inputChan);
58 set.setCardIn(devices.back().raw_name);
59 set.setDefaultIn(devices.back().inputChan);
62 if(default_device_out != devices.end())
64 set.setCardOut(default_device_out->raw_name);
65 set.setDefaultOut(default_device_out->outputChan);
66 set.setRate(default_device_out->rate);
70 set.setCardOut(devices.back().raw_name);
71 set.setDefaultOut(devices.back().outputChan);
72 set.setRate(devices.back().rate);
79 if(device_out != devices.end())
81 set.setDefaultIn(device_out->inputChan);
82 set.setDefaultOut(device_out->outputChan);
83 set.setRate(device_out->rate);
93 PortAudioScope portaudio;
95 devices.push_back(PortAudioCard{{}, {}, QObject::tr(
"No device"), -1, 0, 0, {}});
96 for(
int i = 0; i < Pa_GetHostApiCount(); i++)
98 auto hostapi = Pa_GetHostApiInfo(i);
99 if(hostapi->type == PaHostApiTypeId::paCoreAudio)
101 for(
int card = 0; card < hostapi->deviceCount; card++)
103 auto dev_idx = Pa_HostApiDeviceIndexToDeviceIndex(i, card);
104 auto dev = Pa_GetDeviceInfo(dev_idx);
105 auto raw_name = QString::fromUtf8(Pa_GetDeviceInfo(dev_idx)->name);
107 devices.push_back(PortAudioCard{
108 "CoreAudio", raw_name, raw_name, dev_idx, dev->maxInputChannels,
109 dev->maxOutputChannels, hostapi->type, dev->defaultSampleRate,
110 hostapi->defaultInputDevice == dev_idx
111 || hostapi->defaultOutputDevice == dev_idx});
117 QString prettyName()
const override {
return QObject::tr(
"CoreAudio"); }
118 std::shared_ptr<ossia::audio_engine> make_engine(
121 return std::make_shared<ossia::portaudio_engine>(
122 "ossia score", set.getCardIn().toStdString(), set.getCardOut().toStdString(),
123 set.getDefaultIn(), set.getDefaultOut(), set.getRate(), set.getBufferSize(),
127 void setCard(QComboBox* combo, QString val)
129 auto dev_it = ossia::find_if(
130 devices, [&](
const PortAudioCard& d) {
return d.raw_name == val; });
131 if(dev_it != devices.end())
133 combo->setCurrentIndex(dev_it->out_index);
137 QWidget* make_settings(
141 auto w =
new QWidget{parent};
142 auto lay =
new QFormLayout{w};
144 auto card_list =
new QComboBox{w};
147 card_list->addItem(devices.front().pretty_name, 0);
148 devices.front().out_index = 0;
151 for(std::size_t i = 1; i < devices.size(); i++)
153 auto& card = devices[i];
154 card_list->addItem(card.pretty_name, (
int)i);
155 card.out_index = card_list->count() - 1;
161 lay->addRow(QObject::tr(
"Device"), card_list);
163 auto update_dev = [=, &m, &m_disp](
const PortAudioCard& dev) {
164 if(dev.raw_name != m.getCardOut())
166 m_disp.submitDeferredCommand<Audio::Settings::SetModelCardIn>(m, dev.raw_name);
167 m_disp.submitDeferredCommand<Audio::Settings::SetModelCardOut>(
169 m_disp.submitDeferredCommand<Audio::Settings::SetModelDefaultIn>(
171 m_disp.submitDeferredCommand<Audio::Settings::SetModelDefaultOut>(
177 card_list, SignalUtils::QComboBox_currentIndexChanged_int(), &v, [=](
int i) {
178 auto& device = devices[card_list->itemData(i).toInt()];
182 if(m.getCardOut().isEmpty())
186 update_dev(devices.front());
191 setCard(card_list, m.getCardOut());
195 addBufferSizeWidget(*w, m, v);
196 addSampleRateWidget(*w, m, v);
198 con(m, &Model::changed, w, [=, &m] { setCard(card_list, m.getCardOut()); });
Definition: score-plugin-audio/Audio/Settings/Model.hpp:22
Definition: score-plugin-audio/Audio/Settings/View.hpp:19
Definition: SettingsCommandDispatcher.hpp:10
Used to access all the application-wide state and structures.
Definition: ApplicationContext.hpp:24