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.

1453 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. /*
  774. double factor = 0.9;
  775. if (wd.deltaY < 0.0)
  776. factor = 1.11111;
  777. double normt = viewXToNormalized(e.x);
  778. double curlen = m_view_range.getLength();
  779. double newlen = curlen * factor;
  780. double oldt0 = m_view_range.getStart();
  781. double oldt1 = m_view_range.getEnd();
  782. double t0 = jlimit(0.0,1.0, normt + (curlen - newlen));
  783. double t1 = jlimit(0.0,1.0, t0+newlen);
  784. jassert(t1 > t0);
  785. m_view_range = { t0,t1 };
  786. //m_view_range = m_view_range.constrainRange({ 0.0, 1.0 });
  787. jassert(m_view_range.getStart() >= 0.0 && m_view_range.getEnd() <= 1.0);
  788. jassert(m_view_range.getLength() > 0.001);
  789. if (ViewRangeChangedCallback)
  790. ViewRangeChangedCallback(m_view_range);
  791. m_image_dirty = true;
  792. repaint();
  793. */
  794. }
  795. Range<double> WaveformComponent::getTimeSelection()
  796. {
  797. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  798. return { m_time_sel_start, m_time_sel_end };
  799. return { 0.0, 1.0 };
  800. }
  801. void WaveformComponent::setTimeSelection(Range<double> rng)
  802. {
  803. if (m_lock_timesel_set == true)
  804. return;
  805. if (rng.isEmpty())
  806. rng = { -1.0,1.0 };
  807. m_time_sel_start = rng.getStart();
  808. m_time_sel_end = rng.getEnd();
  809. repaint();
  810. }
  811. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  812. {
  813. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  814. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  815. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  816. return 1;
  817. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  818. return 2;
  819. return 0;
  820. }
  821. SpectralVisualizer::SpectralVisualizer()
  822. {
  823. m_img = Image(Image::RGB, 500, 200, true);
  824. }
  825. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  826. {
  827. double t0 = Time::getMillisecondCounterHiRes();
  828. double hz = 440.0;
  829. int numharmonics = 40;
  830. double scaler = 1.0 / numharmonics;
  831. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  832. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  833. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  834. {
  835. m_nfreqs = nfreqs;
  836. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  837. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  838. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  839. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  840. m_fft = std::make_unique<FFT>(nfreqs*2);
  841. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  842. for (int i = 0; i < nfreqs; ++i)
  843. {
  844. for (int j = 0; j < numharmonics; ++j)
  845. {
  846. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  847. m_insamples[i] += scaler * oscgain * sin(2 * c_PI / samplerate * i* (hz + hz * j));
  848. }
  849. }
  850. }
  851. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  852. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  853. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  854. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  855. for (int i = 0; i < nfreqs; ++i)
  856. {
  857. m_fft->smp[i] = m_insamples[i];
  858. }
  859. m_fft->applywindow(W_HAMMING);
  860. m_fft->smp2freq();
  861. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  862. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  863. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  864. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  865. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  866. //if (pars.harmonics.enabled)
  867. // spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  868. //else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  869. Graphics g(m_img);
  870. g.fillAll(Colours::black);
  871. g.setColour(Colours::white);
  872. for (int i = 0; i < nfreqs; ++i)
  873. {
  874. double binfreq = (samplerate / 2 / nfreqs)*i;
  875. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  876. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  877. ycor = jlimit<double>(0.0, getHeight(), ycor);
  878. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  879. }
  880. double t1 = Time::getMillisecondCounterHiRes();
  881. m_elapsed = t1 - t0;
  882. repaint();
  883. }
  884. void SpectralVisualizer::paint(Graphics & g)
  885. {
  886. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  887. g.setColour(Colours::yellow);
  888. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  889. }
  890. void SpectralChainEditor::paint(Graphics & g)
  891. {
  892. g.fillAll(Colours::black);
  893. if (m_src == nullptr)
  894. return;
  895. int box_w = getWidth() / m_order.size();
  896. int box_h = getHeight();
  897. for (int i = 0; i < m_order.size(); ++i)
  898. {
  899. //if (i!=m_cur_index)
  900. drawBox(g, i, i*box_w, 0, box_w - 20, box_h);
  901. if (i<m_order.size() - 1)
  902. 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);
  903. }
  904. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  905. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  906. }
  907. void SpectralChainEditor::setSource(StretchAudioSource * src)
  908. {
  909. m_src = src;
  910. m_order = m_src->getSpectrumProcessOrder();
  911. repaint();
  912. }
  913. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  914. {
  915. m_did_drag = false;
  916. int box_w = getWidth() / m_order.size();
  917. int box_h = getHeight();
  918. m_cur_index = ev.x / box_w;
  919. if (m_cur_index >= 0)
  920. {
  921. if (ModuleSelectedCallback)
  922. ModuleSelectedCallback(m_order[m_cur_index].m_index);
  923. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  924. if (r.contains(ev.x, ev.y))
  925. {
  926. toggleBool(m_order[m_cur_index].m_enabled);
  927. //m_src->setSpectrumProcessOrder(m_order);
  928. //if (ModuleOrderOrEnabledChangedCallback)
  929. // ModuleOrderOrEnabledChangedCallback();
  930. repaint();
  931. return;
  932. }
  933. }
  934. m_drag_x = ev.x;
  935. repaint();
  936. }
  937. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  938. {
  939. int box_w = getWidth() / m_order.size();
  940. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  941. if (r.contains(ev.x, ev.y))
  942. return;
  943. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  944. {
  945. int box_h = getHeight();
  946. int new_index = ev.x / box_w;
  947. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  948. {
  949. swapSpectrumProcesses(m_order[m_cur_index], m_order[new_index]);
  950. m_cur_index = new_index;
  951. m_did_drag = true;
  952. m_src->setSpectrumProcessOrder(m_order);
  953. if (ModuleOrderOrEnabledChangedCallback)
  954. ModuleOrderOrEnabledChangedCallback();
  955. }
  956. int diff = m_drag_x - ev.x;
  957. m_drag_x -= diff;
  958. repaint();
  959. }
  960. }
  961. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  962. {
  963. m_drag_x = -1;
  964. //m_cur_index = -1;
  965. repaint();
  966. }
  967. void SpectralChainEditor::setModuleSelected(int id)
  968. {
  969. if (id != m_cur_index)
  970. {
  971. m_cur_index = id;
  972. repaint();
  973. }
  974. }
  975. void SpectralChainEditor::moveModule(int old_id, int new_id)
  976. {
  977. if (old_id == m_cur_index)
  978. return;
  979. std::swap(m_order[old_id], m_order[new_id]);
  980. m_cur_index = new_id;
  981. m_src->setSpectrumProcessOrder(m_order);
  982. repaint();
  983. if (ModuleOrderOrEnabledChangedCallback)
  984. ModuleOrderOrEnabledChangedCallback();
  985. }
  986. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  987. {
  988. jassert(m_order[index].m_enabled != nullptr);
  989. String txt;
  990. if (m_order[index].m_index == 0)
  991. txt = "Harmonics";
  992. if (m_order[index].m_index == 1)
  993. txt = "Tonal vs Noise";
  994. if (m_order[index].m_index == 2)
  995. txt = "Frequency shift";
  996. if (m_order[index].m_index == 3)
  997. txt = "Pitch shift";
  998. if (m_order[index].m_index == 4)
  999. txt = "Ratios";
  1000. if (m_order[index].m_index == 5)
  1001. txt = "Spread";
  1002. if (m_order[index].m_index == 6)
  1003. txt = "Filter";
  1004. if (m_order[index].m_index == 8)
  1005. txt = "Compressor";
  1006. if (m_order[index].m_index == 7)
  1007. txt = "Free filter";
  1008. if (index == m_cur_index)
  1009. {
  1010. g.setColour(Colours::darkgrey);
  1011. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  1012. g.fillRect(x, y, w, h);
  1013. }
  1014. g.setColour(Colours::white);
  1015. g.drawRect(x, y, w, h);
  1016. g.drawFittedText(txt, x,y,w,h-5, Justification::centredBottom, 3);
  1017. //g.drawFittedText(m_order[index].m_enabled->name, x, y, w, h, Justification::centred, 3);
  1018. g.setColour(Colours::gold);
  1019. g.drawRect(x + 2, y + 2, 12, 12);
  1020. if ((bool)*m_order[index].m_enabled == true)
  1021. {
  1022. g.drawLine(x+2, y+2, x+14, y+14);
  1023. g.drawLine(x+2, y+14, x+14, y+2);
  1024. }
  1025. g.setColour(Colours::white);
  1026. }
  1027. ParameterComponent::ParameterComponent(AudioProcessorParameter * par, bool notifyOnlyOnRelease) : m_par(par)
  1028. {
  1029. addAndMakeVisible(&m_label);
  1030. m_labeldefcolor = m_label.findColour(Label::textColourId);
  1031. m_label.setText(par->getName(50), dontSendNotification);
  1032. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  1033. if (floatpar)
  1034. {
  1035. m_slider = std::make_unique<MySlider>(&floatpar->range);
  1036. m_notify_only_on_release = notifyOnlyOnRelease;
  1037. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  1038. m_slider->setValue(*floatpar, dontSendNotification);
  1039. m_slider->addListener(this);
  1040. m_slider->setDoubleClickReturnValue(true, floatpar->range.convertFrom0to1(par->getDefaultValue()));
  1041. addAndMakeVisible(m_slider.get());
  1042. }
  1043. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(par);
  1044. if (intpar)
  1045. {
  1046. m_slider = std::make_unique<MySlider>();
  1047. m_notify_only_on_release = notifyOnlyOnRelease;
  1048. m_slider->setRange(intpar->getRange().getStart(), intpar->getRange().getEnd(), 1.0);
  1049. m_slider->setValue(*intpar, dontSendNotification);
  1050. m_slider->addListener(this);
  1051. addAndMakeVisible(m_slider.get());
  1052. }
  1053. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  1054. if (choicepar)
  1055. {
  1056. }
  1057. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  1058. if (boolpar)
  1059. {
  1060. m_togglebut = std::make_unique<ToggleButton>();
  1061. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  1062. m_togglebut->addListener(this);
  1063. m_togglebut->setButtonText(par->getName(50));
  1064. addAndMakeVisible(m_togglebut.get());
  1065. }
  1066. }
  1067. void ParameterComponent::resized()
  1068. {
  1069. if (m_slider)
  1070. {
  1071. int labw = 200;
  1072. if (getWidth() < 400)
  1073. labw = 100;
  1074. m_label.setBounds(0, 0, labw, 24);
  1075. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  1076. }
  1077. if (m_togglebut)
  1078. m_togglebut->setBounds(1, 0, getWidth() - 1, 24);
  1079. }
  1080. void ParameterComponent::sliderValueChanged(Slider * slid)
  1081. {
  1082. if (m_notify_only_on_release == true)
  1083. return;
  1084. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1085. if (floatpar != nullptr)
  1086. *floatpar = slid->getValue();
  1087. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1088. if (intpar != nullptr)
  1089. *intpar = slid->getValue();
  1090. }
  1091. void ParameterComponent::sliderDragStarted(Slider * slid)
  1092. {
  1093. m_dragging = true;
  1094. }
  1095. void ParameterComponent::sliderDragEnded(Slider * slid)
  1096. {
  1097. m_dragging = false;
  1098. if (m_notify_only_on_release == false)
  1099. return;
  1100. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1101. if (floatpar != nullptr)
  1102. *floatpar = slid->getValue();
  1103. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1104. if (intpar != nullptr)
  1105. *intpar = slid->getValue();
  1106. }
  1107. void ParameterComponent::buttonClicked(Button * but)
  1108. {
  1109. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  1110. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  1111. {
  1112. *boolpar = m_togglebut->getToggleState();
  1113. }
  1114. }
  1115. void ParameterComponent::updateComponent()
  1116. {
  1117. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  1118. if (floatpar != nullptr && m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  1119. {
  1120. m_slider->setValue(*floatpar, dontSendNotification);
  1121. }
  1122. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  1123. if (intpar != nullptr && m_slider != nullptr && m_dragging == false && (int)m_slider->getValue() != *intpar)
  1124. {
  1125. m_slider->setValue(*intpar, dontSendNotification);
  1126. }
  1127. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  1128. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  1129. {
  1130. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  1131. }
  1132. }
  1133. void ParameterComponent::setHighLighted(bool b)
  1134. {
  1135. if (b == false)
  1136. {
  1137. m_label.setColour(Label::textColourId, m_labeldefcolor);
  1138. if (m_togglebut)
  1139. m_togglebut->setColour(ToggleButton::textColourId, m_labeldefcolor);
  1140. }
  1141. else
  1142. {
  1143. m_label.setColour(Label::textColourId, Colours::yellow);
  1144. if (m_togglebut)
  1145. m_togglebut->setColour(ToggleButton::textColourId, Colours::yellow);
  1146. }
  1147. }
  1148. MySlider::MySlider(NormalisableRange<float>* range) : m_range(range)
  1149. {
  1150. }
  1151. double MySlider::proportionOfLengthToValue(double x)
  1152. {
  1153. if (m_range)
  1154. return m_range->convertFrom0to1(x);
  1155. return Slider::proportionOfLengthToValue(x);
  1156. }
  1157. double MySlider::valueToProportionOfLength(double x)
  1158. {
  1159. if (m_range)
  1160. return m_range->convertTo0to1(x);
  1161. return Slider::valueToProportionOfLength(x);
  1162. }
  1163. PerfMeterComponent::PerfMeterComponent(PaulstretchpluginAudioProcessor * p)
  1164. : m_proc(p)
  1165. {
  1166. m_gradient.isRadial = false;
  1167. m_gradient.addColour(0.0, Colours::red);
  1168. m_gradient.addColour(0.25, Colours::yellow);
  1169. m_gradient.addColour(1.0, Colours::green);
  1170. }
  1171. void PerfMeterComponent::paint(Graphics & g)
  1172. {
  1173. m_gradient.point1 = {0.0f,0.0f};
  1174. m_gradient.point2 = {(float)getWidth(),0.0f};
  1175. g.fillAll(Colours::grey);
  1176. double amt = m_proc->getPreBufferingPercent();
  1177. g.setColour(Colours::green);
  1178. int w = amt * getWidth();
  1179. //g.setGradientFill(m_gradient);
  1180. g.fillRect(0, 0, w, getHeight());
  1181. g.setColour(Colours::white);
  1182. g.drawRect(0, 0, getWidth(), getHeight());
  1183. g.setFont(10.0f);
  1184. if (m_proc->getPreBufferAmount()>0)
  1185. g.drawText("PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  1186. else
  1187. g.drawText("NO PREBUFFER", 0, 0, getWidth(), getHeight(), Justification::centred);
  1188. }
  1189. void PerfMeterComponent::mouseDown(const MouseEvent & ev)
  1190. {
  1191. PopupMenu bufferingmenu;
  1192. int curbufamount = m_proc->getPreBufferAmount();
  1193. bufferingmenu.addItem(100, "None", true, curbufamount == -1);
  1194. bufferingmenu.addItem(101, "Small", true, curbufamount == 1);
  1195. bufferingmenu.addItem(102, "Medium", true, curbufamount == 2);
  1196. bufferingmenu.addItem(103, "Large", true, curbufamount == 3);
  1197. bufferingmenu.addItem(104, "Very large", true, curbufamount == 4);
  1198. bufferingmenu.addItem(105, "Huge", true, curbufamount == 5);
  1199. int r = bufferingmenu.show();
  1200. if (r >= 100 && r < 200)
  1201. {
  1202. if (r == 100)
  1203. m_proc->m_use_backgroundbuffering = false;
  1204. if (r > 100)
  1205. m_proc->setPreBufferAmount(r - 100);
  1206. }
  1207. }
  1208. void zoom_scrollbar::mouseDown(const MouseEvent &e)
  1209. {
  1210. m_drag_start_x = e.x;
  1211. }
  1212. void zoom_scrollbar::mouseMove(const MouseEvent &e)
  1213. {
  1214. auto ha = get_hot_area(e.x, e.y);
  1215. if (ha == ha_left_edge || m_hot_area == ha_right_edge)
  1216. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  1217. else
  1218. setMouseCursor(MouseCursor::NormalCursor);
  1219. if (ha != m_hot_area)
  1220. {
  1221. m_hot_area = ha;
  1222. repaint();
  1223. }
  1224. }
  1225. void zoom_scrollbar::mouseDrag(const MouseEvent &e)
  1226. {
  1227. if (m_hot_area == ha_none)
  1228. return;
  1229. if (m_hot_area == ha_left_edge)
  1230. {
  1231. double new_left_edge = 1.0 / getWidth()*e.x;
  1232. m_therange.setStart(jlimit(0.0, m_therange.getEnd() - 0.01, new_left_edge));
  1233. repaint();
  1234. }
  1235. if (m_hot_area == ha_right_edge)
  1236. {
  1237. double new_right_edge = 1.0 / getWidth()*e.x;
  1238. m_therange.setEnd(jlimit(m_therange.getStart() + 0.01, 1.0, new_right_edge));
  1239. repaint();
  1240. }
  1241. if (m_hot_area == ha_handle)
  1242. {
  1243. double delta = 1.0 / getWidth()*(e.x - m_drag_start_x);
  1244. //double old_start = m_start;
  1245. //double old_end = m_end;
  1246. double old_len = m_therange.getLength();
  1247. m_therange.setStart(jlimit(0.0, 1.0 - old_len, m_therange.getStart() + delta));
  1248. m_therange.setEnd(jlimit(old_len, m_therange.getStart() + old_len, m_therange.getEnd() + delta));
  1249. m_drag_start_x = e.x;
  1250. repaint();
  1251. }
  1252. if (RangeChanged)
  1253. RangeChanged(m_therange);
  1254. }
  1255. void zoom_scrollbar::mouseEnter(const MouseEvent & event)
  1256. {
  1257. m_hot_area = get_hot_area(event.x, event.y);
  1258. repaint();
  1259. }
  1260. void zoom_scrollbar::mouseExit(const MouseEvent &)
  1261. {
  1262. m_hot_area = ha_none;
  1263. repaint();
  1264. }
  1265. void zoom_scrollbar::paint(Graphics &g)
  1266. {
  1267. g.setColour(Colours::darkgrey);
  1268. g.fillRect(0, 0, getWidth(), getHeight());
  1269. int x0 = (int)(getWidth()*m_therange.getStart());
  1270. int x1 = (int)(getWidth()*m_therange.getEnd());
  1271. if (m_hot_area != ha_none)
  1272. g.setColour(Colours::white);
  1273. else g.setColour(Colours::lightgrey);
  1274. g.fillRect(x0, 0, x1 - x0, getHeight());
  1275. }
  1276. void zoom_scrollbar::setRange(Range<double> rng, bool docallback)
  1277. {
  1278. if (rng.isEmpty())
  1279. return;
  1280. m_therange = rng.constrainRange({ 0.0,1.0 });
  1281. if (RangeChanged && docallback)
  1282. RangeChanged(m_therange);
  1283. repaint();
  1284. }
  1285. zoom_scrollbar::hot_area zoom_scrollbar::get_hot_area(int x, int)
  1286. {
  1287. int x0 = (int)(getWidth()*m_therange.getStart());
  1288. int x1 = (int)(getWidth()*m_therange.getEnd());
  1289. if (is_in_range(x, x0 - 5, x0 + 5))
  1290. return ha_left_edge;
  1291. if (is_in_range(x, x1 - 5, x1 + 5))
  1292. return ha_right_edge;
  1293. if (is_in_range(x, x0 + 5, x1 - 5))
  1294. return ha_handle;
  1295. return ha_none;
  1296. }
  1297. RatioMixerEditor::RatioMixerEditor(int numratios)
  1298. {
  1299. for (int i = 0; i < numratios; ++i)
  1300. {
  1301. auto ratslid = std::make_unique<Slider>(Slider::LinearHorizontal,Slider::TextBoxBelow);
  1302. ratslid->setRange(0.125, 8.0);
  1303. ratslid->onValueChange = [this,i]() {OnRatioChanged(i, m_ratio_sliders[i]->getValue()); };
  1304. addAndMakeVisible(ratslid.get());
  1305. m_ratio_sliders.emplace_back(std::move(ratslid));
  1306. auto ratlevslid = std::make_unique<Slider>();
  1307. ratlevslid->setRange(0.0, 1.0);
  1308. ratlevslid->setSliderStyle(Slider::LinearVertical);
  1309. if (i==3)
  1310. ratlevslid->setValue(1.0,dontSendNotification);
  1311. else ratlevslid->setValue(0.0,dontSendNotification);
  1312. ratlevslid->onValueChange = [this, i]() { OnRatioLevelChanged(i, m_ratio_level_sliders[i]->getValue()); };
  1313. addAndMakeVisible(ratlevslid.get());
  1314. m_ratio_level_sliders.emplace_back(std::move(ratlevslid));
  1315. }
  1316. startTimer(200);
  1317. setOpaque(true);
  1318. }
  1319. void RatioMixerEditor::resized()
  1320. {
  1321. int nsliders = m_ratio_sliders.size();
  1322. int slidw = getWidth() / nsliders;
  1323. for (int i = 0; i < nsliders; ++i)
  1324. {
  1325. m_ratio_level_sliders[i]->setBounds(slidw/2+slidw * i-10, 1, 20, getHeight() - 50);
  1326. m_ratio_sliders[i]->setBounds(slidw * i, getHeight() - 48, slidw - 5, 47);
  1327. }
  1328. }
  1329. void RatioMixerEditor::timerCallback()
  1330. {
  1331. for (int i = 0; i < m_ratio_level_sliders.size(); ++i)
  1332. {
  1333. double v = GetParameterValue(0, i);
  1334. if (v!=m_ratio_sliders[i]->getValue())
  1335. m_ratio_sliders[i]->setValue(v, dontSendNotification);
  1336. v = GetParameterValue(1, i);
  1337. if (v!=m_ratio_level_sliders[i]->getValue())
  1338. m_ratio_level_sliders[i]->setValue(v, dontSendNotification);
  1339. }
  1340. }
  1341. void RatioMixerEditor::paint(Graphics & g)
  1342. {
  1343. g.fillAll(Colours::grey);
  1344. }