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.

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