Audio plugin host https://kx.studio/carla
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.

juce_MPESynthesiserVoice.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Represents an MPE voice that an MPESynthesiser can use to play a sound.
  22. A voice plays a single sound at a time, and a synthesiser holds an array of
  23. voices so that it can play polyphonically.
  24. @see MPESynthesiser, MPENote
  25. */
  26. class JUCE_API MPESynthesiserVoice
  27. {
  28. public:
  29. //==============================================================================
  30. /** Constructor. */
  31. MPESynthesiserVoice();
  32. /** Destructor. */
  33. virtual ~MPESynthesiserVoice();
  34. /** Returns the MPENote that this voice is currently playing.
  35. Returns an invalid MPENote if no note is playing
  36. (you can check this using MPENote::isValid() or MPEVoice::isActive()).
  37. */
  38. MPENote getCurrentlyPlayingNote() const noexcept { return currentlyPlayingNote; }
  39. /** Returns true if the voice is currently playing the given MPENote
  40. (as identified by the note's initial note number and MIDI channel).
  41. */
  42. bool isCurrentlyPlayingNote (MPENote note) const noexcept;
  43. /** Returns true if this voice is currently busy playing a sound.
  44. By default this just checks whether getCurrentlyPlayingNote()
  45. returns a valid MPE note, but can be overridden for more advanced checking.
  46. */
  47. virtual bool isActive() const { return currentlyPlayingNote.isValid(); }
  48. /** Returns true if a voice is sounding in its release phase. **/
  49. bool isPlayingButReleased() const noexcept;
  50. /** Called by the MPESynthesiser to let the voice know that a new note has started on it.
  51. This will be called during the rendering callback, so must be fast and thread-safe.
  52. */
  53. virtual void noteStarted() = 0;
  54. /** Called by the MPESynthesiser to let the voice know that its currently playing note has stopped.
  55. This will be called during the rendering callback, so must be fast and thread-safe.
  56. If allowTailOff is false or the voice doesn't want to tail-off, then it must stop all
  57. sound immediately, and must call clearCurrentNote() to reset the state of this voice
  58. and allow the synth to reassign it another sound.
  59. If allowTailOff is true and the voice decides to do a tail-off, then it's allowed to
  60. begin fading out its sound, and it can stop playing until it's finished. As soon as it
  61. finishes playing (during the rendering callback), it must make sure that it calls
  62. clearCurrentNote().
  63. */
  64. virtual void noteStopped (bool allowTailOff) = 0;
  65. /** Called by the MPESynthesiser to let the voice know that its currently playing note
  66. has changed its pressure value.
  67. This will be called during the rendering callback, so must be fast and thread-safe.
  68. */
  69. virtual void notePressureChanged() = 0;
  70. /** Called by the MPESynthesiser to let the voice know that its currently playing note
  71. has changed its pitchbend value.
  72. This will be called during the rendering callback, so must be fast and thread-safe.
  73. Note: You can call currentlyPlayingNote.getFrequencyInHertz() to find out the effective frequency
  74. of the note, as a sum of the initial note number, the per-note pitchbend and the master pitchbend.
  75. */
  76. virtual void notePitchbendChanged() = 0;
  77. /** Called by the MPESynthesiser to let the voice know that its currently playing note
  78. has changed its timbre value.
  79. This will be called during the rendering callback, so must be fast and thread-safe.
  80. */
  81. virtual void noteTimbreChanged() = 0;
  82. /** Called by the MPESynthesiser to let the voice know that its currently playing note
  83. has changed its key state.
  84. This typically happens when a sustain or sostenuto pedal is pressed or released (on
  85. an MPE channel relevant for this note), or if the note key is lifted while the sustained
  86. or sostenuto pedal is still held down.
  87. This will be called during the rendering callback, so must be fast and thread-safe.
  88. */
  89. virtual void noteKeyStateChanged() = 0;
  90. /** Renders the next block of data for this voice.
  91. The output audio data must be added to the current contents of the buffer provided.
  92. Only the region of the buffer between startSample and (startSample + numSamples)
  93. should be altered by this method.
  94. If the voice is currently silent, it should just return without doing anything.
  95. If the sound that the voice is playing finishes during the course of this rendered
  96. block, it must call clearCurrentNote(), to tell the synthesiser that it has finished.
  97. The size of the blocks that are rendered can change each time it is called, and may
  98. involve rendering as little as 1 sample at a time. In between rendering callbacks,
  99. the voice's methods will be called to tell it about note and controller events.
  100. */
  101. virtual void renderNextBlock (AudioBuffer<float>& outputBuffer,
  102. int startSample,
  103. int numSamples) = 0;
  104. /** Renders the next block of 64-bit data for this voice.
  105. Support for 64-bit audio is optional. You can choose to not override this method if
  106. you don't need it (the default implementation simply does nothing).
  107. */
  108. virtual void renderNextBlock (AudioBuffer<double>& /*outputBuffer*/,
  109. int /*startSample*/,
  110. int /*numSamples*/) {}
  111. /** Changes the voice's reference sample rate.
  112. The rate is set so that subclasses know the output rate and can set their pitch
  113. accordingly.
  114. This method is called by the synth, and subclasses can access the current rate with
  115. the currentSampleRate member.
  116. */
  117. virtual void setCurrentSampleRate (double newRate) { currentSampleRate = newRate; }
  118. /** Returns the current target sample rate at which rendering is being done.
  119. Subclasses may need to know this so that they can pitch things correctly.
  120. */
  121. double getSampleRate() const noexcept { return currentSampleRate; }
  122. /** Returns true if this voice started playing its current note before the other voice did. */
  123. bool wasStartedBefore (const MPESynthesiserVoice& other) const noexcept;
  124. protected:
  125. //==============================================================================
  126. /** Resets the state of this voice after a sound has finished playing.
  127. The subclass must call this when it finishes playing a note and becomes available
  128. to play new ones.
  129. It must either call it in the stopNote() method, or if the voice is tailing off,
  130. then it should call it later during the renderNextBlock method, as soon as it
  131. finishes its tail-off.
  132. It can also be called at any time during the render callback if the sound happens
  133. to have finished, e.g. if it's playing a sample and the sample finishes.
  134. */
  135. void clearCurrentNote() noexcept;
  136. //==============================================================================
  137. double currentSampleRate;
  138. MPENote currentlyPlayingNote;
  139. private:
  140. //==============================================================================
  141. friend class MPESynthesiser;
  142. uint32 noteStartTime;
  143. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MPESynthesiserVoice)
  144. };
  145. } // namespace juce