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.

1543 lines
48KB

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