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.

150 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. ==============================================================================
  5. */
  6. #include "../JuceLibraryCode/JuceHeader.h"
  7. //==============================================================================
  8. class MainContentComponent : public AudioAppComponent
  9. {
  10. public:
  11. //==============================================================================
  12. MainContentComponent()
  13. : phase (0.0f),
  14. phaseDelta (0.0f),
  15. frequency (5000.0f),
  16. amplitude (0.2f),
  17. sampleRate (0.0),
  18. expectedSamplesPerBlock (0)
  19. {
  20. setSize (800, 600);
  21. // Specify the number of input and output channels that we want to open.
  22. setAudioChannels (0, 2);
  23. }
  24. ~MainContentComponent()
  25. {
  26. shutdownAudio();
  27. }
  28. //==============================================================================
  29. void prepareToPlay (int samplesPerBlockExpected, double newSampleRate) override
  30. {
  31. sampleRate = newSampleRate;
  32. expectedSamplesPerBlock = samplesPerBlockExpected;
  33. }
  34. /* This method generates the actual audio samples.
  35. In this example the buffer is filled with a sine wave whose frequency and
  36. amplitude are controlled by the mouse position.
  37. */
  38. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  39. {
  40. bufferToFill.clearActiveBufferRegion();
  41. const float originalPhase = phase;
  42. for (int chan = 0; chan < bufferToFill.buffer->getNumChannels(); ++chan)
  43. {
  44. phase = originalPhase;
  45. float* const channelData = bufferToFill.buffer->getWritePointer (chan, bufferToFill.startSample);
  46. for (int i = 0; i < bufferToFill.numSamples ; ++i)
  47. {
  48. channelData[i] = amplitude * std::sin (phase);
  49. // increment the phase step for the next sample
  50. phase = std::fmod (phase + phaseDelta, float_Pi * 2.0f);
  51. }
  52. }
  53. }
  54. void releaseResources() override
  55. {
  56. // This gets automatically called when audio device parameters change
  57. // or device is restarted.
  58. }
  59. //==============================================================================
  60. void paint (Graphics& g) override
  61. {
  62. // (Our component is opaque, so we must completely fill the background with a solid colour)
  63. g.fillAll (Colours::black);
  64. const float centreY = getHeight() / 2.0f;
  65. const float radius = amplitude * 200.0f;
  66. // Draw an ellipse based on the mouse position and audio volume
  67. g.setColour (Colours::lightgreen);
  68. g.fillEllipse (lastMousePosition.x - radius / 2.0f,
  69. lastMousePosition.y - radius / 2.0f,
  70. radius, radius);
  71. // Draw a representative sine wave.
  72. Path wavePath;
  73. wavePath.startNewSubPath (0, centreY);
  74. for (float x = 1.0f; x < getWidth(); ++x)
  75. wavePath.lineTo (x, centreY + amplitude * getHeight() * 2.0f
  76. * std::sin (x * frequency * 0.0001f));
  77. g.setColour (Colours::grey);
  78. g.strokePath (wavePath, PathStrokeType (2.0f));
  79. }
  80. // Mouse handling..
  81. void mouseDown (const MouseEvent& e) override
  82. {
  83. mouseDrag (e);
  84. }
  85. void mouseDrag (const MouseEvent& e) override
  86. {
  87. lastMousePosition = e.position;
  88. frequency = (getHeight() - e.y) * 10.0f;
  89. amplitude = jmin (0.9f, 0.2f * e.position.x / getWidth());
  90. phaseDelta = (float) (2.0 * double_Pi * frequency / sampleRate);
  91. repaint();
  92. }
  93. void mouseUp (const MouseEvent&) override
  94. {
  95. amplitude = 0.0f;
  96. repaint();
  97. }
  98. void resized() override
  99. {
  100. // This is called when the MainContentComponent is resized.
  101. // If you add any child components, this is where you should
  102. // update their positions.
  103. }
  104. private:
  105. //==============================================================================
  106. float phase;
  107. float phaseDelta;
  108. float frequency;
  109. float amplitude;
  110. double sampleRate;
  111. int expectedSamplesPerBlock;
  112. Point<float> lastMousePosition;
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
  114. };
  115. Component* createMainContentComponent() { return new MainContentComponent(); };