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.

648 lines
21KB

  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. //==============================================================================
  20. PaulstretchpluginAudioProcessorEditor::PaulstretchpluginAudioProcessorEditor (PaulstretchpluginAudioProcessor& p)
  21. : AudioProcessorEditor (&p),
  22. m_wavecomponent(p.m_afm),
  23. processor (p)
  24. {
  25. addAndMakeVisible(&m_import_button);
  26. m_import_button.setButtonText("Import file...");
  27. attachCallback(m_import_button, [this]() { chooseFile(); });
  28. addAndMakeVisible(&m_info_label);
  29. addAndMakeVisible(&m_wavecomponent);
  30. const auto& pars = processor.getParameters();
  31. for (int i=0;i<pars.size();++i)
  32. {
  33. AudioProcessorParameterWithID* parid = dynamic_cast<AudioProcessorParameterWithID*>(pars[i]);
  34. jassert(parid);
  35. if (parid)
  36. {
  37. bool notifyonlyonrelease = false;
  38. if (parid->paramID.startsWith("fftsize"))
  39. notifyonlyonrelease = true;
  40. if (parid->paramID.startsWith("numoutchans"))
  41. notifyonlyonrelease = true;
  42. m_parcomps.push_back(std::make_shared<ParameterComponent>(pars[i],notifyonlyonrelease));
  43. addAndMakeVisible(m_parcomps.back().get());
  44. }
  45. }
  46. //addAndMakeVisible(&m_specvis);
  47. setSize (1000, 30+(pars.size()/2)*25+200);
  48. m_wavecomponent.TimeSelectionChangedCallback = [this](Range<double> range, int which)
  49. {
  50. *processor.getFloatParameter(5) = range.getStart();
  51. *processor.getFloatParameter(6) = range.getEnd();
  52. };
  53. m_wavecomponent.CursorPosCallback = [this]()
  54. {
  55. return processor.getStretchSource()->getInfilePositionPercent();
  56. };
  57. m_wavecomponent.ShowFileCacheRange = true;
  58. m_spec_order_ed.setSource(processor.getStretchSource());
  59. addAndMakeVisible(&m_spec_order_ed);
  60. startTimer(1, 100);
  61. startTimer(2, 1000);
  62. startTimer(3, 200);
  63. m_wavecomponent.startTimer(100);
  64. }
  65. PaulstretchpluginAudioProcessorEditor::~PaulstretchpluginAudioProcessorEditor()
  66. {
  67. }
  68. void PaulstretchpluginAudioProcessorEditor::paint (Graphics& g)
  69. {
  70. g.fillAll(Colours::darkgrey);
  71. }
  72. void PaulstretchpluginAudioProcessorEditor::resized()
  73. {
  74. m_import_button.setBounds(1, 1, 60, 24);
  75. m_import_button.changeWidthToFitText();
  76. m_info_label.setBounds(m_import_button.getRight() + 1, m_import_button.getY(), getWidth()-m_import_button.getRight()-1, 24);
  77. std::array<int,30> indexes;
  78. indexes[cpi_main_volume] = 0; indexes[cpi_pause_enabled] = 1;
  79. indexes[cpi_capture_enabled] = 2; indexes[cpi_max_capture_len] = 3;
  80. indexes[cpi_freeze] = 4; indexes[cpi_num_outchans] = 5;
  81. indexes[cpi_fftsize] = 6; indexes[cpi_stretchamount] = 7;
  82. indexes[cpi_pitchshift] = 8; indexes[cpi_frequencyshift] = 9;
  83. indexes[cpi_octavesm2] = 10; indexes[cpi_octavesm1] = 11;
  84. indexes[cpi_octaves0] = 12; indexes[cpi_octaves1] = 13;
  85. indexes[cpi_octaves15] = 14; indexes[cpi_octaves2] = 15;
  86. indexes[cpi_numharmonics] = 16; indexes[cpi_harmonicsfreq] = 17;
  87. indexes[cpi_harmonicsbw] = 18; indexes[cpi_harmonicsgauss] = 19;
  88. indexes[cpi_spreadamount] = 20; indexes[cpi_compress] = 21;
  89. indexes[cpi_tonalvsnoisebw] = 22; indexes[cpi_tonalvsnoisepreserve] = 23;
  90. indexes[cpi_soundstart] = 24; indexes[cpi_soundend] = 25;
  91. indexes[cpi_filter_low] = 26; indexes[cpi_filter_high] = 27;
  92. indexes[cpi_loopxfadelen] = 28; indexes[cpi_onsetdetection] = 29;
  93. for (int i = 0; i < indexes.size(); ++i)
  94. {
  95. int tempfoo = indexes[i];
  96. int gridx = tempfoo % 2;
  97. int gridy = tempfoo / 2;
  98. m_parcomps[i]->setBounds(1+gridx*(getWidth()/2), 30 + gridy * 25, getWidth()/2-2, 24);
  99. }
  100. int yoffs = m_parcomps[cpi_loopxfadelen]->getBottom() + 1;
  101. int remain_h = getHeight() - 1 - yoffs;
  102. m_spec_order_ed.setBounds(1, yoffs, getWidth() - 2, remain_h / 5 * 1);
  103. m_wavecomponent.setBounds(1, m_spec_order_ed.getBottom()+1, getWidth()-2, remain_h/5*4);
  104. //m_specvis.setBounds(1, yoffs, getWidth() - 2, getHeight() - 1 - yoffs);
  105. }
  106. void PaulstretchpluginAudioProcessorEditor::timerCallback(int id)
  107. {
  108. if (id == 1)
  109. {
  110. for (auto& e : m_parcomps)
  111. e->updateComponent();
  112. if (processor.isRecordingEnabled())
  113. {
  114. m_wavecomponent.setRecordingPosition(processor.getRecordingPositionPercent());
  115. } else
  116. m_wavecomponent.setRecordingPosition(-1.0);
  117. String infotext = String(processor.getPreBufferingPercent(), 1) + "% buffered "
  118. + String(processor.getStretchSource()->m_param_change_count)+" param changes "+m_last_err+" FFT size "+
  119. String(processor.getStretchSource()->getFFTSize());
  120. if (processor.m_abnormal_output_samples > 0)
  121. infotext += " " + String(processor.m_abnormal_output_samples) + " invalid sample values";
  122. if (processor.isNonRealtime())
  123. infotext += " (offline rendering)";
  124. m_info_label.setText(infotext, dontSendNotification);
  125. }
  126. if (id == 2)
  127. {
  128. if (processor.getAudioFile() != File() && processor.getAudioFile() != m_wavecomponent.getAudioFile())
  129. {
  130. m_wavecomponent.setAudioFile(processor.getAudioFile());
  131. }
  132. if (processor.getAudioFile()==File() && processor.isRecordingEnabled()==false && m_wavecomponent.isUsingAudioBuffer()==false)
  133. {
  134. auto bufptr = processor.getStretchSource()->getSourceAudioBuffer();
  135. if (bufptr!=nullptr)
  136. m_wavecomponent.setAudioBuffer(bufptr,
  137. processor.getSampleRateChecked(), bufptr->getNumSamples());
  138. }
  139. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  140. }
  141. if (id == 3)
  142. {
  143. //m_specvis.setState(processor.getStretchSource()->getProcessParameters(), processor.getStretchSource()->getFFTSize() / 2,
  144. // processor.getSampleRate());
  145. }
  146. }
  147. void PaulstretchpluginAudioProcessorEditor::setAudioFile(File f)
  148. {
  149. m_wavecomponent.setAudioFile(f);
  150. }
  151. void PaulstretchpluginAudioProcessorEditor::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  152. {
  153. MessageManager::callAsync([this,buf, samplerate, len]()
  154. {
  155. m_wavecomponent.setAudioBuffer(buf, samplerate, len);
  156. });
  157. }
  158. void PaulstretchpluginAudioProcessorEditor::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  159. {
  160. m_wavecomponent.beginAddingAudioBlocks(channels, samplerate, totalllen);
  161. }
  162. void PaulstretchpluginAudioProcessorEditor::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  163. {
  164. m_wavecomponent.addAudioBlock(buf, samplerate, pos);
  165. }
  166. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  167. {
  168. File initialloc = File::getSpecialLocation(File::userHomeDirectory);
  169. String filterstring = processor.m_afm->getWildcardForAllFormats();
  170. FileChooser myChooser("Please select audio file...",
  171. initialloc,
  172. filterstring,true);
  173. if (myChooser.browseForFileToOpen())
  174. {
  175. File resu = myChooser.getResult();
  176. String pathname = resu.getFullPathName();
  177. if (pathname.startsWith("/localhost"))
  178. {
  179. pathname = pathname.substring(10);
  180. resu = File(pathname);
  181. }
  182. m_last_err = processor.setAudioFile(resu);
  183. if (processor.getAudioFile() != File())
  184. {
  185. m_wavecomponent.setAudioFile(processor.getAudioFile());
  186. }
  187. }
  188. }
  189. WaveformComponent::WaveformComponent(AudioFormatManager* afm)
  190. {
  191. TimeSelectionChangedCallback = [](Range<double>, int) {};
  192. if (m_use_opengl == true)
  193. m_ogl.attachTo(*this);
  194. // The default priority of 2 is a bit too low in some cases, it seems...
  195. m_thumbcache->getTimeSliceThread().setPriority(3);
  196. m_thumb = std::make_unique<AudioThumbnail>(512, *afm, *m_thumbcache);
  197. m_thumb->addChangeListener(this);
  198. setOpaque(true);
  199. }
  200. WaveformComponent::~WaveformComponent()
  201. {
  202. if (m_use_opengl == true)
  203. m_ogl.detach();
  204. }
  205. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  206. {
  207. m_waveimage = Image();
  208. repaint();
  209. }
  210. void WaveformComponent::paint(Graphics & g)
  211. {
  212. //Logger::writeToLog("Waveform component paint");
  213. g.fillAll(Colours::black);
  214. g.setColour(Colours::darkgrey);
  215. g.fillRect(0, 0, getWidth(), m_topmargin);
  216. if (m_thumb == nullptr || m_thumb->getTotalLength() < 0.1)
  217. {
  218. g.setColour(Colours::aqua.darker());
  219. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  220. return;
  221. }
  222. g.setColour(Colours::lightslategrey);
  223. double thumblen = m_thumb->getTotalLength();
  224. double tick_interval = 1.0;
  225. if (thumblen > 60.0)
  226. tick_interval = 5.0;
  227. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  228. {
  229. float tickxcor = (float)jmap<double>(secs,
  230. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  231. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  232. }
  233. bool m_use_cached_image = true;
  234. if (m_use_cached_image == true)
  235. {
  236. if (m_waveimage.isValid() == false || m_waveimage.getWidth() != getWidth()
  237. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  238. {
  239. //Logger::writeToLog("updating cached waveform image");
  240. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  241. Graphics tempg(m_waveimage);
  242. tempg.fillAll(Colours::black);
  243. tempg.setColour(Colours::darkgrey);
  244. m_thumb->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  245. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  246. }
  247. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  248. }
  249. else
  250. {
  251. //g.fillAll(Colours::black);
  252. g.setColour(Colours::darkgrey);
  253. m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  254. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  255. }
  256. //g.setColour(Colours::darkgrey);
  257. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  258. // 0.0, thumblen, 1.0f);
  259. g.setColour(Colours::white.withAlpha(0.5f));
  260. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  261. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  262. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  263. if (m_file_cached.first.getLength() > 0.0 &&
  264. (bool)ShowFileCacheRange.getValue())
  265. {
  266. g.setColour(Colours::red.withAlpha(0.2f));
  267. xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  268. xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  269. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  270. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  271. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  272. if (xcorright - xcorleft>0)
  273. {
  274. g.setColour(Colours::blue.withAlpha(0.2f));
  275. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  276. }
  277. }
  278. g.setColour(Colours::white);
  279. if (CursorPosCallback)
  280. {
  281. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  282. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  283. }
  284. if (m_rec_pos >= 0.0)
  285. {
  286. g.setColour(Colours::lightpink);
  287. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  288. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  289. }
  290. g.setColour(Colours::aqua.darker());
  291. g.drawText(m_curfile.getFullPathName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  292. }
  293. void WaveformComponent::setAudioFile(File f)
  294. {
  295. if (f.existsAsFile())
  296. {
  297. m_waveimage = Image();
  298. if (m_thumb != nullptr && f == m_curfile) // reloading same file, might happen that the overview needs to be redone...
  299. m_thumbcache->removeThumb(m_thumb->getHashCode());
  300. if (m_thumb != nullptr)
  301. m_thumb->reset(0, 0.0);
  302. m_thumb->setSource(new FileInputSource(f));
  303. m_curfile = f;
  304. m_using_audio_buffer = false;
  305. }
  306. else
  307. {
  308. m_thumb->setSource(nullptr);
  309. m_curfile = File();
  310. }
  311. repaint();
  312. }
  313. void WaveformComponent::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  314. {
  315. jassert(buf!=nullptr);
  316. m_using_audio_buffer = true;
  317. m_waveimage = Image();
  318. m_curfile = File();
  319. m_thumb->reset(buf->getNumChannels(), samplerate, len);
  320. m_thumb->addBlock(0, *buf, 0, len);
  321. }
  322. void WaveformComponent::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  323. {
  324. m_waveimage = Image();
  325. m_curfile = File();
  326. m_thumb->reset(channels, samplerate, totalllen);
  327. }
  328. void WaveformComponent::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  329. {
  330. m_thumb->addBlock(pos, buf, 0, buf.getNumSamples());
  331. }
  332. void WaveformComponent::timerCallback()
  333. {
  334. repaint();
  335. }
  336. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  337. {
  338. m_file_cached = rng;
  339. //repaint();
  340. }
  341. void WaveformComponent::setTimerEnabled(bool b)
  342. {
  343. if (b == true)
  344. startTimer(100);
  345. else
  346. stopTimer();
  347. }
  348. void WaveformComponent::setViewRange(Range<double> rng)
  349. {
  350. m_view_range = rng;
  351. m_waveimage = Image();
  352. repaint();
  353. }
  354. void WaveformComponent::mouseDown(const MouseEvent & e)
  355. {
  356. m_mousedown = true;
  357. m_lock_timesel_set = true;
  358. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  359. if (e.y < m_topmargin)
  360. {
  361. if (SeekCallback)
  362. SeekCallback(pos);
  363. m_didseek = true;
  364. }
  365. else
  366. {
  367. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  368. m_drag_time_start = pos;
  369. if (m_time_sel_drag_target == 0)
  370. {
  371. m_time_sel_start = -1.0;
  372. m_time_sel_end = -1.0;
  373. }
  374. }
  375. repaint();
  376. }
  377. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  378. {
  379. m_lock_timesel_set = false;
  380. m_mousedown = false;
  381. m_didseek = false;
  382. if (m_didchangetimeselection)
  383. {
  384. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  385. m_didchangetimeselection = false;
  386. }
  387. }
  388. void WaveformComponent::mouseDrag(const MouseEvent & e)
  389. {
  390. if (m_didseek == true)
  391. return;
  392. if (m_time_sel_drag_target == 0)
  393. {
  394. m_time_sel_start = m_drag_time_start;
  395. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  396. }
  397. if (m_time_sel_drag_target == 1)
  398. {
  399. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  400. }
  401. if (m_time_sel_drag_target == 2)
  402. {
  403. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  404. }
  405. if (m_time_sel_start > m_time_sel_end)
  406. {
  407. std::swap(m_time_sel_start, m_time_sel_end);
  408. if (m_time_sel_drag_target == 1)
  409. m_time_sel_drag_target = 2;
  410. else if (m_time_sel_drag_target == 2)
  411. m_time_sel_drag_target = 1;
  412. }
  413. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  414. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  415. if (TimeSelectionChangedCallback)
  416. {
  417. if (m_time_sel_end>m_time_sel_start)
  418. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  419. else
  420. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  421. }
  422. m_didchangetimeselection = true;
  423. repaint();
  424. }
  425. void WaveformComponent::mouseMove(const MouseEvent & e)
  426. {
  427. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  428. if (m_time_sel_drag_target == 0)
  429. setMouseCursor(MouseCursor::NormalCursor);
  430. if (m_time_sel_drag_target == 1)
  431. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  432. if (m_time_sel_drag_target == 2)
  433. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  434. }
  435. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  436. {
  437. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  438. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  439. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  440. return 1;
  441. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  442. return 2;
  443. return 0;
  444. }
  445. SpectralVisualizer::SpectralVisualizer()
  446. {
  447. m_img = Image(Image::RGB, 500, 200, true);
  448. }
  449. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  450. {
  451. double t0 = Time::getMillisecondCounterHiRes();
  452. double hz = 440.0;
  453. int numharmonics = 40;
  454. double scaler = 1.0 / numharmonics;
  455. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  456. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  457. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  458. {
  459. m_nfreqs = nfreqs;
  460. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  461. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  462. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  463. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  464. m_fft = std::make_unique<FFT>(nfreqs*2);
  465. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  466. for (int i = 0; i < nfreqs; ++i)
  467. {
  468. for (int j = 0; j < numharmonics; ++j)
  469. {
  470. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  471. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  472. }
  473. }
  474. }
  475. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  476. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  477. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  478. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  479. for (int i = 0; i < nfreqs; ++i)
  480. {
  481. m_fft->smp[i] = m_insamples[i];
  482. }
  483. m_fft->applywindow(W_HAMMING);
  484. m_fft->smp2freq();
  485. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  486. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  487. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  488. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  489. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  490. if (pars.harmonics.enabled)
  491. spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  492. else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  493. Graphics g(m_img);
  494. g.fillAll(Colours::black);
  495. g.setColour(Colours::white);
  496. for (int i = 0; i < nfreqs; ++i)
  497. {
  498. double binfreq = (samplerate / 2 / nfreqs)*i;
  499. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  500. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  501. ycor = jlimit<double>(0.0, getHeight(), ycor);
  502. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  503. }
  504. double t1 = Time::getMillisecondCounterHiRes();
  505. m_elapsed = t1 - t0;
  506. repaint();
  507. }
  508. void SpectralVisualizer::paint(Graphics & g)
  509. {
  510. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  511. g.setColour(Colours::yellow);
  512. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  513. }
  514. void SpectralChainEditor::paint(Graphics & g)
  515. {
  516. g.fillAll(Colours::black);
  517. if (m_src == nullptr)
  518. return;
  519. int box_w = getWidth() / m_order.size();
  520. int box_h = getHeight();
  521. for (int i = 0; i < m_order.size(); ++i)
  522. {
  523. //if (i!=m_cur_index)
  524. drawBox(g, i, i*box_w, 0, box_w - 30, box_h);
  525. if (i<m_order.size() - 1)
  526. 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);
  527. }
  528. if (m_drag_x>=0 && m_drag_x<getWidth() && m_cur_index>=0)
  529. drawBox(g, m_cur_index, m_drag_x, 0, box_w - 30, box_h);
  530. }
  531. void SpectralChainEditor::mouseDown(const MouseEvent & ev)
  532. {
  533. m_did_drag = false;
  534. int box_w = getWidth() / m_order.size();
  535. int box_h = getHeight();
  536. m_cur_index = ev.x / box_w;
  537. m_drag_x = -1;
  538. repaint();
  539. }
  540. void SpectralChainEditor::mouseDrag(const MouseEvent & ev)
  541. {
  542. if (m_cur_index >= 0 && m_cur_index < m_order.size())
  543. {
  544. int box_w = getWidth() / m_order.size();
  545. int box_h = getHeight();
  546. int new_index = ev.x / box_w;
  547. if (new_index >= 0 && new_index < m_order.size() && new_index != m_cur_index)
  548. {
  549. std::swap(m_order[m_cur_index], m_order[new_index]);
  550. m_cur_index = new_index;
  551. m_did_drag = true;
  552. m_src->setSpectrumProcessOrder(m_order);
  553. }
  554. m_drag_x = ev.x;
  555. repaint();
  556. }
  557. }
  558. void SpectralChainEditor::mouseUp(const MouseEvent & ev)
  559. {
  560. m_drag_x = -1;
  561. //m_cur_index = -1;
  562. repaint();
  563. }
  564. void SpectralChainEditor::drawBox(Graphics & g, int index, int x, int y, int w, int h)
  565. {
  566. String txt;
  567. if (m_order[index] == 0)
  568. txt = "Harmonics";
  569. if (m_order[index] == 1)
  570. txt = "Tonal vs Noise";
  571. if (m_order[index] == 2)
  572. txt = "Frequency shift";
  573. if (m_order[index] == 3)
  574. txt = "Pitch shift";
  575. if (m_order[index] == 4)
  576. txt = "Octaves";
  577. if (m_order[index] == 5)
  578. txt = "Spread";
  579. if (m_order[index] == 6)
  580. txt = "Filter";
  581. if (m_order[index] == 7)
  582. txt = "Compressor";
  583. if (index == m_cur_index)
  584. {
  585. g.setColour(Colours::darkgrey);
  586. //g.fillRect(i*box_w, 0, box_w - 30, box_h - 1);
  587. g.fillRect(x, y, w, h);
  588. }
  589. g.setColour(Colours::white);
  590. g.drawRect(x, y, w, h);
  591. g.drawFittedText(txt, x,y,w,h, Justification::centred, 3);
  592. }