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.

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