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.

224 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. It contains the basic framework code for a JUCE plugin editor.
  5. ==============================================================================
  6. */
  7. #pragma once
  8. #include "../JuceLibraryCode/JuceHeader.h"
  9. #include "PluginProcessor.h"
  10. #include <memory>
  11. #include <vector>
  12. inline void attachCallback(Button& button, std::function<void()> callback)
  13. {
  14. struct ButtonCallback : public Button::Listener,
  15. private ComponentListener
  16. {
  17. ButtonCallback(Button& b, std::function<void()> f) : target(b), fn(f)
  18. {
  19. target.addListener(this);
  20. target.addComponentListener(this);
  21. }
  22. ~ButtonCallback()
  23. {
  24. target.removeListener(this);
  25. }
  26. void componentBeingDeleted(Component&) override { delete this; }
  27. void buttonClicked(Button*) override { fn(); }
  28. Button& target;
  29. std::function<void()> fn;
  30. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ButtonCallback)
  31. };
  32. new ButtonCallback(button, callback);
  33. }
  34. class ParameterComponent : public Component,
  35. public Slider::Listener, public Button::Listener
  36. {
  37. public:
  38. ParameterComponent(AudioProcessorParameter* par, bool notifyOnlyOnRelease) : m_par(par)
  39. {
  40. addAndMakeVisible(&m_label);
  41. m_label.setText(par->getName(50),dontSendNotification);
  42. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(par);
  43. if (floatpar)
  44. {
  45. m_slider = std::make_unique<Slider>();
  46. m_notify_only_on_release = notifyOnlyOnRelease;
  47. m_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  48. m_slider->setValue(*floatpar, dontSendNotification);
  49. m_slider->addListener(this);
  50. addAndMakeVisible(m_slider.get());
  51. }
  52. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  53. if (choicepar)
  54. {
  55. }
  56. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  57. if (boolpar)
  58. {
  59. m_togglebut = std::make_unique<ToggleButton>();
  60. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  61. m_togglebut->addListener(this);
  62. addAndMakeVisible(m_togglebut.get());
  63. }
  64. }
  65. void resized() override
  66. {
  67. m_label.setBounds(0, 0, 200, 24);
  68. if (m_slider)
  69. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  70. if (m_togglebut)
  71. m_togglebut->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  72. }
  73. void sliderValueChanged(Slider* slid) override
  74. {
  75. if (m_notify_only_on_release == true)
  76. return;
  77. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  78. *floatpar = slid->getValue();
  79. }
  80. void sliderDragStarted(Slider* slid) override
  81. {
  82. m_dragging = true;
  83. }
  84. void sliderDragEnded(Slider* slid) override
  85. {
  86. m_dragging = false;
  87. if (m_notify_only_on_release == false)
  88. return;
  89. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  90. *floatpar = slid->getValue();
  91. }
  92. void buttonClicked(Button* but) override
  93. {
  94. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  95. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  96. {
  97. *boolpar = m_togglebut->getToggleState();
  98. }
  99. }
  100. void updateComponent()
  101. {
  102. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  103. if (m_slider != nullptr && m_dragging == false && (float)m_slider->getValue() != *floatpar)
  104. {
  105. m_slider->setValue(*floatpar, dontSendNotification);
  106. }
  107. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  108. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  109. {
  110. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  111. }
  112. }
  113. private:
  114. Label m_label;
  115. AudioProcessorParameter* m_par = nullptr;
  116. std::unique_ptr<Slider> m_slider;
  117. std::unique_ptr<ComboBox> m_combobox;
  118. std::unique_ptr<ToggleButton> m_togglebut;
  119. bool m_notify_only_on_release = false;
  120. bool m_dragging = false;
  121. };
  122. class WaveformComponent : public Component, public ChangeListener, public Timer
  123. {
  124. public:
  125. WaveformComponent(AudioFormatManager* afm);
  126. ~WaveformComponent();
  127. void changeListenerCallback(ChangeBroadcaster* cb) override;
  128. void paint(Graphics& g) override;
  129. void setAudioFile(File f);
  130. const File& getAudioFile() const { return m_curfile; }
  131. void setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len);
  132. void beginAddingAudioBlocks(int channels, int samplerate, int totalllen);
  133. void addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos);
  134. void timerCallback() override;
  135. std::function<double()> CursorPosCallback;
  136. std::function<void(double)> SeekCallback;
  137. std::function<void(Range<double>, int)> TimeSelectionChangedCallback;
  138. void mouseDown(const MouseEvent& e) override;
  139. void mouseUp(const MouseEvent& e) override;
  140. void mouseDrag(const MouseEvent& e) override;
  141. void mouseMove(const MouseEvent& e) override;
  142. Range<double> getTimeSelection()
  143. {
  144. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  145. return { m_time_sel_start, m_time_sel_end };
  146. return { 0.0, 1.0 };
  147. }
  148. void setTimeSelection(Range<double> rng)
  149. {
  150. if (m_lock_timesel_set == true)
  151. return;
  152. if (rng.isEmpty())
  153. rng = { -1.0,1.0 };
  154. m_time_sel_start = rng.getStart();
  155. m_time_sel_end = rng.getEnd();
  156. repaint();
  157. }
  158. void setFileCachedRange(std::pair<Range<double>, Range<double>> rng);
  159. void setTimerEnabled(bool b);
  160. void setViewRange(Range<double> rng);
  161. Value ShowFileCacheRange;
  162. void setRecordingPosition(double pos) { m_rec_pos = pos; }
  163. private:
  164. AudioThumbnailCache m_thumbcache;
  165. std::unique_ptr<AudioThumbnail> m_thumb;
  166. Range<double> m_view_range{ 0.0,1.0 };
  167. int m_time_sel_drag_target = 0;
  168. double m_time_sel_start = -1.0;
  169. double m_time_sel_end = -1.0;
  170. double m_drag_time_start = 0.0;
  171. bool m_mousedown = false;
  172. bool m_didseek = false;
  173. bool m_didchangetimeselection = false;
  174. int m_topmargin = 0;
  175. int getTimeSelectionEdge(int x, int y);
  176. std::pair<Range<double>, Range<double>> m_file_cached;
  177. File m_curfile;
  178. Image m_waveimage;
  179. OpenGLContext m_ogl;
  180. bool m_use_opengl = false;
  181. double m_rec_pos = 0.0;
  182. bool m_lock_timesel_set = false;
  183. };
  184. class PaulstretchpluginAudioProcessorEditor : public AudioProcessorEditor,
  185. public MultiTimer
  186. {
  187. public:
  188. PaulstretchpluginAudioProcessorEditor (PaulstretchpluginAudioProcessor&);
  189. ~PaulstretchpluginAudioProcessorEditor();
  190. void paint (Graphics&) override;
  191. void resized() override;
  192. void timerCallback(int id) override;
  193. void setAudioFile(File f);
  194. void setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len);
  195. void beginAddingAudioBlocks(int channels, int samplerate, int totalllen);
  196. void addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos);
  197. WaveformComponent m_wavecomponent;
  198. private:
  199. PaulstretchpluginAudioProcessor& processor;
  200. std::vector<std::shared_ptr<ParameterComponent>> m_parcomps;
  201. ToggleButton m_rec_enable;
  202. TextButton m_import_button;
  203. Label m_info_label;
  204. void chooseFile();
  205. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PaulstretchpluginAudioProcessorEditor)
  206. };