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.

187 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: AudioAppDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Simple audio application.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_processors, juce_audio_utils, juce_core,
  26. juce_data_structures, juce_events, juce_graphics,
  27. juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: AudioAppDemo
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. //==============================================================================
  37. class AudioAppDemo final : public AudioAppComponent
  38. {
  39. public:
  40. //==============================================================================
  41. AudioAppDemo()
  42. #ifdef JUCE_DEMO_RUNNER
  43. : AudioAppComponent (getSharedAudioDeviceManager (0, 2))
  44. #endif
  45. {
  46. setAudioChannels (0, 2);
  47. setSize (800, 600);
  48. }
  49. ~AudioAppDemo() override
  50. {
  51. shutdownAudio();
  52. }
  53. //==============================================================================
  54. void prepareToPlay (int samplesPerBlockExpected, double newSampleRate) override
  55. {
  56. sampleRate = newSampleRate;
  57. expectedSamplesPerBlock = samplesPerBlockExpected;
  58. }
  59. /* This method generates the actual audio samples.
  60. In this example the buffer is filled with a sine wave whose frequency and
  61. amplitude are controlled by the mouse position.
  62. */
  63. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  64. {
  65. bufferToFill.clearActiveBufferRegion();
  66. auto originalPhase = phase;
  67. for (auto chan = 0; chan < bufferToFill.buffer->getNumChannels(); ++chan)
  68. {
  69. phase = originalPhase;
  70. auto* channelData = bufferToFill.buffer->getWritePointer (chan, bufferToFill.startSample);
  71. for (auto i = 0; i < bufferToFill.numSamples ; ++i)
  72. {
  73. channelData[i] = amplitude * std::sin (phase);
  74. // increment the phase step for the next sample
  75. phase = std::fmod (phase + phaseDelta, MathConstants<float>::twoPi);
  76. }
  77. }
  78. }
  79. void releaseResources() override
  80. {
  81. // This gets automatically called when audio device parameters change
  82. // or device is restarted.
  83. }
  84. //==============================================================================
  85. void paint (Graphics& g) override
  86. {
  87. // (Our component is opaque, so we must completely fill the background with a solid colour)
  88. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  89. auto centreY = (float) getHeight() / 2.0f;
  90. auto radius = amplitude * 200.0f;
  91. if (radius >= 0.0f)
  92. {
  93. // Draw an ellipse based on the mouse position and audio volume
  94. g.setColour (Colours::lightgreen);
  95. g.fillEllipse (jmax (0.0f, lastMousePosition.x) - radius / 2.0f,
  96. jmax (0.0f, lastMousePosition.y) - radius / 2.0f,
  97. radius, radius);
  98. }
  99. // Draw a representative sine wave.
  100. Path wavePath;
  101. wavePath.startNewSubPath (0, centreY);
  102. for (auto x = 1.0f; x < (float) getWidth(); ++x)
  103. wavePath.lineTo (x, centreY + amplitude * (float) getHeight() * 2.0f
  104. * std::sin (x * frequency * 0.0001f));
  105. g.setColour (getLookAndFeel().findColour (Slider::thumbColourId));
  106. g.strokePath (wavePath, PathStrokeType (2.0f));
  107. }
  108. // Mouse handling..
  109. void mouseDown (const MouseEvent& e) override
  110. {
  111. mouseDrag (e);
  112. }
  113. void mouseDrag (const MouseEvent& e) override
  114. {
  115. lastMousePosition = e.position;
  116. frequency = (float) (getHeight() - e.y) * 10.0f;
  117. amplitude = jmin (0.9f, 0.2f * e.position.x / (float) getWidth());
  118. phaseDelta = (float) (MathConstants<double>::twoPi * frequency / sampleRate);
  119. repaint();
  120. }
  121. void mouseUp (const MouseEvent&) override
  122. {
  123. amplitude = 0.0f;
  124. repaint();
  125. }
  126. void resized() override
  127. {
  128. // This is called when the component is resized.
  129. // If you add any child components, this is where you should
  130. // update their positions.
  131. }
  132. private:
  133. //==============================================================================
  134. float phase = 0.0f;
  135. float phaseDelta = 0.0f;
  136. float frequency = 5000.0f;
  137. float amplitude = 0.2f;
  138. double sampleRate = 0.0;
  139. int expectedSamplesPerBlock = 0;
  140. Point<float> lastMousePosition;
  141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioAppDemo)
  142. };