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.

350 lines
11KB

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