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.

374 lines
12KB

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