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.

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