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.

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