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.

928 lines
30KB

  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)
  25. {
  26. addAndMakeVisible(&m_import_button);
  27. m_import_button.setButtonText("Import file...");
  28. m_import_button.onClick = [this]() { chooseFile(); };
  29. addAndMakeVisible(&m_settings_button);
  30. m_settings_button.setButtonText("Settings...");
  31. m_settings_button.onClick = [this]() { showSettingsMenu(); };
  32. addAndMakeVisible(&m_info_label);
  33. m_info_label.setJustificationType(Justification::centredRight);
  34. m_wavecomponent.GetFileCallback = [this]() { return processor.getAudioFile(); };
  35. addAndMakeVisible(&m_wavecomponent);
  36. const auto& pars = processor.getParameters();
  37. for (int i=0;i<pars.size();++i)
  38. {
  39. AudioProcessorParameterWithID* parid = dynamic_cast<AudioProcessorParameterWithID*>(pars[i]);
  40. jassert(parid);
  41. bool notifyonlyonrelease = false;
  42. if (parid->paramID.startsWith("fftsize") || parid->paramID.startsWith("numoutchans"))
  43. notifyonlyonrelease = true;
  44. m_parcomps.push_back(std::make_shared<ParameterComponent>(pars[i],notifyonlyonrelease));
  45. addAndMakeVisible(m_parcomps.back().get());
  46. }
  47. //addAndMakeVisible(&m_specvis);
  48. setSize (1000, 30+(pars.size()/2)*25+200);
  49. m_wavecomponent.TimeSelectionChangedCallback = [this](Range<double> range, int which)
  50. {
  51. *processor.getFloatParameter(cpi_soundstart) = range.getStart();
  52. *processor.getFloatParameter(cpi_soundend) = range.getEnd();
  53. };
  54. m_wavecomponent.CursorPosCallback = [this]()
  55. {
  56. return processor.getStretchSource()->getInfilePositionPercent();
  57. };
  58. m_wavecomponent.ShowFileCacheRange = true;
  59. m_spec_order_ed.setSource(processor.getStretchSource());
  60. addAndMakeVisible(&m_spec_order_ed);
  61. startTimer(1, 100);
  62. startTimer(2, 1000);
  63. startTimer(3, 200);
  64. m_wavecomponent.startTimer(100);
  65. }
  66. PaulstretchpluginAudioProcessorEditor::~PaulstretchpluginAudioProcessorEditor()
  67. {
  68. }
  69. void PaulstretchpluginAudioProcessorEditor::paint (Graphics& g)
  70. {
  71. g.fillAll(Colours::darkgrey);
  72. }
  73. void PaulstretchpluginAudioProcessorEditor::resized()
  74. {
  75. m_import_button.setBounds(1, 1, 60, 24);
  76. m_import_button.changeWidthToFitText();
  77. m_settings_button.setBounds(m_import_button.getRight() + 1, 1, 60, 24);
  78. m_settings_button.changeWidthToFitText();
  79. m_info_label.setBounds(m_settings_button.getRight() + 1, m_settings_button.getY(),
  80. getWidth()-m_settings_button.getRight()-1, 24);
  81. int w = getWidth();
  82. int xoffs = 1;
  83. int yoffs = 30;
  84. int div = w / 4;
  85. m_parcomps[cpi_capture_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  86. //xoffs += div;
  87. //m_parcomps[cpi_max_capture_len]->setBounds(xoffs, yoffs, div - 1, 24);
  88. xoffs += div;
  89. m_parcomps[cpi_passthrough]->setBounds(xoffs, yoffs, div - 1, 24);
  90. xoffs += div;
  91. m_parcomps[cpi_pause_enabled]->setBounds(xoffs, yoffs, div-1, 24);
  92. xoffs += div;
  93. m_parcomps[cpi_freeze]->setBounds(xoffs, yoffs, div - 1, 24);
  94. xoffs = 1;
  95. yoffs += 25;
  96. div = w / 2;
  97. m_parcomps[cpi_main_volume]->setBounds(xoffs, yoffs, div-1, 24);
  98. xoffs += div;
  99. m_parcomps[cpi_num_outchans]->setBounds(xoffs, yoffs, div-1, 24);
  100. xoffs = 1;
  101. yoffs += 25;
  102. m_parcomps[cpi_fftsize]->setBounds(xoffs, yoffs, div - 1, 24);
  103. xoffs += div;
  104. m_parcomps[cpi_stretchamount]->setBounds(xoffs, yoffs, div - 1, 24);
  105. xoffs = 1;
  106. yoffs += 25;
  107. m_parcomps[cpi_pitchshift]->setBounds(xoffs, yoffs, div - 1, 24);
  108. xoffs += div;
  109. m_parcomps[cpi_frequencyshift]->setBounds(xoffs, yoffs, div - 1, 24);
  110. xoffs = 1;
  111. yoffs += 25;
  112. m_parcomps[cpi_octavesm2]->setBounds(xoffs, yoffs, div - 1, 24);
  113. xoffs += div;
  114. m_parcomps[cpi_octavesm1]->setBounds(xoffs, yoffs, div - 1, 24);
  115. xoffs = 1;
  116. yoffs += 25;
  117. m_parcomps[cpi_octaves0]->setBounds(xoffs, yoffs, div - 1, 24);
  118. xoffs += div;
  119. m_parcomps[cpi_octaves1]->setBounds(xoffs, yoffs, div - 1, 24);
  120. xoffs = 1;
  121. yoffs += 25;
  122. m_parcomps[cpi_octaves15]->setBounds(xoffs, yoffs, div - 1, 24);
  123. xoffs += div;
  124. m_parcomps[cpi_octaves2]->setBounds(xoffs, yoffs, div - 1, 24);
  125. xoffs = 1;
  126. yoffs += 25;
  127. m_parcomps[cpi_numharmonics]->setBounds(xoffs, yoffs, div - 1, 24);
  128. xoffs += div;
  129. m_parcomps[cpi_harmonicsfreq]->setBounds(xoffs, yoffs, div - 1, 24);
  130. xoffs = 1;
  131. yoffs += 25;
  132. m_parcomps[cpi_harmonicsbw]->setBounds(xoffs, yoffs, div - 1, 24);
  133. xoffs += div;
  134. m_parcomps[cpi_harmonicsgauss]->setBounds(xoffs, yoffs, div - 1, 24);
  135. xoffs = 1;
  136. yoffs += 25;
  137. m_parcomps[cpi_spreadamount]->setBounds(xoffs, yoffs, div - 1, 24);
  138. xoffs += div;
  139. m_parcomps[cpi_compress]->setBounds(xoffs, yoffs, div - 1, 24);
  140. xoffs = 1;
  141. yoffs += 25;
  142. m_parcomps[cpi_tonalvsnoisebw]->setBounds(xoffs, yoffs, div - 1, 24);
  143. xoffs += div;
  144. m_parcomps[cpi_tonalvsnoisepreserve]->setBounds(xoffs, yoffs, div - 1, 24);
  145. xoffs = 1;
  146. yoffs += 25;
  147. m_parcomps[cpi_soundstart]->setBounds(xoffs, yoffs, div - 1, 24);
  148. xoffs += div;
  149. m_parcomps[cpi_soundend]->setBounds(xoffs, yoffs, div - 1, 24);
  150. xoffs = 1;
  151. yoffs += 25;
  152. m_parcomps[cpi_filter_low]->setBounds(xoffs, yoffs, div - 1, 24);
  153. xoffs += div;
  154. m_parcomps[cpi_filter_high]->setBounds(xoffs, yoffs, div - 1, 24);
  155. xoffs = 1;
  156. yoffs += 25;
  157. m_parcomps[cpi_loopxfadelen]->setBounds(xoffs, yoffs, div - 1, 24);
  158. xoffs += div;
  159. m_parcomps[cpi_onsetdetection]->setBounds(xoffs, yoffs, div - 1, 24);
  160. yoffs += 25;
  161. int remain_h = getHeight() - 1 - yoffs;
  162. m_spec_order_ed.setBounds(1, yoffs, getWidth() - 2, remain_h / 5 * 1);
  163. m_wavecomponent.setBounds(1, m_spec_order_ed.getBottom()+1, getWidth()-2, remain_h/5*4);
  164. //m_specvis.setBounds(1, yoffs, getWidth() - 2, getHeight() - 1 - yoffs);
  165. }
  166. void PaulstretchpluginAudioProcessorEditor::timerCallback(int id)
  167. {
  168. if (id == 1)
  169. {
  170. for (auto& e : m_parcomps)
  171. e->updateComponent();
  172. if (processor.isRecordingEnabled())
  173. {
  174. m_wavecomponent.setRecordingPosition(processor.getRecordingPositionPercent());
  175. } else
  176. m_wavecomponent.setRecordingPosition(-1.0);
  177. String infotext = String(processor.getPreBufferingPercent()*100.0, 1) + "% buffered "
  178. + String(processor.getStretchSource()->m_param_change_count)+" param changes "+m_last_err+" FFT size "+
  179. String(processor.getStretchSource()->getFFTSize());
  180. if (processor.m_abnormal_output_samples > 0)
  181. infotext += " " + String(processor.m_abnormal_output_samples) + " invalid sample values";
  182. if (processor.isNonRealtime())
  183. infotext += " (offline rendering)";
  184. if (processor.m_playposinfo.isPlaying)
  185. infotext += " "+String(processor.m_playposinfo.timeInSeconds,1);
  186. m_info_label.setText(infotext, dontSendNotification);
  187. }
  188. if (id == 2)
  189. {
  190. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  191. if (processor.m_state_dirty)
  192. {
  193. m_spec_order_ed.setSource(processor.getStretchSource());
  194. processor.m_state_dirty = false;
  195. }
  196. }
  197. if (id == 3)
  198. {
  199. //m_specvis.setState(processor.getStretchSource()->getProcessParameters(), processor.getStretchSource()->getFFTSize() / 2,
  200. // processor.getSampleRate());
  201. }
  202. }
  203. bool PaulstretchpluginAudioProcessorEditor::isInterestedInFileDrag(const StringArray & files)
  204. {
  205. if (files.size() == 0)
  206. return false;
  207. File f(files[0]);
  208. String extension = f.getFileExtension().toLowerCase();
  209. if (processor.m_afm->getWildcardForAllFormats().containsIgnoreCase(extension))
  210. return true;
  211. return false;
  212. }
  213. void PaulstretchpluginAudioProcessorEditor::filesDropped(const StringArray & files, int x, int y)
  214. {
  215. if (files.size() > 0)
  216. {
  217. File f(files[0]);
  218. processor.setAudioFile(f);
  219. toFront(true);
  220. }
  221. }
  222. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  223. {
  224. String initiallocfn = processor.m_propsfile->m_props_file->getValue("importfilefolder",
  225. File::getSpecialLocation(File::userHomeDirectory).getFullPathName());
  226. File initialloc(initiallocfn);
  227. String filterstring = processor.m_afm->getWildcardForAllFormats();
  228. FileChooser myChooser("Please select audio file...",
  229. initialloc,
  230. filterstring,true);
  231. if (myChooser.browseForFileToOpen())
  232. {
  233. File resu = myChooser.getResult();
  234. String pathname = resu.getFullPathName();
  235. if (pathname.startsWith("/localhost"))
  236. {
  237. pathname = pathname.substring(10);
  238. resu = File(pathname);
  239. }
  240. processor.m_propsfile->m_props_file->setValue("importfilefolder", resu.getParentDirectory().getFullPathName());
  241. m_last_err = processor.setAudioFile(resu);
  242. }
  243. }
  244. void PaulstretchpluginAudioProcessorEditor::showSettingsMenu()
  245. {
  246. PopupMenu menu;
  247. menu.addItem(4, "Reset parameters", true, false);
  248. menu.addItem(5, "Load file with plugin state", true, processor.m_load_file_with_state);
  249. menu.addItem(1, "Play when host transport running", true, processor.m_play_when_host_plays);
  250. menu.addItem(2, "Capture when host transport running", true, processor.m_capture_when_host_plays);
  251. PopupMenu bufferingmenu;
  252. int curbufamount = processor.getPreBufferAmount();
  253. bufferingmenu.addItem(100,"None",true,curbufamount == -1);
  254. bufferingmenu.addItem(101,"Small",true,curbufamount == 1);
  255. bufferingmenu.addItem(102,"Medium",true,curbufamount == 2);
  256. bufferingmenu.addItem(103,"Large",true,curbufamount == 3);
  257. bufferingmenu.addItem(104,"Very large",true,curbufamount == 4);
  258. bufferingmenu.addItem(105,"Huge",true,curbufamount == 5);
  259. menu.addSubMenu("Prebuffering", bufferingmenu);
  260. int capturelen = *processor.getFloatParameter(cpi_max_capture_len);
  261. PopupMenu capturelenmenu;
  262. std::vector<int> capturelens{ 2,5,10,30,60,120 };
  263. for (int i=0;i<capturelens.size();++i)
  264. capturelenmenu.addItem(200+i, String(capturelens[i])+" seconds", true, capturelen == capturelens[i]);
  265. menu.addSubMenu("Capture buffer length", capturelenmenu);
  266. menu.addItem(3, "About...", true, false);
  267. #ifdef JUCE_DEBUG
  268. menu.addItem(6, "Dump preset to clipboard", true, false);
  269. #endif
  270. int r = menu.show();
  271. if (r >= 200 && r < 210)
  272. {
  273. int caplen = capturelens[r - 200];
  274. *processor.getFloatParameter(cpi_max_capture_len) = (float)caplen;
  275. }
  276. if (r == 1)
  277. {
  278. processor.m_play_when_host_plays = !processor.m_play_when_host_plays;
  279. }
  280. if (r == 2)
  281. {
  282. processor.m_capture_when_host_plays = !processor.m_capture_when_host_plays;
  283. }
  284. if (r == 4)
  285. {
  286. processor.resetParameters();
  287. }
  288. if (r == 5)
  289. {
  290. processor.m_load_file_with_state = !processor.m_load_file_with_state;
  291. }
  292. if (r == 3)
  293. {
  294. String fftlib = fftwf_version;
  295. String juceversiontxt = String("JUCE ") + String(JUCE_MAJOR_VERSION) + "." + String(JUCE_MINOR_VERSION);
  296. AlertWindow::showMessageBoxAsync(AlertWindow::InfoIcon,
  297. g_plugintitle,
  298. "Plugin for extreme time stretching and other sound processing\nBuilt on " + String(__DATE__) + " " + String(__TIME__) + "\n"
  299. "Copyright (C) 2006-2011 Nasca Octavian Paul, Tg. Mures, Romania\n"
  300. "(C) 2017-2018 Xenakios\n\n"
  301. "Using " + fftlib + " for FFT\n\n"
  302. + juceversiontxt + " (c) Roli. Used under the GPL license.\n\n"
  303. "GPL licensed source code for this plugin at : https://bitbucket.org/xenakios/paulstretchplugin/overview\n"
  304. , "OK",
  305. this);
  306. }
  307. if (r >= 100 && r < 200)
  308. {
  309. if (r == 100)
  310. processor.m_use_backgroundbuffering = false;
  311. if (r > 100)
  312. processor.setPreBufferAmount(r-100);
  313. }
  314. if (r == 6)
  315. {
  316. ValueTree tree = processor.getStateTree(true,true);
  317. MemoryBlock destData;
  318. MemoryOutputStream stream(destData, true);
  319. tree.writeToStream(stream);
  320. String txt = Base64::toBase64(destData.getData(), destData.getSize());
  321. SystemClipboard::copyTextToClipboard(txt);
  322. }
  323. }
  324. WaveformComponent::WaveformComponent(AudioFormatManager* afm, AudioThumbnail* thumb)
  325. {
  326. TimeSelectionChangedCallback = [](Range<double>, int) {};
  327. if (m_use_opengl == true)
  328. m_ogl.attachTo(*this);
  329. m_thumbnail = thumb;
  330. m_thumbnail->addChangeListener(this);
  331. setOpaque(true);
  332. }
  333. WaveformComponent::~WaveformComponent()
  334. {
  335. if (m_use_opengl == true)
  336. m_ogl.detach();
  337. m_thumbnail->removeChangeListener(this);
  338. }
  339. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  340. {
  341. m_waveimage = Image();
  342. repaint();
  343. }
  344. void WaveformComponent::paint(Graphics & g)
  345. {
  346. jassert(GetFileCallback);
  347. //Logger::writeToLog("Waveform component paint");
  348. g.fillAll(Colours::black);
  349. g.setColour(Colours::darkgrey);
  350. g.fillRect(0, 0, getWidth(), m_topmargin);
  351. if (m_thumbnail == nullptr || m_thumbnail->getTotalLength() < 0.01)
  352. {
  353. g.setColour(Colours::aqua.darker());
  354. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  355. return;
  356. }
  357. g.setColour(Colours::lightslategrey);
  358. double thumblen = m_thumbnail->getTotalLength();
  359. double tick_interval = 1.0;
  360. if (thumblen > 60.0)
  361. tick_interval = 5.0;
  362. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  363. {
  364. float tickxcor = (float)jmap<double>(secs,
  365. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  366. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  367. }
  368. bool m_use_cached_image = true;
  369. if (m_use_cached_image == true)
  370. {
  371. if (m_waveimage.isValid() == false || m_waveimage.getWidth() != getWidth()
  372. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  373. {
  374. //Logger::writeToLog("updating cached waveform image");
  375. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  376. Graphics tempg(m_waveimage);
  377. tempg.fillAll(Colours::black);
  378. tempg.setColour(Colours::darkgrey);
  379. m_thumbnail->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  380. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  381. }
  382. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  383. }
  384. else
  385. {
  386. //g.fillAll(Colours::black);
  387. g.setColour(Colours::darkgrey);
  388. m_thumbnail->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  389. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  390. }
  391. //g.setColour(Colours::darkgrey);
  392. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  393. // 0.0, thumblen, 1.0f);
  394. g.setColour(Colours::white.withAlpha(0.5f));
  395. double sel_len = m_time_sel_end - m_time_sel_start;
  396. //if (sel_len > 0.0 && sel_len < 1.0)
  397. {
  398. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  399. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  400. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  401. }
  402. if (m_file_cached.first.getLength() > 0.0 &&
  403. (bool)ShowFileCacheRange.getValue())
  404. {
  405. g.setColour(Colours::red.withAlpha(0.2f));
  406. int xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  407. int xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  408. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  409. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  410. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  411. if (xcorright - xcorleft>0)
  412. {
  413. g.setColour(Colours::blue.withAlpha(0.2f));
  414. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  415. }
  416. }
  417. g.setColour(Colours::white);
  418. if (CursorPosCallback)
  419. {
  420. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  421. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  422. }
  423. if (m_rec_pos >= 0.0)
  424. {
  425. g.setColour(Colours::lightpink);
  426. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  427. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  428. }
  429. g.setColour(Colours::aqua.darker());
  430. g.drawText(GetFileCallback().getFileName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  431. }
  432. void WaveformComponent::timerCallback()
  433. {
  434. repaint();
  435. }
  436. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  437. {
  438. m_file_cached = rng;
  439. //repaint();
  440. }
  441. void WaveformComponent::setTimerEnabled(bool b)
  442. {
  443. if (b == true)
  444. startTimer(100);
  445. else
  446. stopTimer();
  447. }
  448. void WaveformComponent::setViewRange(Range<double> rng)
  449. {
  450. m_view_range = rng;
  451. m_waveimage = Image();
  452. repaint();
  453. }
  454. void WaveformComponent::mouseDown(const MouseEvent & e)
  455. {
  456. m_mousedown = true;
  457. m_lock_timesel_set = true;
  458. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  459. if (e.y < m_topmargin)
  460. {
  461. if (SeekCallback)
  462. SeekCallback(pos);
  463. m_didseek = true;
  464. }
  465. else
  466. {
  467. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  468. m_drag_time_start = pos;
  469. if (m_time_sel_drag_target == 0)
  470. {
  471. //m_time_sel_start = 0.0;
  472. //m_time_sel_end = 1.0;
  473. }
  474. }
  475. repaint();
  476. }
  477. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  478. {
  479. m_lock_timesel_set = false;
  480. m_mousedown = false;
  481. m_didseek = false;
  482. if (m_didchangetimeselection)
  483. {
  484. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  485. m_didchangetimeselection = false;
  486. }
  487. }
  488. void WaveformComponent::mouseDrag(const MouseEvent & e)
  489. {
  490. if (m_didseek == true)
  491. return;
  492. if (m_time_sel_drag_target == 0)
  493. {
  494. m_time_sel_start = m_drag_time_start;
  495. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  496. }
  497. if (m_time_sel_drag_target == 1)
  498. {
  499. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  500. }
  501. if (m_time_sel_drag_target == 2)
  502. {
  503. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  504. }
  505. if (m_time_sel_start > m_time_sel_end)
  506. {
  507. std::swap(m_time_sel_start, m_time_sel_end);
  508. if (m_time_sel_drag_target == 1)
  509. m_time_sel_drag_target = 2;
  510. else if (m_time_sel_drag_target == 2)
  511. m_time_sel_drag_target = 1;
  512. }
  513. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  514. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  515. if (TimeSelectionChangedCallback)
  516. {
  517. if (m_time_sel_end>m_time_sel_start)
  518. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  519. else
  520. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  521. }
  522. m_didchangetimeselection = true;
  523. repaint();
  524. }
  525. void WaveformComponent::mouseMove(const MouseEvent & e)
  526. {
  527. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  528. if (m_time_sel_drag_target == 0)
  529. setMouseCursor(MouseCursor::NormalCursor);
  530. if (m_time_sel_drag_target == 1)
  531. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  532. if (m_time_sel_drag_target == 2)
  533. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  534. }
  535. void WaveformComponent::mouseDoubleClick(const MouseEvent & e)
  536. {
  537. m_time_sel_start = 0.0;
  538. m_time_sel_end = 1.0;
  539. TimeSelectionChangedCallback({ 0.0,1.0 }, 0);
  540. repaint();
  541. }
  542. Range<double> WaveformComponent::getTimeSelection()
  543. {
  544. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  545. return { m_time_sel_start, m_time_sel_end };
  546. return { 0.0, 1.0 };
  547. }
  548. void WaveformComponent::setTimeSelection(Range<double> rng)
  549. {
  550. if (m_lock_timesel_set == true)
  551. return;
  552. if (rng.isEmpty())
  553. rng = { -1.0,1.0 };
  554. m_time_sel_start = rng.getStart();
  555. m_time_sel_end = rng.getEnd();
  556. repaint();
  557. }
  558. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  559. {
  560. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  561. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  562. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  563. return 1;
  564. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  565. return 2;
  566. return 0;
  567. }
  568. SpectralVisualizer::SpectralVisualizer()
  569. {
  570. m_img = Image(Image::RGB, 500, 200, true);
  571. }
  572. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  573. {
  574. double t0 = Time::getMillisecondCounterHiRes();
  575. double hz = 440.0;
  576. int numharmonics = 40;
  577. double scaler = 1.0 / numharmonics;
  578. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  579. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  580. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  581. {
  582. m_nfreqs = nfreqs;
  583. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  584. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  585. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  586. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  587. m_fft = std::make_unique<FFT>(nfreqs*2);
  588. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  589. for (int i = 0; i < nfreqs; ++i)
  590. {
  591. for (int j = 0; j < numharmonics; ++j)
  592. {
  593. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  594. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  595. }
  596. }
  597. }
  598. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  599. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  600. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  601. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  602. for (int i = 0; i < nfreqs; ++i)
  603. {
  604. m_fft->smp[i] = m_insamples[i];
  605. }
  606. m_fft->applywindow(W_HAMMING);
  607. m_fft->smp2freq();
  608. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  609. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  610. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  611. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  612. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  613. if (pars.harmonics.enabled)
  614. spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  615. else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  616. Graphics g(m_img);
  617. g.fillAll(Colours::black);
  618. g.setColour(Colours::white);
  619. for (int i = 0; i < nfreqs; ++i)
  620. {
  621. double binfreq = (samplerate / 2 / nfreqs)*i;
  622. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  623. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  624. ycor = jlimit<double>(0.0, getHeight(), ycor);
  625. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  626. }
  627. double t1 = Time::getMillisecondCounterHiRes();
  628. m_elapsed = t1 - t0;
  629. repaint();
  630. }
  631. void SpectralVisualizer::paint(Graphics & g)
  632. {
  633. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  634. g.setColour(Colours::yellow);
  635. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  636. }
  637. void SpectralChainEditor::paint(Graphics & g)
  638. {
  639. g.fillAll(Colours::black);
  640. if (m_src == nullptr)
  641. return;
  642. int box_w = getWidth() / m_order.size();
  643. int box_h = getHeight();
  644. for (int i = 0; i < m_order.size(); ++i)
  645. {
  646. //if (i!=m_cur_index)
  647. drawBox(g, i, i*box_w, 0, box_w - 30, box_h);
  648. if (i<m_order.size() - 1)
  649. g.drawArrow(juce::Line<float>(i*box_w + (box_w - 30), box_h / 2, i*box_w + box_w, box_h / 2), 2.0f, 15.0f, 15.0f);
  650. }
  651. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  652. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  653. }
  654. void SpectralChainEditor::setSource(StretchAudioSource * src)
  655. {
  656. m_src = src;
  657. m_order = m_src->getSpectrumProcessOrder();
  658. repaint();
  659. }
  660. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  661. {
  662. m_did_drag = false;
  663. int box_w = getWidth() / m_order.size();
  664. int box_h = getHeight();
  665. m_cur_index = ev.x / box_w;
  666. if (m_cur_index >= 0)
  667. {
  668. juce::Rectangle<int> r(box_w*m_cur_index, 1, 12, 12);
  669. if (r.contains(ev.x, ev.y))
  670. {
  671. m_order[m_cur_index].m_enabled = !m_order[m_cur_index].m_enabled;
  672. m_src->setSpectrumProcessOrder(m_order);
  673. repaint();
  674. return;
  675. }
  676. }
  677. m_drag_x = -1;
  678. repaint();
  679. }
  680. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  681. {
  682. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  683. {
  684. int box_w = getWidth() / m_order.size();
  685. int box_h = getHeight();
  686. int new_index = ev.x / box_w;
  687. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  688. {
  689. std::swap(m_order[m_cur_index], m_order[new_index]);
  690. m_cur_index = new_index;
  691. m_did_drag = true;
  692. m_src->setSpectrumProcessOrder(m_order);
  693. }
  694. m_drag_x = ev.x;
  695. repaint();
  696. }
  697. }
  698. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  699. {
  700. m_drag_x = -1;
  701. //m_cur_index = -1;
  702. repaint();
  703. }
  704. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  705. {
  706. String txt;
  707. if (m_order[index].m_index == 0)
  708. txt = "Harmonics";
  709. if (m_order[index].m_index == 1)
  710. txt = "Tonal vs Noise";
  711. if (m_order[index].m_index == 2)
  712. txt = "Frequency shift";
  713. if (m_order[index].m_index == 3)
  714. txt = "Pitch shift";
  715. if (m_order[index].m_index == 4)
  716. txt = "Octaves";
  717. if (m_order[index].m_index == 5)
  718. txt = "Spread";
  719. if (m_order[index].m_index == 6)
  720. txt = "Filter";
  721. if (m_order[index].m_index == 7)
  722. txt = "Compressor";
  723. if (index == m_cur_index)
  724. {
  725. g.setColour(Colours::darkgrey);
  726. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  727. g.fillRect(x, y, w, h);
  728. }
  729. g.setColour(Colours::white);
  730. g.drawRect(x, y, w, h);
  731. g.drawFittedText(txt, x,y,w,h, Justification::centred, 3);
  732. g.setColour(Colours::gold);
  733. g.drawRect(x + 2, y + 2, 12, 12);
  734. if (m_order[index].m_enabled == true)
  735. {
  736. g.drawLine(x+2, y+2, x+14, y+14);
  737. g.drawLine(x+2, y+14, x+14, y+2);
  738. }
  739. g.setColour(Colours::white);
  740. }
  741. ParameterComponent::ParameterComponent(AudioProcessorParameter * par, bool notifyOnlyOnRelease) : m_par(par)
  742. {
  743. addAndMakeVisible(&m_label);
  744. m_label.setText(par->getName(50), dontSendNotification);
  745. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  746. if (floatpar)
  747. {
  748. m_slider = std::make_unique<MySlider>(&floatpar->range);
  749. m_notify_only_on_release = notifyOnlyOnRelease;
  750. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  751. m_slider->setValue(*floatpar, dontSendNotification);
  752. m_slider->addListener(this);
  753. addAndMakeVisible(m_slider.get());
  754. }
  755. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(par);
  756. if (intpar)
  757. {
  758. m_slider = std::make_unique<MySlider>();
  759. m_notify_only_on_release = notifyOnlyOnRelease;
  760. m_slider->setRange(intpar->getRange().getStart(), intpar->getRange().getEnd(), 1.0);
  761. m_slider->setValue(*intpar, dontSendNotification);
  762. m_slider->addListener(this);
  763. addAndMakeVisible(m_slider.get());
  764. }
  765. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  766. if (choicepar)
  767. {
  768. }
  769. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  770. if (boolpar)
  771. {
  772. m_togglebut = std::make_unique<ToggleButton>();
  773. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  774. m_togglebut->addListener(this);
  775. m_togglebut->setButtonText(par->getName(50));
  776. addAndMakeVisible(m_togglebut.get());
  777. }
  778. }
  779. void ParameterComponent::resized()
  780. {
  781. if (m_slider)
  782. {
  783. m_label.setBounds(0, 0, 200, 24);
  784. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  785. }
  786. if (m_togglebut)
  787. m_togglebut->setBounds(1, 0, getWidth() - 1, 24);
  788. }
  789. void ParameterComponent::sliderValueChanged(Slider * slid)
  790. {
  791. if (m_notify_only_on_release == true)
  792. return;
  793. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  794. if (floatpar != nullptr)
  795. *floatpar = slid->getValue();
  796. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  797. if (intpar != nullptr)
  798. *intpar = slid->getValue();
  799. }
  800. void ParameterComponent::sliderDragStarted(Slider * slid)
  801. {
  802. m_dragging = true;
  803. }
  804. void ParameterComponent::sliderDragEnded(Slider * slid)
  805. {
  806. m_dragging = false;
  807. if (m_notify_only_on_release == false)
  808. return;
  809. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  810. if (floatpar != nullptr)
  811. *floatpar = slid->getValue();
  812. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  813. if (intpar != nullptr)
  814. *intpar = slid->getValue();
  815. }
  816. void ParameterComponent::buttonClicked(Button * but)
  817. {
  818. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  819. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  820. {
  821. *boolpar = m_togglebut->getToggleState();
  822. }
  823. }
  824. void ParameterComponent::updateComponent()
  825. {
  826. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  827. if (floatpar != nullptr && m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  828. {
  829. m_slider->setValue(*floatpar, dontSendNotification);
  830. }
  831. AudioParameterInt* intpar = dynamic_cast<AudioParameterInt*>(m_par);
  832. if (intpar != nullptr && m_slider != nullptr && m_dragging == false && (int)m_slider->getValue() != *intpar)
  833. {
  834. m_slider->setValue(*intpar, dontSendNotification);
  835. }
  836. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  837. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  838. {
  839. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  840. }
  841. }
  842. MySlider::MySlider(NormalisableRange<float>* range) : m_range(range)
  843. {
  844. }
  845. double MySlider::proportionOfLengthToValue(double x)
  846. {
  847. if (m_range)
  848. return m_range->convertFrom0to1(x);
  849. return Slider::proportionOfLengthToValue(x);
  850. }
  851. double MySlider::valueToProportionOfLength(double x)
  852. {
  853. if (m_range)
  854. return m_range->convertTo0to1(x);
  855. return Slider::valueToProportionOfLength(x);
  856. }