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.

1330 lines
42KB

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