OpenShot Audio Library | OpenShotAudio  0.3.0
juce_Synthesiser.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
42 {
43 protected:
44  //==============================================================================
46 
47 public:
49  ~SynthesiserSound() override;
50 
51  //==============================================================================
57  virtual bool appliesToNote (int midiNoteNumber) = 0;
58 
64  virtual bool appliesToChannel (int midiChannel) = 0;
65 
68 
69 
70 private:
71  //==============================================================================
72  JUCE_LEAK_DETECTOR (SynthesiserSound)
73 };
74 
75 
76 //==============================================================================
87 class JUCE_API SynthesiserVoice
88 {
89 public:
90  //==============================================================================
93 
95  virtual ~SynthesiserVoice();
96 
97  //==============================================================================
101  int getCurrentlyPlayingNote() const noexcept { return currentlyPlayingNote; }
102 
106  SynthesiserSound::Ptr getCurrentlyPlayingSound() const noexcept { return currentlyPlayingSound; }
107 
117  virtual bool canPlaySound (SynthesiserSound*) = 0;
118 
122  virtual void startNote (int midiNoteNumber,
123  float velocity,
124  SynthesiserSound* sound,
125  int currentPitchWheelPosition) = 0;
126 
142  virtual void stopNote (float velocity, bool allowTailOff) = 0;
143 
148  virtual bool isVoiceActive() const;
149 
153  virtual void pitchWheelMoved (int newPitchWheelValue) = 0;
154 
158  virtual void controllerMoved (int controllerNumber, int newControllerValue) = 0;
159 
163  virtual void aftertouchChanged (int newAftertouchValue);
164 
168  virtual void channelPressureChanged (int newChannelPressureValue);
169 
170  //==============================================================================
186  virtual void renderNextBlock (AudioBuffer<float>& outputBuffer,
187  int startSample,
188  int numSamples) = 0;
189 
191  virtual void renderNextBlock (AudioBuffer<double>& outputBuffer,
192  int startSample,
193  int numSamples);
194 
203  virtual void setCurrentPlaybackSampleRate (double newRate);
204 
210  virtual bool isPlayingChannel (int midiChannel) const;
211 
215  double getSampleRate() const noexcept { return currentSampleRate; }
216 
221  bool isKeyDown() const noexcept { return keyIsDown; }
222 
226  void setKeyDown (bool isNowDown) noexcept { keyIsDown = isNowDown; }
227 
229  bool isSustainPedalDown() const noexcept { return sustainPedalDown; }
230 
232  void setSustainPedalDown (bool isNowDown) noexcept { sustainPedalDown = isNowDown; }
233 
235  bool isSostenutoPedalDown() const noexcept { return sostenutoPedalDown; }
236 
238  void setSostenutoPedalDown (bool isNowDown) noexcept { sostenutoPedalDown = isNowDown; }
239 
241  bool isPlayingButReleased() const noexcept
242  {
243  return isVoiceActive() && ! (isKeyDown() || isSostenutoPedalDown() || isSustainPedalDown());
244  }
245 
247  bool wasStartedBefore (const SynthesiserVoice& other) const noexcept;
248 
249 protected:
262  void clearCurrentNote();
263 
264 
265 private:
266  //==============================================================================
267  friend class Synthesiser;
268 
269  double currentSampleRate = 44100.0;
270  int currentlyPlayingNote = -1, currentPlayingMidiChannel = 0;
271  uint32 noteOnTime = 0;
272  SynthesiserSound::Ptr currentlyPlayingSound;
273  bool keyIsDown = false, sustainPedalDown = false, sostenutoPedalDown = false;
274 
275  AudioBuffer<float> tempBuffer;
276 
277  JUCE_LEAK_DETECTOR (SynthesiserVoice)
278 };
279 
280 
281 //==============================================================================
307 class JUCE_API Synthesiser
308 {
309 public:
310  //==============================================================================
314  Synthesiser();
315 
317  virtual ~Synthesiser();
318 
319  //==============================================================================
321  void clearVoices();
322 
324  int getNumVoices() const noexcept { return voices.size(); }
325 
327  SynthesiserVoice* getVoice (int index) const;
328 
337  SynthesiserVoice* addVoice (SynthesiserVoice* newVoice);
338 
340  void removeVoice (int index);
341 
342  //==============================================================================
344  void clearSounds();
345 
347  int getNumSounds() const noexcept { return sounds.size(); }
348 
350  SynthesiserSound::Ptr getSound (int index) const noexcept { return sounds[index]; }
351 
357  SynthesiserSound* addSound (const SynthesiserSound::Ptr& newSound);
358 
360  void removeSound (int index);
361 
362  //==============================================================================
369  void setNoteStealingEnabled (bool shouldStealNotes);
370 
374  bool isNoteStealingEnabled() const noexcept { return shouldStealNotes; }
375 
376  //==============================================================================
390  virtual void noteOn (int midiChannel,
391  int midiNoteNumber,
392  float velocity);
393 
406  virtual void noteOff (int midiChannel,
407  int midiNoteNumber,
408  float velocity,
409  bool allowTailOff);
410 
425  virtual void allNotesOff (int midiChannel,
426  bool allowTailOff);
427 
439  virtual void handlePitchWheel (int midiChannel,
440  int wheelValue);
441 
454  virtual void handleController (int midiChannel,
455  int controllerNumber,
456  int controllerValue);
457 
471  virtual void handleAftertouch (int midiChannel, int midiNoteNumber, int aftertouchValue);
472 
485  virtual void handleChannelPressure (int midiChannel, int channelPressureValue);
486 
488  virtual void handleSustainPedal (int midiChannel, bool isDown);
489 
491  virtual void handleSostenutoPedal (int midiChannel, bool isDown);
492 
494  virtual void handleSoftPedal (int midiChannel, bool isDown);
495 
500  virtual void handleProgramChange (int midiChannel,
501  int programNumber);
502 
503  //==============================================================================
509  virtual void setCurrentPlaybackSampleRate (double sampleRate);
510 
523  void renderNextBlock (AudioBuffer<float>& outputAudio,
524  const MidiBuffer& inputMidi,
525  int startSample,
526  int numSamples);
527 
528  void renderNextBlock (AudioBuffer<double>& outputAudio,
529  const MidiBuffer& inputMidi,
530  int startSample,
531  int numSamples);
532 
536  double getSampleRate() const noexcept { return sampleRate; }
537 
558  void setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict = false) noexcept;
559 
560 protected:
561  //==============================================================================
564 
567 
569  int lastPitchWheelValues [16];
570 
575  virtual void renderVoices (AudioBuffer<float>& outputAudio,
576  int startSample, int numSamples);
577  virtual void renderVoices (AudioBuffer<double>& outputAudio,
578  int startSample, int numSamples);
579 
588  virtual SynthesiserVoice* findFreeVoice (SynthesiserSound* soundToPlay,
589  int midiChannel,
590  int midiNoteNumber,
591  bool stealIfNoneAvailable) const;
592 
598  virtual SynthesiserVoice* findVoiceToSteal (SynthesiserSound* soundToPlay,
599  int midiChannel,
600  int midiNoteNumber) const;
601 
606  void startVoice (SynthesiserVoice* voice,
607  SynthesiserSound* sound,
608  int midiChannel,
609  int midiNoteNumber,
610  float velocity);
611 
617  void stopVoice (SynthesiserVoice*, float velocity, bool allowTailOff);
618 
620  virtual void handleMidiEvent (const MidiMessage&);
621 
622 private:
623  //==============================================================================
624  double sampleRate = 0;
625  uint32 lastNoteOnCounter = 0;
626  int minimumSubBlockSize = 32;
627  bool subBlockSubdivisionIsStrict = false;
628  bool shouldStealNotes = true;
629  BigInteger sustainPedalsDown;
630 
631  template <typename floatType>
632  void processNextBlock (AudioBuffer<floatType>&, const MidiBuffer&, int startSample, int numSamples);
633 
634  #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
635  // Note the new parameters for these methods.
636  virtual int findFreeVoice (const bool) const { return 0; }
637  virtual int noteOff (int, int, int) { return 0; }
638  virtual int findFreeVoice (SynthesiserSound*, const bool) { return 0; }
639  virtual int findVoiceToSteal (SynthesiserSound*) const { return 0; }
640  #endif
641 
642  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Synthesiser)
643 };
644 
645 } // namespace juce
virtual bool appliesToNote(int midiNoteNumber)=0
virtual bool appliesToChannel(int midiChannel)=0
bool isSostenutoPedalDown() const noexcept
double getSampleRate() const noexcept
virtual void stopNote(float velocity, bool allowTailOff)=0
bool isSustainPedalDown() const noexcept
void setSustainPedalDown(bool isNowDown) noexcept
void setSostenutoPedalDown(bool isNowDown) noexcept
virtual void renderNextBlock(AudioBuffer< float > &outputBuffer, int startSample, int numSamples)=0
bool isKeyDown() const noexcept
void setKeyDown(bool isNowDown) noexcept
virtual void controllerMoved(int controllerNumber, int newControllerValue)=0
int getCurrentlyPlayingNote() const noexcept
virtual void startNote(int midiNoteNumber, float velocity, SynthesiserSound *sound, int currentPitchWheelPosition)=0
virtual bool canPlaySound(SynthesiserSound *)=0
virtual void pitchWheelMoved(int newPitchWheelValue)=0
bool isPlayingButReleased() const noexcept
SynthesiserSound::Ptr getCurrentlyPlayingSound() const noexcept
int getNumSounds() const noexcept
double getSampleRate() const noexcept
int getNumVoices() const noexcept
bool isNoteStealingEnabled() const noexcept
SynthesiserSound::Ptr getSound(int index) const noexcept