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.

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