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.

1576 lines
50KB

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