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.

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