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.

363 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MPESynthesiser::MPESynthesiser()
  24. {
  25. }
  26. MPESynthesiser::MPESynthesiser (MPEInstrument* instrument) : MPESynthesiserBase (instrument)
  27. {
  28. }
  29. MPESynthesiser::~MPESynthesiser()
  30. {
  31. }
  32. //==============================================================================
  33. void MPESynthesiser::startVoice (MPESynthesiserVoice* voice, MPENote noteToStart)
  34. {
  35. jassert (voice != nullptr);
  36. voice->currentlyPlayingNote = noteToStart;
  37. voice->noteStarted();
  38. }
  39. void MPESynthesiser::stopVoice (MPESynthesiserVoice* voice, MPENote noteToStop, bool allowTailOff)
  40. {
  41. jassert (voice != nullptr);
  42. voice->currentlyPlayingNote = noteToStop;
  43. voice->noteStopped (allowTailOff);
  44. }
  45. //==============================================================================
  46. void MPESynthesiser::noteAdded (MPENote newNote)
  47. {
  48. const ScopedLock sl (voicesLock);
  49. if (MPESynthesiserVoice* voice = findFreeVoice (newNote, shouldStealVoices))
  50. startVoice (voice, newNote);
  51. }
  52. void MPESynthesiser::notePressureChanged (MPENote changedNote)
  53. {
  54. const ScopedLock sl (voicesLock);
  55. for (int i = 0; i < voices.size(); ++i)
  56. {
  57. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  58. if (voice->isCurrentlyPlayingNote (changedNote))
  59. {
  60. voice->currentlyPlayingNote = changedNote;
  61. voice->notePressureChanged();
  62. }
  63. }
  64. }
  65. void MPESynthesiser::notePitchbendChanged (MPENote changedNote)
  66. {
  67. const ScopedLock sl (voicesLock);
  68. for (int i = 0; i < voices.size(); ++i)
  69. {
  70. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  71. if (voice->isCurrentlyPlayingNote (changedNote))
  72. {
  73. voice->currentlyPlayingNote = changedNote;
  74. voice->notePitchbendChanged();
  75. }
  76. }
  77. }
  78. void MPESynthesiser::noteTimbreChanged (MPENote changedNote)
  79. {
  80. const ScopedLock sl (voicesLock);
  81. for (int i = 0; i < voices.size(); ++i)
  82. {
  83. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  84. if (voice->isCurrentlyPlayingNote (changedNote))
  85. {
  86. voice->currentlyPlayingNote = changedNote;
  87. voice->noteTimbreChanged();
  88. }
  89. }
  90. }
  91. void MPESynthesiser::noteKeyStateChanged (MPENote changedNote)
  92. {
  93. const ScopedLock sl (voicesLock);
  94. for (int i = 0; i < voices.size(); ++i)
  95. {
  96. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  97. if (voice->isCurrentlyPlayingNote (changedNote))
  98. {
  99. voice->currentlyPlayingNote = changedNote;
  100. voice->noteKeyStateChanged();
  101. }
  102. }
  103. }
  104. void MPESynthesiser::noteReleased (MPENote finishedNote)
  105. {
  106. const ScopedLock sl (voicesLock);
  107. for (int i = voices.size(); --i >= 0;)
  108. {
  109. MPESynthesiserVoice* const voice = voices.getUnchecked (i);
  110. if (voice->isCurrentlyPlayingNote(finishedNote))
  111. stopVoice (voice, finishedNote, true);
  112. }
  113. }
  114. void MPESynthesiser::setCurrentPlaybackSampleRate (const double newRate)
  115. {
  116. MPESynthesiserBase::setCurrentPlaybackSampleRate (newRate);
  117. const ScopedLock sl (voicesLock);
  118. turnOffAllVoices (false);
  119. for (int i = voices.size(); --i >= 0;)
  120. voices.getUnchecked (i)->setCurrentSampleRate (newRate);
  121. }
  122. void MPESynthesiser::handleMidiEvent (const MidiMessage& m)
  123. {
  124. if (m.isController())
  125. handleController (m.getChannel(), m.getControllerNumber(), m.getControllerValue());
  126. else if (m.isProgramChange())
  127. handleProgramChange (m.getChannel(), m.getProgramChangeNumber());
  128. MPESynthesiserBase::handleMidiEvent (m);
  129. }
  130. MPESynthesiserVoice* MPESynthesiser::findFreeVoice (MPENote noteToFindVoiceFor, bool stealIfNoneAvailable) const
  131. {
  132. const ScopedLock sl (voicesLock);
  133. for (int i = 0; i < voices.size(); ++i)
  134. {
  135. MPESynthesiserVoice* const voice = voices.getUnchecked (i);
  136. if (! voice->isActive())
  137. return voice;
  138. }
  139. if (stealIfNoneAvailable)
  140. return findVoiceToSteal (noteToFindVoiceFor);
  141. return nullptr;
  142. }
  143. struct MPEVoiceAgeSorter
  144. {
  145. static int compareElements (MPESynthesiserVoice* v1, MPESynthesiserVoice* v2) noexcept
  146. {
  147. return v1->wasStartedBefore (*v2) ? -1 : (v2->wasStartedBefore (*v1) ? 1 : 0);
  148. }
  149. };
  150. MPESynthesiserVoice* MPESynthesiser::findVoiceToSteal (MPENote noteToStealVoiceFor) const
  151. {
  152. // This voice-stealing algorithm applies the following heuristics:
  153. // - Re-use the oldest notes first
  154. // - Protect the lowest & topmost notes, even if sustained, but not if they've been released.
  155. // apparently you are trying to render audio without having any voices...
  156. jassert (voices.size() > 0);
  157. // These are the voices we want to protect (ie: only steal if unavoidable)
  158. MPESynthesiserVoice* low = nullptr; // Lowest sounding note, might be sustained, but NOT in release phase
  159. MPESynthesiserVoice* top = nullptr; // Highest sounding note, might be sustained, but NOT in release phase
  160. // this is a list of voices we can steal, sorted by how long they've been running
  161. Array<MPESynthesiserVoice*> usableVoices;
  162. usableVoices.ensureStorageAllocated (voices.size());
  163. for (int i = 0; i < voices.size(); ++i)
  164. {
  165. MPESynthesiserVoice* const voice = voices.getUnchecked (i);
  166. jassert (voice->isActive()); // We wouldn't be here otherwise
  167. MPEVoiceAgeSorter sorter;
  168. usableVoices.addSorted (sorter, voice);
  169. if (! voice->isPlayingButReleased()) // Don't protect released notes
  170. {
  171. const int noteNumber = voice->getCurrentlyPlayingNote().initialNote;
  172. if (low == nullptr || noteNumber < low->getCurrentlyPlayingNote().initialNote)
  173. low = voice;
  174. if (top == nullptr || noteNumber > top->getCurrentlyPlayingNote().initialNote)
  175. top = voice;
  176. }
  177. }
  178. // Eliminate pathological cases (ie: only 1 note playing): we always give precedence to the lowest note(s)
  179. if (top == low)
  180. top = nullptr;
  181. const int numUsableVoices = usableVoices.size();
  182. // If we want to re-use the voice to trigger a new note,
  183. // then The oldest note that's playing the same note number is ideal.
  184. if (noteToStealVoiceFor.isValid())
  185. {
  186. for (int i = 0; i < numUsableVoices; ++i)
  187. {
  188. MPESynthesiserVoice* const voice = usableVoices.getUnchecked (i);
  189. if (voice->getCurrentlyPlayingNote().initialNote == noteToStealVoiceFor.initialNote)
  190. return voice;
  191. }
  192. }
  193. // Oldest voice that has been released (no finger on it and not held by sustain pedal)
  194. for (int i = 0; i < numUsableVoices; ++i)
  195. {
  196. MPESynthesiserVoice* const voice = usableVoices.getUnchecked (i);
  197. if (voice != low && voice != top && voice->isPlayingButReleased())
  198. return voice;
  199. }
  200. // Oldest voice that doesn't have a finger on it:
  201. for (int i = 0; i < numUsableVoices; ++i)
  202. {
  203. MPESynthesiserVoice* const voice = usableVoices.getUnchecked (i);
  204. if (voice != low && voice != top
  205. && voice->getCurrentlyPlayingNote().keyState != MPENote::keyDown
  206. && voice->getCurrentlyPlayingNote().keyState != MPENote::keyDownAndSustained)
  207. return voice;
  208. }
  209. // Oldest voice that isn't protected
  210. for (int i = 0; i < numUsableVoices; ++i)
  211. {
  212. MPESynthesiserVoice* const voice = usableVoices.getUnchecked (i);
  213. if (voice != low && voice != top)
  214. return voice;
  215. }
  216. // We've only got "protected" voices now: lowest note takes priority
  217. jassert (low != nullptr);
  218. // Duophonic synth: give priority to the bass note:
  219. if (top != nullptr)
  220. return top;
  221. return low;
  222. }
  223. //==============================================================================
  224. void MPESynthesiser::addVoice (MPESynthesiserVoice* const newVoice)
  225. {
  226. const ScopedLock sl (voicesLock);
  227. newVoice->setCurrentSampleRate (getSampleRate());
  228. voices.add (newVoice);
  229. }
  230. void MPESynthesiser::clearVoices()
  231. {
  232. const ScopedLock sl (voicesLock);
  233. voices.clear();
  234. }
  235. MPESynthesiserVoice* MPESynthesiser::getVoice (const int index) const
  236. {
  237. const ScopedLock sl (voicesLock);
  238. return voices [index];
  239. }
  240. void MPESynthesiser::removeVoice (const int index)
  241. {
  242. const ScopedLock sl (voicesLock);
  243. voices.remove (index);
  244. }
  245. void MPESynthesiser::reduceNumVoices (const int newNumVoices)
  246. {
  247. // we can't possibly get to a negative number of voices...
  248. jassert (newNumVoices >= 0);
  249. const ScopedLock sl (voicesLock);
  250. while (voices.size() > newNumVoices)
  251. {
  252. if (MPESynthesiserVoice* voice = findFreeVoice (MPENote(), true))
  253. voices.removeObject (voice);
  254. else
  255. voices.remove (0); // if there's no voice to steal, kill the oldest voice
  256. }
  257. }
  258. void MPESynthesiser::turnOffAllVoices (bool allowTailOff)
  259. {
  260. // first turn off all voices (it's more efficient to do this immediately
  261. // rather than to go through the MPEInstrument for this).
  262. for (int i = voices.size(); --i >= 0;)
  263. voices.getUnchecked (i)->noteStopped (allowTailOff);
  264. // finally make sure the MPE Instrument also doesn't have any notes anymore.
  265. instrument->releaseAllNotes();
  266. }
  267. //==============================================================================
  268. void MPESynthesiser::renderNextSubBlock (AudioBuffer<float>& buffer, int startSample, int numSamples)
  269. {
  270. for (int i = voices.size(); --i >= 0;)
  271. {
  272. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  273. if (voice->isActive())
  274. voice->renderNextBlock (buffer, startSample, numSamples);
  275. }
  276. }
  277. void MPESynthesiser::renderNextSubBlock (AudioBuffer<double>& buffer, int startSample, int numSamples)
  278. {
  279. for (int i = voices.size(); --i >= 0;)
  280. {
  281. MPESynthesiserVoice* voice = voices.getUnchecked (i);
  282. if (voice->isActive())
  283. voice->renderNextBlock (buffer, startSample, numSamples);
  284. }
  285. }