SettingsPages.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. #include "ui/SettingsPages.h"
  2. #include "core/ToolbarLayout.h"
  3. #include "engine/AudioEngine.h"
  4. #include "engine/PluginHost.h"
  5. #include "ui/AutomationUi.h"
  6. #include "ui/I18n.h"
  7. namespace lynx
  8. {
  9. using namespace settingsui;
  10. juce::File autosaveFolder()
  11. {
  12. return juce::File::getSpecialLocation (juce::File::userApplicationDataDirectory).getChildFile ("LYNX/autosave");
  13. }
  14. namespace
  15. {
  16. constexpr int labelWidth = 190;
  17. constexpr int rowHeight = 28;
  18. constexpr int rowGap = 8;
  19. constexpr int sectionHeight = 28;
  20. void styleCombo (juce::ComboBox& box)
  21. {
  22. box.setColour (juce::ComboBox::outlineColourId, theme::colour::borderStrong);
  23. }
  24. void styleToggle (juce::ToggleButton& t, const juce::String& text)
  25. {
  26. t.setButtonText (text);
  27. }
  28. // Lays out rows top to bottom inside a page of a given width.
  29. struct Rows
  30. {
  31. juce::Rectangle<int> area;
  32. int y = 0;
  33. explicit Rows (int width) : area (20, 14, width - 40, 100000), y (14) {}
  34. void section (juce::Component& title, bool first = false)
  35. {
  36. if (! first)
  37. y += 10;
  38. title.setBounds (area.getX(), y, area.getWidth(), sectionHeight);
  39. y += sectionHeight + rowGap;
  40. }
  41. // label + control (control width 0 = to the right edge)
  42. juce::Rectangle<int> row (juce::Label& label, int controlWidth = 260, int height = rowHeight)
  43. {
  44. label.setBounds (area.getX(), y, labelWidth, height);
  45. auto r = juce::Rectangle<int> (area.getX() + labelWidth, y, controlWidth > 0 ? controlWidth : area.getWidth() - labelWidth, height);
  46. y += height + rowGap;
  47. return r;
  48. }
  49. juce::Rectangle<int> full (int height = rowHeight)
  50. {
  51. auto r = juce::Rectangle<int> (area.getX(), y, area.getWidth(), height);
  52. y += height + rowGap;
  53. return r;
  54. }
  55. juce::Rectangle<int> indented (int height = rowHeight)
  56. {
  57. auto r = juce::Rectangle<int> (area.getX() + labelWidth, y, area.getWidth() - labelWidth, height);
  58. y += height + rowGap;
  59. return r;
  60. }
  61. int bottom() const { return y + 14; }
  62. };
  63. // ================================================================================================================
  64. // General
  65. // ================================================================================================================
  66. class GeneralPage final : public ScrollingPage
  67. {
  68. public:
  69. GeneralPage (AudioEngine& e, SettingsHooks h, bool keep) : engine (e), hooks (std::move (h)), persistent (keep)
  70. {
  71. if (hooks.getGeneral)
  72. options = hooks.getGeneral();
  73. else
  74. options.language = "en";
  75. for (auto* c : std::initializer_list<juce::Component*> { &languageTitle, &displayTitle, &startupTitle, &editingTitle, &backupTitle,
  76. &pluginsTitle, &languageLabel, &languageBox, &languageNote, &scaleLabel, &scaleBox,
  77. &wavesLabel, &wavesBox, &stereoWaves, &splash, &startupSound, &scanAtStart,
  78. &typingPiano, &keepEditors, &rememberSeek, &editInLabel, &editInBox, &undoLabel,
  79. &undoBox, &autosaveLabel, &autosaveBox, &keepLabel, &keepBox, &openBackups,
  80. &sandbox, &sandboxNote, &managePlugins, &manageShortcuts, &shortcutsNote })
  81. add (*c);
  82. styleLabel (languageLabel, tr ("Language"));
  83. styleNote (languageNote, tr ("A new language is applied when LYNX restarts."));
  84. int id = 1;
  85. for (const auto& l : i18n::languages())
  86. {
  87. languageBox.addItem (juce::String::fromUTF8 (l.nativeName), id);
  88. if (options.language == l.id)
  89. languageBox.setSelectedId (id, juce::dontSendNotification);
  90. ++id;
  91. }
  92. languageBox.onChange = [this] {
  93. const auto index = languageBox.getSelectedId() - 1;
  94. if (juce::isPositiveAndBelow (index, (int) i18n::languages().size()))
  95. change ([&] { options.language = i18n::languages()[(std::size_t) index].id; });
  96. };
  97. styleLabel (scaleLabel, tr ("Interface size"));
  98. for (auto percent : toolbarlayout::uiScales)
  99. scaleBox.addItem (juce::String (percent) + "%", percent);
  100. scaleBox.setSelectedId (options.uiScale, juce::dontSendNotification);
  101. scaleBox.onChange = [this] { change ([&] { options.uiScale = scaleBox.getSelectedId(); }); };
  102. styleLabel (wavesLabel, tr ("Waveforms"));
  103. wavesBox.addItem (tr ("Clean (the clip colour)"), 1);
  104. wavesBox.addItem (tr ("Spectral colours (warm = bass, cool = treble)"), 2);
  105. wavesBox.setSelectedId (options.waveSpectral ? 2 : 1, juce::dontSendNotification);
  106. wavesBox.onChange = [this] { change ([&] { options.waveSpectral = wavesBox.getSelectedId() == 2; }); };
  107. toggle (stereoWaves, tr ("Stereo (left and right channels apart)"), options.waveStereo, [this] (bool on) { options.waveStereo = on; });
  108. toggle (splash, tr ("Show the splash screen at startup"), options.splash, [this] (bool on) { options.splash = on; });
  109. toggle (startupSound, tr ("Play the startup sound"), options.startupSound, [this] (bool on) { options.startupSound = on; });
  110. toggle (scanAtStart, tr ("Look for new plugins in the background at every start"), options.scanAtStart,
  111. [this] (bool on) { options.scanAtStart = on; });
  112. toggle (typingPiano, tr ("Typing keyboard as a piano"), options.typingPiano, [this] (bool on) { options.typingPiano = on; });
  113. toggle (keepEditors, tr ("Keep channel editors open (a channel button click doesn't close the previous editor)"), options.keepEditorsOpen,
  114. [this] (bool on) { options.keepEditorsOpen = on; });
  115. toggle (rememberSeek, tr ("Remember seek time: Stop goes back to the position clicked on the Playlist ruler"), options.rememberSeek,
  116. [this] (bool on) { options.rememberSeek = on; });
  117. styleLabel (editInLabel, tr ("Double-click on a pattern clip opens"));
  118. const char* choices[] = { TR_NOOP ("Piano Roll when it has piano-roll notes, else Channel Rack"), TR_NOOP ("Channel Rack"), TR_NOOP ("Piano Roll") };
  119. for (int i = 0; i < 3; ++i)
  120. editInBox.addItem (tr (choices[i]), i + 1);
  121. editInBox.setSelectedId (options.editPatternsIn + 1, juce::dontSendNotification);
  122. editInBox.onChange = [this] { change ([&] { options.editPatternsIn = editInBox.getSelectedId() - 1; }); };
  123. styleLabel (undoLabel, tr ("Maximum undo levels"));
  124. for (auto n : { 25, 50, 100, 200, 500, 1000 })
  125. undoBox.addItem (juce::String (n), n);
  126. selectNearest (undoBox, options.undoLevels);
  127. undoBox.onChange = [this] { change ([&] { options.undoLevels = undoBox.getSelectedId(); }); };
  128. styleLabel (autosaveLabel, tr ("Save a backup copy"));
  129. for (auto minutes : prefs::autosaveIntervals())
  130. autosaveBox.addItem (minutes == 0 ? tr ("Never")
  131. : minutes == 1 ? tr ("Every minute")
  132. : tr ("Every {0} minutes", { juce::String (minutes) }),
  133. minutes + 1);
  134. autosaveBox.setSelectedId (prefs::clampAutosaveMinutes (options.autosaveMinutes) + 1, juce::dontSendNotification);
  135. autosaveBox.onChange = [this] {
  136. change ([&] { options.autosaveMinutes = autosaveBox.getSelectedId() - 1; });
  137. keepBox.setEnabled (options.autosaveMinutes > 0);
  138. };
  139. styleLabel (keepLabel, tr ("Backups kept per project"));
  140. for (auto n : { 1, 3, 5, 10, 20, 50 })
  141. keepBox.addItem (juce::String (n), n);
  142. selectNearest (keepBox, options.autosaveKeep);
  143. keepBox.setEnabled (options.autosaveMinutes > 0);
  144. keepBox.onChange = [this] { change ([&] { options.autosaveKeep = keepBox.getSelectedId(); }); };
  145. openBackups.onClick = [] {
  146. autosaveFolder().createDirectory();
  147. autosaveFolder().startAsProcess();
  148. };
  149. toggle (sandbox, tr ("Run plugins in a separate process (protects LYNX from crashes)"), options.pluginSandbox,
  150. [this] (bool on) { options.pluginSandbox = on; });
  151. styleNote (sandboxNote, tr ("Applies to plugins opened from now on; reopen the project to move the others."));
  152. managePlugins.onClick = [this] {
  153. if (hooks.managePlugins)
  154. hooks.managePlugins();
  155. };
  156. manageShortcuts.onClick = [this] {
  157. if (hooks.manageShortcuts)
  158. hooks.manageShortcuts();
  159. };
  160. styleNote (shortcutsNote, tr ("See, search and change every keyboard shortcut."));
  161. for (auto* b : { &languageBox, &scaleBox, &wavesBox, &editInBox, &undoBox, &autosaveBox, &keepBox })
  162. styleCombo (*b);
  163. juce::ignoreUnused (engine, persistent);
  164. }
  165. private:
  166. int layout (int width) override
  167. {
  168. Rows r (width);
  169. r.section (languageTitle, true);
  170. languageBox.setBounds (r.row (languageLabel, 220));
  171. languageNote.setBounds (r.indented (16).translated (0, -6));
  172. scaleBox.setBounds (r.row (scaleLabel, 120));
  173. wavesBox.setBounds (r.row (wavesLabel, 0));
  174. stereoWaves.setBounds (r.indented (24));
  175. r.section (startupTitle);
  176. for (auto* t : { &splash, &startupSound, &scanAtStart })
  177. t->setBounds (r.full (24));
  178. r.section (editingTitle);
  179. for (auto* t : { &typingPiano, &keepEditors, &rememberSeek })
  180. t->setBounds (r.full (24));
  181. editInBox.setBounds (r.row (editInLabel, 0));
  182. undoBox.setBounds (r.row (undoLabel, 120));
  183. r.section (backupTitle);
  184. {
  185. auto row = r.row (autosaveLabel, 0);
  186. autosaveBox.setBounds (row.removeFromLeft (220));
  187. row.removeFromLeft (8);
  188. openBackups.setBounds (row.removeFromLeft (juce::jmin (row.getWidth(), 170)));
  189. }
  190. keepBox.setBounds (r.row (keepLabel, 120));
  191. r.section (pluginsTitle);
  192. sandbox.setBounds (r.full (24));
  193. sandboxNote.setBounds (r.full (16).translated (0, -6));
  194. {
  195. auto row = r.full();
  196. managePlugins.setBounds (row.removeFromLeft (180));
  197. row.removeFromLeft (8);
  198. manageShortcuts.setBounds (row.removeFromLeft (180));
  199. row.removeFromLeft (12);
  200. shortcutsNote.setBounds (row.withTrimmedTop (6));
  201. }
  202. displayTitle.setVisible (false);
  203. return r.bottom();
  204. }
  205. void toggle (juce::ToggleButton& t, const juce::String& text, bool value, std::function<void (bool)> set)
  206. {
  207. styleToggle (t, text);
  208. t.setToggleState (value, juce::dontSendNotification);
  209. t.onClick = [this, &t, set = std::move (set)] { change ([&] { set (t.getToggleState()); }); };
  210. }
  211. static void selectNearest (juce::ComboBox& box, int value)
  212. {
  213. auto best = box.getItemId (0);
  214. for (int i = 0; i < box.getNumItems(); ++i)
  215. if (std::abs (box.getItemId (i) - value) < std::abs (best - value))
  216. best = box.getItemId (i);
  217. box.setSelectedId (best, juce::dontSendNotification);
  218. }
  219. void change (const std::function<void()>& fn)
  220. {
  221. fn();
  222. if (hooks.setGeneral)
  223. hooks.setGeneral (options);
  224. }
  225. AudioEngine& engine;
  226. SettingsHooks hooks;
  227. bool persistent;
  228. GeneralOptions options;
  229. SectionTitle languageTitle { tr ("Language and display") }, displayTitle, startupTitle { tr ("Startup") }, editingTitle { tr ("Editing") },
  230. backupTitle { tr ("Backup") }, pluginsTitle { tr ("Plugins and shortcuts") };
  231. juce::Label languageLabel, languageNote, scaleLabel, wavesLabel, editInLabel, undoLabel, autosaveLabel, keepLabel, sandboxNote, shortcutsNote;
  232. juce::ComboBox languageBox, scaleBox, wavesBox, editInBox, undoBox, autosaveBox, keepBox;
  233. juce::ToggleButton stereoWaves, splash, startupSound, scanAtStart, typingPiano, keepEditors, rememberSeek, sandbox;
  234. juce::TextButton openBackups { tr ("Open backup folder") }, managePlugins { tr ("Plugin manager...") }, manageShortcuts { tr ("Manage shortcuts...") };
  235. };
  236. // ================================================================================================================
  237. // Audio
  238. // ================================================================================================================
  239. class AudioPage final : public ScrollingPage,
  240. private juce::ChangeListener,
  241. private juce::Timer
  242. {
  243. public:
  244. AudioPage (AudioEngine& e) : engine (e), devices (e.getDeviceManager())
  245. {
  246. for (auto* c : std::initializer_list<juce::Component*> { &deviceTitle, &statusTitle, &recordTitle, &driverLabel, &driverBox, &outputLabel,
  247. &outputBox, &inputLabel, &inputBox, &rateLabel, &rateBox, &bufferLabel, &bufferBox,
  248. &bufferNote, &panelButton, &testButton, &status, &load, &recordInput, &recordNote })
  249. add (*c);
  250. styleLabel (driverLabel, tr ("Driver"));
  251. styleLabel (outputLabel, tr ("Output device"));
  252. styleLabel (inputLabel, tr ("Input device"));
  253. styleLabel (rateLabel, tr ("Sample rate"));
  254. styleLabel (bufferLabel, tr ("Buffer size"));
  255. styleNote (bufferNote, tr ("Smaller buffers answer faster when you play; larger ones give the CPU more room (fewer clicks)."));
  256. for (auto* l : { &status, &load })
  257. {
  258. styleLabel (*l, {}, false);
  259. l->setJustificationType (juce::Justification::centredLeft);
  260. }
  261. for (auto* b : { &driverBox, &outputBox, &inputBox, &rateBox, &bufferBox })
  262. {
  263. styleCombo (*b);
  264. b->setTextWhenNoChoicesAvailable (tr ("(open an output device first)"));
  265. b->setTextWhenNothingSelected (tr ("(choose)"));
  266. }
  267. driverBox.onChange = [this] {
  268. if (updating)
  269. return;
  270. auto& types = devices.getAvailableDeviceTypes();
  271. const auto index = driverBox.getSelectedId() - 1;
  272. if (juce::isPositiveAndBelow (index, types.size()))
  273. devices.setCurrentAudioDeviceType (types[index]->getTypeName(), true);
  274. refresh();
  275. };
  276. outputBox.onChange = [this] {
  277. apply ([this] (juce::AudioDeviceManager::AudioDeviceSetup& s) { s.outputDeviceName = outputBox.getText(); });
  278. };
  279. inputBox.onChange = [this] {
  280. apply ([this] (juce::AudioDeviceManager::AudioDeviceSetup& s) {
  281. if (inputBox.getSelectedId() <= 1)
  282. {
  283. s.inputDeviceName = {};
  284. s.inputChannels.clear();
  285. }
  286. else
  287. {
  288. s.inputDeviceName = inputBox.getText();
  289. s.useDefaultInputChannels = false;
  290. s.inputChannels.clear();
  291. s.inputChannels.setRange (0, 2, true);
  292. }
  293. });
  294. };
  295. rateBox.onChange = [this] {
  296. apply ([this] (juce::AudioDeviceManager::AudioDeviceSetup& s) { s.sampleRate = (double) rateBox.getSelectedId(); });
  297. };
  298. bufferBox.onChange = [this] {
  299. apply ([this] (juce::AudioDeviceManager::AudioDeviceSetup& s) { s.bufferSize = bufferBox.getSelectedId(); });
  300. };
  301. panelButton.onClick = [this] {
  302. if (auto* device = devices.getCurrentAudioDevice(); device != nullptr && device->hasControlPanel())
  303. {
  304. // the driver's own window (ASIO): the device is reopened afterwards with what was set there
  305. devices.closeAudioDevice();
  306. device->showControlPanel();
  307. devices.restartLastAudioDevice();
  308. refresh();
  309. }
  310. };
  311. testButton.onClick = [this] { devices.playTestSound(); };
  312. styleToggle (recordInput, tr ("Record input audio"));
  313. styleNote (recordNote, tr ("With recording on, the sound card input becomes a Playlist clip (the Input device above)."));
  314. recordInput.onClick = [this] { engine.setAudioRecordingArmed (recordInput.getToggleState()); };
  315. devices.addChangeListener (this);
  316. refresh();
  317. startTimerHz (2);
  318. }
  319. ~AudioPage() override { devices.removeChangeListener (this); }
  320. private:
  321. int layout (int width) override
  322. {
  323. Rows r (width);
  324. r.section (deviceTitle, true);
  325. driverBox.setBounds (r.row (driverLabel, 260));
  326. outputBox.setBounds (r.row (outputLabel, 0));
  327. inputBox.setBounds (r.row (inputLabel, 0));
  328. rateBox.setBounds (r.row (rateLabel, 160));
  329. bufferBox.setBounds (r.row (bufferLabel, 260));
  330. bufferNote.setBounds (r.indented (18).translated (0, -6));
  331. {
  332. auto row = r.indented();
  333. panelButton.setBounds (row.removeFromLeft (170));
  334. row.removeFromLeft (8);
  335. testButton.setBounds (row.removeFromLeft (150));
  336. }
  337. r.section (statusTitle);
  338. status.setBounds (r.full (22));
  339. load.setBounds (r.full (22));
  340. r.section (recordTitle);
  341. recordInput.setBounds (r.full (24));
  342. recordNote.setBounds (r.full (16).translated (0, -6));
  343. return r.bottom();
  344. }
  345. void apply (const std::function<void (juce::AudioDeviceManager::AudioDeviceSetup&)>& change)
  346. {
  347. if (updating)
  348. return;
  349. auto setup = devices.getAudioDeviceSetup();
  350. change (setup);
  351. error = devices.setAudioDeviceSetup (setup, true);
  352. refresh();
  353. }
  354. void refresh()
  355. {
  356. const juce::ScopedValueSetter<bool> guard (updating, true);
  357. auto* type = devices.getCurrentDeviceTypeObject();
  358. auto* device = devices.getCurrentAudioDevice();
  359. const auto setup = devices.getAudioDeviceSetup();
  360. driverBox.clear (juce::dontSendNotification);
  361. auto& types = devices.getAvailableDeviceTypes();
  362. for (int i = 0; i < types.size(); ++i)
  363. {
  364. driverBox.addItem (types[i]->getTypeName(), i + 1);
  365. if (types[i] == type)
  366. driverBox.setSelectedId (i + 1, juce::dontSendNotification);
  367. }
  368. outputBox.clear (juce::dontSendNotification);
  369. inputBox.clear (juce::dontSendNotification);
  370. inputBox.addItem (tr ("(none)"), 1);
  371. inputBox.setSelectedId (1, juce::dontSendNotification);
  372. if (type != nullptr)
  373. {
  374. if (type->getDeviceNames (false).isEmpty())
  375. type->scanForDevices(); // (a driver type not opened yet this session)
  376. const auto outs = type->getDeviceNames (false);
  377. for (int i = 0; i < outs.size(); ++i)
  378. {
  379. outputBox.addItem (outs[i], i + 1);
  380. if (outs[i] == setup.outputDeviceName)
  381. outputBox.setSelectedId (i + 1, juce::dontSendNotification);
  382. }
  383. if (type->hasSeparateInputsAndOutputs())
  384. {
  385. const auto ins = type->getDeviceNames (true);
  386. for (int i = 0; i < ins.size(); ++i)
  387. {
  388. inputBox.addItem (ins[i], i + 2);
  389. if (ins[i] == setup.inputDeviceName && ! setup.inputChannels.isZero())
  390. inputBox.setSelectedId (i + 2, juce::dontSendNotification);
  391. }
  392. }
  393. else if (setup.outputDeviceName.isNotEmpty())
  394. {
  395. // ASIO: one device does both
  396. inputBox.addItem (setup.outputDeviceName, 2);
  397. if (! setup.inputChannels.isZero())
  398. inputBox.setSelectedId (2, juce::dontSendNotification);
  399. }
  400. }
  401. rateBox.clear (juce::dontSendNotification);
  402. bufferBox.clear (juce::dontSendNotification);
  403. if (device != nullptr)
  404. {
  405. const auto rate = device->getCurrentSampleRate();
  406. for (auto r : device->getAvailableSampleRates())
  407. rateBox.addItem (juce::String ((int) r) + " Hz", (int) r);
  408. rateBox.setSelectedId ((int) rate, juce::dontSendNotification);
  409. for (auto b : device->getAvailableBufferSizes())
  410. bufferBox.addItem (tr ("{0} samples ({1} ms)", { juce::String (b), juce::String (prefs::bufferLatencyMs (b, rate), 1) }), b);
  411. bufferBox.setSelectedId (device->getCurrentBufferSizeSamples(), juce::dontSendNotification);
  412. }
  413. for (auto* b : { &outputBox, &inputBox, &rateBox, &bufferBox })
  414. b->setEnabled (b->getNumItems() > 0);
  415. panelButton.setEnabled (device != nullptr && device->hasControlPanel());
  416. testButton.setEnabled (device != nullptr);
  417. recordInput.setToggleState (engine.isAudioRecordingArmed(), juce::dontSendNotification);
  418. updateStatus();
  419. }
  420. void updateStatus()
  421. {
  422. auto* device = devices.getCurrentAudioDevice();
  423. if (device == nullptr)
  424. {
  425. status.setText (error.isNotEmpty() ? error : tr ("No audio device is open: choose an output device."), juce::dontSendNotification);
  426. status.setColour (juce::Label::textColourId, theme::colour::warning);
  427. load.setText ({}, juce::dontSendNotification);
  428. return;
  429. }
  430. const auto rate = device->getCurrentSampleRate();
  431. const auto latency = prefs::bufferLatencyMs (device->getOutputLatencyInSamples() + device->getCurrentBufferSizeSamples(), rate);
  432. status.setText (tr ("{0} is running: {1} Hz, output latency {2} ms",
  433. { device->getName(), juce::String ((int) rate), juce::String (latency, 1) }),
  434. juce::dontSendNotification);
  435. status.setColour (juce::Label::textColourId, theme::colour::textPrimary);
  436. const auto xruns = device->getXRunCount();
  437. const auto rt = engine.getRtStats().snapshot();
  438. const auto dot = juce::String::fromUTF8 (" \xc2\xb7 ");
  439. load.setText (tr ("Audio CPU load: {0}%", { juce::String (juce::roundToInt (devices.getCpuUsage() * 100.0)) })
  440. + (xruns > 0 ? dot + tr ("{0} dropouts (buffer underruns) so far", { juce::String (xruns) }) : juce::String())
  441. + (rt.overBudget > 0 ? dot + tr ("{0} blocks over the deadline (max {1}%)", { juce::String ((juce::int64) rt.overBudget), juce::String (juce::roundToInt (rt.maxLoad * 100.0)) })
  442. : juce::String()),
  443. juce::dontSendNotification);
  444. load.setColour (juce::Label::textColourId, xruns > 0 || rt.overBudget > 0 ? theme::colour::warning : theme::colour::textSecondary);
  445. }
  446. void changeListenerCallback (juce::ChangeBroadcaster*) override { refresh(); }
  447. void timerCallback() override { updateStatus(); }
  448. AudioEngine& engine;
  449. juce::AudioDeviceManager& devices;
  450. bool updating = false;
  451. juce::String error;
  452. SectionTitle deviceTitle { tr ("Audio device") }, statusTitle { tr ("Status") }, recordTitle { tr ("Recording") };
  453. juce::Label driverLabel, outputLabel, inputLabel, rateLabel, bufferLabel, bufferNote, status, load, recordNote;
  454. juce::ComboBox driverBox, outputBox, inputBox, rateBox, bufferBox;
  455. juce::TextButton panelButton { tr ("Driver settings...") }, testButton { tr ("Play a test sound") };
  456. juce::ToggleButton recordInput;
  457. };
  458. // ================================================================================================================
  459. // MIDI
  460. // ================================================================================================================
  461. // One input device: on/off, which MIDI channel it listens to and what it plays.
  462. class MidiInputRow final : public juce::Component
  463. {
  464. public:
  465. MidiInputRow (const juce::String& deviceName, const prefs::MidiInputPrefs& p, const std::vector<std::string>& channels,
  466. std::function<void (const prefs::MidiInputPrefs&)> changed)
  467. : prefsNow (p), onChange (std::move (changed))
  468. {
  469. enabled.setButtonText (deviceName);
  470. enabled.setToggleState (p.enabled, juce::dontSendNotification);
  471. enabled.onClick = [this] {
  472. prefsNow.enabled = enabled.getToggleState();
  473. update();
  474. };
  475. addAndMakeVisible (enabled);
  476. channel.addItem (tr ("Any MIDI channel"), 1);
  477. for (int c = 1; c <= 16; ++c)
  478. channel.addItem (tr ("MIDI channel {0}", { juce::String (c) }), c + 1);
  479. channel.setSelectedId (p.midiChannel + 1, juce::dontSendNotification);
  480. channel.onChange = [this] {
  481. prefsNow.midiChannel = channel.getSelectedId() - 1;
  482. update();
  483. };
  484. plays.addItem (tr ("Plays the selected channels"), 1);
  485. for (int i = 0; i < (int) channels.size(); ++i)
  486. plays.addItem (tr ("Plays {0}", { juce::String (i + 1) + " " + juce::String::fromUTF8 (channels[(std::size_t) i].c_str()) }), i + 2);
  487. if (p.target >= (int) channels.size()) // a channel this project doesn't have (kept)
  488. plays.addItem (tr ("Plays channel {0}", { juce::String (p.target + 1) }), p.target + 2);
  489. plays.setSelectedId (p.target + 2, juce::dontSendNotification);
  490. plays.onChange = [this] {
  491. prefsNow.target = plays.getSelectedId() - 2;
  492. update();
  493. };
  494. for (auto* b : { &channel, &plays })
  495. {
  496. styleCombo (*b);
  497. addAndMakeVisible (b);
  498. }
  499. update (false);
  500. }
  501. void resized() override
  502. {
  503. auto r = getLocalBounds().reduced (8, 4);
  504. plays.setBounds (r.removeFromRight (230));
  505. r.removeFromRight (8);
  506. channel.setBounds (r.removeFromRight (150));
  507. r.removeFromRight (8);
  508. enabled.setBounds (r);
  509. }
  510. void paint (juce::Graphics& g) override
  511. {
  512. g.setColour (theme::colour::bg1);
  513. g.fillRoundedRectangle (getLocalBounds().toFloat(), theme::radius::md);
  514. }
  515. private:
  516. void update (bool notify = true)
  517. {
  518. channel.setEnabled (prefsNow.enabled);
  519. plays.setEnabled (prefsNow.enabled);
  520. if (notify && onChange)
  521. onChange (prefsNow);
  522. }
  523. prefs::MidiInputPrefs prefsNow;
  524. std::function<void (const prefs::MidiInputPrefs&)> onChange;
  525. juce::ToggleButton enabled;
  526. juce::ComboBox channel, plays;
  527. };
  528. class MidiPage final : public ScrollingPage,
  529. private juce::ChangeListener,
  530. private juce::ListBoxModel
  531. {
  532. public:
  533. MidiPage (AudioEngine& e, SettingsHooks h, bool keep) : engine (e), hooks (std::move (h)), persistent (keep)
  534. {
  535. for (auto* c : std::initializer_list<juce::Component*> { &inputsTitle, &notesTitle, &controllersTitle, &inputsNote, &refresh, &noDevices,
  536. &velocityLabel, &velocityBox, &velocityNote, &typeLabel, &typeValue, &pickup,
  537. &pickupNote, &linksLabel, &links, &removeLink, &removeAll })
  538. add (*c);
  539. styleNote (inputsNote, tr ("Each input device can be turned off, listen to one MIDI channel only and play a fixed channel of the "
  540. "Channel Rack instead of the selected ones."));
  541. styleLabel (noDevices, tr ("No MIDI input devices found. Connect one and refresh the list."));
  542. refresh.onClick = [this] { rebuildInputs(); };
  543. styleLabel (velocityLabel, tr ("Velocity"));
  544. velocityBox.addItem (tr ("As played"), 1);
  545. velocityBox.addItem (tr ("Soft (light playing sounds louder)"), 2);
  546. velocityBox.addItem (tr ("Hard (needs harder playing)"), 3);
  547. velocityBox.addItem (tr ("Fixed (every note at full velocity)"), 4);
  548. velocityBox.onChange = [this] {
  549. auto p = engine.getMidiPrefs();
  550. p.velocity = (prefs::VelocityCurve) (velocityBox.getSelectedId() - 1);
  551. set (p);
  552. };
  553. styleCombo (velocityBox);
  554. styleNote (velocityNote, tr ("For notes from MIDI devices (the typing keyboard always plays at the Piano Roll's velocity)."));
  555. styleLabel (typeLabel, tr ("Controller type"));
  556. typeLabel.setJustificationType (juce::Justification::topLeft);
  557. styleLabel (typeValue, tr ("Generic controller: link a knob or fader with a control's right-click menu > Link to MIDI controller."), false);
  558. typeValue.setMinimumHorizontalScale (1.0f);
  559. typeValue.setJustificationType (juce::Justification::topLeft);
  560. styleToggle (pickup, tr ("Pickup (takeover mode)"));
  561. pickup.onClick = [this] {
  562. auto p = engine.getMidiPrefs();
  563. p.pickup = pickup.getToggleState();
  564. set (p);
  565. };
  566. styleNote (pickupNote, tr ("A controller moves a parameter only after reaching its current value, so nothing jumps."));
  567. styleLabel (linksLabel, tr ("Links in this project"));
  568. linksLabel.setJustificationType (juce::Justification::topLeft);
  569. links.setModel (this);
  570. links.setRowHeight (24);
  571. links.setColour (juce::ListBox::backgroundColourId, theme::colour::bg0);
  572. links.setColour (juce::ListBox::outlineColourId, theme::colour::border);
  573. links.setOutlineThickness (1);
  574. removeLink.onClick = [this] {
  575. const auto row = links.getSelectedRow();
  576. if (juce::isPositiveAndBelow (row, (int) engine.getSong().midiLinks.size()))
  577. engine.editSong ([row] (Song& s) { s.midiLinks.erase (s.midiLinks.begin() + row); });
  578. };
  579. removeAll.onClick = [this] { engine.editSong ([] (Song& s) { s.midiLinks.clear(); }); };
  580. if (! persistent)
  581. {
  582. // --snapshot: devices of a typical setup, and two links
  583. sampleDevices = { "Arturia KeyLab 49", "Akai MPD218" };
  584. engine.editSong ([] (Song& s) {
  585. AutomationTarget volume;
  586. volume.kind = AutomationTargetKind::mixerVolume;
  587. volume.index = 0;
  588. AutomationTarget pan;
  589. pan.kind = AutomationTargetKind::channelPan;
  590. pan.index = 0;
  591. s.midiLinks = { { 7, -1, volume }, { 10, 0, pan } };
  592. }, false);
  593. }
  594. engine.addChangeListener (this);
  595. rebuildInputs();
  596. load();
  597. }
  598. ~MidiPage() override { engine.removeChangeListener (this); }
  599. private:
  600. int layout (int width) override
  601. {
  602. Rows r (width);
  603. r.section (inputsTitle, true);
  604. inputsNote.setBounds (r.full (32).translated (0, -4));
  605. if (rows.empty())
  606. noDevices.setBounds (r.full (24));
  607. for (auto& row : rows)
  608. row->setBounds (r.full (34).translated (0, -4));
  609. refresh.setBounds (r.full().removeFromLeft (150));
  610. r.section (notesTitle);
  611. velocityBox.setBounds (r.row (velocityLabel, 300));
  612. velocityNote.setBounds (r.indented (16).translated (0, -6));
  613. r.section (controllersTitle);
  614. {
  615. auto row = r.row (typeLabel, 0, 36);
  616. typeLabel.setBounds (typeLabel.getBounds().withTrimmedTop (6));
  617. typeValue.setBounds (row.withTrimmedTop (6));
  618. }
  619. pickup.setBounds (r.full (24));
  620. pickupNote.setBounds (r.full (16).translated (0, -6));
  621. {
  622. auto row = r.row (linksLabel, 0, 110);
  623. linksLabel.setBounds (linksLabel.getBounds().withTrimmedTop (4));
  624. auto buttons = row.removeFromRight (120);
  625. row.removeFromRight (8);
  626. links.setBounds (row);
  627. removeLink.setBounds (buttons.removeFromTop (26));
  628. buttons.removeFromTop (6);
  629. removeAll.setBounds (buttons.removeFromTop (26));
  630. }
  631. return r.bottom();
  632. }
  633. void set (const prefs::MidiPrefs& p)
  634. {
  635. if (loading)
  636. return;
  637. engine.setMidiPrefs (p, persistent);
  638. if (hooks.midiChanged)
  639. hooks.midiChanged();
  640. }
  641. void load()
  642. {
  643. const juce::ScopedValueSetter<bool> guard (loading, true);
  644. const auto& p = engine.getMidiPrefs();
  645. velocityBox.setSelectedId ((int) p.velocity + 1, juce::dontSendNotification);
  646. pickup.setToggleState (p.pickup, juce::dontSendNotification);
  647. links.updateContent();
  648. links.repaint();
  649. removeLink.setEnabled (links.getSelectedRow() >= 0);
  650. removeAll.setEnabled (! engine.getSong().midiLinks.empty());
  651. }
  652. void rebuildInputs()
  653. {
  654. rows.clear();
  655. juce::StringArray names;
  656. if (persistent)
  657. for (const auto& d : juce::MidiInput::getAvailableDevices())
  658. names.add (d.name);
  659. else
  660. names = sampleDevices;
  661. std::vector<std::string> channels;
  662. for (const auto& c : engine.getSong().channels)
  663. channels.push_back (c.name);
  664. for (const auto& name : names)
  665. {
  666. auto row = std::make_unique<MidiInputRow> (name, engine.getMidiPrefs().input (name.toStdString()), channels,
  667. [this] (const prefs::MidiInputPrefs& in) {
  668. auto p = engine.getMidiPrefs();
  669. p.setInput (in);
  670. set (p);
  671. });
  672. add (*row);
  673. rows.push_back (std::move (row));
  674. }
  675. noDevices.setVisible (rows.empty());
  676. resized();
  677. }
  678. // links list
  679. int getNumRows() override { return juce::jmax (1, (int) engine.getSong().midiLinks.size()); }
  680. void paintListBoxItem (int row, juce::Graphics& g, int width, int height, bool selected) override
  681. {
  682. const auto& list = engine.getSong().midiLinks;
  683. if (list.empty())
  684. {
  685. g.setFont (theme::font (theme::type::small));
  686. g.setColour (theme::colour::textDisabled);
  687. g.drawText (tr ("(none yet)"), 10, 0, width - 20, height, juce::Justification::centredLeft);
  688. return;
  689. }
  690. if (selected)
  691. {
  692. g.setColour (theme::colour::accentMuted);
  693. g.fillRect (0, 0, width, height);
  694. }
  695. const auto& link = list[(std::size_t) row];
  696. g.setFont (theme::font (theme::type::body));
  697. g.setColour (theme::colour::info);
  698. const auto source = "CC " + juce::String (link.cc) + (link.channel >= 0 ? juce::String::fromUTF8 (" \xc2\xb7 ") + tr ("ch. {0}", { juce::String (link.channel + 1) }) : juce::String());
  699. g.drawText (source, 10, 0, 110, height, juce::Justification::centredLeft);
  700. g.setColour (theme::colour::textPrimary);
  701. g.drawText (describeAutomationTarget (engine, link.target), 124, 0, width - 134, height, juce::Justification::centredLeft, true);
  702. }
  703. void selectedRowsChanged (int) override { removeLink.setEnabled (links.getSelectedRow() >= 0 && ! engine.getSong().midiLinks.empty()); }
  704. void changeListenerCallback (juce::ChangeBroadcaster*) override
  705. {
  706. if (engine.getSong().channels.size() != channelCount)
  707. {
  708. channelCount = engine.getSong().channels.size();
  709. rebuildInputs();
  710. }
  711. load();
  712. }
  713. AudioEngine& engine;
  714. SettingsHooks hooks;
  715. bool persistent;
  716. bool loading = false;
  717. std::size_t channelCount = 0;
  718. juce::StringArray sampleDevices;
  719. std::vector<std::unique_ptr<MidiInputRow>> rows;
  720. SectionTitle inputsTitle { tr ("Input devices") }, notesTitle { tr ("Notes") }, controllersTitle { tr ("Controllers") };
  721. juce::Label inputsNote, noDevices, velocityLabel, velocityNote, typeLabel, typeValue, pickupNote, linksLabel;
  722. juce::TextButton refresh { tr ("Refresh device list") }, removeLink { tr ("Remove link") }, removeAll { tr ("Remove all") };
  723. juce::ComboBox velocityBox;
  724. juce::ToggleButton pickup;
  725. juce::ListBox links;
  726. };
  727. } // namespace
  728. std::unique_ptr<juce::Component> makeGeneralPage (AudioEngine& engine, const SettingsHooks& hooks, bool persistent)
  729. {
  730. return std::make_unique<GeneralPage> (engine, hooks, persistent);
  731. }
  732. std::unique_ptr<juce::Component> makeAudioPage (AudioEngine& engine, bool persistent)
  733. {
  734. juce::ignoreUnused (persistent);
  735. return std::make_unique<AudioPage> (engine);
  736. }
  737. std::unique_ptr<juce::Component> makeMidiPage (AudioEngine& engine, const SettingsHooks& hooks, bool persistent)
  738. {
  739. return std::make_unique<MidiPage> (engine, hooks, persistent);
  740. }
  741. } // namespace lynx