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.

1472 lines
46KB

  1. /*
  2. Copyright (C) 2006-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. Copyright (C) 2017 Xenakios
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "PluginProcessor.h"
  17. #include "PluginEditor.h"
  18. #include <array>
  19. extern String g_plugintitle;
  20. //==============================================================================
  21. PaulstretchpluginAudioProcessorEditor::PaulstretchpluginAudioProcessorEditor(PaulstretchpluginAudioProcessor& p)
  22. : AudioProcessorEditor(&p),
  23. m_wavecomponent(p.m_afm,p.m_thumb.get()),
  24. processor(p), m_perfmeter(&p),
  25. m_wavefilter_tab(TabbedButtonBar::TabsAtTop),
  26. m_free_filter_component(p.getStretchSource()->getMutex())
  27. {
  28. m_wave_container = new Component;
  29. m_free_filter_component.set_envelope(processor.m_free_filter_envelope);
  30. m_free_filter_component.TimeFromNormalized = [this](double x)
  31. {
  32. //return jmap<double>(pow(x, 3.0), 0.0, 1.0, 30.0, processor.getSampleRateChecked()/2.0);
  33. return 30.0*pow(1.05946309436, x*115.0);
  34. };
  35. m_free_filter_component.ValueFromNormalized = [this](double x)
  36. {
  37. return jmap<double>(x, 0.0, 1.0, -48.0, 12.0);
  38. };
  39. m_wavefilter_tab.setTabBarDepth(20);
  40. addAndMakeVisible(&m_perfmeter);
  41. addAndMakeVisible(&m_import_button);
  42. m_import_button.setButtonText("Import file...");
  43. m_import_button.onClick = [this]() { chooseFile(); };
  44. addAndMakeVisible(&m_settings_button);
  45. m_settings_button.setButtonText("Settings...");
  46. m_settings_button.onClick = [this]() { showSettingsMenu(); };
  47. addAndMakeVisible(&m_info_label);
  48. m_info_label.setJustificationType(Justification::centredRight);
  49. m_wavecomponent.GetFileCallback = [this]() { return processor.getAudioFile(); };
  50. const auto& pars = processor.getParameters();
  51. for (int i=0;i<pars.size();++i)
  52. {
  53. AudioProcessorParameterWithID* parid = dynamic_cast<AudioProcessorParameterWithID*>(pars[i]);
  54. jassert(parid);
  55. bool notifyonlyonrelease = false;
  56. if (parid->paramID.startsWith("fftsize") || parid->paramID.startsWith("numoutchans")
  57. || parid->paramID.startsWith("numinchans"))
  58. notifyonlyonrelease = true;
  59. int group_id = -1;
  60. if (i == cpi_harmonicsbw || i == cpi_harmonicsfreq || i == cpi_harmonicsgauss || i == cpi_numharmonics)
  61. group_id = 0;
  62. if (i == cpi_octavesm2 || i == cpi_octavesm1 || i == cpi_octaves0 || i == cpi_octaves1 || i == cpi_octaves15 ||
  63. i == cpi_octaves2 || i==cpi_octaves_extra1 || i==cpi_octaves_extra2)
  64. group_id = -2; // -2 for not included in the main parameters page
  65. if (i >= (int)cpi_octaves_ratio0 && i <= (int)cpi_octaves_ratio7)
  66. group_id = -2;
  67. if (i >= (int)cpi_enable_spec_module0 && i <= (int)cpi_enable_spec_module8)
  68. group_id = -2;
  69. if (i == cpi_tonalvsnoisebw || i == cpi_tonalvsnoisepreserve)
  70. group_id = 1;
  71. if (i == cpi_filter_low || i == cpi_filter_high)
  72. group_id = 6;
  73. if (i == cpi_compress)
  74. group_id = 8;
  75. if (i == cpi_spreadamount)
  76. group_id = 5;
  77. if (i == cpi_frequencyshift)
  78. group_id = 2;
  79. if (i == cpi_pitchshift)
  80. group_id = 3;
  81. if (i == cpi_freefilter_scaley || i == cpi_freefilter_shiftx || i == cpi_freefilter_shifty ||
  82. i == cpi_freefilter_tilty || i == cpi_freefilter_randomy_amount || i == cpi_freefilter_randomy_numbands
  83. || i == cpi_freefilter_randomy_rate)
  84. group_id = 7;
  85. if (group_id >= -1)
  86. {
  87. m_parcomps.emplace_back(std::make_unique<ParameterComponent>(pars[i], notifyonlyonrelease));
  88. m_parcomps.back()->m_group_id = group_id;
  89. if (group_id >= -1)
  90. addAndMakeVisible(m_parcomps.back().get());
  91. }
  92. else
  93. {
  94. m_parcomps.push_back(nullptr);
  95. }
  96. }
  97. //addAndMakeVisible(&m_specvis);
  98. m_wave_container->addAndMakeVisible(&m_zs);
  99. m_zs.RangeChanged = [this](Range<double> r)
  100. {
  101. m_wavecomponent.setViewRange(r);
  102. processor.m_wave_view_range = r;
  103. };
  104. m_zs.setRange(processor.m_wave_view_range, true);
  105. m_wavecomponent.ViewRangeChangedCallback = [this](Range<double> rng)
  106. {
  107. m_zs.setRange(rng, false);
  108. };
  109. m_wavecomponent.TimeSelectionChangedCallback = [this](Range<double> range, int which)
  110. {
  111. *processor.getFloatParameter(cpi_soundstart) = range.getStart();
  112. *processor.getFloatParameter(cpi_soundend) = range.getEnd();
  113. };
  114. m_wavecomponent.CursorPosCallback = [this]()
  115. {
  116. return processor.getStretchSource()->getInfilePositionPercent();
  117. };
  118. m_wavecomponent.SeekCallback = [this](double pos)
  119. {
  120. if (processor.getStretchSource()->getPlayRange().contains(pos))
  121. processor.getStretchSource()->seekPercent(pos);
  122. };
  123. m_wavecomponent.ShowFileCacheRange = true;
  124. m_spec_order_ed.setSource(processor.getStretchSource());
  125. addAndMakeVisible(&m_spec_order_ed);
  126. m_spec_order_ed.ModuleSelectedCallback = [this](int id)
  127. {
  128. for (int i = 0; i < m_parcomps.size(); ++i)
  129. {
  130. if (m_parcomps[i] != nullptr)
  131. {
  132. if (m_parcomps[i]->m_group_id == id)
  133. m_parcomps[i]->setHighLighted(true);
  134. else
  135. m_parcomps[i]->setHighLighted(false);
  136. }
  137. }
  138. };
  139. m_spec_order_ed.ModuleOrderOrEnabledChangedCallback = [this]()
  140. {
  141. /*
  142. const auto& specord = processor.getStretchSource()->getSpectrumProcessOrder();
  143. for (int i = 0; i < specord.size(); ++i)
  144. {
  145. int grtofind = specord[i].m_index;
  146. for (int j = 0; j < m_parcomps.size(); ++j)
  147. {
  148. int gid = m_parcomps[j]->m_group_id;
  149. if (gid == grtofind)
  150. {
  151. m_parcomps[j]->setEnabled(specord[i].m_enabled);
  152. }
  153. }
  154. }
  155. */
  156. processor.setDirty();
  157. };
  158. m_ratiomixeditor.GetParameterValue = [this](int which, int index)
  159. {
  160. if (which == 0)
  161. return (double)*processor.getFloatParameter((int)cpi_octaves_ratio0 + index);
  162. if (which == 1)
  163. {
  164. if (index == 0)
  165. return (double)*processor.getFloatParameter(cpi_octavesm2);
  166. if (index == 1)
  167. return (double)*processor.getFloatParameter(cpi_octavesm1);
  168. if (index == 2)
  169. return (double)*processor.getFloatParameter(cpi_octaves0);
  170. if (index == 3)
  171. return (double)*processor.getFloatParameter(cpi_octaves1);
  172. if (index == 4)
  173. return (double)*processor.getFloatParameter(cpi_octaves15);
  174. if (index == 5)
  175. return (double)*processor.getFloatParameter(cpi_octaves2);
  176. if (index == 6)
  177. return (double)*processor.getFloatParameter(cpi_octaves_extra1);
  178. if (index == 7)
  179. return (double)*processor.getFloatParameter(cpi_octaves_extra2);
  180. }
  181. return 0.0;
  182. };
  183. m_ratiomixeditor.OnRatioLevelChanged = [this](int index, double val)
  184. {
  185. if (index == 0)
  186. *processor.getFloatParameter(cpi_octavesm2) = val;
  187. if (index == 1)
  188. *processor.getFloatParameter(cpi_octavesm1) = val;
  189. if (index == 2)
  190. *processor.getFloatParameter(cpi_octaves0) = val;
  191. if (index == 3)
  192. *processor.getFloatParameter(cpi_octaves1) = val;
  193. if (index == 4)
  194. *processor.getFloatParameter(cpi_octaves15) = val;
  195. if (index == 5)
  196. *processor.getFloatParameter(cpi_octaves2) = val;
  197. if (index == 6)
  198. *processor.getFloatParameter(cpi_octaves_extra1) = val;
  199. if (index == 7)
  200. *processor.getFloatParameter(cpi_octaves_extra2) = val;
  201. };
  202. m_ratiomixeditor.OnRatioChanged = [this](int index, double val)
  203. {
  204. *processor.getFloatParameter((int)cpi_octaves_ratio0 + index) = val;
  205. };
  206. m_wave_container->addAndMakeVisible(&m_wavecomponent);
  207. m_wavefilter_tab.addTab("Waveform", Colours::white, m_wave_container, true);
  208. m_wavefilter_tab.addTab("Ratio mixer", Colours::white, &m_ratiomixeditor, false);
  209. m_wavefilter_tab.addTab("Free filter", Colours::white, &m_free_filter_component, false);
  210. addAndMakeVisible(&m_wavefilter_tab);
  211. setSize (1200, 320+14*25);
  212. startTimer(1, 100);
  213. startTimer(2, 1000);
  214. startTimer(3, 200);
  215. m_wavecomponent.startTimer(100);
  216. }
  217. PaulstretchpluginAudioProcessorEditor::~PaulstretchpluginAudioProcessorEditor()
  218. {
  219. }
  220. void PaulstretchpluginAudioProcessorEditor::paint (Graphics& g)
  221. {
  222. g.fillAll(Colours::darkgrey);
  223. }
  224. void PaulstretchpluginAudioProcessorEditor::resized()
  225. {
  226. m_import_button.setBounds(1, 1, 60, 24);
  227. m_import_button.changeWidthToFitText();
  228. m_settings_button.setBounds(m_import_button.getRight() + 1, 1, 60, 24);
  229. m_settings_button.changeWidthToFitText();
  230. m_perfmeter.setBounds(m_settings_button.getRight() + 1, 1, 150, 24);
  231. m_info_label.setBounds(m_perfmeter.getRight() + 1, m_settings_button.getY(),
  232. getWidth()- m_perfmeter.getRight()-1, 24);
  233. int w = getWidth();
  234. int xoffs = 1;
  235. int yoffs = 30;
  236. int div = w / 5;
  237. //std::vector<std::vector<int>> layout;
  238. //layout.emplace_back(cpi_capture_enabled, cpi_passthrough, cpi_pause_enabled, cpi_freeze);
  239. //layout.emplace_back(cpi_main_volume, cpi_num_inchans, cpi_num_outchans);
  240. m_parcomps[cpi_capture_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  241. //xoffs += div;
  242. //m_parcomps[cpi_max_capture_len]->setBounds(xoffs, yoffs, div - 1, 24);
  243. xoffs += div;
  244. m_parcomps[cpi_passthrough]->setBounds(xoffs, yoffs, div - 1, 24);
  245. xoffs += div;
  246. m_parcomps[cpi_pause_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  247. xoffs += div;
  248. m_parcomps[cpi_freeze]->setBounds(xoffs, yoffs, div - 1, 24);
  249. xoffs += div;
  250. m_parcomps[cpi_bypass_stretch]->setBounds(xoffs, yoffs, div - 1, 24);
  251. xoffs = 1;
  252. yoffs += 25;
  253. div = w / 3;
  254. m_parcomps[cpi_main_volume]->setBounds(xoffs, yoffs, div-1, 24);
  255. xoffs += div;
  256. m_parcomps[cpi_num_inchans]->setBounds(xoffs, yoffs, div - 1, 24);
  257. xoffs += div;
  258. m_parcomps[cpi_num_outchans]->setBounds(xoffs, yoffs, div-1, 24);
  259. div = w / 2;
  260. xoffs = 1;
  261. yoffs += 25;
  262. m_parcomps[cpi_fftsize]->setBounds(xoffs, yoffs, div - 1, 24);
  263. xoffs += div;
  264. m_parcomps[cpi_stretchamount]->setBounds(xoffs, yoffs, div - 1, 24);
  265. xoffs = 1;
  266. yoffs += 25;
  267. m_parcomps[cpi_pitchshift]->setBounds(xoffs, yoffs, div - 1, 24);
  268. xoffs += div;
  269. m_parcomps[cpi_frequencyshift]->setBounds(xoffs, yoffs, div - 1, 24);
  270. /*
  271. xoffs = 1;
  272. yoffs += 25;
  273. m_parcomps[cpi_octavesm2]->setBounds(xoffs, yoffs, div - 1, 24);
  274. xoffs += div;
  275. m_parcomps[cpi_octavesm1]->setBounds(xoffs, yoffs, div - 1, 24);
  276. xoffs = 1;
  277. yoffs += 25;
  278. m_parcomps[cpi_octaves0]->setBounds(xoffs, yoffs, div - 1, 24);
  279. xoffs += div;
  280. m_parcomps[cpi_octaves1]->setBounds(xoffs, yoffs, div - 1, 24);
  281. xoffs = 1;
  282. yoffs += 25;
  283. m_parcomps[cpi_octaves15]->setBounds(xoffs, yoffs, div - 1, 24);
  284. xoffs += div;
  285. m_parcomps[cpi_octaves2]->setBounds(xoffs, yoffs, div - 1, 24);
  286. */
  287. xoffs = 1;
  288. yoffs += 25;
  289. m_parcomps[cpi_numharmonics]->setBounds(xoffs, yoffs, div - 1, 24);
  290. xoffs += div;
  291. m_parcomps[cpi_harmonicsfreq]->setBounds(xoffs, yoffs, div - 1, 24);
  292. xoffs = 1;
  293. yoffs += 25;
  294. m_parcomps[cpi_harmonicsbw]->setBounds(xoffs, yoffs, div - 1, 24);
  295. xoffs += div;
  296. m_parcomps[cpi_harmonicsgauss]->setBounds(xoffs, yoffs, div - 1, 24);
  297. xoffs = 1;
  298. yoffs += 25;
  299. m_parcomps[cpi_spreadamount]->setBounds(xoffs, yoffs, div - 1, 24);
  300. xoffs += div;
  301. m_parcomps[cpi_compress]->setBounds(xoffs, yoffs, div - 1, 24);
  302. xoffs = 1;
  303. yoffs += 25;
  304. m_parcomps[cpi_tonalvsnoisebw]->setBounds(xoffs, yoffs, div - 1, 24);
  305. xoffs += div;
  306. m_parcomps[cpi_tonalvsnoisepreserve]->setBounds(xoffs, yoffs, div - 1, 24);
  307. xoffs = 1;
  308. yoffs += 25;
  309. // filter here
  310. m_parcomps[cpi_filter_low]->setBounds(xoffs, yoffs, div - 1, 24);
  311. xoffs += div;
  312. m_parcomps[cpi_filter_high]->setBounds(xoffs, yoffs, div - 1, 24);
  313. xoffs = 1;
  314. yoffs += 25;
  315. m_parcomps[cpi_loopxfadelen]->setBounds(xoffs, yoffs, div - 1, 24);
  316. xoffs += div;
  317. m_parcomps[cpi_onsetdetection]->setBounds(xoffs, yoffs, div - 1, 24);
  318. xoffs = 1;
  319. yoffs += 25;
  320. m_parcomps[cpi_soundstart]->setBounds(xoffs, yoffs, div - 1, 24);
  321. xoffs += div;
  322. m_parcomps[cpi_soundend]->setBounds(xoffs, yoffs, div - 1, 24);
  323. xoffs = 1;
  324. yoffs += 25;
  325. m_parcomps[cpi_freefilter_shiftx]->setBounds(xoffs, yoffs, div - 1, 24);
  326. xoffs += div;
  327. m_parcomps[cpi_freefilter_shifty]->setBounds(xoffs, yoffs, div - 1, 24);
  328. xoffs = 1;
  329. yoffs += 25;
  330. m_parcomps[cpi_freefilter_scaley]->setBounds(xoffs, yoffs, div - 1, 24);
  331. xoffs += div;
  332. m_parcomps[cpi_freefilter_tilty]->setBounds(xoffs, yoffs, div - 1, 24);
  333. xoffs = 1;
  334. yoffs += 25;
  335. div = w / 3;
  336. m_parcomps[cpi_freefilter_randomy_numbands]->setBounds(xoffs, yoffs, div - 1, 24);
  337. xoffs += div;
  338. m_parcomps[cpi_freefilter_randomy_rate]->setBounds(xoffs, yoffs, div - 1, 24);
  339. xoffs += div;
  340. m_parcomps[cpi_freefilter_randomy_amount]->setBounds(xoffs, yoffs, div - 1, 24);
  341. yoffs += 25;
  342. int remain_h = getHeight() - 1 - yoffs;
  343. m_spec_order_ed.setBounds(1, yoffs, getWidth() - 2, remain_h / 9 * 1);
  344. m_wavefilter_tab.setBounds(1, m_spec_order_ed.getBottom() + 1, getWidth() - 2, remain_h / 9 * 8);
  345. m_wavecomponent.setBounds(m_wave_container->getX(), 0, m_wave_container->getWidth(),
  346. m_wave_container->getHeight()-16);
  347. m_zs.setBounds(m_wave_container->getX(), m_wavecomponent.getBottom(), m_wave_container->getWidth(), 15);
  348. //m_wavecomponent.setBounds(1, m_spec_order_ed.getBottom()+1, getWidth()-2, remain_h/5*4);
  349. }
  350. void PaulstretchpluginAudioProcessorEditor::timerCallback(int id)
  351. {
  352. if (id == 1)
  353. {
  354. for (int i = 0; i < m_parcomps.size(); ++i)
  355. {
  356. if (m_parcomps[i]!=nullptr)
  357. m_parcomps[i]->updateComponent();
  358. }
  359. if (processor.isRecordingEnabled())
  360. {
  361. m_wavecomponent.setRecordingPosition(processor.getRecordingPositionPercent());
  362. } else
  363. m_wavecomponent.setRecordingPosition(-1.0);
  364. String infotext;
  365. if (processor.m_show_technical_info)
  366. {
  367. infotext += String(processor.m_prepare_count)+" ";
  368. infotext += String(processor.getStretchSource()->m_param_change_count);
  369. infotext += " param changes ";
  370. }
  371. infotext += m_last_err + " [FFT size " +
  372. String(processor.getStretchSource()->getFFTSize())+"]";
  373. double outlen = processor.getStretchSource()->getOutputDurationSecondsForRange(processor.getStretchSource()->getPlayRange(),
  374. processor.getStretchSource()->getFFTSize());
  375. infotext += " [Output length " + secondsToString2(outlen)+"]";
  376. if (processor.m_abnormal_output_samples > 0)
  377. infotext += " " + String(processor.m_abnormal_output_samples) + " invalid sample values";
  378. if (processor.isNonRealtime())
  379. infotext += " (offline rendering)";
  380. if (processor.m_playposinfo.isPlaying)
  381. infotext += " "+String(processor.m_playposinfo.timeInSeconds,1);
  382. if (processor.m_show_technical_info)
  383. infotext += " " + String(m_wavecomponent.m_image_init_count) + " " + String(m_wavecomponent.m_image_update_count)+ " ";
  384. if (processor.m_offline_render_state >= 0 && processor.m_offline_render_state <= 100)
  385. infotext += String(processor.m_offline_render_state)+"%";
  386. m_info_label.setText(infotext, dontSendNotification);
  387. m_perfmeter.repaint();
  388. }
  389. if (id == 2)
  390. {
  391. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  392. if (processor.m_state_dirty)
  393. {
  394. //m_spec_order_ed.setSource(processor.getStretchSource());
  395. processor.m_state_dirty = false;
  396. }
  397. }
  398. if (id == 3)
  399. {
  400. processor.m_free_filter_envelope->updateMinMaxValues();
  401. m_free_filter_component.repaint();
  402. m_spec_order_ed.repaint();
  403. }
  404. }
  405. bool PaulstretchpluginAudioProcessorEditor::isInterestedInFileDrag(const StringArray & files)
  406. {
  407. if (files.size() == 0)
  408. return false;
  409. File f(files[0]);
  410. String extension = f.getFileExtension().toLowerCase();
  411. if (processor.m_afm->getWildcardForAllFormats().containsIgnoreCase(extension))
  412. return true;
  413. return false;
  414. }
  415. void PaulstretchpluginAudioProcessorEditor::filesDropped(const StringArray & files, int x, int y)
  416. {
  417. if (files.size() > 0)
  418. {
  419. File f(files[0]);
  420. processor.setAudioFile(f);
  421. toFront(true);
  422. }
  423. }
  424. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  425. {
  426. String initiallocfn = processor.m_propsfile->m_props_file->getValue("importfilefolder",
  427. File::getSpecialLocation(File::userHomeDirectory).getFullPathName());
  428. File initialloc(initiallocfn);
  429. String filterstring = processor.m_afm->getWildcardForAllFormats();
  430. FileChooser myChooser("Please select audio file...",
  431. initialloc,
  432. filterstring,true);
  433. if (myChooser.browseForFileToOpen())
  434. {
  435. File resu = myChooser.getResult();
  436. String pathname = resu.getFullPathName();
  437. if (pathname.startsWith("/localhost"))
  438. {
  439. pathname = pathname.substring(10);
  440. resu = File(pathname);
  441. }
  442. processor.m_propsfile->m_props_file->setValue("importfilefolder", resu.getParentDirectory().getFullPathName());
  443. m_last_err = processor.setAudioFile(resu);
  444. }
  445. }
  446. void PaulstretchpluginAudioProcessorEditor::showSettingsMenu()
  447. {
  448. PopupMenu menu;
  449. menu.addItem(4, "Reset parameters", true, false);
  450. menu.addItem(5, "Load file with plugin state", true, processor.m_load_file_with_state);
  451. menu.addItem(1, "Play when host transport running", true, processor.m_play_when_host_plays);
  452. menu.addItem(2, "Capture when host transport running", true, processor.m_capture_when_host_plays);
  453. int capturelen = *processor.getFloatParameter(cpi_max_capture_len);
  454. PopupMenu capturelenmenu;
  455. std::vector<int> capturelens{ 2,5,10,30,60,120 };
  456. for (int i=0;i<capturelens.size();++i)
  457. capturelenmenu.addItem(200+i, String(capturelens[i])+" seconds", true, capturelen == capturelens[i]);
  458. menu.addSubMenu("Capture buffer length", capturelenmenu);
  459. menu.addItem(3, "About...", true, false);
  460. #ifdef JUCE_DEBUG
  461. menu.addItem(6, "Dump preset to clipboard", true, false);
  462. #endif
  463. menu.addItem(7, "Show technical info", true, processor.m_show_technical_info);
  464. /*
  465. if (processor.m_offline_render_state==-1 || processor.m_offline_render_state == 200)
  466. menu.addItem(8, "Offline render...", true, false);
  467. else menu.addItem(9, "Cancel render", true, false);
  468. */
  469. int r = menu.show();
  470. if (r >= 200 && r < 210)
  471. {
  472. int caplen = capturelens[r - 200];
  473. *processor.getFloatParameter(cpi_max_capture_len) = (float)caplen;
  474. }
  475. if (r == 1)
  476. {
  477. toggleBool(processor.m_play_when_host_plays);
  478. }
  479. if (r == 2)
  480. {
  481. toggleBool(processor.m_capture_when_host_plays);
  482. }
  483. if (r == 4)
  484. {
  485. processor.resetParameters();
  486. }
  487. if (r == 5)
  488. {
  489. toggleBool(processor.m_load_file_with_state);
  490. }
  491. if (r == 3)
  492. {
  493. String fftlib = fftwf_version;
  494. String juceversiontxt = String("JUCE ") + String(JUCE_MAJOR_VERSION) + "." + String(JUCE_MINOR_VERSION);
  495. String title = g_plugintitle;
  496. #ifdef JUCE_DEBUG
  497. title += " (DEBUG)";
  498. #endif
  499. AlertWindow::showMessageBoxAsync(AlertWindow::InfoIcon,
  500. title,
  501. "Plugin for extreme time stretching and other sound processing\nBuilt on " + String(__DATE__) + " " + String(__TIME__) + "\n"
  502. "Copyright (C) 2006-2011 Nasca Octavian Paul, Tg. Mures, Romania\n"
  503. "(C) 2017-2018 Xenakios\n\n"
  504. "Using " + fftlib + " for FFT\n\n"
  505. + juceversiontxt + " (c) Roli. Used under the GPL license.\n\n"
  506. "GPL licensed source code for this plugin at : https://bitbucket.org/xenakios/paulstretchplugin/overview\n"
  507. , "OK",
  508. this);
  509. }
  510. if (r == 6)
  511. {
  512. ValueTree tree = processor.getStateTree(true,true);
  513. MemoryBlock destData;
  514. MemoryOutputStream stream(destData, true);
  515. tree.writeToStream(stream);
  516. String txt = Base64::toBase64(destData.getData(), destData.getSize());
  517. SystemClipboard::copyTextToClipboard(txt);
  518. }
  519. if (r == 7)
  520. {
  521. toggleBool(processor.m_show_technical_info);
  522. processor.m_propsfile->m_props_file->setValue("showtechnicalinfo", processor.m_show_technical_info);
  523. }
  524. if (r == 8)
  525. {
  526. #ifdef JUCE_WINDOWS
  527. processor.offlineRender(File("C:\\MusicAudio\\sourcesamples\\paultesmaus\\plugin_offline_test\\out.wav"));
  528. #else
  529. processor.offlineRender(File("/Users/teemu/AudioProjects/sourcesamples/paultests/pspout.wav"));
  530. #endif
  531. }
  532. if (r == 9)
  533. {
  534. processor.m_offline_render_cancel_requested = true;
  535. }
  536. }
  537. WaveformComponent::WaveformComponent(AudioFormatManager* afm, AudioThumbnail* thumb)
  538. {
  539. TimeSelectionChangedCallback = [](Range<double>, int) {};
  540. #ifdef JUCE_MODULE_AVAILABLE_juce_opengl
  541. if (m_use_opengl == true)
  542. m_ogl.attachTo(*this);
  543. #endif
  544. m_thumbnail = thumb;
  545. m_thumbnail->addChangeListener(this);
  546. setOpaque(true);
  547. }
  548. WaveformComponent::~WaveformComponent()
  549. {
  550. #ifdef JUCE_MODULE_AVAILABLE_juce_opengl
  551. if (m_use_opengl == true)
  552. m_ogl.detach();
  553. #endif
  554. m_thumbnail->removeChangeListener(this);
  555. }
  556. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  557. {
  558. jassert(MessageManager::getInstance()->isThisTheMessageThread());
  559. m_image_dirty = true;
  560. //repaint();
  561. }
  562. void WaveformComponent::updateCachedImage()
  563. {
  564. Graphics tempg(m_waveimage);
  565. tempg.fillAll(Colours::black);
  566. tempg.setColour(Colours::darkgrey);
  567. double thumblen = m_thumbnail->getTotalLength();
  568. m_thumbnail->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  569. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  570. m_image_dirty = false;
  571. ++m_image_update_count;
  572. }
  573. void WaveformComponent::paint(Graphics & g)
  574. {
  575. jassert(GetFileCallback);
  576. //Logger::writeToLog("Waveform component paint");
  577. g.fillAll(Colours::black);
  578. g.setColour(Colours::darkgrey);
  579. g.fillRect(0, 0, getWidth(), m_topmargin);
  580. if (m_thumbnail == nullptr || m_thumbnail->getTotalLength() < 0.01)
  581. {
  582. g.setColour(Colours::aqua.darker());
  583. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  584. return;
  585. }
  586. g.setColour(Colours::lightslategrey);
  587. double thumblen = m_thumbnail->getTotalLength();
  588. double tick_interval = 1.0;
  589. if (thumblen > 60.0)
  590. tick_interval = 5.0;
  591. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  592. {
  593. float tickxcor = (float)jmap<double>(secs,
  594. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  595. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  596. }
  597. bool m_use_cached_image = true;
  598. if (m_use_cached_image == true)
  599. {
  600. if (m_image_dirty == true || m_waveimage.getWidth() != getWidth()
  601. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  602. {
  603. if (m_waveimage.getWidth() != getWidth()
  604. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  605. {
  606. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  607. ++m_image_init_count;
  608. }
  609. updateCachedImage();
  610. }
  611. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  612. }
  613. else
  614. {
  615. g.setColour(Colours::darkgrey);
  616. m_thumbnail->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  617. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  618. }
  619. if (m_is_at_selection_drag_area)
  620. g.setColour(Colours::white.withAlpha(0.6f));
  621. else
  622. g.setColour(Colours::white.withAlpha(0.5f));
  623. double sel_len = m_time_sel_end - m_time_sel_start;
  624. //if (sel_len > 0.0 && sel_len < 1.0)
  625. {
  626. int xcorleft = normalizedToViewX<int>(m_time_sel_start);
  627. int xcorright = normalizedToViewX<int>(m_time_sel_end);
  628. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  629. }
  630. if (m_file_cached.first.getLength() > 0.0 &&
  631. (bool)ShowFileCacheRange.getValue())
  632. {
  633. g.setColour(Colours::red.withAlpha(0.2f));
  634. int xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  635. int xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  636. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  637. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  638. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  639. if (xcorright - xcorleft>0)
  640. {
  641. g.setColour(Colours::blue.withAlpha(0.2f));
  642. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  643. }
  644. }
  645. g.setColour(Colours::white);
  646. if (CursorPosCallback)
  647. {
  648. g.fillRect(normalizedToViewX<int>(CursorPosCallback()), m_topmargin, 1, getHeight() - m_topmargin);
  649. }
  650. if (m_rec_pos >= 0.0)
  651. {
  652. g.setColour(Colours::lightpink);
  653. g.fillRect(normalizedToViewX<int>(m_rec_pos), m_topmargin, 1, getHeight() - m_topmargin);
  654. }
  655. g.setColour(Colours::aqua);
  656. g.drawText(GetFileCallback().getFileName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  657. g.drawText(secondsToString2(thumblen), getWidth() - 200, m_topmargin + 2, 200, 20, Justification::topRight);
  658. }
  659. void WaveformComponent::timerCallback()
  660. {
  661. repaint();
  662. }
  663. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  664. {
  665. m_file_cached = rng;
  666. }
  667. void WaveformComponent::setTimerEnabled(bool b)
  668. {
  669. if (b == true)
  670. startTimer(100);
  671. else
  672. stopTimer();
  673. }
  674. void WaveformComponent::setViewRange(Range<double> rng)
  675. {
  676. m_view_range = rng;
  677. m_waveimage = Image();
  678. repaint();
  679. }
  680. void WaveformComponent::mouseDown(const MouseEvent & e)
  681. {
  682. m_mousedown = true;
  683. m_lock_timesel_set = true;
  684. double pos = viewXToNormalized(e.x);
  685. if (e.y < m_topmargin || e.mods.isCommandDown())
  686. {
  687. if (SeekCallback)
  688. SeekCallback(pos);
  689. m_didseek = true;
  690. }
  691. else
  692. {
  693. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  694. m_drag_time_start = pos;
  695. if (m_time_sel_drag_target == 0)
  696. {
  697. //m_time_sel_start = 0.0;
  698. //m_time_sel_end = 1.0;
  699. }
  700. }
  701. repaint();
  702. }
  703. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  704. {
  705. m_is_dragging_selection = false;
  706. m_lock_timesel_set = false;
  707. m_mousedown = false;
  708. m_didseek = false;
  709. if (m_didchangetimeselection)
  710. {
  711. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  712. m_didchangetimeselection = false;
  713. }
  714. }
  715. void WaveformComponent::mouseDrag(const MouseEvent & e)
  716. {
  717. if (m_didseek == true)
  718. return;
  719. if (m_time_sel_drag_target == 0 && e.y>=50 && m_is_dragging_selection==false)
  720. {
  721. m_time_sel_start = m_drag_time_start;
  722. m_time_sel_end = viewXToNormalized(e.x);
  723. }
  724. double curlen = m_time_sel_end - m_time_sel_start;
  725. if (m_time_sel_drag_target == 0 && m_is_at_selection_drag_area)
  726. {
  727. m_is_dragging_selection = true;
  728. double diff = m_drag_time_start - viewXToNormalized(e.x);
  729. m_time_sel_start = jlimit<double>(0.0, 1.0-curlen, m_time_sel_start - diff);
  730. m_time_sel_end = jlimit<double>(curlen, 1.0, m_time_sel_end - diff);
  731. m_drag_time_start -= diff;
  732. }
  733. curlen = m_time_sel_end - m_time_sel_start;
  734. if (m_time_sel_drag_target == 1)
  735. {
  736. m_time_sel_start = viewXToNormalized(e.x);
  737. }
  738. if (m_time_sel_drag_target == 2)
  739. {
  740. m_time_sel_end = viewXToNormalized(e.x);
  741. }
  742. if (m_time_sel_start > m_time_sel_end)
  743. {
  744. std::swap(m_time_sel_start, m_time_sel_end);
  745. if (m_time_sel_drag_target == 1)
  746. m_time_sel_drag_target = 2;
  747. else if (m_time_sel_drag_target == 2)
  748. m_time_sel_drag_target = 1;
  749. }
  750. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  751. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  752. if (TimeSelectionChangedCallback)
  753. {
  754. if (m_time_sel_end>m_time_sel_start)
  755. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  756. else
  757. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  758. }
  759. m_didchangetimeselection = true;
  760. repaint();
  761. }
  762. void WaveformComponent::mouseMove(const MouseEvent & e)
  763. {
  764. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  765. if (m_time_sel_drag_target == 0)
  766. setMouseCursor(MouseCursor::NormalCursor);
  767. if (m_time_sel_drag_target == 1)
  768. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  769. if (m_time_sel_drag_target == 2)
  770. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  771. Range<int> temp(normalizedToViewX<int>(m_time_sel_start), normalizedToViewX<int>(m_time_sel_end));
  772. m_is_at_selection_drag_area = temp.contains(e.x) == true && e.y < 50;
  773. }
  774. void WaveformComponent::mouseDoubleClick(const MouseEvent & e)
  775. {
  776. m_time_sel_start = 0.0;
  777. m_time_sel_end = 1.0;
  778. TimeSelectionChangedCallback({ 0.0,1.0 }, 0);
  779. repaint();
  780. }
  781. void WaveformComponent::mouseWheelMove(const MouseEvent & e, const MouseWheelDetails & wd)
  782. {
  783. return;
  784. /*
  785. double factor = 0.9;
  786. if (wd.deltaY < 0.0)
  787. factor = 1.11111;
  788. double normt = viewXToNormalized(e.x);
  789. double curlen = m_view_range.getLength();
  790. double newlen = curlen * factor;
  791. double oldt0 = m_view_range.getStart();
  792. double oldt1 = m_view_range.getEnd();
  793. double t0 = jlimit(0.0,1.0, normt + (curlen - newlen));
  794. double t1 = jlimit(0.0,1.0, t0+newlen);
  795. jassert(t1 > t0);
  796. m_view_range = { t0,t1 };
  797. //m_view_range = m_view_range.constrainRange({ 0.0, 1.0 });
  798. jassert(m_view_range.getStart() >= 0.0 && m_view_range.getEnd() <= 1.0);
  799. jassert(m_view_range.getLength() > 0.001);
  800. if (ViewRangeChangedCallback)
  801. ViewRangeChangedCallback(m_view_range);
  802. m_image_dirty = true;
  803. repaint();
  804. */
  805. }
  806. Range<double> WaveformComponent::getTimeSelection()
  807. {
  808. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  809. return { m_time_sel_start, m_time_sel_end };
  810. return { 0.0, 1.0 };
  811. }
  812. void WaveformComponent::setTimeSelection(Range<double> rng)
  813. {
  814. if (m_lock_timesel_set == true)
  815. return;
  816. if (rng.isEmpty())
  817. rng = { -1.0,1.0 };
  818. m_time_sel_start = rng.getStart();
  819. m_time_sel_end = rng.getEnd();
  820. repaint();
  821. }
  822. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  823. {
  824. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  825. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  826. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  827. return 1;
  828. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  829. return 2;
  830. return 0;
  831. }
  832. SpectralVisualizer::SpectralVisualizer()
  833. {
  834. m_img = Image(Image::RGB, 500, 200, true);
  835. }
  836. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  837. {
  838. double t0 = Time::getMillisecondCounterHiRes();
  839. double hz = 440.0;
  840. int numharmonics = 40;
  841. double scaler = 1.0 / numharmonics;
  842. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  843. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  844. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  845. {
  846. m_nfreqs = nfreqs;
  847. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  848. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  849. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  850. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  851. m_fft = std::make_unique<FFT>(nfreqs*2);
  852. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  853. for (int i = 0; i < nfreqs; ++i)
  854. {
  855. for (int j = 0; j < numharmonics; ++j)
  856. {
  857. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  858. m_insamples[i] += scaler * oscgain * sin(2 * c_PI / samplerate * i* (hz + hz * j));
  859. }
  860. }
  861. }
  862. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  863. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  864. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  865. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  866. for (int i = 0; i < nfreqs; ++i)
  867. {
  868. m_fft->smp[i] = m_insamples[i];
  869. }
  870. m_fft->applywindow(W_HAMMING);
  871. m_fft->smp2freq();
  872. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  873. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  874. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  875. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  876. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  877. //if (pars.harmonics.enabled)
  878. // spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  879. //else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  880. Graphics g(m_img);
  881. g.fillAll(Colours::black);
  882. g.setColour(Colours::white);
  883. for (int i = 0; i < nfreqs; ++i)
  884. {
  885. double binfreq = (samplerate / 2 / nfreqs)*i;
  886. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  887. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  888. ycor = jlimit<double>(0.0, getHeight(), ycor);
  889. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  890. }
  891. double t1 = Time::getMillisecondCounterHiRes();
  892. m_elapsed = t1 - t0;
  893. repaint();
  894. }
  895. void SpectralVisualizer::paint(Graphics & g)
  896. {
  897. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  898. g.setColour(Colours::yellow);
  899. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  900. }
  901. void SpectralChainEditor::paint(Graphics & g)
  902. {
  903. g.fillAll(Colours::black);
  904. if (m_src == nullptr)
  905. return;
  906. int box_w = getWidth() / m_order.size();
  907. int box_h = getHeight();
  908. for (int i = 0; i < m_order.size(); ++i)
  909. {
  910. //if (i!=m_cur_index)
  911. drawBox(g, i, i*box_w, 0, box_w - 20, box_h);
  912. if (i<m_order.size() - 1)
  913. g.drawArrow(juce::Line<float>(i*box_w + (box_w - 20), box_h / 2, i*box_w + box_w, box_h / 2), 2.0f, 12.0f, 12.0f);
  914. }
  915. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  916. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  917. }
  918. void SpectralChainEditor::setSource(StretchAudioSource * src)
  919. {
  920. m_src = src;
  921. m_order = m_src->getSpectrumProcessOrder();
  922. repaint();
  923. }
  924. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  925. {
  926. m_did_drag = false;
  927. int box_w = getWidth() / m_order.size();
  928. int box_h = getHeight();
  929. m_cur_index = ev.x / box_w;
  930. if (m_cur_index >= 0)
  931. {
  932. if (ModuleSelectedCallback)
  933. ModuleSelectedCallback(m_order[m_cur_index].m_index);
  934. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  935. if (r.contains(ev.x, ev.y))
  936. {
  937. for (int i = 0; i < m_order.size(); ++i)
  938. {
  939. if (m_order[i].m_index == m_cur_index)
  940. {
  941. //Logger::writeToLog("SpectralChainEditor::mouseDown "+m_order[i].m_enabled->name);
  942. //return;
  943. }
  944. }
  945. toggleBool(m_order[m_cur_index].m_enabled);
  946. Logger::writeToLog("SpectralChainEditor::mouseDown " + m_order[m_cur_index].m_enabled->name);
  947. //m_src->setSpectrumProcessOrder(m_order);
  948. //if (ModuleOrderOrEnabledChangedCallback)
  949. // ModuleOrderOrEnabledChangedCallback();
  950. repaint();
  951. return;
  952. }
  953. }
  954. m_drag_x = ev.x;
  955. repaint();
  956. }
  957. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  958. {
  959. int box_w = getWidth() / m_order.size();
  960. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  961. if (r.contains(ev.x, ev.y))
  962. return;
  963. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  964. {
  965. int box_h = getHeight();
  966. int new_index = ev.x / box_w;
  967. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  968. {
  969. swapSpectrumProcesses(m_order[m_cur_index], m_order[new_index]);
  970. m_cur_index = new_index;
  971. m_did_drag = true;
  972. m_src->setSpectrumProcessOrder(m_order);
  973. if (ModuleOrderOrEnabledChangedCallback)
  974. ModuleOrderOrEnabledChangedCallback();
  975. }
  976. int diff = m_drag_x - ev.x;
  977. m_drag_x -= diff;
  978. repaint();
  979. }
  980. }
  981. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  982. {
  983. m_drag_x = -1;
  984. //m_cur_index = -1;
  985. repaint();
  986. }
  987. void SpectralChainEditor::setModuleSelected(int id)
  988. {
  989. if (id != m_cur_index)
  990. {
  991. m_cur_index = id;
  992. repaint();
  993. }
  994. }
  995. void SpectralChainEditor::moveModule(int old_id, int new_id)
  996. {
  997. if (old_id == m_cur_index)
  998. return;
  999. std::swap(m_order[old_id], m_order[new_id]);
  1000. m_cur_index = new_id;
  1001. m_src->setSpectrumProcessOrder(m_order);
  1002. repaint();
  1003. if (ModuleOrderOrEnabledChangedCallback)
  1004. ModuleOrderOrEnabledChangedCallback();
  1005. }
  1006. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  1007. {
  1008. jassert(m_order[index].m_enabled != nullptr);
  1009. String txt;
  1010. if (m_order[index].m_index == 0)
  1011. txt = "Harmonics";
  1012. if (m_order[index].m_index == 1)
  1013. txt = "Tonal vs Noise";
  1014. if (m_order[index].m_index == 2)
  1015. txt = "Frequency shift";
  1016. if (m_order[index].m_index == 3)
  1017. txt = "Pitch shift";
  1018. if (m_order[index].m_index == 4)
  1019. txt = "Ratios";
  1020. if (m_order[index].m_index == 5)
  1021. txt = "Spread";
  1022. if (m_order[index].m_index == 6)
  1023. txt = "Filter";
  1024. if (m_order[index].m_index == 8)
  1025. txt = "Compressor";
  1026. if (m_order[index].m_index == 7)
  1027. txt = "Free filter";
  1028. if (index == m_cur_index)
  1029. {
  1030. g.setColour(Colours::darkgrey);
  1031. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  1032. g.fillRect(x, y, w, h);
  1033. }
  1034. g.setColour(Colours::white);
  1035. g.drawRect(x, y, w, h);
  1036. g.drawFittedText(txt, x,y,w,h-5, Justification::centredBottom, 3);
  1037. //g.drawFittedText(m_order[index].m_enabled->name, x, y, w, h, Justification::centred, 3);
  1038. g.setColour(Colours::gold);
  1039. g.drawRect(x + 2, y + 2, 12, 12);
  1040. if ((bool)*m_order[index].m_enabled == true)
  1041. {
  1042. g.drawLine(x+2, y+2, x+14, y+14);
  1043. g.drawLine(x+2, y+14, x+14, y+2);
  1044. }
  1045. g.setColour(Colours::white);
  1046. }
  1047. ParameterComponent::ParameterComponent(AudioProcessorParameter * par, bool notifyOnlyOnRelease) : m_par(par)
  1048. {
  1049. addAndMakeVisible(&m_label);
  1050. m_labeldefcolor = m_label.findColour(Label::textColourId);
  1051. m_label.setText(par->getName(50), dontSendNotification);
  1052. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  1053. if (floatpar)
  1054. {
  1055. m_slider = std::make_unique<MySlider>(&floatpar->range);
  1056. m_notify_only_on_release = notifyOnlyOnRelease;
  1057. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  1058. m_slider->setValue(*floatpar, dontSendNotification);
  1059. m_slider->addListener(this);
  1060. m_slider->setDoubleClickReturnValue(true, floatpar->range.convertFrom0to1(par->getDefaultValue()));
  1061. addAndMakeVisible(m_slider.get());
  1062. }
  1063. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(par);
  1064. if (intpar)
  1065. {
  1066. m_slider = std::make_unique<MySlider>();
  1067. m_notify_only_on_release = notifyOnlyOnRelease;
  1068. m_slider->setRange(intpar->getRange().getStart(), intpar->getRange().getEnd(), 1.0);
  1069. m_slider->setValue(*intpar, dontSendNotification);
  1070. m_slider->addListener(this);
  1071. addAndMakeVisible(m_slider.get());
  1072. }
  1073. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  1074. if (choicepar)
  1075. {
  1076. }
  1077. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  1078. if (boolpar)
  1079. {
  1080. m_togglebut = std::make_unique<ToggleButton>();
  1081. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  1082. m_togglebut->addListener(this);
  1083. m_togglebut->setButtonText(par->getName(50));
  1084. addAndMakeVisible(m_togglebut.get());
  1085. }
  1086. }
  1087. void ParameterComponent::resized()
  1088. {
  1089. if (m_slider)
  1090. {
  1091. int labw = 200;
  1092. if (getWidth() < 400)
  1093. labw = 100;
  1094. m_label.setBounds(0, 0, labw, 24);
  1095. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  1096. }
  1097. if (m_togglebut)
  1098. m_togglebut->setBounds(1, 0, getWidth() - 1, 24);
  1099. }
  1100. void ParameterComponent::sliderValueChanged(Slider * slid)
  1101. {
  1102. if (m_notify_only_on_release == true)
  1103. return;
  1104. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1105. if (floatpar != nullptr)
  1106. *floatpar = slid->getValue();
  1107. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1108. if (intpar != nullptr)
  1109. *intpar = slid->getValue();
  1110. }
  1111. void ParameterComponent::sliderDragStarted(Slider * slid)
  1112. {
  1113. m_dragging = true;
  1114. }
  1115. void ParameterComponent::sliderDragEnded(Slider * slid)
  1116. {
  1117. m_dragging = false;
  1118. if (m_notify_only_on_release == false)
  1119. return;
  1120. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1121. if (floatpar != nullptr)
  1122. *floatpar = slid->getValue();
  1123. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1124. if (intpar != nullptr)
  1125. *intpar = slid->getValue();
  1126. }
  1127. void ParameterComponent::buttonClicked(Button * but)
  1128. {
  1129. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  1130. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  1131. {
  1132. *boolpar = m_togglebut->getToggleState();
  1133. }
  1134. }
  1135. void ParameterComponent::updateComponent()
  1136. {
  1137. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1138. if (floatpar != nullptr && m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  1139. {
  1140. m_slider->setValue(*floatpar, dontSendNotification);
  1141. }
  1142. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1143. if (intpar != nullptr && m_slider != nullptr && m_dragging == false && (int)m_slider->getValue() != *intpar)
  1144. {
  1145. m_slider->setValue(*intpar, dontSendNotification);
  1146. }
  1147. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  1148. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  1149. {
  1150. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  1151. }
  1152. }
  1153. void ParameterComponent::setHighLighted(bool b)
  1154. {
  1155. if (b == false)
  1156. {
  1157. m_label.setColour(Label::textColourId, m_labeldefcolor);
  1158. if (m_togglebut)
  1159. m_togglebut->setColour(ToggleButton::textColourId, m_labeldefcolor);
  1160. }
  1161. else
  1162. {
  1163. m_label.setColour(Label::textColourId, Colours::yellow);
  1164. if (m_togglebut)
  1165. m_togglebut->setColour(ToggleButton::textColourId, Colours::yellow);
  1166. }
  1167. }
  1168. MySlider::MySlider(NormalisableRange<float>* range) : m_range(range)
  1169. {
  1170. }
  1171. double MySlider::proportionOfLengthToValue(double x)
  1172. {
  1173. if (m_range)
  1174. return m_range->convertFrom0to1(x);
  1175. return Slider::proportionOfLengthToValue(x);
  1176. }
  1177. double MySlider::valueToProportionOfLength(double x)
  1178. {
  1179. if (m_range)
  1180. return m_range->convertTo0to1(x);
  1181. return Slider::valueToProportionOfLength(x);
  1182. }
  1183. PerfMeterComponent::PerfMeterComponent(PaulstretchpluginAudioProcessor * p)
  1184. : m_proc(p)
  1185. {
  1186. m_gradient.isRadial = false;
  1187. m_gradient.addColour(0.0, Colours::red);
  1188. m_gradient.addColour(0.25, Colours::yellow);
  1189. m_gradient.addColour(1.0, Colours::green);
  1190. }
  1191. void PerfMeterComponent::paint(Graphics & g)
  1192. {
  1193. m_gradient.point1 = {0.0f,0.0f};
  1194. m_gradient.point2 = {(float)getWidth(),0.0f};
  1195. g.fillAll(Colours::grey);
  1196. double amt = m_proc->getPreBufferingPercent();
  1197. g.setColour(Colours::green);
  1198. int w = amt * getWidth();
  1199. //g.setGradientFill(m_gradient);
  1200. g.fillRect(0, 0, w, getHeight());
  1201. g.setColour(Colours::white);
  1202. g.drawRect(0, 0, getWidth(), getHeight());
  1203. g.setFont(10.0f);
  1204. if (m_proc->getPreBufferAmount()>0)
  1205. g.drawText("PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  1206. else
  1207. g.drawText("NO PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  1208. }
  1209. void PerfMeterComponent::mouseDown(const MouseEvent & ev)
  1210. {
  1211. PopupMenu bufferingmenu;
  1212. int curbufamount = m_proc->getPreBufferAmount();
  1213. bufferingmenu.addItem(100, "None", true, curbufamount == -1);
  1214. bufferingmenu.addItem(101, "Small", true, curbufamount == 1);
  1215. bufferingmenu.addItem(102, "Medium", true, curbufamount == 2);
  1216. bufferingmenu.addItem(103, "Large", true, curbufamount == 3);
  1217. bufferingmenu.addItem(104, "Very large", true, curbufamount == 4);
  1218. bufferingmenu.addItem(105, "Huge", true, curbufamount == 5);
  1219. int r = bufferingmenu.show();
  1220. if (r >= 100 && r < 200)
  1221. {
  1222. if (r == 100)
  1223. m_proc->m_use_backgroundbuffering = false;
  1224. if (r > 100)
  1225. m_proc->setPreBufferAmount(r - 100);
  1226. }
  1227. }
  1228. void zoom_scrollbar::mouseDown(const MouseEvent &e)
  1229. {
  1230. m_drag_start_x = e.x;
  1231. }
  1232. void zoom_scrollbar::mouseMove(const MouseEvent &e)
  1233. {
  1234. auto ha = get_hot_area(e.x, e.y);
  1235. if (ha == ha_left_edge || m_hot_area == ha_right_edge)
  1236. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  1237. else
  1238. setMouseCursor(MouseCursor::NormalCursor);
  1239. if (ha != m_hot_area)
  1240. {
  1241. m_hot_area = ha;
  1242. repaint();
  1243. }
  1244. }
  1245. void zoom_scrollbar::mouseDrag(const MouseEvent &e)
  1246. {
  1247. if (m_hot_area == ha_none)
  1248. return;
  1249. if (m_hot_area == ha_left_edge)
  1250. {
  1251. double new_left_edge = 1.0 / getWidth()*e.x;
  1252. m_therange.setStart(jlimit(0.0, m_therange.getEnd() - 0.01, new_left_edge));
  1253. repaint();
  1254. }
  1255. if (m_hot_area == ha_right_edge)
  1256. {
  1257. double new_right_edge = 1.0 / getWidth()*e.x;
  1258. m_therange.setEnd(jlimit(m_therange.getStart() + 0.01, 1.0, new_right_edge));
  1259. repaint();
  1260. }
  1261. if (m_hot_area == ha_handle)
  1262. {
  1263. double delta = 1.0 / getWidth()*(e.x - m_drag_start_x);
  1264. //double old_start = m_start;
  1265. //double old_end = m_end;
  1266. double old_len = m_therange.getLength();
  1267. m_therange.setStart(jlimit(0.0, 1.0 - old_len, m_therange.getStart() + delta));
  1268. m_therange.setEnd(jlimit(old_len, m_therange.getStart() + old_len, m_therange.getEnd() + delta));
  1269. m_drag_start_x = e.x;
  1270. repaint();
  1271. }
  1272. if (RangeChanged)
  1273. RangeChanged(m_therange);
  1274. }
  1275. void zoom_scrollbar::mouseEnter(const MouseEvent & event)
  1276. {
  1277. m_hot_area = get_hot_area(event.x, event.y);
  1278. repaint();
  1279. }
  1280. void zoom_scrollbar::mouseExit(const MouseEvent &)
  1281. {
  1282. m_hot_area = ha_none;
  1283. repaint();
  1284. }
  1285. void zoom_scrollbar::paint(Graphics &g)
  1286. {
  1287. g.setColour(Colours::darkgrey);
  1288. g.fillRect(0, 0, getWidth(), getHeight());
  1289. int x0 = (int)(getWidth()*m_therange.getStart());
  1290. int x1 = (int)(getWidth()*m_therange.getEnd());
  1291. if (m_hot_area != ha_none)
  1292. g.setColour(Colours::white);
  1293. else g.setColour(Colours::lightgrey);
  1294. g.fillRect(x0, 0, x1 - x0, getHeight());
  1295. }
  1296. void zoom_scrollbar::setRange(Range<double> rng, bool docallback)
  1297. {
  1298. if (rng.isEmpty())
  1299. return;
  1300. m_therange = rng.constrainRange({ 0.0,1.0 });
  1301. if (RangeChanged && docallback)
  1302. RangeChanged(m_therange);
  1303. repaint();
  1304. }
  1305. zoom_scrollbar::hot_area zoom_scrollbar::get_hot_area(int x, int)
  1306. {
  1307. int x0 = (int)(getWidth()*m_therange.getStart());
  1308. int x1 = (int)(getWidth()*m_therange.getEnd());
  1309. if (is_in_range(x, x0 - 5, x0 + 5))
  1310. return ha_left_edge;
  1311. if (is_in_range(x, x1 - 5, x1 + 5))
  1312. return ha_right_edge;
  1313. if (is_in_range(x, x0 + 5, x1 - 5))
  1314. return ha_handle;
  1315. return ha_none;
  1316. }
  1317. RatioMixerEditor::RatioMixerEditor(int numratios)
  1318. {
  1319. for (int i = 0; i < numratios; ++i)
  1320. {
  1321. auto ratslid = std::make_unique<Slider>(Slider::LinearHorizontal,Slider::TextBoxBelow);
  1322. ratslid->setRange(0.125, 8.0);
  1323. ratslid->onValueChange = [this,i]() {OnRatioChanged(i, m_ratio_sliders[i]->getValue()); };
  1324. addAndMakeVisible(ratslid.get());
  1325. m_ratio_sliders.emplace_back(std::move(ratslid));
  1326. auto ratlevslid = std::make_unique<Slider>();
  1327. ratlevslid->setRange(0.0, 1.0);
  1328. ratlevslid->setSliderStyle(Slider::LinearVertical);
  1329. if (i==3)
  1330. ratlevslid->setValue(1.0,dontSendNotification);
  1331. else ratlevslid->setValue(0.0,dontSendNotification);
  1332. ratlevslid->onValueChange = [this, i]() { OnRatioLevelChanged(i, m_ratio_level_sliders[i]->getValue()); };
  1333. addAndMakeVisible(ratlevslid.get());
  1334. m_ratio_level_sliders.emplace_back(std::move(ratlevslid));
  1335. }
  1336. startTimer(200);
  1337. setOpaque(true);
  1338. }
  1339. void RatioMixerEditor::resized()
  1340. {
  1341. int nsliders = m_ratio_sliders.size();
  1342. int slidw = getWidth() / nsliders;
  1343. for (int i = 0; i < nsliders; ++i)
  1344. {
  1345. m_ratio_level_sliders[i]->setBounds(slidw/2+slidw * i-10, 1, 20, getHeight() - 50);
  1346. m_ratio_sliders[i]->setBounds(slidw * i, getHeight() - 48, slidw - 5, 47);
  1347. }
  1348. }
  1349. void RatioMixerEditor::timerCallback()
  1350. {
  1351. if (!GetParameterValue)
  1352. return;
  1353. for (int i = 0; i < m_ratio_level_sliders.size(); ++i)
  1354. {
  1355. double v = GetParameterValue(0, i);
  1356. if (v!=m_ratio_sliders[i]->getValue())
  1357. m_ratio_sliders[i]->setValue(v, dontSendNotification);
  1358. v = GetParameterValue(1, i);
  1359. if (v!=m_ratio_level_sliders[i]->getValue())
  1360. m_ratio_level_sliders[i]->setValue(v, dontSendNotification);
  1361. }
  1362. }
  1363. void RatioMixerEditor::paint(Graphics & g)
  1364. {
  1365. g.fillAll(Colours::grey);
  1366. }