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.

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