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.

207 lines
6.4KB

  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) : 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_slider->setRange(floatpar->range.start, floatpar->range.end, floatpar->range.interval);
  47. m_slider->setValue(*floatpar, dontSendNotification);
  48. m_slider->addListener(this);
  49. addAndMakeVisible(m_slider.get());
  50. }
  51. AudioParameterChoice* choicepar = dynamic_cast<AudioParameterChoice*>(par);
  52. if (choicepar)
  53. {
  54. }
  55. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(par);
  56. if (boolpar)
  57. {
  58. m_togglebut = std::make_unique<ToggleButton>();
  59. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  60. m_togglebut->addListener(this);
  61. addAndMakeVisible(m_togglebut.get());
  62. }
  63. }
  64. void resized() override
  65. {
  66. m_label.setBounds(0, 0, 200, 24);
  67. if (m_slider)
  68. m_slider->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  69. if (m_togglebut)
  70. m_togglebut->setBounds(m_label.getRight() + 1, 0, getWidth() - 2 - m_label.getWidth(), 24);
  71. }
  72. void sliderValueChanged(Slider* slid) override
  73. {
  74. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  75. *floatpar = slid->getValue();
  76. }
  77. void buttonClicked(Button* but) override
  78. {
  79. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  80. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  81. {
  82. *boolpar = m_togglebut->getToggleState();
  83. }
  84. }
  85. void updateComponent()
  86. {
  87. AudioParameterFloat* floatpar = dynamic_cast<AudioParameterFloat*>(m_par);
  88. if (m_slider != nullptr && (float)m_slider->getValue() != *floatpar)
  89. {
  90. m_slider->setValue(*floatpar, dontSendNotification);
  91. }
  92. AudioParameterBool* boolpar = dynamic_cast<AudioParameterBool*>(m_par);
  93. if (m_togglebut != nullptr && m_togglebut->getToggleState() != *boolpar)
  94. {
  95. m_togglebut->setToggleState(*boolpar, dontSendNotification);
  96. }
  97. }
  98. private:
  99. Label m_label;
  100. AudioProcessorParameter* m_par = nullptr;
  101. std::unique_ptr<Slider> m_slider;
  102. std::unique_ptr<ComboBox> m_combobox;
  103. std::unique_ptr<ToggleButton> m_togglebut;
  104. };
  105. class WaveformComponent : public Component, public ChangeListener, public Timer
  106. {
  107. public:
  108. WaveformComponent(AudioFormatManager* afm);
  109. ~WaveformComponent();
  110. void changeListenerCallback(ChangeBroadcaster* cb) override;
  111. void paint(Graphics& g) override;
  112. void setAudioFile(File f);
  113. const File& getAudioFile() const { return m_curfile; }
  114. void setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len);
  115. void beginAddingAudioBlocks(int channels, int samplerate, int totalllen);
  116. void addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos);
  117. void timerCallback() override;
  118. std::function<double()> CursorPosCallback;
  119. std::function<void(double)> SeekCallback;
  120. std::function<void(Range<double>, int)> TimeSelectionChangedCallback;
  121. void mouseDown(const MouseEvent& e) override;
  122. void mouseUp(const MouseEvent& e) override;
  123. void mouseDrag(const MouseEvent& e) override;
  124. void mouseMove(const MouseEvent& e) override;
  125. Range<double> getTimeSelection()
  126. {
  127. if (m_time_sel_start >= 0.0 && m_time_sel_end>m_time_sel_start + 0.001)
  128. return { m_time_sel_start, m_time_sel_end };
  129. return { 0.0, 1.0 };
  130. }
  131. void setTimeSelection(Range<double> rng)
  132. {
  133. if (m_lock_timesel_set == true)
  134. return;
  135. if (rng.isEmpty())
  136. rng = { -1.0,1.0 };
  137. m_time_sel_start = rng.getStart();
  138. m_time_sel_end = rng.getEnd();
  139. repaint();
  140. }
  141. void setFileCachedRange(std::pair<Range<double>, Range<double>> rng);
  142. void setTimerEnabled(bool b);
  143. void setViewRange(Range<double> rng);
  144. Value ShowFileCacheRange;
  145. void setRecordingPosition(double pos) { m_rec_pos = pos; }
  146. private:
  147. AudioThumbnailCache m_thumbcache;
  148. std::unique_ptr<AudioThumbnail> m_thumb;
  149. Range<double> m_view_range{ 0.0,1.0 };
  150. int m_time_sel_drag_target = 0;
  151. double m_time_sel_start = -1.0;
  152. double m_time_sel_end = -1.0;
  153. double m_drag_time_start = 0.0;
  154. bool m_mousedown = false;
  155. bool m_didseek = false;
  156. bool m_didchangetimeselection = false;
  157. int m_topmargin = 0;
  158. int getTimeSelectionEdge(int x, int y);
  159. std::pair<Range<double>, Range<double>> m_file_cached;
  160. File m_curfile;
  161. Image m_waveimage;
  162. OpenGLContext m_ogl;
  163. bool m_use_opengl = false;
  164. double m_rec_pos = 0.0;
  165. bool m_lock_timesel_set = false;
  166. };
  167. class PaulstretchpluginAudioProcessorEditor : public AudioProcessorEditor,
  168. public MultiTimer
  169. {
  170. public:
  171. PaulstretchpluginAudioProcessorEditor (PaulstretchpluginAudioProcessor&);
  172. ~PaulstretchpluginAudioProcessorEditor();
  173. void paint (Graphics&) override;
  174. void resized() override;
  175. void timerCallback(int id) override;
  176. void setAudioFile(File f);
  177. void setAudioBuffer(AudioBuffer<float>* buf, int samplerate, int len);
  178. void beginAddingAudioBlocks(int channels, int samplerate, int totalllen);
  179. void addAudioBlock(AudioBuffer<float>& buf, int samplerate, int pos);
  180. WaveformComponent m_wavecomponent;
  181. private:
  182. PaulstretchpluginAudioProcessor& processor;
  183. std::vector<std::shared_ptr<ParameterComponent>> m_parcomps;
  184. ToggleButton m_rec_enable;
  185. TextButton m_import_button;
  186. Label m_info_label;
  187. void chooseFile();
  188. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PaulstretchpluginAudioProcessorEditor)
  189. };