The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1121 lines
39KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class SimpleDeviceManagerInputLevelMeter : public Component,
  19. public Timer
  20. {
  21. public:
  22. SimpleDeviceManagerInputLevelMeter (AudioDeviceManager* const manager_)
  23. : manager (manager_),
  24. level (0)
  25. {
  26. startTimer (50);
  27. manager->enableInputLevelMeasurement (true);
  28. }
  29. ~SimpleDeviceManagerInputLevelMeter()
  30. {
  31. manager->enableInputLevelMeasurement (false);
  32. }
  33. void timerCallback()
  34. {
  35. const float newLevel = (float) manager->getCurrentInputLevel();
  36. if (std::abs (level - newLevel) > 0.005f)
  37. {
  38. level = newLevel;
  39. repaint();
  40. }
  41. }
  42. void paint (Graphics& g)
  43. {
  44. getLookAndFeel().drawLevelMeter (g, getWidth(), getHeight(),
  45. (float) exp (log (level) / 3.0)); // (add a bit of a skew to make the level more obvious)
  46. }
  47. private:
  48. AudioDeviceManager* const manager;
  49. float level;
  50. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleDeviceManagerInputLevelMeter)
  51. };
  52. //==============================================================================
  53. class AudioDeviceSelectorComponent::MidiInputSelectorComponentListBox : public ListBox,
  54. private ListBoxModel
  55. {
  56. public:
  57. //==============================================================================
  58. MidiInputSelectorComponentListBox (AudioDeviceManager& deviceManager_,
  59. const String& noItemsMessage_,
  60. const int minNumber_,
  61. const int maxNumber_)
  62. : ListBox (String::empty, nullptr),
  63. deviceManager (deviceManager_),
  64. noItemsMessage (noItemsMessage_),
  65. minNumber (minNumber_),
  66. maxNumber (maxNumber_)
  67. {
  68. items = MidiInput::getDevices();
  69. setModel (this);
  70. setOutlineThickness (1);
  71. }
  72. int getNumRows()
  73. {
  74. return items.size();
  75. }
  76. void paintListBoxItem (int row,
  77. Graphics& g,
  78. int width, int height,
  79. bool rowIsSelected)
  80. {
  81. if (isPositiveAndBelow (row, items.size()))
  82. {
  83. if (rowIsSelected)
  84. g.fillAll (findColour (TextEditor::highlightColourId)
  85. .withMultipliedAlpha (0.3f));
  86. const String item (items [row]);
  87. bool enabled = deviceManager.isMidiInputEnabled (item);
  88. const int x = getTickX();
  89. const float tickW = height * 0.75f;
  90. getLookAndFeel().drawTickBox (g, *this, x - tickW, (height - tickW) / 2, tickW, tickW,
  91. enabled, true, true, false);
  92. g.setFont (height * 0.6f);
  93. g.setColour (findColour (ListBox::textColourId, true).withMultipliedAlpha (enabled ? 1.0f : 0.6f));
  94. g.drawText (item, x, 0, width - x - 2, height, Justification::centredLeft, true);
  95. }
  96. }
  97. void listBoxItemClicked (int row, const MouseEvent& e)
  98. {
  99. selectRow (row);
  100. if (e.x < getTickX())
  101. flipEnablement (row);
  102. }
  103. void listBoxItemDoubleClicked (int row, const MouseEvent&)
  104. {
  105. flipEnablement (row);
  106. }
  107. void returnKeyPressed (int row)
  108. {
  109. flipEnablement (row);
  110. }
  111. void paint (Graphics& g)
  112. {
  113. ListBox::paint (g);
  114. if (items.size() == 0)
  115. {
  116. g.setColour (Colours::grey);
  117. g.setFont (13.0f);
  118. g.drawText (noItemsMessage,
  119. 0, 0, getWidth(), getHeight() / 2,
  120. Justification::centred, true);
  121. }
  122. }
  123. int getBestHeight (const int preferredHeight)
  124. {
  125. const int extra = getOutlineThickness() * 2;
  126. return jmax (getRowHeight() * 2 + extra,
  127. jmin (getRowHeight() * getNumRows() + extra,
  128. preferredHeight));
  129. }
  130. private:
  131. //==============================================================================
  132. AudioDeviceManager& deviceManager;
  133. const String noItemsMessage;
  134. StringArray items;
  135. int minNumber, maxNumber;
  136. void flipEnablement (const int row)
  137. {
  138. if (isPositiveAndBelow (row, items.size()))
  139. {
  140. const String item (items [row]);
  141. deviceManager.setMidiInputEnabled (item, ! deviceManager.isMidiInputEnabled (item));
  142. }
  143. }
  144. int getTickX() const
  145. {
  146. return getRowHeight() + 5;
  147. }
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInputSelectorComponentListBox)
  149. };
  150. //==============================================================================
  151. struct AudioDeviceSetupDetails
  152. {
  153. AudioDeviceManager* manager;
  154. int minNumInputChannels, maxNumInputChannels;
  155. int minNumOutputChannels, maxNumOutputChannels;
  156. bool useStereoPairs;
  157. };
  158. //==============================================================================
  159. class AudioDeviceSettingsPanel : public Component,
  160. private ChangeListener,
  161. private ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  162. private ButtonListener
  163. {
  164. public:
  165. AudioDeviceSettingsPanel (AudioIODeviceType* type_,
  166. AudioDeviceSetupDetails& setup_,
  167. const bool hideAdvancedOptionsWithButton)
  168. : type (type_),
  169. setup (setup_)
  170. {
  171. if (hideAdvancedOptionsWithButton)
  172. {
  173. addAndMakeVisible (showAdvancedSettingsButton = new TextButton (TRANS("Show advanced settings...")));
  174. showAdvancedSettingsButton->addListener (this);
  175. }
  176. type->scanForDevices();
  177. setup.manager->addChangeListener (this);
  178. updateAllControls();
  179. }
  180. ~AudioDeviceSettingsPanel()
  181. {
  182. setup.manager->removeChangeListener (this);
  183. }
  184. void resized()
  185. {
  186. const int lx = proportionOfWidth (0.35f);
  187. const int w = proportionOfWidth (0.4f);
  188. const int h = 24;
  189. const int space = 6;
  190. const int dh = h + space;
  191. int y = 0;
  192. if (outputDeviceDropDown != nullptr)
  193. {
  194. outputDeviceDropDown->setBounds (lx, y, w, h);
  195. if (testButton != nullptr)
  196. testButton->setBounds (proportionOfWidth (0.77f),
  197. outputDeviceDropDown->getY(),
  198. proportionOfWidth (0.18f),
  199. h);
  200. y += dh;
  201. }
  202. if (inputDeviceDropDown != nullptr)
  203. {
  204. inputDeviceDropDown->setBounds (lx, y, w, h);
  205. inputLevelMeter->setBounds (proportionOfWidth (0.77f),
  206. inputDeviceDropDown->getY(),
  207. proportionOfWidth (0.18f),
  208. h);
  209. y += dh;
  210. }
  211. const int maxBoxHeight = 100;
  212. if (outputChanList != nullptr)
  213. {
  214. const int bh = outputChanList->getBestHeight (maxBoxHeight);
  215. outputChanList->setBounds (lx, y, proportionOfWidth (0.55f), bh);
  216. y += bh + space;
  217. }
  218. if (inputChanList != nullptr)
  219. {
  220. const int bh = inputChanList->getBestHeight (maxBoxHeight);
  221. inputChanList->setBounds (lx, y, proportionOfWidth (0.55f), bh);
  222. y += bh + space;
  223. }
  224. y += space * 2;
  225. if (showAdvancedSettingsButton != nullptr)
  226. {
  227. showAdvancedSettingsButton->changeWidthToFitText (h);
  228. showAdvancedSettingsButton->setTopLeftPosition (lx, y);
  229. }
  230. if (sampleRateDropDown != nullptr)
  231. {
  232. sampleRateDropDown->setVisible (showAdvancedSettingsButton == nullptr
  233. || ! showAdvancedSettingsButton->isVisible());
  234. sampleRateDropDown->setBounds (lx, y, w, h);
  235. y += dh;
  236. }
  237. if (bufferSizeDropDown != nullptr)
  238. {
  239. bufferSizeDropDown->setVisible (showAdvancedSettingsButton == nullptr
  240. || ! showAdvancedSettingsButton->isVisible());
  241. bufferSizeDropDown->setBounds (lx, y, w, h);
  242. y += dh;
  243. }
  244. if (showUIButton != nullptr)
  245. {
  246. showUIButton->setVisible (showAdvancedSettingsButton == nullptr
  247. || ! showAdvancedSettingsButton->isVisible());
  248. showUIButton->changeWidthToFitText (h);
  249. showUIButton->setTopLeftPosition (lx, y);
  250. }
  251. }
  252. void comboBoxChanged (ComboBox* comboBoxThatHasChanged)
  253. {
  254. if (comboBoxThatHasChanged == nullptr)
  255. return;
  256. AudioDeviceManager::AudioDeviceSetup config;
  257. setup.manager->getAudioDeviceSetup (config);
  258. String error;
  259. if (comboBoxThatHasChanged == outputDeviceDropDown
  260. || comboBoxThatHasChanged == inputDeviceDropDown)
  261. {
  262. if (outputDeviceDropDown != nullptr)
  263. config.outputDeviceName = outputDeviceDropDown->getSelectedId() < 0 ? String::empty
  264. : outputDeviceDropDown->getText();
  265. if (inputDeviceDropDown != nullptr)
  266. config.inputDeviceName = inputDeviceDropDown->getSelectedId() < 0 ? String::empty
  267. : inputDeviceDropDown->getText();
  268. if (! type->hasSeparateInputsAndOutputs())
  269. config.inputDeviceName = config.outputDeviceName;
  270. if (comboBoxThatHasChanged == inputDeviceDropDown)
  271. config.useDefaultInputChannels = true;
  272. else
  273. config.useDefaultOutputChannels = true;
  274. error = setup.manager->setAudioDeviceSetup (config, true);
  275. showCorrectDeviceName (inputDeviceDropDown, true);
  276. showCorrectDeviceName (outputDeviceDropDown, false);
  277. updateControlPanelButton();
  278. resized();
  279. }
  280. else if (comboBoxThatHasChanged == sampleRateDropDown)
  281. {
  282. if (sampleRateDropDown->getSelectedId() > 0)
  283. {
  284. config.sampleRate = sampleRateDropDown->getSelectedId();
  285. error = setup.manager->setAudioDeviceSetup (config, true);
  286. }
  287. }
  288. else if (comboBoxThatHasChanged == bufferSizeDropDown)
  289. {
  290. if (bufferSizeDropDown->getSelectedId() > 0)
  291. {
  292. config.bufferSize = bufferSizeDropDown->getSelectedId();
  293. error = setup.manager->setAudioDeviceSetup (config, true);
  294. }
  295. }
  296. if (error.isNotEmpty())
  297. {
  298. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  299. "Error when trying to open audio device!",
  300. error);
  301. }
  302. }
  303. bool showDeviceControlPanel()
  304. {
  305. AudioIODevice* const device = setup.manager->getCurrentAudioDevice();
  306. if (device == nullptr)
  307. return false;
  308. Component modalWindow (String::empty);
  309. modalWindow.setOpaque (true);
  310. modalWindow.addToDesktop (0);
  311. modalWindow.enterModalState();
  312. return device->showControlPanel();
  313. }
  314. void buttonClicked (Button* button)
  315. {
  316. if (button == showAdvancedSettingsButton)
  317. {
  318. showAdvancedSettingsButton->setVisible (false);
  319. resized();
  320. }
  321. else if (button == showUIButton)
  322. {
  323. if (showDeviceControlPanel())
  324. {
  325. setup.manager->closeAudioDevice();
  326. setup.manager->restartLastAudioDevice();
  327. getTopLevelComponent()->toFront (true);
  328. }
  329. }
  330. else if (button == testButton && testButton != nullptr)
  331. {
  332. setup.manager->playTestSound();
  333. }
  334. }
  335. void updateAllControls()
  336. {
  337. updateOutputsComboBox();
  338. updateInputsComboBox();
  339. updateControlPanelButton();
  340. if (AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice())
  341. {
  342. if (setup.maxNumOutputChannels > 0
  343. && setup.minNumOutputChannels < setup.manager->getCurrentAudioDevice()->getOutputChannelNames().size())
  344. {
  345. if (outputChanList == nullptr)
  346. {
  347. addAndMakeVisible (outputChanList
  348. = new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioOutputType,
  349. TRANS ("(no audio output channels found)")));
  350. outputChanLabel = new Label (String::empty, TRANS ("active output channels:"));
  351. outputChanLabel->attachToComponent (outputChanList, true);
  352. }
  353. outputChanList->refresh();
  354. }
  355. else
  356. {
  357. outputChanLabel = nullptr;
  358. outputChanList = nullptr;
  359. }
  360. if (setup.maxNumInputChannels > 0
  361. && setup.minNumInputChannels < setup.manager->getCurrentAudioDevice()->getInputChannelNames().size())
  362. {
  363. if (inputChanList == nullptr)
  364. {
  365. addAndMakeVisible (inputChanList
  366. = new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioInputType,
  367. TRANS ("(no audio input channels found)")));
  368. inputChanLabel = new Label (String::empty, TRANS ("active input channels:"));
  369. inputChanLabel->attachToComponent (inputChanList, true);
  370. }
  371. inputChanList->refresh();
  372. }
  373. else
  374. {
  375. inputChanLabel = nullptr;
  376. inputChanList = nullptr;
  377. }
  378. updateSampleRateComboBox (currentDevice);
  379. updateBufferSizeComboBox (currentDevice);
  380. }
  381. else
  382. {
  383. jassert (setup.manager->getCurrentAudioDevice() == nullptr); // not the correct device type!
  384. sampleRateLabel = nullptr;
  385. bufferSizeLabel = nullptr;
  386. sampleRateDropDown = nullptr;
  387. bufferSizeDropDown = nullptr;
  388. if (outputDeviceDropDown != nullptr)
  389. outputDeviceDropDown->setSelectedId (-1, true);
  390. if (inputDeviceDropDown != nullptr)
  391. inputDeviceDropDown->setSelectedId (-1, true);
  392. }
  393. resized();
  394. setSize (getWidth(), getLowestY() + 4);
  395. }
  396. void changeListenerCallback (ChangeBroadcaster*)
  397. {
  398. updateAllControls();
  399. }
  400. private:
  401. AudioIODeviceType* const type;
  402. const AudioDeviceSetupDetails setup;
  403. ScopedPointer<ComboBox> outputDeviceDropDown, inputDeviceDropDown, sampleRateDropDown, bufferSizeDropDown;
  404. ScopedPointer<Label> outputDeviceLabel, inputDeviceLabel, sampleRateLabel, bufferSizeLabel, inputChanLabel, outputChanLabel;
  405. ScopedPointer<TextButton> testButton;
  406. ScopedPointer<Component> inputLevelMeter;
  407. ScopedPointer<TextButton> showUIButton, showAdvancedSettingsButton;
  408. void showCorrectDeviceName (ComboBox* const box, const bool isInput)
  409. {
  410. if (box != nullptr)
  411. {
  412. AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();
  413. const int index = type->getIndexOfDevice (currentDevice, isInput);
  414. box->setSelectedId (index + 1, true);
  415. if (testButton != nullptr && ! isInput)
  416. testButton->setEnabled (index >= 0);
  417. }
  418. }
  419. void addNamesToDeviceBox (ComboBox& combo, bool isInputs)
  420. {
  421. const StringArray devs (type->getDeviceNames (isInputs));
  422. combo.clear (true);
  423. for (int i = 0; i < devs.size(); ++i)
  424. combo.addItem (devs[i], i + 1);
  425. combo.addItem (TRANS("<< none >>"), -1);
  426. combo.setSelectedId (-1, true);
  427. }
  428. int getLowestY() const
  429. {
  430. int y = 0;
  431. for (int i = getNumChildComponents(); --i >= 0;)
  432. y = jmax (y, getChildComponent (i)->getBottom());
  433. return y;
  434. }
  435. void updateControlPanelButton()
  436. {
  437. AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();
  438. showUIButton = nullptr;
  439. if (currentDevice != nullptr && currentDevice->hasControlPanel())
  440. {
  441. addAndMakeVisible (showUIButton = new TextButton (TRANS ("show this device's control panel"),
  442. TRANS ("opens the device's own control panel")));
  443. showUIButton->addListener (this);
  444. }
  445. resized();
  446. }
  447. void updateOutputsComboBox()
  448. {
  449. if (setup.maxNumOutputChannels > 0 || ! type->hasSeparateInputsAndOutputs())
  450. {
  451. if (outputDeviceDropDown == nullptr)
  452. {
  453. outputDeviceDropDown = new ComboBox (String::empty);
  454. outputDeviceDropDown->addListener (this);
  455. addAndMakeVisible (outputDeviceDropDown);
  456. outputDeviceLabel = new Label (String::empty,
  457. type->hasSeparateInputsAndOutputs() ? TRANS ("output:")
  458. : TRANS ("device:"));
  459. outputDeviceLabel->attachToComponent (outputDeviceDropDown, true);
  460. if (setup.maxNumOutputChannels > 0)
  461. {
  462. addAndMakeVisible (testButton = new TextButton (TRANS ("Test")));
  463. testButton->addListener (this);
  464. }
  465. }
  466. addNamesToDeviceBox (*outputDeviceDropDown, false);
  467. }
  468. showCorrectDeviceName (outputDeviceDropDown, false);
  469. }
  470. void updateInputsComboBox()
  471. {
  472. if (setup.maxNumInputChannels > 0 && type->hasSeparateInputsAndOutputs())
  473. {
  474. if (inputDeviceDropDown == nullptr)
  475. {
  476. inputDeviceDropDown = new ComboBox (String::empty);
  477. inputDeviceDropDown->addListener (this);
  478. addAndMakeVisible (inputDeviceDropDown);
  479. inputDeviceLabel = new Label (String::empty, TRANS ("input:"));
  480. inputDeviceLabel->attachToComponent (inputDeviceDropDown, true);
  481. addAndMakeVisible (inputLevelMeter
  482. = new SimpleDeviceManagerInputLevelMeter (setup.manager));
  483. }
  484. addNamesToDeviceBox (*inputDeviceDropDown, true);
  485. }
  486. showCorrectDeviceName (inputDeviceDropDown, true);
  487. }
  488. void updateSampleRateComboBox (AudioIODevice* currentDevice)
  489. {
  490. if (sampleRateDropDown == nullptr)
  491. {
  492. addAndMakeVisible (sampleRateDropDown = new ComboBox (String::empty));
  493. sampleRateLabel = new Label (String::empty, TRANS ("sample rate:"));
  494. sampleRateLabel->attachToComponent (sampleRateDropDown, true);
  495. }
  496. else
  497. {
  498. sampleRateDropDown->clear();
  499. sampleRateDropDown->removeListener (this);
  500. }
  501. const int numRates = currentDevice->getNumSampleRates();
  502. for (int i = 0; i < numRates; ++i)
  503. {
  504. const int rate = roundToInt (currentDevice->getSampleRate (i));
  505. sampleRateDropDown->addItem (String (rate) + " Hz", rate);
  506. }
  507. sampleRateDropDown->setSelectedId (roundToInt (currentDevice->getCurrentSampleRate()), true);
  508. sampleRateDropDown->addListener (this);
  509. }
  510. void updateBufferSizeComboBox (AudioIODevice* currentDevice)
  511. {
  512. if (bufferSizeDropDown == nullptr)
  513. {
  514. addAndMakeVisible (bufferSizeDropDown = new ComboBox (String::empty));
  515. bufferSizeLabel = new Label (String::empty, TRANS ("audio buffer size:"));
  516. bufferSizeLabel->attachToComponent (bufferSizeDropDown, true);
  517. }
  518. else
  519. {
  520. bufferSizeDropDown->clear();
  521. bufferSizeDropDown->removeListener (this);
  522. }
  523. const int numBufferSizes = currentDevice->getNumBufferSizesAvailable();
  524. double currentRate = currentDevice->getCurrentSampleRate();
  525. if (currentRate == 0)
  526. currentRate = 48000.0;
  527. for (int i = 0; i < numBufferSizes; ++i)
  528. {
  529. const int bs = currentDevice->getBufferSizeSamples (i);
  530. bufferSizeDropDown->addItem (String (bs)
  531. + " samples ("
  532. + String (bs * 1000.0 / currentRate, 1)
  533. + " ms)",
  534. bs);
  535. }
  536. bufferSizeDropDown->setSelectedId (currentDevice->getCurrentBufferSizeSamples(), true);
  537. bufferSizeDropDown->addListener (this);
  538. }
  539. public:
  540. //==============================================================================
  541. class ChannelSelectorListBox : public ListBox,
  542. private ListBoxModel
  543. {
  544. public:
  545. enum BoxType
  546. {
  547. audioInputType,
  548. audioOutputType
  549. };
  550. //==============================================================================
  551. ChannelSelectorListBox (const AudioDeviceSetupDetails& setup_,
  552. const BoxType type_,
  553. const String& noItemsMessage_)
  554. : ListBox (String::empty, nullptr),
  555. setup (setup_),
  556. type (type_),
  557. noItemsMessage (noItemsMessage_)
  558. {
  559. refresh();
  560. setModel (this);
  561. setOutlineThickness (1);
  562. }
  563. void refresh()
  564. {
  565. items.clear();
  566. if (AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice())
  567. {
  568. if (type == audioInputType)
  569. items = currentDevice->getInputChannelNames();
  570. else if (type == audioOutputType)
  571. items = currentDevice->getOutputChannelNames();
  572. if (setup.useStereoPairs)
  573. {
  574. StringArray pairs;
  575. for (int i = 0; i < items.size(); i += 2)
  576. {
  577. const String& name = items[i];
  578. if (i + 1 >= items.size())
  579. pairs.add (name.trim());
  580. else
  581. pairs.add (getNameForChannelPair (name, items[i + 1]));
  582. }
  583. items = pairs;
  584. }
  585. }
  586. updateContent();
  587. repaint();
  588. }
  589. int getNumRows()
  590. {
  591. return items.size();
  592. }
  593. void paintListBoxItem (int row, Graphics& g, int width, int height, bool rowIsSelected)
  594. {
  595. if (isPositiveAndBelow (row, items.size()))
  596. {
  597. if (rowIsSelected)
  598. g.fillAll (findColour (TextEditor::highlightColourId)
  599. .withMultipliedAlpha (0.3f));
  600. const String item (items [row]);
  601. bool enabled = false;
  602. AudioDeviceManager::AudioDeviceSetup config;
  603. setup.manager->getAudioDeviceSetup (config);
  604. if (setup.useStereoPairs)
  605. {
  606. if (type == audioInputType)
  607. enabled = config.inputChannels [row * 2] || config.inputChannels [row * 2 + 1];
  608. else if (type == audioOutputType)
  609. enabled = config.outputChannels [row * 2] || config.outputChannels [row * 2 + 1];
  610. }
  611. else
  612. {
  613. if (type == audioInputType)
  614. enabled = config.inputChannels [row];
  615. else if (type == audioOutputType)
  616. enabled = config.outputChannels [row];
  617. }
  618. const int x = getTickX();
  619. const float tickW = height * 0.75f;
  620. getLookAndFeel().drawTickBox (g, *this, x - tickW, (height - tickW) / 2, tickW, tickW,
  621. enabled, true, true, false);
  622. g.setFont (height * 0.6f);
  623. g.setColour (findColour (ListBox::textColourId, true).withMultipliedAlpha (enabled ? 1.0f : 0.6f));
  624. g.drawText (item, x, 0, width - x - 2, height, Justification::centredLeft, true);
  625. }
  626. }
  627. void listBoxItemClicked (int row, const MouseEvent& e)
  628. {
  629. selectRow (row);
  630. if (e.x < getTickX())
  631. flipEnablement (row);
  632. }
  633. void listBoxItemDoubleClicked (int row, const MouseEvent&)
  634. {
  635. flipEnablement (row);
  636. }
  637. void returnKeyPressed (int row)
  638. {
  639. flipEnablement (row);
  640. }
  641. void paint (Graphics& g)
  642. {
  643. ListBox::paint (g);
  644. if (items.size() == 0)
  645. {
  646. g.setColour (Colours::grey);
  647. g.setFont (13.0f);
  648. g.drawText (noItemsMessage,
  649. 0, 0, getWidth(), getHeight() / 2,
  650. Justification::centred, true);
  651. }
  652. }
  653. int getBestHeight (int maxHeight)
  654. {
  655. return getRowHeight() * jlimit (2, jmax (2, maxHeight / getRowHeight()),
  656. getNumRows())
  657. + getOutlineThickness() * 2;
  658. }
  659. private:
  660. //==============================================================================
  661. const AudioDeviceSetupDetails setup;
  662. const BoxType type;
  663. const String noItemsMessage;
  664. StringArray items;
  665. static String getNameForChannelPair (const String& name1, const String& name2)
  666. {
  667. String commonBit;
  668. for (int j = 0; j < name1.length(); ++j)
  669. if (name1.substring (0, j).equalsIgnoreCase (name2.substring (0, j)))
  670. commonBit = name1.substring (0, j);
  671. // Make sure we only split the name at a space, because otherwise, things
  672. // like "input 11" + "input 12" would become "input 11 + 2"
  673. while (commonBit.isNotEmpty() && ! CharacterFunctions::isWhitespace (commonBit.getLastCharacter()))
  674. commonBit = commonBit.dropLastCharacters (1);
  675. return name1.trim() + " + " + name2.substring (commonBit.length()).trim();
  676. }
  677. void flipEnablement (const int row)
  678. {
  679. jassert (type == audioInputType || type == audioOutputType);
  680. if (isPositiveAndBelow (row, items.size()))
  681. {
  682. AudioDeviceManager::AudioDeviceSetup config;
  683. setup.manager->getAudioDeviceSetup (config);
  684. if (setup.useStereoPairs)
  685. {
  686. BigInteger bits;
  687. BigInteger& original = (type == audioInputType ? config.inputChannels
  688. : config.outputChannels);
  689. for (int i = 0; i < 256; i += 2)
  690. bits.setBit (i / 2, original [i] || original [i + 1]);
  691. if (type == audioInputType)
  692. {
  693. config.useDefaultInputChannels = false;
  694. flipBit (bits, row, setup.minNumInputChannels / 2, setup.maxNumInputChannels / 2);
  695. }
  696. else
  697. {
  698. config.useDefaultOutputChannels = false;
  699. flipBit (bits, row, setup.minNumOutputChannels / 2, setup.maxNumOutputChannels / 2);
  700. }
  701. for (int i = 0; i < 256; ++i)
  702. original.setBit (i, bits [i / 2]);
  703. }
  704. else
  705. {
  706. if (type == audioInputType)
  707. {
  708. config.useDefaultInputChannels = false;
  709. flipBit (config.inputChannels, row, setup.minNumInputChannels, setup.maxNumInputChannels);
  710. }
  711. else
  712. {
  713. config.useDefaultOutputChannels = false;
  714. flipBit (config.outputChannels, row, setup.minNumOutputChannels, setup.maxNumOutputChannels);
  715. }
  716. }
  717. String error (setup.manager->setAudioDeviceSetup (config, true));
  718. if (error.isNotEmpty())
  719. {
  720. //xxx
  721. }
  722. }
  723. }
  724. static void flipBit (BigInteger& chans, int index, int minNumber, int maxNumber)
  725. {
  726. const int numActive = chans.countNumberOfSetBits();
  727. if (chans [index])
  728. {
  729. if (numActive > minNumber)
  730. chans.setBit (index, false);
  731. }
  732. else
  733. {
  734. if (numActive >= maxNumber)
  735. {
  736. const int firstActiveChan = chans.findNextSetBit (0);
  737. chans.setBit (index > firstActiveChan
  738. ? firstActiveChan : chans.getHighestBit(),
  739. false);
  740. }
  741. chans.setBit (index, true);
  742. }
  743. }
  744. int getTickX() const
  745. {
  746. return getRowHeight() + 5;
  747. }
  748. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelSelectorListBox)
  749. };
  750. private:
  751. ScopedPointer<ChannelSelectorListBox> inputChanList, outputChanList;
  752. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioDeviceSettingsPanel)
  753. };
  754. //==============================================================================
  755. AudioDeviceSelectorComponent::AudioDeviceSelectorComponent (AudioDeviceManager& deviceManager_,
  756. const int minInputChannels_,
  757. const int maxInputChannels_,
  758. const int minOutputChannels_,
  759. const int maxOutputChannels_,
  760. const bool showMidiInputOptions,
  761. const bool showMidiOutputSelector,
  762. const bool showChannelsAsStereoPairs_,
  763. const bool hideAdvancedOptionsWithButton_)
  764. : deviceManager (deviceManager_),
  765. minOutputChannels (minOutputChannels_),
  766. maxOutputChannels (maxOutputChannels_),
  767. minInputChannels (minInputChannels_),
  768. maxInputChannels (maxInputChannels_),
  769. showChannelsAsStereoPairs (showChannelsAsStereoPairs_),
  770. hideAdvancedOptionsWithButton (hideAdvancedOptionsWithButton_)
  771. {
  772. jassert (minOutputChannels >= 0 && minOutputChannels <= maxOutputChannels);
  773. jassert (minInputChannels >= 0 && minInputChannels <= maxInputChannels);
  774. const OwnedArray<AudioIODeviceType>& types = deviceManager_.getAvailableDeviceTypes();
  775. if (types.size() > 1)
  776. {
  777. deviceTypeDropDown = new ComboBox (String::empty);
  778. for (int i = 0; i < types.size(); ++i)
  779. deviceTypeDropDown->addItem (types.getUnchecked(i)->getTypeName(), i + 1);
  780. addAndMakeVisible (deviceTypeDropDown);
  781. deviceTypeDropDown->addListener (this);
  782. deviceTypeDropDownLabel = new Label (String::empty, TRANS ("Audio device type:"));
  783. deviceTypeDropDownLabel->setJustificationType (Justification::centredRight);
  784. deviceTypeDropDownLabel->attachToComponent (deviceTypeDropDown, true);
  785. }
  786. if (showMidiInputOptions)
  787. {
  788. addAndMakeVisible (midiInputsList
  789. = new MidiInputSelectorComponentListBox (deviceManager,
  790. TRANS("(No MIDI inputs available)"),
  791. 0, 0));
  792. midiInputsLabel = new Label (String::empty, TRANS ("Active MIDI inputs:"));
  793. midiInputsLabel->setJustificationType (Justification::topRight);
  794. midiInputsLabel->attachToComponent (midiInputsList, true);
  795. }
  796. else
  797. {
  798. midiInputsList = nullptr;
  799. midiInputsLabel = nullptr;
  800. }
  801. if (showMidiOutputSelector)
  802. {
  803. addAndMakeVisible (midiOutputSelector = new ComboBox (String::empty));
  804. midiOutputSelector->addListener (this);
  805. midiOutputLabel = new Label ("lm", TRANS("MIDI Output:"));
  806. midiOutputLabel->attachToComponent (midiOutputSelector, true);
  807. }
  808. else
  809. {
  810. midiOutputSelector = nullptr;
  811. midiOutputLabel = nullptr;
  812. }
  813. deviceManager_.addChangeListener (this);
  814. updateAllControls();
  815. }
  816. AudioDeviceSelectorComponent::~AudioDeviceSelectorComponent()
  817. {
  818. deviceManager.removeChangeListener (this);
  819. }
  820. void AudioDeviceSelectorComponent::resized()
  821. {
  822. const int lx = proportionOfWidth (0.35f);
  823. const int w = proportionOfWidth (0.4f);
  824. const int h = 24;
  825. const int space = 6;
  826. const int dh = h + space;
  827. int y = 15;
  828. if (deviceTypeDropDown != nullptr)
  829. {
  830. deviceTypeDropDown->setBounds (lx, y, proportionOfWidth (0.3f), h);
  831. y += dh + space * 2;
  832. }
  833. if (audioDeviceSettingsComp != nullptr)
  834. {
  835. audioDeviceSettingsComp->setBounds (0, y, getWidth(), audioDeviceSettingsComp->getHeight());
  836. y += audioDeviceSettingsComp->getHeight() + space;
  837. }
  838. if (midiInputsList != nullptr)
  839. {
  840. const int bh = midiInputsList->getBestHeight (jmin (h * 8, getHeight() - y - space - h));
  841. midiInputsList->setBounds (lx, y, w, bh);
  842. y += bh + space;
  843. }
  844. if (midiOutputSelector != nullptr)
  845. midiOutputSelector->setBounds (lx, y, w, h);
  846. }
  847. void AudioDeviceSelectorComponent::childBoundsChanged (Component* child)
  848. {
  849. if (child == audioDeviceSettingsComp)
  850. resized();
  851. }
  852. void AudioDeviceSelectorComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
  853. {
  854. if (comboBoxThatHasChanged == deviceTypeDropDown)
  855. {
  856. if (AudioIODeviceType* const type = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown->getSelectedId() - 1])
  857. {
  858. audioDeviceSettingsComp = nullptr;
  859. deviceManager.setCurrentAudioDeviceType (type->getTypeName(), true);
  860. updateAllControls(); // needed in case the type hasn't actally changed
  861. }
  862. }
  863. else if (comboBoxThatHasChanged == midiOutputSelector)
  864. {
  865. deviceManager.setDefaultMidiOutput (midiOutputSelector->getText());
  866. }
  867. }
  868. void AudioDeviceSelectorComponent::changeListenerCallback (ChangeBroadcaster*)
  869. {
  870. updateAllControls();
  871. }
  872. void AudioDeviceSelectorComponent::updateAllControls()
  873. {
  874. if (deviceTypeDropDown != nullptr)
  875. deviceTypeDropDown->setText (deviceManager.getCurrentAudioDeviceType(), false);
  876. if (audioDeviceSettingsComp == nullptr
  877. || audioDeviceSettingsCompType != deviceManager.getCurrentAudioDeviceType())
  878. {
  879. audioDeviceSettingsCompType = deviceManager.getCurrentAudioDeviceType();
  880. audioDeviceSettingsComp = nullptr;
  881. if (AudioIODeviceType* const type
  882. = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown == nullptr
  883. ? 0 : deviceTypeDropDown->getSelectedId() - 1])
  884. {
  885. AudioDeviceSetupDetails details;
  886. details.manager = &deviceManager;
  887. details.minNumInputChannels = minInputChannels;
  888. details.maxNumInputChannels = maxInputChannels;
  889. details.minNumOutputChannels = minOutputChannels;
  890. details.maxNumOutputChannels = maxOutputChannels;
  891. details.useStereoPairs = showChannelsAsStereoPairs;
  892. audioDeviceSettingsComp = new AudioDeviceSettingsPanel (type, details, hideAdvancedOptionsWithButton);
  893. if (audioDeviceSettingsComp != nullptr)
  894. {
  895. addAndMakeVisible (audioDeviceSettingsComp);
  896. audioDeviceSettingsComp->resized();
  897. }
  898. }
  899. }
  900. if (midiInputsList != nullptr)
  901. {
  902. midiInputsList->updateContent();
  903. midiInputsList->repaint();
  904. }
  905. if (midiOutputSelector != nullptr)
  906. {
  907. midiOutputSelector->clear();
  908. const StringArray midiOuts (MidiOutput::getDevices());
  909. midiOutputSelector->addItem (TRANS("<< none >>"), -1);
  910. midiOutputSelector->addSeparator();
  911. for (int i = 0; i < midiOuts.size(); ++i)
  912. midiOutputSelector->addItem (midiOuts[i], i + 1);
  913. int current = -1;
  914. if (deviceManager.getDefaultMidiOutput() != nullptr)
  915. current = 1 + midiOuts.indexOf (deviceManager.getDefaultMidiOutputName());
  916. midiOutputSelector->setSelectedId (current, true);
  917. }
  918. resized();
  919. }