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.

1123 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 (SimpleDeviceManagerInputLevelMeter);
  51. };
  52. //==============================================================================
  53. class AudioDeviceSelectorComponent::MidiInputSelectorComponentListBox : public ListBox,
  54. public 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. public ChangeListener,
  161. public ComboBoxListener, // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  162. public 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;//(getHeight() - y - dh * 2) / numBoxes;
  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. AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();
  341. if (currentDevice != nullptr)
  342. {
  343. if (setup.maxNumOutputChannels > 0
  344. && setup.minNumOutputChannels < setup.manager->getCurrentAudioDevice()->getOutputChannelNames().size())
  345. {
  346. if (outputChanList == nullptr)
  347. {
  348. addAndMakeVisible (outputChanList
  349. = new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioOutputType,
  350. TRANS ("(no audio output channels found)")));
  351. outputChanLabel = new Label (String::empty, TRANS ("active output channels:"));
  352. outputChanLabel->attachToComponent (outputChanList, true);
  353. }
  354. outputChanList->refresh();
  355. }
  356. else
  357. {
  358. outputChanLabel = nullptr;
  359. outputChanList = nullptr;
  360. }
  361. if (setup.maxNumInputChannels > 0
  362. && setup.minNumInputChannels < setup.manager->getCurrentAudioDevice()->getInputChannelNames().size())
  363. {
  364. if (inputChanList == nullptr)
  365. {
  366. addAndMakeVisible (inputChanList
  367. = new ChannelSelectorListBox (setup, ChannelSelectorListBox::audioInputType,
  368. TRANS ("(no audio input channels found)")));
  369. inputChanLabel = new Label (String::empty, TRANS ("active input channels:"));
  370. inputChanLabel->attachToComponent (inputChanList, true);
  371. }
  372. inputChanList->refresh();
  373. }
  374. else
  375. {
  376. inputChanLabel = nullptr;
  377. inputChanList = nullptr;
  378. }
  379. updateSampleRateComboBox (currentDevice);
  380. updateBufferSizeComboBox (currentDevice);
  381. }
  382. else
  383. {
  384. jassert (setup.manager->getCurrentAudioDevice() == nullptr); // not the correct device type!
  385. sampleRateLabel = nullptr;
  386. bufferSizeLabel = nullptr;
  387. sampleRateDropDown = nullptr;
  388. bufferSizeDropDown = nullptr;
  389. if (outputDeviceDropDown != nullptr)
  390. outputDeviceDropDown->setSelectedId (-1, true);
  391. if (inputDeviceDropDown != nullptr)
  392. inputDeviceDropDown->setSelectedId (-1, true);
  393. }
  394. resized();
  395. setSize (getWidth(), getLowestY() + 4);
  396. }
  397. void changeListenerCallback (ChangeBroadcaster*)
  398. {
  399. updateAllControls();
  400. }
  401. private:
  402. AudioIODeviceType* const type;
  403. const AudioDeviceSetupDetails setup;
  404. ScopedPointer<ComboBox> outputDeviceDropDown, inputDeviceDropDown, sampleRateDropDown, bufferSizeDropDown;
  405. ScopedPointer<Label> outputDeviceLabel, inputDeviceLabel, sampleRateLabel, bufferSizeLabel, inputChanLabel, outputChanLabel;
  406. ScopedPointer<TextButton> testButton;
  407. ScopedPointer<Component> inputLevelMeter;
  408. ScopedPointer<TextButton> showUIButton, showAdvancedSettingsButton;
  409. void showCorrectDeviceName (ComboBox* const box, const bool isInput)
  410. {
  411. if (box != nullptr)
  412. {
  413. AudioIODevice* const currentDevice = dynamic_cast <AudioIODevice*> (setup.manager->getCurrentAudioDevice());
  414. const int index = type->getIndexOfDevice (currentDevice, isInput);
  415. box->setSelectedId (index + 1, true);
  416. if (testButton != nullptr && ! isInput)
  417. testButton->setEnabled (index >= 0);
  418. }
  419. }
  420. void addNamesToDeviceBox (ComboBox& combo, bool isInputs)
  421. {
  422. const StringArray devs (type->getDeviceNames (isInputs));
  423. combo.clear (true);
  424. for (int i = 0; i < devs.size(); ++i)
  425. combo.addItem (devs[i], i + 1);
  426. combo.addItem (TRANS("<< none >>"), -1);
  427. combo.setSelectedId (-1, true);
  428. }
  429. int getLowestY() const
  430. {
  431. int y = 0;
  432. for (int i = getNumChildComponents(); --i >= 0;)
  433. y = jmax (y, getChildComponent (i)->getBottom());
  434. return y;
  435. }
  436. void updateControlPanelButton()
  437. {
  438. AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();
  439. showUIButton = nullptr;
  440. if (currentDevice != nullptr && currentDevice->hasControlPanel())
  441. {
  442. addAndMakeVisible (showUIButton = new TextButton (TRANS ("show this device's control panel"),
  443. TRANS ("opens the device's own control panel")));
  444. showUIButton->addListener (this);
  445. }
  446. resized();
  447. }
  448. void updateOutputsComboBox()
  449. {
  450. if (setup.maxNumOutputChannels > 0 || ! type->hasSeparateInputsAndOutputs())
  451. {
  452. if (outputDeviceDropDown == nullptr)
  453. {
  454. outputDeviceDropDown = new ComboBox (String::empty);
  455. outputDeviceDropDown->addListener (this);
  456. addAndMakeVisible (outputDeviceDropDown);
  457. outputDeviceLabel = new Label (String::empty,
  458. type->hasSeparateInputsAndOutputs() ? TRANS ("output:")
  459. : TRANS ("device:"));
  460. outputDeviceLabel->attachToComponent (outputDeviceDropDown, true);
  461. if (setup.maxNumOutputChannels > 0)
  462. {
  463. addAndMakeVisible (testButton = new TextButton (TRANS ("Test")));
  464. testButton->addListener (this);
  465. }
  466. }
  467. addNamesToDeviceBox (*outputDeviceDropDown, false);
  468. }
  469. showCorrectDeviceName (outputDeviceDropDown, false);
  470. }
  471. void updateInputsComboBox()
  472. {
  473. if (setup.maxNumInputChannels > 0 && type->hasSeparateInputsAndOutputs())
  474. {
  475. if (inputDeviceDropDown == nullptr)
  476. {
  477. inputDeviceDropDown = new ComboBox (String::empty);
  478. inputDeviceDropDown->addListener (this);
  479. addAndMakeVisible (inputDeviceDropDown);
  480. inputDeviceLabel = new Label (String::empty, TRANS ("input:"));
  481. inputDeviceLabel->attachToComponent (inputDeviceDropDown, true);
  482. addAndMakeVisible (inputLevelMeter
  483. = new SimpleDeviceManagerInputLevelMeter (setup.manager));
  484. }
  485. addNamesToDeviceBox (*inputDeviceDropDown, true);
  486. }
  487. showCorrectDeviceName (inputDeviceDropDown, true);
  488. }
  489. void updateSampleRateComboBox (AudioIODevice* currentDevice)
  490. {
  491. if (sampleRateDropDown == nullptr)
  492. {
  493. addAndMakeVisible (sampleRateDropDown = new ComboBox (String::empty));
  494. sampleRateLabel = new Label (String::empty, TRANS ("sample rate:"));
  495. sampleRateLabel->attachToComponent (sampleRateDropDown, true);
  496. }
  497. else
  498. {
  499. sampleRateDropDown->clear();
  500. sampleRateDropDown->removeListener (this);
  501. }
  502. const int numRates = currentDevice->getNumSampleRates();
  503. for (int i = 0; i < numRates; ++i)
  504. {
  505. const int rate = roundToInt (currentDevice->getSampleRate (i));
  506. sampleRateDropDown->addItem (String (rate) + " Hz", rate);
  507. }
  508. sampleRateDropDown->setSelectedId (roundToInt (currentDevice->getCurrentSampleRate()), true);
  509. sampleRateDropDown->addListener (this);
  510. }
  511. void updateBufferSizeComboBox (AudioIODevice* currentDevice)
  512. {
  513. if (bufferSizeDropDown == nullptr)
  514. {
  515. addAndMakeVisible (bufferSizeDropDown = new ComboBox (String::empty));
  516. bufferSizeLabel = new Label (String::empty, TRANS ("audio buffer size:"));
  517. bufferSizeLabel->attachToComponent (bufferSizeDropDown, true);
  518. }
  519. else
  520. {
  521. bufferSizeDropDown->clear();
  522. bufferSizeDropDown->removeListener (this);
  523. }
  524. const int numBufferSizes = currentDevice->getNumBufferSizesAvailable();
  525. double currentRate = currentDevice->getCurrentSampleRate();
  526. if (currentRate == 0)
  527. currentRate = 48000.0;
  528. for (int i = 0; i < numBufferSizes; ++i)
  529. {
  530. const int bs = currentDevice->getBufferSizeSamples (i);
  531. bufferSizeDropDown->addItem (String (bs)
  532. + " samples ("
  533. + String (bs * 1000.0 / currentRate, 1)
  534. + " ms)",
  535. bs);
  536. }
  537. bufferSizeDropDown->setSelectedId (currentDevice->getCurrentBufferSizeSamples(), true);
  538. bufferSizeDropDown->addListener (this);
  539. }
  540. public:
  541. //==============================================================================
  542. class ChannelSelectorListBox : public ListBox,
  543. public ListBoxModel
  544. {
  545. public:
  546. enum BoxType
  547. {
  548. audioInputType,
  549. audioOutputType
  550. };
  551. //==============================================================================
  552. ChannelSelectorListBox (const AudioDeviceSetupDetails& setup_,
  553. const BoxType type_,
  554. const String& noItemsMessage_)
  555. : ListBox (String::empty, nullptr),
  556. setup (setup_),
  557. type (type_),
  558. noItemsMessage (noItemsMessage_)
  559. {
  560. refresh();
  561. setModel (this);
  562. setOutlineThickness (1);
  563. }
  564. void refresh()
  565. {
  566. items.clear();
  567. AudioIODevice* const currentDevice = setup.manager->getCurrentAudioDevice();
  568. if (currentDevice != nullptr)
  569. {
  570. if (type == audioInputType)
  571. items = currentDevice->getInputChannelNames();
  572. else if (type == audioOutputType)
  573. items = currentDevice->getOutputChannelNames();
  574. if (setup.useStereoPairs)
  575. {
  576. StringArray pairs;
  577. for (int i = 0; i < items.size(); i += 2)
  578. {
  579. const String name (items[i]);
  580. const String name2 (items[i + 1]);
  581. String commonBit;
  582. for (int j = 0; j < name.length(); ++j)
  583. if (name.substring (0, j).equalsIgnoreCase (name2.substring (0, j)))
  584. commonBit = name.substring (0, j);
  585. // Make sure we only split the name at a space, because otherwise, things
  586. // like "input 11" + "input 12" would become "input 11 + 2"
  587. while (commonBit.isNotEmpty() && ! CharacterFunctions::isWhitespace (commonBit.getLastCharacter()))
  588. commonBit = commonBit.dropLastCharacters (1);
  589. pairs.add (name.trim() + " + " + name2.substring (commonBit.length()).trim());
  590. }
  591. items = pairs;
  592. }
  593. }
  594. updateContent();
  595. repaint();
  596. }
  597. int getNumRows()
  598. {
  599. return items.size();
  600. }
  601. void paintListBoxItem (int row, Graphics& g, int width, int height, bool rowIsSelected)
  602. {
  603. if (isPositiveAndBelow (row, items.size()))
  604. {
  605. if (rowIsSelected)
  606. g.fillAll (findColour (TextEditor::highlightColourId)
  607. .withMultipliedAlpha (0.3f));
  608. const String item (items [row]);
  609. bool enabled = false;
  610. AudioDeviceManager::AudioDeviceSetup config;
  611. setup.manager->getAudioDeviceSetup (config);
  612. if (setup.useStereoPairs)
  613. {
  614. if (type == audioInputType)
  615. enabled = config.inputChannels [row * 2] || config.inputChannels [row * 2 + 1];
  616. else if (type == audioOutputType)
  617. enabled = config.outputChannels [row * 2] || config.outputChannels [row * 2 + 1];
  618. }
  619. else
  620. {
  621. if (type == audioInputType)
  622. enabled = config.inputChannels [row];
  623. else if (type == audioOutputType)
  624. enabled = config.outputChannels [row];
  625. }
  626. const int x = getTickX();
  627. const float tickW = height * 0.75f;
  628. getLookAndFeel().drawTickBox (g, *this, x - tickW, (height - tickW) / 2, tickW, tickW,
  629. enabled, true, true, false);
  630. g.setFont (height * 0.6f);
  631. g.setColour (findColour (ListBox::textColourId, true).withMultipliedAlpha (enabled ? 1.0f : 0.6f));
  632. g.drawText (item, x, 0, width - x - 2, height, Justification::centredLeft, true);
  633. }
  634. }
  635. void listBoxItemClicked (int row, const MouseEvent& e)
  636. {
  637. selectRow (row);
  638. if (e.x < getTickX())
  639. flipEnablement (row);
  640. }
  641. void listBoxItemDoubleClicked (int row, const MouseEvent&)
  642. {
  643. flipEnablement (row);
  644. }
  645. void returnKeyPressed (int row)
  646. {
  647. flipEnablement (row);
  648. }
  649. void paint (Graphics& g)
  650. {
  651. ListBox::paint (g);
  652. if (items.size() == 0)
  653. {
  654. g.setColour (Colours::grey);
  655. g.setFont (13.0f);
  656. g.drawText (noItemsMessage,
  657. 0, 0, getWidth(), getHeight() / 2,
  658. Justification::centred, true);
  659. }
  660. }
  661. int getBestHeight (int maxHeight)
  662. {
  663. return getRowHeight() * jlimit (2, jmax (2, maxHeight / getRowHeight()),
  664. getNumRows())
  665. + getOutlineThickness() * 2;
  666. }
  667. private:
  668. //==============================================================================
  669. const AudioDeviceSetupDetails setup;
  670. const BoxType type;
  671. const String noItemsMessage;
  672. StringArray items;
  673. void flipEnablement (const int row)
  674. {
  675. jassert (type == audioInputType || type == audioOutputType);
  676. if (isPositiveAndBelow (row, items.size()))
  677. {
  678. AudioDeviceManager::AudioDeviceSetup config;
  679. setup.manager->getAudioDeviceSetup (config);
  680. if (setup.useStereoPairs)
  681. {
  682. BigInteger bits;
  683. BigInteger& original = (type == audioInputType ? config.inputChannels
  684. : config.outputChannels);
  685. int i;
  686. for (i = 0; i < 256; i += 2)
  687. bits.setBit (i / 2, original [i] || original [i + 1]);
  688. if (type == audioInputType)
  689. {
  690. config.useDefaultInputChannels = false;
  691. flipBit (bits, row, setup.minNumInputChannels / 2, setup.maxNumInputChannels / 2);
  692. }
  693. else
  694. {
  695. config.useDefaultOutputChannels = false;
  696. flipBit (bits, row, setup.minNumOutputChannels / 2, setup.maxNumOutputChannels / 2);
  697. }
  698. for (i = 0; i < 256; ++i)
  699. original.setBit (i, bits [i / 2]);
  700. }
  701. else
  702. {
  703. if (type == audioInputType)
  704. {
  705. config.useDefaultInputChannels = false;
  706. flipBit (config.inputChannels, row, setup.minNumInputChannels, setup.maxNumInputChannels);
  707. }
  708. else
  709. {
  710. config.useDefaultOutputChannels = false;
  711. flipBit (config.outputChannels, row, setup.minNumOutputChannels, setup.maxNumOutputChannels);
  712. }
  713. }
  714. String error (setup.manager->setAudioDeviceSetup (config, true));
  715. if (! error.isEmpty())
  716. {
  717. //xxx
  718. }
  719. }
  720. }
  721. static void flipBit (BigInteger& chans, int index, int minNumber, int maxNumber)
  722. {
  723. const int numActive = chans.countNumberOfSetBits();
  724. if (chans [index])
  725. {
  726. if (numActive > minNumber)
  727. chans.setBit (index, false);
  728. }
  729. else
  730. {
  731. if (numActive >= maxNumber)
  732. {
  733. const int firstActiveChan = chans.findNextSetBit();
  734. chans.setBit (index > firstActiveChan
  735. ? firstActiveChan : chans.getHighestBit(),
  736. false);
  737. }
  738. chans.setBit (index, true);
  739. }
  740. }
  741. int getTickX() const
  742. {
  743. return getRowHeight() + 5;
  744. }
  745. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelSelectorListBox);
  746. };
  747. private:
  748. ScopedPointer<ChannelSelectorListBox> inputChanList, outputChanList;
  749. JUCE_DECLARE_NON_COPYABLE (AudioDeviceSettingsPanel);
  750. };
  751. //==============================================================================
  752. AudioDeviceSelectorComponent::AudioDeviceSelectorComponent (AudioDeviceManager& deviceManager_,
  753. const int minInputChannels_,
  754. const int maxInputChannels_,
  755. const int minOutputChannels_,
  756. const int maxOutputChannels_,
  757. const bool showMidiInputOptions,
  758. const bool showMidiOutputSelector,
  759. const bool showChannelsAsStereoPairs_,
  760. const bool hideAdvancedOptionsWithButton_)
  761. : deviceManager (deviceManager_),
  762. minOutputChannels (minOutputChannels_),
  763. maxOutputChannels (maxOutputChannels_),
  764. minInputChannels (minInputChannels_),
  765. maxInputChannels (maxInputChannels_),
  766. showChannelsAsStereoPairs (showChannelsAsStereoPairs_),
  767. hideAdvancedOptionsWithButton (hideAdvancedOptionsWithButton_)
  768. {
  769. jassert (minOutputChannels >= 0 && minOutputChannels <= maxOutputChannels);
  770. jassert (minInputChannels >= 0 && minInputChannels <= maxInputChannels);
  771. const OwnedArray<AudioIODeviceType>& types = deviceManager_.getAvailableDeviceTypes();
  772. if (types.size() > 1)
  773. {
  774. deviceTypeDropDown = new ComboBox (String::empty);
  775. for (int i = 0; i < types.size(); ++i)
  776. deviceTypeDropDown->addItem (types.getUnchecked(i)->getTypeName(), i + 1);
  777. addAndMakeVisible (deviceTypeDropDown);
  778. deviceTypeDropDown->addListener (this);
  779. deviceTypeDropDownLabel = new Label (String::empty, TRANS ("audio device type:"));
  780. deviceTypeDropDownLabel->setJustificationType (Justification::centredRight);
  781. deviceTypeDropDownLabel->attachToComponent (deviceTypeDropDown, true);
  782. }
  783. if (showMidiInputOptions)
  784. {
  785. addAndMakeVisible (midiInputsList
  786. = new MidiInputSelectorComponentListBox (deviceManager,
  787. TRANS("(no midi inputs available)"),
  788. 0, 0));
  789. midiInputsLabel = new Label (String::empty, TRANS ("active midi inputs:"));
  790. midiInputsLabel->setJustificationType (Justification::topRight);
  791. midiInputsLabel->attachToComponent (midiInputsList, true);
  792. }
  793. else
  794. {
  795. midiInputsList = nullptr;
  796. midiInputsLabel = nullptr;
  797. }
  798. if (showMidiOutputSelector)
  799. {
  800. addAndMakeVisible (midiOutputSelector = new ComboBox (String::empty));
  801. midiOutputSelector->addListener (this);
  802. midiOutputLabel = new Label ("lm", TRANS("Midi Output:"));
  803. midiOutputLabel->attachToComponent (midiOutputSelector, true);
  804. }
  805. else
  806. {
  807. midiOutputSelector = nullptr;
  808. midiOutputLabel = nullptr;
  809. }
  810. deviceManager_.addChangeListener (this);
  811. updateAllControls();
  812. }
  813. AudioDeviceSelectorComponent::~AudioDeviceSelectorComponent()
  814. {
  815. deviceManager.removeChangeListener (this);
  816. }
  817. void AudioDeviceSelectorComponent::resized()
  818. {
  819. const int lx = proportionOfWidth (0.35f);
  820. const int w = proportionOfWidth (0.4f);
  821. const int h = 24;
  822. const int space = 6;
  823. const int dh = h + space;
  824. int y = 15;
  825. if (deviceTypeDropDown != nullptr)
  826. {
  827. deviceTypeDropDown->setBounds (lx, y, proportionOfWidth (0.3f), h);
  828. y += dh + space * 2;
  829. }
  830. if (audioDeviceSettingsComp != nullptr)
  831. {
  832. audioDeviceSettingsComp->setBounds (0, y, getWidth(), audioDeviceSettingsComp->getHeight());
  833. y += audioDeviceSettingsComp->getHeight() + space;
  834. }
  835. if (midiInputsList != nullptr)
  836. {
  837. const int bh = midiInputsList->getBestHeight (jmin (h * 8, getHeight() - y - space - h));
  838. midiInputsList->setBounds (lx, y, w, bh);
  839. y += bh + space;
  840. }
  841. if (midiOutputSelector != nullptr)
  842. midiOutputSelector->setBounds (lx, y, w, h);
  843. }
  844. void AudioDeviceSelectorComponent::childBoundsChanged (Component* child)
  845. {
  846. if (child == audioDeviceSettingsComp)
  847. resized();
  848. }
  849. void AudioDeviceSelectorComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
  850. {
  851. if (comboBoxThatHasChanged == deviceTypeDropDown)
  852. {
  853. AudioIODeviceType* const type = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown->getSelectedId() - 1];
  854. if (type != nullptr)
  855. {
  856. audioDeviceSettingsComp = nullptr;
  857. deviceManager.setCurrentAudioDeviceType (type->getTypeName(), true);
  858. updateAllControls(); // needed in case the type hasn't actally changed
  859. }
  860. }
  861. else if (comboBoxThatHasChanged == midiOutputSelector)
  862. {
  863. deviceManager.setDefaultMidiOutput (midiOutputSelector->getText());
  864. }
  865. }
  866. void AudioDeviceSelectorComponent::changeListenerCallback (ChangeBroadcaster*)
  867. {
  868. updateAllControls();
  869. }
  870. void AudioDeviceSelectorComponent::updateAllControls()
  871. {
  872. if (deviceTypeDropDown != nullptr)
  873. deviceTypeDropDown->setText (deviceManager.getCurrentAudioDeviceType(), false);
  874. if (audioDeviceSettingsComp == nullptr
  875. || audioDeviceSettingsCompType != deviceManager.getCurrentAudioDeviceType())
  876. {
  877. audioDeviceSettingsCompType = deviceManager.getCurrentAudioDeviceType();
  878. audioDeviceSettingsComp = nullptr;
  879. AudioIODeviceType* const type
  880. = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown == nullptr
  881. ? 0 : deviceTypeDropDown->getSelectedId() - 1];
  882. if (type != nullptr)
  883. {
  884. AudioDeviceSetupDetails details;
  885. details.manager = &deviceManager;
  886. details.minNumInputChannels = minInputChannels;
  887. details.maxNumInputChannels = maxInputChannels;
  888. details.minNumOutputChannels = minOutputChannels;
  889. details.maxNumOutputChannels = maxOutputChannels;
  890. details.useStereoPairs = showChannelsAsStereoPairs;
  891. audioDeviceSettingsComp = new AudioDeviceSettingsPanel (type, details, hideAdvancedOptionsWithButton);
  892. if (audioDeviceSettingsComp != nullptr)
  893. {
  894. addAndMakeVisible (audioDeviceSettingsComp);
  895. audioDeviceSettingsComp->resized();
  896. }
  897. }
  898. }
  899. if (midiInputsList != nullptr)
  900. {
  901. midiInputsList->updateContent();
  902. midiInputsList->repaint();
  903. }
  904. if (midiOutputSelector != nullptr)
  905. {
  906. midiOutputSelector->clear();
  907. const StringArray midiOuts (MidiOutput::getDevices());
  908. midiOutputSelector->addItem (TRANS("<< none >>"), -1);
  909. midiOutputSelector->addSeparator();
  910. for (int i = 0; i < midiOuts.size(); ++i)
  911. midiOutputSelector->addItem (midiOuts[i], i + 1);
  912. int current = -1;
  913. if (deviceManager.getDefaultMidiOutput() != nullptr)
  914. current = 1 + midiOuts.indexOf (deviceManager.getDefaultMidiOutputName());
  915. midiOutputSelector->setSelectedId (current, true);
  916. }
  917. resized();
  918. }