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.

155 lines
4.7KB

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