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.

516 lines
16KB

  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. m_wavecomponent.setTimeSelection(processor.getTimeSelection());
  101. }
  102. if (id == 3)
  103. {
  104. //m_specvis.setState(processor.getStretchSource()->getProcessParameters(), processor.getStretchSource()->getFFTSize() / 2,
  105. // processor.getSampleRate());
  106. }
  107. }
  108. void PaulstretchpluginAudioProcessorEditor::setAudioFile(File f)
  109. {
  110. m_wavecomponent.setAudioFile(f);
  111. }
  112. void PaulstretchpluginAudioProcessorEditor::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  113. {
  114. MessageManager::callAsync([this,buf, samplerate, len]()
  115. {
  116. m_wavecomponent.setAudioBuffer(buf, samplerate, len);
  117. });
  118. }
  119. void PaulstretchpluginAudioProcessorEditor::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  120. {
  121. m_wavecomponent.beginAddingAudioBlocks(channels, samplerate, totalllen);
  122. }
  123. void PaulstretchpluginAudioProcessorEditor::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  124. {
  125. m_wavecomponent.addAudioBlock(buf, samplerate, pos);
  126. }
  127. void PaulstretchpluginAudioProcessorEditor::chooseFile()
  128. {
  129. #ifdef WIN32
  130. File initialloc("C:/MusicAudio/sourcesamples");
  131. #else
  132. File initialloc("/Users/teemu/AudioProjects/sourcesamples");
  133. #endif
  134. FileChooser myChooser("Please select audio file...",
  135. initialloc,
  136. "*.wav");
  137. if (myChooser.browseForFileToOpen())
  138. {
  139. processor.setAudioFile(myChooser.getResult());
  140. if (processor.getAudioFile() != File())
  141. {
  142. m_wavecomponent.setAudioFile(processor.getAudioFile());
  143. }
  144. }
  145. }
  146. WaveformComponent::WaveformComponent(AudioFormatManager* afm)
  147. {
  148. TimeSelectionChangedCallback = [](Range<double>, int) {};
  149. if (m_use_opengl == true)
  150. m_ogl.attachTo(*this);
  151. // The default priority of 2 is a bit too low in some cases, it seems...
  152. m_thumbcache->getTimeSliceThread().setPriority(3);
  153. m_thumb = std::make_unique<AudioThumbnail>(512, *afm, *m_thumbcache);
  154. m_thumb->addChangeListener(this);
  155. setOpaque(true);
  156. }
  157. WaveformComponent::~WaveformComponent()
  158. {
  159. if (m_use_opengl == true)
  160. m_ogl.detach();
  161. }
  162. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  163. {
  164. m_waveimage = Image();
  165. repaint();
  166. }
  167. void WaveformComponent::paint(Graphics & g)
  168. {
  169. //Logger::writeToLog("Waveform component paint");
  170. g.fillAll(Colours::black);
  171. g.setColour(Colours::darkgrey);
  172. g.fillRect(0, 0, getWidth(), m_topmargin);
  173. if (m_thumb == nullptr || m_thumb->getTotalLength() < 0.1)
  174. {
  175. g.setColour(Colours::aqua.darker());
  176. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  177. return;
  178. }
  179. g.setColour(Colours::lightslategrey);
  180. double thumblen = m_thumb->getTotalLength();
  181. double tick_interval = 1.0;
  182. if (thumblen > 60.0)
  183. tick_interval = 5.0;
  184. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  185. {
  186. float tickxcor = (float)jmap<double>(secs,
  187. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  188. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  189. }
  190. bool m_use_cached_image = true;
  191. if (m_use_cached_image == true)
  192. {
  193. if (m_waveimage.isValid() == false || m_waveimage.getWidth() != getWidth()
  194. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  195. {
  196. //Logger::writeToLog("updating cached waveform image");
  197. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  198. Graphics tempg(m_waveimage);
  199. tempg.fillAll(Colours::black);
  200. tempg.setColour(Colours::darkgrey);
  201. m_thumb->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  202. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  203. }
  204. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  205. }
  206. else
  207. {
  208. //g.fillAll(Colours::black);
  209. g.setColour(Colours::darkgrey);
  210. m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  211. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  212. }
  213. //g.setColour(Colours::darkgrey);
  214. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  215. // 0.0, thumblen, 1.0f);
  216. g.setColour(Colours::white.withAlpha(0.5f));
  217. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  218. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  219. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  220. if (m_file_cached.first.getLength() > 0.0 &&
  221. (bool)ShowFileCacheRange.getValue())
  222. {
  223. g.setColour(Colours::red.withAlpha(0.2f));
  224. xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  225. xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  226. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  227. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  228. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  229. if (xcorright - xcorleft>0)
  230. {
  231. g.setColour(Colours::blue.withAlpha(0.2f));
  232. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  233. }
  234. }
  235. g.setColour(Colours::white);
  236. if (CursorPosCallback)
  237. {
  238. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  239. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  240. }
  241. if (m_rec_pos >= 0.0)
  242. {
  243. g.setColour(Colours::lightpink);
  244. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  245. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  246. }
  247. g.setColour(Colours::aqua.darker());
  248. g.drawText(m_curfile.getFullPathName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  249. }
  250. void WaveformComponent::setAudioFile(File f)
  251. {
  252. if (f.existsAsFile())
  253. {
  254. m_waveimage = Image();
  255. if (m_thumb != nullptr && f == m_curfile) // reloading same file, might happen that the overview needs to be redone...
  256. m_thumbcache->removeThumb(m_thumb->getHashCode());
  257. if (m_thumb != nullptr)
  258. m_thumb->reset(0, 0.0);
  259. m_thumb->setSource(new FileInputSource(f));
  260. m_curfile = f;
  261. }
  262. else
  263. {
  264. m_thumb->setSource(nullptr);
  265. m_curfile = File();
  266. }
  267. repaint();
  268. }
  269. void WaveformComponent::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  270. {
  271. m_waveimage = Image();
  272. m_curfile = File();
  273. m_thumb->reset(buf->getNumChannels(), samplerate, len);
  274. m_thumb->addBlock(0, *buf, 0, len);
  275. }
  276. void WaveformComponent::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  277. {
  278. m_waveimage = Image();
  279. m_curfile = File();
  280. m_thumb->reset(channels, samplerate, totalllen);
  281. }
  282. void WaveformComponent::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  283. {
  284. m_thumb->addBlock(pos, buf, 0, buf.getNumSamples());
  285. }
  286. void WaveformComponent::timerCallback()
  287. {
  288. repaint();
  289. }
  290. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  291. {
  292. m_file_cached = rng;
  293. //repaint();
  294. }
  295. void WaveformComponent::setTimerEnabled(bool b)
  296. {
  297. if (b == true)
  298. startTimer(100);
  299. else
  300. stopTimer();
  301. }
  302. void WaveformComponent::setViewRange(Range<double> rng)
  303. {
  304. m_view_range = rng;
  305. m_waveimage = Image();
  306. repaint();
  307. }
  308. void WaveformComponent::mouseDown(const MouseEvent & e)
  309. {
  310. m_mousedown = true;
  311. m_lock_timesel_set = true;
  312. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  313. if (e.y < m_topmargin)
  314. {
  315. if (SeekCallback)
  316. SeekCallback(pos);
  317. m_didseek = true;
  318. }
  319. else
  320. {
  321. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  322. m_drag_time_start = pos;
  323. if (m_time_sel_drag_target == 0)
  324. {
  325. m_time_sel_start = -1.0;
  326. m_time_sel_end = -1.0;
  327. }
  328. }
  329. repaint();
  330. }
  331. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  332. {
  333. m_lock_timesel_set = false;
  334. m_mousedown = false;
  335. m_didseek = false;
  336. if (m_didchangetimeselection)
  337. {
  338. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  339. m_didchangetimeselection = false;
  340. }
  341. }
  342. void WaveformComponent::mouseDrag(const MouseEvent & e)
  343. {
  344. if (m_didseek == true)
  345. return;
  346. if (m_time_sel_drag_target == 0)
  347. {
  348. m_time_sel_start = m_drag_time_start;
  349. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  350. }
  351. if (m_time_sel_drag_target == 1)
  352. {
  353. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  354. }
  355. if (m_time_sel_drag_target == 2)
  356. {
  357. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  358. }
  359. if (m_time_sel_start > m_time_sel_end)
  360. {
  361. std::swap(m_time_sel_start, m_time_sel_end);
  362. if (m_time_sel_drag_target == 1)
  363. m_time_sel_drag_target = 2;
  364. else if (m_time_sel_drag_target == 2)
  365. m_time_sel_drag_target = 1;
  366. }
  367. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  368. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  369. if (TimeSelectionChangedCallback)
  370. {
  371. if (m_time_sel_end>m_time_sel_start)
  372. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  373. else
  374. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  375. }
  376. m_didchangetimeselection = true;
  377. repaint();
  378. }
  379. void WaveformComponent::mouseMove(const MouseEvent & e)
  380. {
  381. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  382. if (m_time_sel_drag_target == 0)
  383. setMouseCursor(MouseCursor::NormalCursor);
  384. if (m_time_sel_drag_target == 1)
  385. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  386. if (m_time_sel_drag_target == 2)
  387. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  388. }
  389. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  390. {
  391. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  392. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  393. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  394. return 1;
  395. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  396. return 2;
  397. return 0;
  398. }
  399. SpectralVisualizer::SpectralVisualizer()
  400. {
  401. m_img = Image(Image::RGB, 500, 200, true);
  402. }
  403. void SpectralVisualizer::setState(const ProcessParameters & pars, int nfreqs, double samplerate)
  404. {
  405. double t0 = Time::getMillisecondCounterHiRes();
  406. double hz = 440.0;
  407. int numharmonics = 40;
  408. double scaler = 1.0 / numharmonics;
  409. if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight())
  410. m_img = Image(Image::RGB, getWidth(), getHeight(), true);
  411. if (m_nfreqs == 0 || nfreqs != m_nfreqs)
  412. {
  413. m_nfreqs = nfreqs;
  414. m_insamples = std::vector<REALTYPE>(nfreqs * 2);
  415. m_freqs1 = std::vector<REALTYPE>(nfreqs);
  416. m_freqs2 = std::vector<REALTYPE>(nfreqs);
  417. m_freqs3 = std::vector<REALTYPE>(nfreqs);
  418. m_fft = std::make_unique<FFT>(nfreqs*2);
  419. std::fill(m_insamples.begin(), m_insamples.end(), 0.0f);
  420. for (int i = 0; i < nfreqs; ++i)
  421. {
  422. for (int j = 0; j < numharmonics; ++j)
  423. {
  424. double oscgain = 1.0 - (1.0 / numharmonics)*j;
  425. m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j));
  426. }
  427. }
  428. }
  429. //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f);
  430. //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f);
  431. //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f);
  432. //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f);
  433. for (int i = 0; i < nfreqs; ++i)
  434. {
  435. m_fft->smp[i] = m_insamples[i];
  436. }
  437. m_fft->applywindow(W_HAMMING);
  438. m_fft->smp2freq();
  439. double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f);
  440. spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio);
  441. spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data());
  442. spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data());
  443. spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth);
  444. if (pars.harmonics.enabled)
  445. spectrum_do_harmonics(pars, m_freqs3, nfreqs, samplerate, m_freqs1.data(), m_freqs2.data());
  446. else spectrum_copy(nfreqs, m_freqs1.data(), m_freqs2.data());
  447. Graphics g(m_img);
  448. g.fillAll(Colours::black);
  449. g.setColour(Colours::white);
  450. for (int i = 0; i < nfreqs; ++i)
  451. {
  452. double binfreq = (samplerate / 2 / nfreqs)*i;
  453. double xcor = jmap<double>(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth());
  454. double ycor = getHeight()- jmap<double>(m_freqs2[i], 0.0, nfreqs/128, 0.0, getHeight());
  455. ycor = jlimit<double>(0.0, getHeight(), ycor);
  456. g.drawLine(xcor, getHeight(), xcor, ycor, 1.0);
  457. }
  458. double t1 = Time::getMillisecondCounterHiRes();
  459. m_elapsed = t1 - t0;
  460. repaint();
  461. }
  462. void SpectralVisualizer::paint(Graphics & g)
  463. {
  464. g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight());
  465. g.setColour(Colours::yellow);
  466. g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft);
  467. }