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.

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