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.

275 lines
8.6KB

  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: BouncingBallWavetableDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Wavetable synthesis with a bouncing ball.
  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
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: BouncingBallWavetableDemo
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. //==============================================================================
  37. class BouncingBallWavetableDemo final : public AudioAppComponent,
  38. private Timer
  39. {
  40. public:
  41. //==============================================================================
  42. BouncingBallWavetableDemo()
  43. #ifdef JUCE_DEMO_RUNNER
  44. : AudioAppComponent (getSharedAudioDeviceManager (0, 2))
  45. #endif
  46. {
  47. setSize (600, 600);
  48. for (auto i = 0; i < numElementsInArray (waveValues); ++i)
  49. zeromem (waveValues[i], sizeof (waveValues[i]));
  50. // specify the number of input and output channels that we want to open
  51. setAudioChannels (2, 2);
  52. startTimerHz (60);
  53. }
  54. ~BouncingBallWavetableDemo() override
  55. {
  56. shutdownAudio();
  57. }
  58. //==============================================================================
  59. void prepareToPlay (int samplesPerBlockExpected, double newSampleRate) override
  60. {
  61. sampleRate = newSampleRate;
  62. expectedSamplesPerBlock = samplesPerBlockExpected;
  63. }
  64. /* This method generates the actual audio samples.
  65. In this example the buffer is filled with a sine wave whose frequency and
  66. amplitude are controlled by the mouse position.
  67. */
  68. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  69. {
  70. bufferToFill.clearActiveBufferRegion();
  71. for (auto chan = 0; chan < bufferToFill.buffer->getNumChannels(); ++chan)
  72. {
  73. auto ind = waveTableIndex;
  74. auto* channelData = bufferToFill.buffer->getWritePointer (chan, bufferToFill.startSample);
  75. for (auto i = 0; i < bufferToFill.numSamples; ++i)
  76. {
  77. if (isPositiveAndBelow (chan, numElementsInArray (waveValues)))
  78. {
  79. channelData[i] = waveValues[chan][ind % wavetableSize];
  80. ++ind;
  81. }
  82. }
  83. }
  84. waveTableIndex = (int) (waveTableIndex + bufferToFill.numSamples) % wavetableSize;
  85. }
  86. void releaseResources() override
  87. {
  88. // This gets automatically called when audio device parameters change
  89. // or device is restarted.
  90. stopTimer();
  91. }
  92. //==============================================================================
  93. void paint (Graphics& g) override
  94. {
  95. // (Our component is opaque, so we must completely fill the background with a solid colour)
  96. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  97. auto nextPos = pos + delta;
  98. if (nextPos.x < 10 || nextPos.x + 10 > (float) getWidth())
  99. {
  100. delta.x = -delta.x;
  101. nextPos.x = pos.x + delta.x;
  102. }
  103. if (nextPos.y < 50 || nextPos.y + 10 > (float) getHeight())
  104. {
  105. delta.y = -delta.y;
  106. nextPos.y = pos.y + delta.y;
  107. }
  108. if (! dragging)
  109. {
  110. writeInterpolatedValue (pos, nextPos);
  111. pos = nextPos;
  112. }
  113. else
  114. {
  115. pos = lastMousePosition;
  116. }
  117. // draw a circle
  118. g.setColour (getLookAndFeel().findColour (Slider::thumbColourId));
  119. g.fillEllipse (pos.x, pos.y, 20, 20);
  120. drawWaveform (g, 20.0f, 0);
  121. drawWaveform (g, 40.0f, 1);
  122. }
  123. void drawWaveform (Graphics& g, float y, int channel) const
  124. {
  125. auto pathWidth = 2000;
  126. Path wavePath;
  127. wavePath.startNewSubPath (0.0f, y);
  128. for (auto i = 1; i < pathWidth; ++i)
  129. wavePath.lineTo ((float) i, (1.0f + waveValues[channel][i * numElementsInArray (waveValues[0]) / pathWidth]) * 10.0f);
  130. g.strokePath (wavePath, PathStrokeType (1.0f),
  131. wavePath.getTransformToScaleToFit (Rectangle<float> (0.0f, y, (float) getWidth(), 20.0f), false));
  132. }
  133. // Mouse handling..
  134. void mouseDown (const MouseEvent& e) override
  135. {
  136. lastMousePosition = e.position;
  137. mouseDrag (e);
  138. dragging = true;
  139. }
  140. void mouseDrag (const MouseEvent& e) override
  141. {
  142. dragging = true;
  143. if (e.position != lastMousePosition)
  144. {
  145. // calculate movement vector
  146. delta = e.position - lastMousePosition;
  147. waveValues[0][bufferIndex % wavetableSize] = xToAmplitude (e.position.x);
  148. waveValues[1][bufferIndex % wavetableSize] = yToAmplitude (e.position.y);
  149. ++bufferIndex;
  150. lastMousePosition = e.position;
  151. }
  152. }
  153. void mouseUp (const MouseEvent&) override
  154. {
  155. dragging = false;
  156. }
  157. void writeInterpolatedValue (Point<float> lastPosition,
  158. Point<float> currentPosition)
  159. {
  160. Point<float> start, finish;
  161. if (lastPosition.getX() > currentPosition.getX())
  162. {
  163. finish = lastPosition;
  164. start = currentPosition;
  165. }
  166. else
  167. {
  168. start = lastPosition;
  169. finish = currentPosition;
  170. }
  171. for (auto i = 0; i < steps; ++i)
  172. {
  173. auto p = start + ((finish - start) * i) / (int) steps;
  174. auto index = (bufferIndex + i) % wavetableSize;
  175. waveValues[1][index] = yToAmplitude (p.y);
  176. waveValues[0][index] = xToAmplitude (p.x);
  177. }
  178. bufferIndex = (bufferIndex + steps) % wavetableSize;
  179. }
  180. float indexToX (int indexValue) const noexcept
  181. {
  182. return (float) indexValue;
  183. }
  184. float amplitudeToY (float amp) const noexcept
  185. {
  186. return (float) getHeight() - (amp + 1.0f) * (float) getHeight() / 2.0f;
  187. }
  188. float xToAmplitude (float x) const noexcept
  189. {
  190. return jlimit (-1.0f, 1.0f, 2.0f * ((float) getWidth() - x) / (float) getWidth() - 1.0f);
  191. }
  192. float yToAmplitude (float y) const noexcept
  193. {
  194. return jlimit (-1.0f, 1.0f, 2.0f * ((float) getHeight() - y) / (float) getHeight() - 1.0f);
  195. }
  196. void timerCallback() override
  197. {
  198. repaint();
  199. }
  200. private:
  201. //==============================================================================
  202. enum
  203. {
  204. wavetableSize = 36000,
  205. steps = 10
  206. };
  207. Point<float> pos = { 299.0f, 299.0f };
  208. Point<float> delta = { 0.0f, 0.0f };
  209. int waveTableIndex = 0;
  210. int bufferIndex = 0;
  211. double sampleRate = 0.0;
  212. int expectedSamplesPerBlock = 0;
  213. Point<float> lastMousePosition;
  214. float waveValues[2][wavetableSize];
  215. bool dragging = false;
  216. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BouncingBallWavetableDemo)
  217. };