The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

160 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. ==============================================================================
  5. */
  6. #ifndef MAINCOMPONENT_H_INCLUDED
  7. #define MAINCOMPONENT_H_INCLUDED
  8. #include "../JuceLibraryCode/JuceHeader.h"
  9. //==============================================================================
  10. /*
  11. This component lives inside our window, and this is where you should put all
  12. your controls and content.
  13. */
  14. class MainContentComponent : public AudioAppComponent
  15. {
  16. public:
  17. //==============================================================================
  18. MainContentComponent() : phase (0.0f),
  19. delta (0.0f),
  20. frequency (5000.0f),
  21. amplitude (0.2f),
  22. sampleRate (0.0)
  23. {
  24. setSize (500, 400);
  25. // the the input and output channels (currently Mono in and out)
  26. setAudioChannels (1, 1);
  27. }
  28. ~MainContentComponent()
  29. {
  30. shutdownAudio();
  31. }
  32. //=======================================================================
  33. // HANDLE AUDIO
  34. void prepareToPlay (int samplesPerBlockExpected, double newSampleRate) override
  35. {
  36. sampleRate = newSampleRate;
  37. }
  38. /* This is where the audio is created. In this example we
  39. fill the audio buffer with a sine wave whose frequency is
  40. controlled by the mouse Y position and whose volume is
  41. controlled by the mouse X potition.
  42. */
  43. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  44. {
  45. bufferToFill.clearActiveBufferRegion();
  46. // iterate over each sample of the sample buffer
  47. for (int i = bufferToFill.startSample; i < bufferToFill.numSamples + bufferToFill.startSample; ++i)
  48. {
  49. bufferToFill.buffer->getWritePointer (0)[i] = amplitude * sinf (phase);
  50. // increment the phase step for the next sample
  51. phase += delta;
  52. // reset the phase when it reaches 2PI to avoid large numbers
  53. while (phase >= 2.0f * float_Pi) phase -= 2.0f * float_Pi;
  54. }
  55. }
  56. void releaseResources() override
  57. {
  58. // This gets automatically called when audio device paramters change
  59. // or device is restarted.
  60. }
  61. //=======================================================================
  62. // HANDLE DRAWING
  63. void paint (Graphics& g)
  64. {
  65. // fill background
  66. g.fillAll (Colours::black);
  67. // Set the drawing colour to white
  68. g.setColour (Colours::white);
  69. // Draw an ellipse based on the mouse position and audio volume
  70. int radius = amplitude * 200;
  71. g.fillEllipse (mouse.x - radius/2, mouse.y - radius/2, radius, radius);
  72. // draw a representative sinewave
  73. Path wave;
  74. for (int i = 0; i < getWidth(); i++)
  75. {
  76. if (i == 0) wave.startNewSubPath (0, getHeight()/2);
  77. else wave.lineTo (i, getHeight()/2 + amplitude * getHeight() * 2.0f * sin (i*frequency*0.0001f));
  78. }
  79. g.strokePath (wave, PathStrokeType (2));
  80. }
  81. // Mouse handling
  82. void mouseUp(const MouseEvent& e) override
  83. {
  84. amplitude = 0.0f;
  85. }
  86. void mouseDown (const MouseEvent& e) override
  87. {
  88. mouseDrag (e);
  89. }
  90. void mouseDrag (const MouseEvent& e) override
  91. {
  92. // Update the mouse position variable
  93. mouse.setXY (e.x, e.y);
  94. repaint();
  95. frequency = (getHeight() - e.y) * 10.0f;
  96. amplitude = e.x/float(getWidth()) * 0.2f;
  97. delta = 2.0f * float_Pi * frequency / sampleRate;
  98. }
  99. void resized()
  100. {
  101. // This is called when the MainContentComponent is resized.
  102. // If you add any child components, this is where you should
  103. // update their positions.
  104. }
  105. private:
  106. //==============================================================================
  107. // private member variables
  108. float phase;
  109. float delta;
  110. float frequency;
  111. float amplitude;
  112. double sampleRate;
  113. Point<int> mouse;
  114. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
  115. };
  116. Component* createMainContentComponent() { return new MainContentComponent(); };
  117. #endif // MAINCOMPONENT_H_INCLUDED