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.

390 lines
14KB

  1. /*
  2. ==============================================================================
  3. This is an automatically generated file created by the Jucer!
  4. Creation date: 1 May 2011 12:06:00pm
  5. Be careful when adding custom code to these files, as only the code within
  6. the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  7. and re-saved.
  8. Jucer version: 1.12
  9. ------------------------------------------------------------------------------
  10. The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  11. Copyright 2004-6 by Raw Material Software ltd.
  12. ==============================================================================
  13. */
  14. //[Headers] You can add your own extra header files here...
  15. //[/Headers]
  16. #include "AudioDemoSynthPage.h"
  17. //[MiscUserDefs] You can add your own user definitions and misc code here...
  18. //==============================================================================
  19. /** Our demo synth sound is just a basic sine wave..
  20. */
  21. class SineWaveSound : public SynthesiserSound
  22. {
  23. public:
  24. SineWaveSound()
  25. {
  26. }
  27. bool appliesToNote (const int /*midiNoteNumber*/) { return true; }
  28. bool appliesToChannel (const int /*midiChannel*/) { return true; }
  29. };
  30. //==============================================================================
  31. /** Our demo synth voice just plays a sine wave..
  32. */
  33. class SineWaveVoice : public SynthesiserVoice
  34. {
  35. public:
  36. SineWaveVoice()
  37. : angleDelta (0.0),
  38. tailOff (0.0)
  39. {
  40. }
  41. bool canPlaySound (SynthesiserSound* sound)
  42. {
  43. return dynamic_cast <SineWaveSound*> (sound) != 0;
  44. }
  45. void startNote (const int midiNoteNumber, const float velocity,
  46. SynthesiserSound* /*sound*/, const int /*currentPitchWheelPosition*/)
  47. {
  48. currentAngle = 0.0;
  49. level = velocity * 0.15;
  50. tailOff = 0.0;
  51. double cyclesPerSecond = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
  52. double cyclesPerSample = cyclesPerSecond / getSampleRate();
  53. angleDelta = cyclesPerSample * 2.0 * double_Pi;
  54. }
  55. void stopNote (const bool allowTailOff)
  56. {
  57. if (allowTailOff)
  58. {
  59. // start a tail-off by setting this flag. The render callback will pick up on
  60. // this and do a fade out, calling clearCurrentNote() when it's finished.
  61. if (tailOff == 0.0) // we only need to begin a tail-off if it's not already doing so - the
  62. // stopNote method could be called more than once.
  63. tailOff = 1.0;
  64. }
  65. else
  66. {
  67. // we're being told to stop playing immediately, so reset everything..
  68. clearCurrentNote();
  69. angleDelta = 0.0;
  70. }
  71. }
  72. void pitchWheelMoved (const int /*newValue*/)
  73. {
  74. // can't be bothered implementing this for the demo!
  75. }
  76. void controllerMoved (const int /*controllerNumber*/, const int /*newValue*/)
  77. {
  78. // not interested in controllers in this case.
  79. }
  80. void renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples)
  81. {
  82. if (angleDelta != 0.0)
  83. {
  84. if (tailOff > 0)
  85. {
  86. while (--numSamples >= 0)
  87. {
  88. const float currentSample = (float) (sin (currentAngle) * level * tailOff);
  89. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  90. *outputBuffer.getSampleData (i, startSample) += currentSample;
  91. currentAngle += angleDelta;
  92. ++startSample;
  93. tailOff *= 0.99;
  94. if (tailOff <= 0.005)
  95. {
  96. clearCurrentNote();
  97. angleDelta = 0.0;
  98. break;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. while (--numSamples >= 0)
  105. {
  106. const float currentSample = (float) (sin (currentAngle) * level);
  107. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  108. *outputBuffer.getSampleData (i, startSample) += currentSample;
  109. currentAngle += angleDelta;
  110. ++startSample;
  111. }
  112. }
  113. }
  114. }
  115. private:
  116. double currentAngle, angleDelta, level, tailOff;
  117. };
  118. // This is an audio source that streams the output of our demo synth.
  119. class SynthAudioSource : public AudioSource
  120. {
  121. public:
  122. //==============================================================================
  123. // this collects real-time midi messages from the midi input device, and
  124. // turns them into blocks that we can process in our audio callback
  125. MidiMessageCollector midiCollector;
  126. // this represents the state of which keys on our on-screen keyboard are held
  127. // down. When the mouse is clicked on the keyboard component, this object also
  128. // generates midi messages for this, which we can pass on to our synth.
  129. MidiKeyboardState& keyboardState;
  130. // the synth itself!
  131. Synthesiser synth;
  132. //==============================================================================
  133. SynthAudioSource (MidiKeyboardState& keyboardState_)
  134. : keyboardState (keyboardState_)
  135. {
  136. // add some voices to our synth, to play the sounds..
  137. for (int i = 4; --i >= 0;)
  138. {
  139. synth.addVoice (new SineWaveVoice()); // These voices will play our custom sine-wave sounds..
  140. synth.addVoice (new SamplerVoice()); // and these ones play the sampled sounds
  141. }
  142. // and add some sounds for them to play...
  143. setUsingSineWaveSound();
  144. }
  145. void setUsingSineWaveSound()
  146. {
  147. synth.clearSounds();
  148. synth.addSound (new SineWaveSound());
  149. }
  150. void setUsingSampledSound()
  151. {
  152. synth.clearSounds();
  153. WavAudioFormat wavFormat;
  154. ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav,
  155. BinaryData::cello_wavSize,
  156. false),
  157. true));
  158. BigInteger allNotes;
  159. allNotes.setRange (0, 128, true);
  160. synth.addSound (new SamplerSound ("demo sound",
  161. *audioReader,
  162. allNotes,
  163. 74, // root midi note
  164. 0.1, // attack time
  165. 0.1, // release time
  166. 10.0 // maximum sample length
  167. ));
  168. }
  169. void prepareToPlay (int /*samplesPerBlockExpected*/, double sampleRate)
  170. {
  171. midiCollector.reset (sampleRate);
  172. synth.setCurrentPlaybackSampleRate (sampleRate);
  173. }
  174. void releaseResources()
  175. {
  176. }
  177. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  178. {
  179. // the synth always adds its output to the audio buffer, so we have to clear it
  180. // first..
  181. bufferToFill.clearActiveBufferRegion();
  182. // fill a midi buffer with incoming messages from the midi input.
  183. MidiBuffer incomingMidi;
  184. midiCollector.removeNextBlockOfMessages (incomingMidi, bufferToFill.numSamples);
  185. // pass these messages to the keyboard state so that it can update the component
  186. // to show on-screen which keys are being pressed on the physical midi keyboard.
  187. // This call will also add midi messages to the buffer which were generated by
  188. // the mouse-clicking on the on-screen keyboard.
  189. keyboardState.processNextMidiBuffer (incomingMidi, 0, bufferToFill.numSamples, true);
  190. // and now get the synth to process the midi events and generate its output.
  191. synth.renderNextBlock (*bufferToFill.buffer, incomingMidi, 0, bufferToFill.numSamples);
  192. }
  193. };
  194. //[/MiscUserDefs]
  195. //==============================================================================
  196. AudioDemoSynthPage::AudioDemoSynthPage (AudioDeviceManager& deviceManager_)
  197. : deviceManager (deviceManager_),
  198. keyboardComponent (0),
  199. sineButton (0),
  200. sampledButton (0),
  201. liveAudioDisplayComp (0)
  202. {
  203. addAndMakeVisible (keyboardComponent = new MidiKeyboardComponent (keyboardState, MidiKeyboardComponent::horizontalKeyboard));
  204. addAndMakeVisible (sineButton = new ToggleButton (String::empty));
  205. sineButton->setButtonText (L"Use sine wave");
  206. sineButton->setRadioGroupId (321);
  207. sineButton->addListener (this);
  208. sineButton->setToggleState (true, false);
  209. addAndMakeVisible (sampledButton = new ToggleButton (String::empty));
  210. sampledButton->setButtonText (L"Use sampled sound");
  211. sampledButton->setRadioGroupId (321);
  212. sampledButton->addListener (this);
  213. addAndMakeVisible (liveAudioDisplayComp = new LiveAudioInputDisplayComp());
  214. //[UserPreSize]
  215. //[/UserPreSize]
  216. setSize (600, 400);
  217. //[Constructor] You can add your own custom stuff here..
  218. deviceManager.addAudioCallback (liveAudioDisplayComp);
  219. synthAudioSource = new SynthAudioSource (keyboardState);
  220. audioSourcePlayer.setSource (synthAudioSource);
  221. deviceManager.addAudioCallback (&audioSourcePlayer);
  222. deviceManager.addMidiInputCallback (String::empty, &(synthAudioSource->midiCollector));
  223. //[/Constructor]
  224. }
  225. AudioDemoSynthPage::~AudioDemoSynthPage()
  226. {
  227. //[Destructor_pre]. You can add your own custom destruction code here..
  228. audioSourcePlayer.setSource (0);
  229. deviceManager.removeMidiInputCallback (String::empty, &(synthAudioSource->midiCollector));
  230. deviceManager.removeAudioCallback (&audioSourcePlayer);
  231. deviceManager.removeAudioCallback (liveAudioDisplayComp);
  232. //[/Destructor_pre]
  233. deleteAndZero (keyboardComponent);
  234. deleteAndZero (sineButton);
  235. deleteAndZero (sampledButton);
  236. deleteAndZero (liveAudioDisplayComp);
  237. //[Destructor]. You can add your own custom destruction code here..
  238. //[/Destructor]
  239. }
  240. //==============================================================================
  241. void AudioDemoSynthPage::paint (Graphics& g)
  242. {
  243. //[UserPrePaint] Add your own custom painting code here..
  244. //[/UserPrePaint]
  245. g.fillAll (Colours::lightgrey);
  246. //[UserPaint] Add your own custom painting code here..
  247. //[/UserPaint]
  248. }
  249. void AudioDemoSynthPage::resized()
  250. {
  251. keyboardComponent->setBounds (8, 96, getWidth() - 16, 64);
  252. sineButton->setBounds (16, 176, 150, 24);
  253. sampledButton->setBounds (16, 200, 150, 24);
  254. liveAudioDisplayComp->setBounds (8, 8, getWidth() - 16, 64);
  255. //[UserResized] Add your own custom resize handling here..
  256. //[/UserResized]
  257. }
  258. void AudioDemoSynthPage::buttonClicked (Button* buttonThatWasClicked)
  259. {
  260. //[UserbuttonClicked_Pre]
  261. //[/UserbuttonClicked_Pre]
  262. if (buttonThatWasClicked == sineButton)
  263. {
  264. //[UserButtonCode_sineButton] -- add your button handler code here..
  265. synthAudioSource->setUsingSineWaveSound();
  266. //[/UserButtonCode_sineButton]
  267. }
  268. else if (buttonThatWasClicked == sampledButton)
  269. {
  270. //[UserButtonCode_sampledButton] -- add your button handler code here..
  271. synthAudioSource->setUsingSampledSound();
  272. //[/UserButtonCode_sampledButton]
  273. }
  274. //[UserbuttonClicked_Post]
  275. //[/UserbuttonClicked_Post]
  276. }
  277. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  278. //[/MiscUserCode]
  279. //==============================================================================
  280. #if 0
  281. /* -- Jucer information section --
  282. This is where the Jucer puts all of its metadata, so don't change anything in here!
  283. BEGIN_JUCER_METADATA
  284. <JUCER_COMPONENT documentType="Component" className="AudioDemoSynthPage" componentName=""
  285. parentClasses="public Component" constructorParams="AudioDeviceManager&amp; deviceManager_"
  286. variableInitialisers="deviceManager (deviceManager_)" snapPixels="8"
  287. snapActive="1" snapShown="1" overlayOpacity="0.330000013" fixedSize="0"
  288. initialWidth="600" initialHeight="400">
  289. <BACKGROUND backgroundColour="ffd3d3d3"/>
  290. <GENERICCOMPONENT name="" id="86605ec4f02c4320" memberName="keyboardComponent"
  291. virtualName="" explicitFocusOrder="0" pos="8 96 16M 64" class="MidiKeyboardComponent"
  292. params="keyboardState, MidiKeyboardComponent::horizontalKeyboard"/>
  293. <TOGGLEBUTTON name="" id="d75101df45006ba9" memberName="sineButton" virtualName=""
  294. explicitFocusOrder="0" pos="16 176 150 24" buttonText="Use sine wave"
  295. connectedEdges="0" needsCallback="1" radioGroupId="321" state="1"/>
  296. <TOGGLEBUTTON name="" id="2d687b4ac3dad628" memberName="sampledButton" virtualName=""
  297. explicitFocusOrder="0" pos="16 200 150 24" buttonText="Use sampled sound"
  298. connectedEdges="0" needsCallback="1" radioGroupId="321" state="0"/>
  299. <GENERICCOMPONENT name="" id="7d70eb2617f56220" memberName="liveAudioDisplayComp"
  300. virtualName="" explicitFocusOrder="0" pos="8 8 16M 64" class="LiveAudioInputDisplayComp"
  301. params=""/>
  302. </JUCER_COMPONENT>
  303. END_JUCER_METADATA
  304. */
  305. #endif