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.

389 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. {
  198. addAndMakeVisible (keyboardComponent = new MidiKeyboardComponent (keyboardState, MidiKeyboardComponent::horizontalKeyboard));
  199. addAndMakeVisible (sineButton = new ToggleButton (String::empty));
  200. sineButton->setButtonText ("Use sine wave");
  201. sineButton->setRadioGroupId (321);
  202. sineButton->addListener (this);
  203. sineButton->setToggleState (true, false);
  204. addAndMakeVisible (sampledButton = new ToggleButton (String::empty));
  205. sampledButton->setButtonText ("Use sampled sound");
  206. sampledButton->setRadioGroupId (321);
  207. sampledButton->addListener (this);
  208. addAndMakeVisible (liveAudioDisplayComp = new LiveAudioInputDisplayComp());
  209. //[UserPreSize]
  210. //[/UserPreSize]
  211. setSize (600, 400);
  212. //[Constructor] You can add your own custom stuff here..
  213. deviceManager.addAudioCallback (liveAudioDisplayComp);
  214. synthAudioSource = new SynthAudioSource (keyboardState);
  215. audioSourcePlayer.setSource (synthAudioSource);
  216. deviceManager.addAudioCallback (&audioSourcePlayer);
  217. deviceManager.addMidiInputCallback (String::empty, &(synthAudioSource->midiCollector));
  218. //[/Constructor]
  219. }
  220. AudioDemoSynthPage::~AudioDemoSynthPage()
  221. {
  222. //[Destructor_pre]. You can add your own custom destruction code here..
  223. audioSourcePlayer.setSource (0);
  224. deviceManager.removeMidiInputCallback (String::empty, &(synthAudioSource->midiCollector));
  225. deviceManager.removeAudioCallback (&audioSourcePlayer);
  226. deviceManager.removeAudioCallback (liveAudioDisplayComp);
  227. //[/Destructor_pre]
  228. keyboardComponent = nullptr;
  229. sineButton = nullptr;
  230. sampledButton = nullptr;
  231. liveAudioDisplayComp = nullptr;
  232. //[Destructor]. You can add your own custom destruction code here..
  233. //[/Destructor]
  234. }
  235. //==============================================================================
  236. void AudioDemoSynthPage::paint (Graphics& g)
  237. {
  238. //[UserPrePaint] Add your own custom painting code here..
  239. //[/UserPrePaint]
  240. g.fillAll (Colours::lightgrey);
  241. //[UserPaint] Add your own custom painting code here..
  242. //[/UserPaint]
  243. }
  244. void AudioDemoSynthPage::resized()
  245. {
  246. keyboardComponent->setBounds (8, 96, getWidth() - 16, 64);
  247. sineButton->setBounds (16, 176, 150, 24);
  248. sampledButton->setBounds (16, 200, 150, 24);
  249. liveAudioDisplayComp->setBounds (8, 8, getWidth() - 16, 64);
  250. //[UserResized] Add your own custom resize handling here..
  251. //[/UserResized]
  252. }
  253. void AudioDemoSynthPage::buttonClicked (Button* buttonThatWasClicked)
  254. {
  255. //[UserbuttonClicked_Pre]
  256. //[/UserbuttonClicked_Pre]
  257. if (buttonThatWasClicked == sineButton)
  258. {
  259. //[UserButtonCode_sineButton] -- add your button handler code here..
  260. synthAudioSource->setUsingSineWaveSound();
  261. //[/UserButtonCode_sineButton]
  262. }
  263. else if (buttonThatWasClicked == sampledButton)
  264. {
  265. //[UserButtonCode_sampledButton] -- add your button handler code here..
  266. synthAudioSource->setUsingSampledSound();
  267. //[/UserButtonCode_sampledButton]
  268. }
  269. //[UserbuttonClicked_Post]
  270. //[/UserbuttonClicked_Post]
  271. }
  272. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  273. //[/MiscUserCode]
  274. //==============================================================================
  275. #if 0
  276. /* -- Introjucer information section --
  277. This is where the Introjucer stores the metadata that describe this GUI layout, so
  278. make changes in here at your peril!
  279. BEGIN_JUCER_METADATA
  280. <JUCER_COMPONENT documentType="Component" className="AudioDemoSynthPage" componentName=""
  281. parentClasses="public Component" constructorParams="AudioDeviceManager&amp; deviceManager_"
  282. variableInitialisers="deviceManager (deviceManager_)" snapPixels="8"
  283. snapActive="1" snapShown="1" overlayOpacity="0.330000013" fixedSize="0"
  284. initialWidth="600" initialHeight="400">
  285. <BACKGROUND backgroundColour="ffd3d3d3"/>
  286. <GENERICCOMPONENT name="" id="86605ec4f02c4320" memberName="keyboardComponent"
  287. virtualName="" explicitFocusOrder="0" pos="8 96 16M 64" class="MidiKeyboardComponent"
  288. params="keyboardState, MidiKeyboardComponent::horizontalKeyboard"/>
  289. <TOGGLEBUTTON name="" id="d75101df45006ba9" memberName="sineButton" virtualName=""
  290. explicitFocusOrder="0" pos="16 176 150 24" buttonText="Use sine wave"
  291. connectedEdges="0" needsCallback="1" radioGroupId="321" state="1"/>
  292. <TOGGLEBUTTON name="" id="2d687b4ac3dad628" memberName="sampledButton" virtualName=""
  293. explicitFocusOrder="0" pos="16 200 150 24" buttonText="Use sampled sound"
  294. connectedEdges="0" needsCallback="1" radioGroupId="321" state="0"/>
  295. <GENERICCOMPONENT name="" id="7d70eb2617f56220" memberName="liveAudioDisplayComp"
  296. virtualName="" explicitFocusOrder="0" pos="8 8 16M 64" class="LiveAudioInputDisplayComp"
  297. params=""/>
  298. </JUCER_COMPONENT>
  299. END_JUCER_METADATA
  300. */
  301. #endif
  302. //[EndFile] You can add extra defines here...
  303. //[/EndFile]