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.

434 lines
13KB

  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. 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. WaveformComponent::WaveformComponent(AudioFormatManager* afm)
  137. {
  138. TimeSelectionChangedCallback = [](Range<double>, int) {};
  139. if (m_use_opengl == true)
  140. m_ogl.attachTo(*this);
  141. // The default priority of 2 is a bit too low in some cases, it seems...
  142. m_thumbcache->getTimeSliceThread().setPriority(3);
  143. m_thumb = std::make_unique<AudioThumbnail>(512, *afm, *m_thumbcache);
  144. m_thumb->addChangeListener(this);
  145. setOpaque(true);
  146. }
  147. WaveformComponent::~WaveformComponent()
  148. {
  149. if (m_use_opengl == true)
  150. m_ogl.detach();
  151. }
  152. void WaveformComponent::changeListenerCallback(ChangeBroadcaster * /*cb*/)
  153. {
  154. m_waveimage = Image();
  155. repaint();
  156. }
  157. void WaveformComponent::paint(Graphics & g)
  158. {
  159. //Logger::writeToLog("Waveform component paint");
  160. g.fillAll(Colours::black);
  161. g.setColour(Colours::darkgrey);
  162. g.fillRect(0, 0, getWidth(), m_topmargin);
  163. if (m_thumb == nullptr || m_thumb->getTotalLength() < 0.1)
  164. {
  165. g.setColour(Colours::aqua.darker());
  166. g.drawText("No file loaded", 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  167. return;
  168. }
  169. g.setColour(Colours::lightslategrey);
  170. double thumblen = m_thumb->getTotalLength();
  171. double tick_interval = 1.0;
  172. if (thumblen > 60.0)
  173. tick_interval = 5.0;
  174. for (double secs = 0.0; secs < thumblen; secs += tick_interval)
  175. {
  176. float tickxcor = (float)jmap<double>(secs,
  177. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 0.0f, (float)getWidth());
  178. g.drawLine(tickxcor, 0.0, tickxcor, (float)m_topmargin, 1.0f);
  179. }
  180. bool m_use_cached_image = true;
  181. if (m_use_cached_image == true)
  182. {
  183. if (m_waveimage.isValid() == false || m_waveimage.getWidth() != getWidth()
  184. || m_waveimage.getHeight() != getHeight() - m_topmargin)
  185. {
  186. //Logger::writeToLog("updating cached waveform image");
  187. m_waveimage = Image(Image::ARGB, getWidth(), getHeight() - m_topmargin, true);
  188. Graphics tempg(m_waveimage);
  189. tempg.fillAll(Colours::black);
  190. tempg.setColour(Colours::darkgrey);
  191. m_thumb->drawChannels(tempg, { 0,0,getWidth(),getHeight() - m_topmargin },
  192. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  193. }
  194. g.drawImage(m_waveimage, 0, m_topmargin, getWidth(), getHeight() - m_topmargin, 0, 0, getWidth(), getHeight() - m_topmargin);
  195. }
  196. else
  197. {
  198. //g.fillAll(Colours::black);
  199. g.setColour(Colours::darkgrey);
  200. m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight() - m_topmargin },
  201. thumblen*m_view_range.getStart(), thumblen*m_view_range.getEnd(), 1.0f);
  202. }
  203. //g.setColour(Colours::darkgrey);
  204. //m_thumb->drawChannels(g, { 0,m_topmargin,getWidth(),getHeight()-m_topmargin },
  205. // 0.0, thumblen, 1.0f);
  206. g.setColour(Colours::white.withAlpha(0.5f));
  207. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  208. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  209. g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin);
  210. if (m_file_cached.first.getLength() > 0.0 &&
  211. (bool)ShowFileCacheRange.getValue())
  212. {
  213. g.setColour(Colours::red.withAlpha(0.2f));
  214. xcorleft = (int)jmap<double>(m_file_cached.first.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  215. xcorright = (int)jmap<double>(m_file_cached.first.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  216. g.fillRect(xcorleft, 0, xcorright - xcorleft, m_topmargin / 2);
  217. xcorleft = (int)jmap<double>(m_file_cached.second.getStart(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  218. xcorright = (int)jmap<double>(m_file_cached.second.getEnd(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  219. if (xcorright - xcorleft>0)
  220. {
  221. g.setColour(Colours::blue.withAlpha(0.2f));
  222. g.fillRect(xcorleft, m_topmargin / 2, xcorright - xcorleft, m_topmargin / 2);
  223. }
  224. }
  225. g.setColour(Colours::white);
  226. if (CursorPosCallback)
  227. {
  228. double pos = jmap<double>(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  229. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  230. }
  231. if (m_rec_pos >= 0.0)
  232. {
  233. g.setColour(Colours::lightpink);
  234. double pos = jmap<double>(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  235. g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin);
  236. }
  237. g.setColour(Colours::aqua.darker());
  238. g.drawText(m_curfile.getFullPathName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft);
  239. }
  240. void WaveformComponent::setAudioFile(File f)
  241. {
  242. if (f.existsAsFile())
  243. {
  244. m_waveimage = Image();
  245. if (m_thumb != nullptr && f == m_curfile) // reloading same file, might happen that the overview needs to be redone...
  246. m_thumbcache->removeThumb(m_thumb->getHashCode());
  247. if (m_thumb != nullptr)
  248. m_thumb->reset(0, 0.0);
  249. m_thumb->setSource(new FileInputSource(f));
  250. m_curfile = f;
  251. }
  252. else
  253. {
  254. m_thumb->setSource(nullptr);
  255. m_curfile = File();
  256. }
  257. repaint();
  258. }
  259. void WaveformComponent::setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len)
  260. {
  261. m_waveimage = Image();
  262. m_curfile = File();
  263. m_thumb->reset(buf->getNumChannels(), samplerate, len);
  264. m_thumb->addBlock(0, *buf, 0, len);
  265. }
  266. void WaveformComponent::beginAddingAudioBlocks(int channels, int samplerate, int totalllen)
  267. {
  268. m_waveimage = Image();
  269. m_curfile = File();
  270. m_thumb->reset(channels, samplerate, totalllen);
  271. }
  272. void WaveformComponent::addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos)
  273. {
  274. m_thumb->addBlock(pos, buf, 0, buf.getNumSamples());
  275. }
  276. void WaveformComponent::timerCallback()
  277. {
  278. repaint();
  279. }
  280. void WaveformComponent::setFileCachedRange(std::pair<Range<double>, Range<double>> rng)
  281. {
  282. m_file_cached = rng;
  283. //repaint();
  284. }
  285. void WaveformComponent::setTimerEnabled(bool b)
  286. {
  287. if (b == true)
  288. startTimer(100);
  289. else
  290. stopTimer();
  291. }
  292. void WaveformComponent::setViewRange(Range<double> rng)
  293. {
  294. m_view_range = rng;
  295. m_waveimage = Image();
  296. repaint();
  297. }
  298. void WaveformComponent::mouseDown(const MouseEvent & e)
  299. {
  300. m_mousedown = true;
  301. m_lock_timesel_set = true;
  302. double pos = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  303. if (e.y < m_topmargin)
  304. {
  305. if (SeekCallback)
  306. SeekCallback(pos);
  307. m_didseek = true;
  308. }
  309. else
  310. {
  311. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  312. m_drag_time_start = pos;
  313. if (m_time_sel_drag_target == 0)
  314. {
  315. m_time_sel_start = -1.0;
  316. m_time_sel_end = -1.0;
  317. }
  318. }
  319. repaint();
  320. }
  321. void WaveformComponent::mouseUp(const MouseEvent & /*e*/)
  322. {
  323. m_lock_timesel_set = false;
  324. m_mousedown = false;
  325. m_didseek = false;
  326. if (m_didchangetimeselection)
  327. {
  328. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 1);
  329. m_didchangetimeselection = false;
  330. }
  331. }
  332. void WaveformComponent::mouseDrag(const MouseEvent & e)
  333. {
  334. if (m_didseek == true)
  335. return;
  336. if (m_time_sel_drag_target == 0)
  337. {
  338. m_time_sel_start = m_drag_time_start;
  339. m_time_sel_end = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  340. }
  341. if (m_time_sel_drag_target == 1)
  342. {
  343. m_time_sel_start = jmap<double>(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd());
  344. }
  345. if (m_time_sel_drag_target == 2)
  346. {
  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_start > m_time_sel_end)
  350. {
  351. std::swap(m_time_sel_start, m_time_sel_end);
  352. if (m_time_sel_drag_target == 1)
  353. m_time_sel_drag_target = 2;
  354. else if (m_time_sel_drag_target == 2)
  355. m_time_sel_drag_target = 1;
  356. }
  357. m_time_sel_start = jlimit(0.0, 1.0, m_time_sel_start);
  358. m_time_sel_end = jlimit(0.0, 1.0, m_time_sel_end);
  359. if (TimeSelectionChangedCallback)
  360. {
  361. if (m_time_sel_end>m_time_sel_start)
  362. TimeSelectionChangedCallback(Range<double>(m_time_sel_start, m_time_sel_end), 0);
  363. else
  364. TimeSelectionChangedCallback(Range<double>(0.0, 1.0), 0);
  365. }
  366. m_didchangetimeselection = true;
  367. repaint();
  368. }
  369. void WaveformComponent::mouseMove(const MouseEvent & e)
  370. {
  371. m_time_sel_drag_target = getTimeSelectionEdge(e.x, e.y);
  372. if (m_time_sel_drag_target == 0)
  373. setMouseCursor(MouseCursor::NormalCursor);
  374. if (m_time_sel_drag_target == 1)
  375. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  376. if (m_time_sel_drag_target == 2)
  377. setMouseCursor(MouseCursor::LeftRightResizeCursor);
  378. }
  379. int WaveformComponent::getTimeSelectionEdge(int x, int y)
  380. {
  381. int xcorleft = (int)jmap<double>(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  382. int xcorright = (int)jmap<double>(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth());
  383. if (juce::Rectangle<int>(xcorleft - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  384. return 1;
  385. if (juce::Rectangle<int>(xcorright - 5, m_topmargin, 10, getHeight() - m_topmargin).contains(x, y))
  386. return 2;
  387. return 0;
  388. }