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.

1465 lines
46KB

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