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.

447 lines
14KB

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