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.

273 lines
8.4KB

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