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.

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