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.

317 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. #include "AudioLiveScrollingDisplay.h"
  20. //==============================================================================
  21. /** Our demo synth sound is just a basic sine wave.. */
  22. struct SineWaveSound : public SynthesiserSound
  23. {
  24. SineWaveSound() {}
  25. bool appliesToNote (int /*midiNoteNumber*/) override { return true; }
  26. bool appliesToChannel (int /*midiChannel*/) override { return true; }
  27. };
  28. //==============================================================================
  29. /** Our demo synth voice just plays a sine wave.. */
  30. struct SineWaveVoice : public SynthesiserVoice
  31. {
  32. SineWaveVoice() : currentAngle (0), angleDelta (0), level (0), tailOff (0)
  33. {
  34. }
  35. bool canPlaySound (SynthesiserSound* sound) override
  36. {
  37. return dynamic_cast<SineWaveSound*> (sound) != nullptr;
  38. }
  39. void startNote (int midiNoteNumber, float velocity,
  40. SynthesiserSound*, int /*currentPitchWheelPosition*/) override
  41. {
  42. currentAngle = 0.0;
  43. level = velocity * 0.15;
  44. tailOff = 0.0;
  45. double cyclesPerSecond = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
  46. double cyclesPerSample = cyclesPerSecond / getSampleRate();
  47. angleDelta = cyclesPerSample * 2.0 * double_Pi;
  48. }
  49. void stopNote (float /*velocity*/, bool allowTailOff) override
  50. {
  51. if (allowTailOff)
  52. {
  53. // start a tail-off by setting this flag. The render callback will pick up on
  54. // this and do a fade out, calling clearCurrentNote() when it's finished.
  55. if (tailOff == 0.0) // we only need to begin a tail-off if it's not already doing so - the
  56. // stopNote method could be called more than once.
  57. tailOff = 1.0;
  58. }
  59. else
  60. {
  61. // we're being told to stop playing immediately, so reset everything..
  62. clearCurrentNote();
  63. angleDelta = 0.0;
  64. }
  65. }
  66. void pitchWheelMoved (int /*newValue*/) override
  67. {
  68. // can't be bothered implementing this for the demo!
  69. }
  70. void controllerMoved (int /*controllerNumber*/, int /*newValue*/) override
  71. {
  72. // not interested in controllers in this case.
  73. }
  74. void renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples) override
  75. {
  76. if (angleDelta != 0.0)
  77. {
  78. if (tailOff > 0)
  79. {
  80. while (--numSamples >= 0)
  81. {
  82. const float currentSample = std::sin (currentAngle) * level * tailOff;
  83. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  84. outputBuffer.addSample (i, startSample, currentSample);
  85. currentAngle += angleDelta;
  86. ++startSample;
  87. tailOff *= 0.99;
  88. if (tailOff <= 0.005)
  89. {
  90. clearCurrentNote();
  91. angleDelta = 0.0;
  92. break;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. while (--numSamples >= 0)
  99. {
  100. const float currentSample = std::sin (currentAngle) * level;
  101. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  102. outputBuffer.addSample (i, startSample, currentSample);
  103. currentAngle += angleDelta;
  104. ++startSample;
  105. }
  106. }
  107. }
  108. }
  109. private:
  110. double currentAngle, angleDelta, level, tailOff;
  111. };
  112. //==============================================================================
  113. // This is an audio source that streams the output of our demo synth.
  114. struct SynthAudioSource : public AudioSource
  115. {
  116. SynthAudioSource (MidiKeyboardState& keyState) : keyboardState (keyState)
  117. {
  118. // Add some voices to our synth, to play the sounds..
  119. for (int i = 4; --i >= 0;)
  120. {
  121. synth.addVoice (new SineWaveVoice()); // These voices will play our custom sine-wave sounds..
  122. synth.addVoice (new SamplerVoice()); // and these ones play the sampled sounds
  123. }
  124. // ..and add a sound for them to play...
  125. setUsingSineWaveSound();
  126. }
  127. void setUsingSineWaveSound()
  128. {
  129. synth.clearSounds();
  130. synth.addSound (new SineWaveSound());
  131. }
  132. void setUsingSampledSound()
  133. {
  134. WavAudioFormat wavFormat;
  135. ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav,
  136. BinaryData::cello_wavSize,
  137. false),
  138. true));
  139. BigInteger allNotes;
  140. allNotes.setRange (0, 128, true);
  141. synth.clearSounds();
  142. synth.addSound (new SamplerSound ("demo sound",
  143. *audioReader,
  144. allNotes,
  145. 74, // root midi note
  146. 0.1, // attack time
  147. 0.1, // release time
  148. 10.0 // maximum sample length
  149. ));
  150. }
  151. void prepareToPlay (int /*samplesPerBlockExpected*/, double sampleRate) override
  152. {
  153. midiCollector.reset (sampleRate);
  154. synth.setCurrentPlaybackSampleRate (sampleRate);
  155. }
  156. void releaseResources() override
  157. {
  158. }
  159. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  160. {
  161. // the synth always adds its output to the audio buffer, so we have to clear it
  162. // first..
  163. bufferToFill.clearActiveBufferRegion();
  164. // fill a midi buffer with incoming messages from the midi input.
  165. MidiBuffer incomingMidi;
  166. midiCollector.removeNextBlockOfMessages (incomingMidi, bufferToFill.numSamples);
  167. // pass these messages to the keyboard state so that it can update the component
  168. // to show on-screen which keys are being pressed on the physical midi keyboard.
  169. // This call will also add midi messages to the buffer which were generated by
  170. // the mouse-clicking on the on-screen keyboard.
  171. keyboardState.processNextMidiBuffer (incomingMidi, 0, bufferToFill.numSamples, true);
  172. // and now get the synth to process the midi events and generate its output.
  173. synth.renderNextBlock (*bufferToFill.buffer, incomingMidi, 0, bufferToFill.numSamples);
  174. }
  175. //==============================================================================
  176. // this collects real-time midi messages from the midi input device, and
  177. // turns them into blocks that we can process in our audio callback
  178. MidiMessageCollector midiCollector;
  179. // this represents the state of which keys on our on-screen keyboard are held
  180. // down. When the mouse is clicked on the keyboard component, this object also
  181. // generates midi messages for this, which we can pass on to our synth.
  182. MidiKeyboardState& keyboardState;
  183. // the synth itself!
  184. Synthesiser synth;
  185. };
  186. //==============================================================================
  187. class AudioSynthesiserDemo : public Component,
  188. private Button::Listener
  189. {
  190. public:
  191. AudioSynthesiserDemo()
  192. : deviceManager (MainAppWindow::getSharedAudioDeviceManager()),
  193. synthAudioSource (keyboardState),
  194. keyboardComponent (keyboardState, MidiKeyboardComponent::horizontalKeyboard)
  195. {
  196. addAndMakeVisible (keyboardComponent);
  197. addAndMakeVisible (sineButton);
  198. sineButton.setButtonText ("Use sine wave");
  199. sineButton.setRadioGroupId (321);
  200. sineButton.addListener (this);
  201. sineButton.setToggleState (true, dontSendNotification);
  202. addAndMakeVisible (sampledButton);
  203. sampledButton.setButtonText ("Use sampled sound");
  204. sampledButton.setRadioGroupId (321);
  205. sampledButton.addListener (this);
  206. addAndMakeVisible (liveAudioDisplayComp);
  207. deviceManager.addAudioCallback (&liveAudioDisplayComp);
  208. audioSourcePlayer.setSource (&synthAudioSource);
  209. deviceManager.addAudioCallback (&audioSourcePlayer);
  210. deviceManager.addMidiInputCallback (String::empty, &(synthAudioSource.midiCollector));
  211. setOpaque (true);
  212. setSize (640, 480);
  213. }
  214. ~AudioSynthesiserDemo()
  215. {
  216. audioSourcePlayer.setSource (nullptr);
  217. deviceManager.removeMidiInputCallback (String::empty, &(synthAudioSource.midiCollector));
  218. deviceManager.removeAudioCallback (&audioSourcePlayer);
  219. deviceManager.removeAudioCallback (&liveAudioDisplayComp);
  220. }
  221. //==============================================================================
  222. void paint (Graphics& g) override
  223. {
  224. fillTiledBackground (g);
  225. }
  226. void resized() override
  227. {
  228. keyboardComponent.setBounds (8, 96, getWidth() - 16, 64);
  229. sineButton.setBounds (16, 176, 150, 24);
  230. sampledButton.setBounds (16, 200, 150, 24);
  231. liveAudioDisplayComp.setBounds (8, 8, getWidth() - 16, 64);
  232. }
  233. private:
  234. AudioDeviceManager& deviceManager;
  235. MidiKeyboardState keyboardState;
  236. AudioSourcePlayer audioSourcePlayer;
  237. SynthAudioSource synthAudioSource;
  238. MidiKeyboardComponent keyboardComponent;
  239. ToggleButton sineButton;
  240. ToggleButton sampledButton;
  241. LiveScrollingAudioDisplay liveAudioDisplayComp;
  242. //==============================================================================
  243. void buttonClicked (Button* buttonThatWasClicked) override
  244. {
  245. if (buttonThatWasClicked == &sineButton)
  246. synthAudioSource.setUsingSineWaveSound();
  247. else if (buttonThatWasClicked == &sampledButton)
  248. synthAudioSource.setUsingSampledSound();
  249. }
  250. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSynthesiserDemo)
  251. };
  252. // This static object will register this demo type in a global list of demos..
  253. static JuceDemoType<AudioSynthesiserDemo> demo ("31 Audio: Synthesisers");