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.

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