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.

494 lines
15KB

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