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.

526 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. It contains the basic framework code for a JUCE plugin editor.
  5. ==============================================================================
  6. */
  7. #include "PluginProcessor.h"
  8. #include "PluginEditor.h"
  9. //==============================================================================
  10. PaulstretchpluginAudioProcessorEditor::PaulstretchpluginAudioProcessorEditor (PaulstretchpluginAudioProcessor& p)
  11. : AudioProcessorEditor (&p),
  12. m_wavecomponent(p.m_afm),
  13. processor (p)
  14. {
  15. addAndMakeVisible(&m_import_button);
  16. m_import_button.setButtonText("Import file...");
  17. attachCallback(m_import_button, [this]() { chooseFile(); });
  18. addAndMakeVisible(&m_info_label);
  19. addAndMakeVisible(&m_wavecomponent);
  20. const auto& pars = processor.getParameters();
  21. for (int i=0;i<pars.size();++i)
  22. {
  23. AudioProcessorParameterWithID* parid = dynamic_cast<AudioProcessorParameterWithID*>(pars[i]);
  24. jassert(parid);
  25. if (parid)
  26. {
  27. bool notifyonlyonrelease = false;
  28. if (parid->paramID.startsWith("fftsize"))
  29. notifyonlyonrelease = true;
  30. m_parcomps.push_back(std::make_shared<ParameterComponent>(pars[i],notifyonlyonrelease));
  31. addAndMakeVisible(m_parcomps.back().get());
  32. }
  33. }
  34. addAndMakeVisible(&m_rec_enable);
  35. m_rec_enable.setButtonText("Capture");
  36. attachCallback(m_rec_enable, [this]() { processor.setRecordingEnabled(m_rec_enable.getToggleState()); });
  37. //addAndMakeVisible(&m_specvis);
  38. setSize (1000, 30+(pars.size()/2)*25+200);
  39. m_wavecomponent.TimeSelectionChangedCallback = [this](Range<double> range, int which)
  40. {
  41. *processor.getFloatParameter(5) = range.getStart();
  42. *processor.getFloatParameter(6) = range.getEnd();
  43. };
  44. m_wavecomponent.CursorPosCallback = [this]()
  45. {
  46. return processor.getStretchSource()->getInfilePositionPercent();
  47. };
  48. m_wavecomponent.ShowFileCacheRange = true;
  49. startTimer(1, 100);
  50. startTimer(2, 1000);
  51. startTimer(3, 200);
  52. m_wavecomponent.startTimer(100);
  53. }
  54. PaulstretchpluginAudioProcessorEditor::~PaulstretchpluginAudioProcessorEditor()
  55. {
  56. }
  57. void PaulstretchpluginAudioProcessorEditor::paint (Graphics& g)
  58. {
  59. g.fillAll(Colours::darkgrey);
  60. }
  61. void PaulstretchpluginAudioProcessorEditor::resized()
  62. {
  63. m_import_button.setBounds(1, 1, 60, 24);
  64. m_import_button.changeWidthToFitText();
  65. m_rec_enable.setBounds(m_import_button.getRight()+1, 1, 10, 24);
  66. m_rec_enable.changeWidthToFitText();
  67. m_info_label.setBounds(m_rec_enable.getRight() + 1, m_rec_enable.getY(), getWidth()-m_rec_enable.getRight()-1, 24);
  68. for (int i = 0; i < m_parcomps.size(); ++i)
  69. {
  70. int gridx = i % 2;
  71. int gridy = i / 2;
  72. m_parcomps[i]->setBounds(1+gridx*(getWidth()/2), 30 + gridy * 25, getWidth()/2-2, 24);
  73. }
  74. int yoffs = m_parcomps.back()->getBottom() + 1;
  75. m_wavecomponent.setBounds(1, yoffs, getWidth()-2, getHeight()-1-yoffs);
  76. //m_specvis.setBounds(1, yoffs, getWidth() - 2, getHeight() - 1 - yoffs);
  77. }
  78. void PaulstretchpluginAudioProcessorEditor::timerCallback(int id)
  79. {
  80. if (id == 1)
  81. {
  82. for (auto& e : m_parcomps)
  83. e->updateComponent();
  84. if (processor.isRecordingEnabled() != m_rec_enable.getToggleState())
  85. m_rec_enable.setToggleState(processor.isRecordingEnabled(), dontSendNotification);
  86. if (processor.isRecordingEnabled())
  87. {
  88. m_wavecomponent.setRecordingPosition(processor.getRecordingPositionPercent());
  89. } else
  90. m_wavecomponent.setRecordingPosition(-1.0);
  91. String infotext = String(processor.getPreBufferingPercent(), 1) + " " + String(processor.getStretchSource()->m_param_change_count);
  92. m_info_label.setText(infotext, dontSendNotification);
  93. }
  94. if (id == 2)
  95. {
  96. if (processor.getAudioFile() != File() && processor.getAudioFile() != m_wavecomponent.getAudioFile())
  97. {
  98. m_wavecomponent.setAudioFile(processor.getAudioFile());
  99. }
  100. if (processor.getAudioFile()==File() && processor.isRecordingEnabled()==false && m_wavecomponent.isUsingAudioBuffer()==false)
  101. {
  102. auto bufptr = processor.getStretchSource()->getSourceAudioBuffer();
  103. if (bufptr!=nullptr)
  104. m_wavecomponent.setAudioBuffer(bufptr,
  105. processor.getSampleRate(), bufptr->getNumSamples());
  106. }
  107. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  108. }
  109. if (id == 3)
  110. {
  111. //m_specvis.setState(processor.getStretchSource()->getProcessParameters(), processor.getStretchSource()->getFFTSize() / 2,
  112. // processor.getSampleRate());
  113. }
  114. }
  115. void PaulstretchpluginAudioProcessorEditor::setAudioFile(File f)
  116. {
  117. m_wavecomponent.setAudioFile(f);
  118. }
  119. void PaulstretchpluginAudioProcessorEditor::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  120. {
  121. MessageManager::callAsync([this,buf, samplerate, len]()
  122. {
  123. m_wavecomponent.setAudioBuffer(buf, samplerate, len);
  124. });
  125. }
  126. void PaulstretchpluginAudioProcessorEditor::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  127. {
  128. m_wavecomponent.beginAddingAudioBlocks(channels, samplerate, totalllen);
  129. }
  130. void PaulstretchpluginAudioProcessorEditor::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  131. {
  132. m_wavecomponent.addAudioBlock(buf, samplerate, pos);
  133. }
  134. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  135. {
  136. #ifdef WIN32
  137. File initialloc("C:/MusicAudio/sourcesamples");
  138. #else
  139. File initialloc("/Users/teemu/AudioProjects/sourcesamples");
  140. #endif
  141. FileChooser myChooser("Please select audio file...",
  142. initialloc,
  143. "*.wav");
  144. if (myChooser.browseForFileToOpen())
  145. {
  146. processor.setAudioFile(myChooser.getResult());
  147. if (processor.getAudioFile() != File())
  148. {
  149. m_wavecomponent.setAudioFile(processor.getAudioFile());
  150. }
  151. }
  152. }
  153. WaveformComponent::WaveformComponent(AudioFormatManager* afm)
  154. {
  155. TimeSelectionChangedCallback = [](Range<double>, int) {};
  156. if (m_use_opengl == true)
  157. m_ogl.attachTo(*this);
  158. // The default priority of 2 is a bit too low in some cases, it seems...
  159. m_thumbcache->getTimeSliceThread().setPriority(3);
  160. m_thumb = std::make_unique<AudioThumbnail>(512, *afm, *m_thumbcache);
  161. m_thumb->addChangeListener(this);
  162. setOpaque(true);
  163. }
  164. WaveformComponent::~WaveformComponent()
  165. {
  166. if (m_use_opengl == true)
  167. m_ogl.detach();
  168. }
  169. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  170. {
  171. m_waveimage = Image();
  172. repaint();
  173. }
  174. void WaveformComponent::paint(Graphics & g)
  175. {
  176. //Logger::writeToLog("Waveform component paint");
  177. g.fillAll(Colours::black);
  178. g.setColour(Colours::darkgrey);
  179. g.fillRect(0, 0, getWidth(), m_topmargin);
  180. if (m_thumb == nullptr || m_thumb->getTotalLength() < 0.1)
  181. {
  182. g.setColour(Colours::aqua.darker());
  183. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  184. return;
  185. }
  186. g.setColour(Colours::lightslategrey);
  187. double thumblen = m_thumb->getTotalLength();
  188. double tick_interval = 1.0;
  189. if (thumblen > 60.0)
  190. tick_interval = 5.0;
  191. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  192. {
  193. float tickxcor = (float)jmap<double>(secs,
  194. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  195. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  196. }
  197. bool m_use_cached_image = true;
  198. if (m_use_cached_image == true)
  199. {
  200. if (m_waveimage.isValid() == false || m_waveimage.getWidth() != getWidth()
  201. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  202. {
  203. //Logger::writeToLog("updating cached waveform image");
  204. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  205. Graphics tempg(m_waveimage);
  206. tempg.fillAll(Colours::black);
  207. tempg.setColour(Colours::darkgrey);
  208. m_thumb->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  209. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  210. }
  211. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  212. }
  213. else
  214. {
  215. //g.fillAll(Colours::black);
  216. g.setColour(Colours::darkgrey);
  217. m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  218. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  219. }
  220. //g.setColour(Colours::darkgrey);
  221. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  222. // 0.0, thumblen, 1.0f);
  223. g.setColour(Colours::white.withAlpha(0.5f));
  224. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  225. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  226. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  227. if (m_file_cached.first.getLength() > 0.0 &&
  228. (bool)ShowFileCacheRange.getValue())
  229. {
  230. g.setColour(Colours::red.withAlpha(0.2f));
  231. xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  232. xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  233. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  234. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  235. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  236. if (xcorright - xcorleft>0)
  237. {
  238. g.setColour(Colours::blue.withAlpha(0.2f));
  239. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  240. }
  241. }
  242. g.setColour(Colours::white);
  243. if (CursorPosCallback)
  244. {
  245. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  246. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  247. }
  248. if (m_rec_pos >= 0.0)
  249. {
  250. g.setColour(Colours::lightpink);
  251. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  252. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  253. }
  254. g.setColour(Colours::aqua.darker());
  255. g.drawText(m_curfile.getFullPathName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  256. }
  257. void WaveformComponent::setAudioFile(File f)
  258. {
  259. if (f.existsAsFile())
  260. {
  261. m_waveimage = Image();
  262. if (m_thumb != nullptr && f == m_curfile) // reloading same file, might happen that the overview needs to be redone...
  263. m_thumbcache->removeThumb(m_thumb->getHashCode());
  264. if (m_thumb != nullptr)
  265. m_thumb->reset(0, 0.0);
  266. m_thumb->setSource(new FileInputSource(f));
  267. m_curfile = f;
  268. m_using_audio_buffer = false;
  269. }
  270. else
  271. {
  272. m_thumb->setSource(nullptr);
  273. m_curfile = File();
  274. }
  275. repaint();
  276. }
  277. void WaveformComponent::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  278. {
  279. jassert(buf!=nullptr);
  280. m_using_audio_buffer = true;
  281. m_waveimage = Image();
  282. m_curfile = File();
  283. m_thumb->reset(buf->getNumChannels(), samplerate, len);
  284. m_thumb->addBlock(0, *buf, 0, len);
  285. }
  286. void WaveformComponent::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  287. {
  288. m_waveimage = Image();
  289. m_curfile = File();
  290. m_thumb->reset(channels, samplerate, totalllen);
  291. }
  292. void WaveformComponent::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  293. {
  294. m_thumb->addBlock(pos, buf, 0, buf.getNumSamples());
  295. }
  296. void WaveformComponent::timerCallback()
  297. {
  298. repaint();
  299. }
  300. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  301. {
  302. m_file_cached = rng;
  303. //repaint();
  304. }
  305. void WaveformComponent::setTimerEnabled(bool b)
  306. {
  307. if (b == true)
  308. startTimer(100);
  309. else
  310. stopTimer();
  311. }
  312. void WaveformComponent::setViewRange(Range<double> rng)
  313. {
  314. m_view_range = rng;
  315. m_waveimage = Image();
  316. repaint();
  317. }
  318. void WaveformComponent::mouseDown(const MouseEvent & e)
  319. {
  320. m_mousedown = true;
  321. m_lock_timesel_set = true;
  322. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  323. if (e.y < m_topmargin)
  324. {
  325. if (SeekCallback)
  326. SeekCallback(pos);
  327. m_didseek = true;
  328. }
  329. else
  330. {
  331. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  332. m_drag_time_start = pos;
  333. if (m_time_sel_drag_target == 0)
  334. {
  335. m_time_sel_start = -1.0;
  336. m_time_sel_end = -1.0;
  337. }
  338. }
  339. repaint();
  340. }
  341. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  342. {
  343. m_lock_timesel_set = false;
  344. m_mousedown = false;
  345. m_didseek = false;
  346. if (m_didchangetimeselection)
  347. {
  348. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  349. m_didchangetimeselection = false;
  350. }
  351. }
  352. void WaveformComponent::mouseDrag(const MouseEvent & e)
  353. {
  354. if (m_didseek == true)
  355. return;
  356. if (m_time_sel_drag_target == 0)
  357. {
  358. m_time_sel_start = m_drag_time_start;
  359. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  360. }
  361. if (m_time_sel_drag_target == 1)
  362. {
  363. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  364. }
  365. if (m_time_sel_drag_target == 2)
  366. {
  367. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  368. }
  369. if (m_time_sel_start > m_time_sel_end)
  370. {
  371. std::swap(m_time_sel_start, m_time_sel_end);
  372. if (m_time_sel_drag_target == 1)
  373. m_time_sel_drag_target = 2;
  374. else if (m_time_sel_drag_target == 2)
  375. m_time_sel_drag_target = 1;
  376. }
  377. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  378. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  379. if (TimeSelectionChangedCallback)
  380. {
  381. if (m_time_sel_end>m_time_sel_start)
  382. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  383. else
  384. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  385. }
  386. m_didchangetimeselection = true;
  387. repaint();
  388. }
  389. void WaveformComponent::mouseMove(const MouseEvent & e)
  390. {
  391. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  392. if (m_time_sel_drag_target == 0)
  393. setMouseCursor(MouseCursor::NormalCursor);
  394. if (m_time_sel_drag_target == 1)
  395. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  396. if (m_time_sel_drag_target == 2)
  397. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  398. }
  399. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  400. {
  401. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  402. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  403. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  404. return 1;
  405. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  406. return 2;
  407. return 0;
  408. }
  409. SpectralVisualizer::SpectralVisualizer()
  410. {
  411. m_img = Image(Image::RGB, 500, 200, true);
  412. }
  413. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  414. {
  415. double t0 = Time::getMillisecondCounterHiRes();
  416. double hz = 440.0;
  417. int numharmonics = 40;
  418. double scaler = 1.0 / numharmonics;
  419. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  420. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  421. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  422. {
  423. m_nfreqs = nfreqs;
  424. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  425. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  426. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  427. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  428. m_fft = std::make_unique<FFT>(nfreqs*2);
  429. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  430. for (int i = 0; i < nfreqs; ++i)
  431. {
  432. for (int j = 0; j < numharmonics; ++j)
  433. {
  434. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  435. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  436. }
  437. }
  438. }
  439. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  440. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  441. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  442. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  443. for (int i = 0; i < nfreqs; ++i)
  444. {
  445. m_fft->smp[i] = m_insamples[i];
  446. }
  447. m_fft->applywindow(W_HAMMING);
  448. m_fft->smp2freq();
  449. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  450. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  451. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  452. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  453. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  454. if (pars.harmonics.enabled)
  455. spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  456. else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  457. Graphics g(m_img);
  458. g.fillAll(Colours::black);
  459. g.setColour(Colours::white);
  460. for (int i = 0; i < nfreqs; ++i)
  461. {
  462. double binfreq = (samplerate / 2 / nfreqs)*i;
  463. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  464. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  465. ycor = jlimit<double>(0.0, getHeight(), ycor);
  466. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  467. }
  468. double t1 = Time::getMillisecondCounterHiRes();
  469. m_elapsed = t1 - t0;
  470. repaint();
  471. }
  472. void SpectralVisualizer::paint(Graphics & g)
  473. {
  474. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  475. g.setColour(Colours::yellow);
  476. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  477. }