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.

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