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_MPESynthesiser.cpp 11KB

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