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.

908 lines
29KB

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