JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Synthesiser.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
23namespace juce
24{
25
26//==============================================================================
42{
43protected:
44 //==============================================================================
46
47public:
49 ~SynthesiserSound() override;
50
51 //==============================================================================
57 virtual bool appliesToNote (int midiNoteNumber) = 0;
58
64 virtual bool appliesToChannel (int midiChannel) = 0;
65
68
69
70private:
71 //==============================================================================
73};
74
75
76//==============================================================================
87class JUCE_API SynthesiserVoice
88{
89public:
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
249protected:
262 void clearCurrentNote();
263
264
265private:
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
278};
279
280
281//==============================================================================
307class JUCE_API Synthesiser
308{
309public:
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
560protected:
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
622private:
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 mutable CriticalSection stealLock;
631 mutable Array<SynthesiserVoice*> usableVoicesToStealArray;
632
633 template <typename floatType>
634 void processNextBlock (AudioBuffer<floatType>&, const MidiBuffer&, int startSample, int numSamples);
635
637};
638
639} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
A multi-channel buffer containing floating point audio samples.
An arbitrarily large integer class.
Holds a sequence of time-stamped midi events.
Encapsulates a MIDI message.
An array designed for holding objects.
Holds a list of objects derived from ReferenceCountedObject, or which implement basic reference-count...
A base class which provides methods for reference-counting.
Describes one of the sounds that a Synthesiser can play.
virtual bool appliesToNote(int midiNoteNumber)=0
Returns true if this sound should be played when a given midi note is pressed.
virtual bool appliesToChannel(int midiChannel)=0
Returns true if the sound should be triggered by midi events on a given channel.
Represents a voice that a Synthesiser can use to play a SynthesiserSound.
bool isSostenutoPedalDown() const noexcept
Returns true if the sostenuto pedal is currently active for this voice.
double getSampleRate() const noexcept
Returns the current target sample rate at which rendering is being done.
virtual void stopNote(float velocity, bool allowTailOff)=0
Called to stop a note.
bool isSustainPedalDown() const noexcept
Returns true if the sustain pedal is currently active for this voice.
void setSustainPedalDown(bool isNowDown) noexcept
Modifies the sustain pedal flag.
void setSostenutoPedalDown(bool isNowDown) noexcept
Modifies the sostenuto pedal flag.
virtual void renderNextBlock(AudioBuffer< float > &outputBuffer, int startSample, int numSamples)=0
Renders the next block of data for this voice.
bool isKeyDown() const noexcept
Returns true if the key that triggered this voice is still held down.
void setKeyDown(bool isNowDown) noexcept
Allows you to modify the flag indicating that the key that triggered this voice is still held down.
virtual void controllerMoved(int controllerNumber, int newControllerValue)=0
Called to let the voice know that a midi controller has been moved.
int getCurrentlyPlayingNote() const noexcept
Returns the midi note that this voice is currently playing.
virtual void startNote(int midiNoteNumber, float velocity, SynthesiserSound *sound, int currentPitchWheelPosition)=0
Called to start a new note.
virtual bool canPlaySound(SynthesiserSound *)=0
Must return true if this voice object is capable of playing the given sound.
virtual void pitchWheelMoved(int newPitchWheelValue)=0
Called to let the voice know that the pitch wheel has been moved.
bool isPlayingButReleased() const noexcept
Returns true if a voice is sounding in its release phase.
SynthesiserSound::Ptr getCurrentlyPlayingSound() const noexcept
Returns the sound that this voice is currently playing.
Base class for a musical device that can play sounds.
int getNumSounds() const noexcept
Returns the number of sounds that have been added to the synth.
double getSampleRate() const noexcept
Returns the current target sample rate at which rendering is being done.
int getNumVoices() const noexcept
Returns the number of voices that have been added.
bool isNoteStealingEnabled() const noexcept
Returns true if note-stealing is enabled.
SynthesiserSound::Ptr getSound(int index) const noexcept
Returns one of the sounds.
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
unsigned int uint32
A platform-independent 32-bit unsigned integer type.