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.

1451 lines
45KB

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