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.

388 lines
14KB

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