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.

1473 lines
46KB

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