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_MPESynthesiserBase.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. MPESynthesiserBase::MPESynthesiserBase()
  20. : instrument (defaultInstrument)
  21. {
  22. instrument.addListener (this);
  23. }
  24. MPESynthesiserBase::MPESynthesiserBase (MPEInstrument& inst)
  25. : instrument (inst)
  26. {
  27. instrument.addListener (this);
  28. }
  29. //==============================================================================
  30. MPEZoneLayout MPESynthesiserBase::getZoneLayout() const noexcept
  31. {
  32. return instrument.getZoneLayout();
  33. }
  34. void MPESynthesiserBase::setZoneLayout (MPEZoneLayout newLayout)
  35. {
  36. instrument.setZoneLayout (newLayout);
  37. }
  38. //==============================================================================
  39. void MPESynthesiserBase::enableLegacyMode (int pitchbendRange, Range<int> channelRange)
  40. {
  41. instrument.enableLegacyMode (pitchbendRange, channelRange);
  42. }
  43. bool MPESynthesiserBase::isLegacyModeEnabled() const noexcept
  44. {
  45. return instrument.isLegacyModeEnabled();
  46. }
  47. Range<int> MPESynthesiserBase::getLegacyModeChannelRange() const noexcept
  48. {
  49. return instrument.getLegacyModeChannelRange();
  50. }
  51. void MPESynthesiserBase::setLegacyModeChannelRange (Range<int> channelRange)
  52. {
  53. instrument.setLegacyModeChannelRange (channelRange);
  54. }
  55. int MPESynthesiserBase::getLegacyModePitchbendRange() const noexcept
  56. {
  57. return instrument.getLegacyModePitchbendRange();
  58. }
  59. void MPESynthesiserBase::setLegacyModePitchbendRange (int pitchbendRange)
  60. {
  61. instrument.setLegacyModePitchbendRange (pitchbendRange);
  62. }
  63. //==============================================================================
  64. void MPESynthesiserBase::setPressureTrackingMode (TrackingMode modeToUse)
  65. {
  66. instrument.setPressureTrackingMode (modeToUse);
  67. }
  68. void MPESynthesiserBase::setPitchbendTrackingMode (TrackingMode modeToUse)
  69. {
  70. instrument.setPitchbendTrackingMode (modeToUse);
  71. }
  72. void MPESynthesiserBase::setTimbreTrackingMode (TrackingMode modeToUse)
  73. {
  74. instrument.setTimbreTrackingMode (modeToUse);
  75. }
  76. //==============================================================================
  77. void MPESynthesiserBase::handleMidiEvent (const MidiMessage& m)
  78. {
  79. instrument.processNextMidiEvent (m);
  80. }
  81. //==============================================================================
  82. template <typename floatType>
  83. void MPESynthesiserBase::renderNextBlock (AudioBuffer<floatType>& outputAudio,
  84. const MidiBuffer& inputMidi,
  85. int startSample,
  86. int numSamples)
  87. {
  88. // you must set the sample rate before using this!
  89. jassert (sampleRate != 0);
  90. const ScopedLock sl (noteStateLock);
  91. auto prevSample = startSample;
  92. const auto endSample = startSample + numSamples;
  93. for (auto it = inputMidi.findNextSamplePosition (startSample); it != inputMidi.cend(); ++it)
  94. {
  95. const auto metadata = *it;
  96. if (metadata.samplePosition >= endSample)
  97. break;
  98. const auto smallBlockAllowed = (prevSample == startSample && ! subBlockSubdivisionIsStrict);
  99. const auto thisBlockSize = smallBlockAllowed ? 1 : minimumSubBlockSize;
  100. if (metadata.samplePosition >= prevSample + thisBlockSize)
  101. {
  102. renderNextSubBlock (outputAudio, prevSample, metadata.samplePosition - prevSample);
  103. prevSample = metadata.samplePosition;
  104. }
  105. handleMidiEvent (metadata.getMessage());
  106. }
  107. if (prevSample < endSample)
  108. renderNextSubBlock (outputAudio, prevSample, endSample - prevSample);
  109. }
  110. // explicit instantiation for supported float types:
  111. template void MPESynthesiserBase::renderNextBlock<float> (AudioBuffer<float>&, const MidiBuffer&, int, int);
  112. template void MPESynthesiserBase::renderNextBlock<double> (AudioBuffer<double>&, const MidiBuffer&, int, int);
  113. //==============================================================================
  114. void MPESynthesiserBase::setCurrentPlaybackSampleRate (const double newRate)
  115. {
  116. if (sampleRate != newRate)
  117. {
  118. const ScopedLock sl (noteStateLock);
  119. instrument.releaseAllNotes();
  120. sampleRate = newRate;
  121. }
  122. }
  123. //==============================================================================
  124. void MPESynthesiserBase::setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict) noexcept
  125. {
  126. jassert (numSamples > 0); // it wouldn't make much sense for this to be less than 1
  127. minimumSubBlockSize = numSamples;
  128. subBlockSubdivisionIsStrict = shouldBeStrict;
  129. }
  130. #if JUCE_UNIT_TESTS
  131. namespace
  132. {
  133. class MpeSynthesiserBaseTests : public UnitTest
  134. {
  135. enum class CallbackKind { process, midi };
  136. struct StartAndLength
  137. {
  138. StartAndLength (int s, int l) : start (s), length (l) {}
  139. int start = 0;
  140. int length = 0;
  141. std::tuple<const int&, const int&> tie() const noexcept { return std::tie (start, length); }
  142. bool operator== (const StartAndLength& other) const noexcept { return tie() == other.tie(); }
  143. bool operator!= (const StartAndLength& other) const noexcept { return tie() != other.tie(); }
  144. bool operator< (const StartAndLength& other) const noexcept { return tie() < other.tie(); }
  145. };
  146. struct Events
  147. {
  148. std::vector<StartAndLength> blocks;
  149. std::vector<MidiMessage> messages;
  150. std::vector<CallbackKind> order;
  151. };
  152. class MockSynthesiser : public MPESynthesiserBase
  153. {
  154. public:
  155. Events events;
  156. void handleMidiEvent (const MidiMessage& m) override
  157. {
  158. events.messages.emplace_back (m);
  159. events.order.emplace_back (CallbackKind::midi);
  160. }
  161. private:
  162. using MPESynthesiserBase::renderNextSubBlock;
  163. void renderNextSubBlock (AudioBuffer<float>&,
  164. int startSample,
  165. int numSamples) override
  166. {
  167. events.blocks.push_back ({ startSample, numSamples });
  168. events.order.emplace_back (CallbackKind::process);
  169. }
  170. };
  171. static MidiBuffer makeTestBuffer (const int bufferLength)
  172. {
  173. MidiBuffer result;
  174. for (int i = 0; i != bufferLength; ++i)
  175. result.addEvent ({}, i);
  176. return result;
  177. }
  178. public:
  179. MpeSynthesiserBaseTests()
  180. : UnitTest ("MPE Synthesiser Base", UnitTestCategories::midi) {}
  181. void runTest() override
  182. {
  183. const auto sumBlockLengths = [] (const std::vector<StartAndLength>& b)
  184. {
  185. const auto addBlock = [] (int acc, const StartAndLength& info) { return acc + info.length; };
  186. return std::accumulate (b.begin(), b.end(), 0, addBlock);
  187. };
  188. beginTest ("Rendering sparse subblocks works");
  189. {
  190. const int blockSize = 512;
  191. const auto midi = [&] { MidiBuffer b; b.addEvent ({}, blockSize / 2); return b; }();
  192. AudioBuffer<float> audio (1, blockSize);
  193. const auto processEvents = [&] (int start, int length)
  194. {
  195. MockSynthesiser synth;
  196. synth.setMinimumRenderingSubdivisionSize (1, false);
  197. synth.setCurrentPlaybackSampleRate (44100);
  198. synth.renderNextBlock (audio, midi, start, length);
  199. return synth.events;
  200. };
  201. {
  202. const auto e = processEvents (0, blockSize);
  203. expect (e.blocks.size() == 2);
  204. expect (e.messages.size() == 1);
  205. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  206. expect (sumBlockLengths (e.blocks) == blockSize);
  207. expect (e.order == std::vector<CallbackKind> { CallbackKind::process,
  208. CallbackKind::midi,
  209. CallbackKind::process });
  210. }
  211. }
  212. beginTest ("Rendering subblocks processes only contained midi events");
  213. {
  214. const int blockSize = 512;
  215. const auto midi = makeTestBuffer (blockSize);
  216. AudioBuffer<float> audio (1, blockSize);
  217. const auto processEvents = [&] (int start, int length)
  218. {
  219. MockSynthesiser synth;
  220. synth.setMinimumRenderingSubdivisionSize (1, false);
  221. synth.setCurrentPlaybackSampleRate (44100);
  222. synth.renderNextBlock (audio, midi, start, length);
  223. return synth.events;
  224. };
  225. {
  226. const int subBlockLength = 0;
  227. const auto e = processEvents (0, subBlockLength);
  228. expect (e.blocks.size() == 0);
  229. expect (e.messages.size() == 0);
  230. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  231. expect (sumBlockLengths (e.blocks) == subBlockLength);
  232. }
  233. {
  234. const int subBlockLength = 0;
  235. const auto e = processEvents (1, subBlockLength);
  236. expect (e.blocks.size() == 0);
  237. expect (e.messages.size() == 0);
  238. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  239. expect (sumBlockLengths (e.blocks) == subBlockLength);
  240. }
  241. {
  242. const int subBlockLength = 1;
  243. const auto e = processEvents (1, subBlockLength);
  244. expect (e.blocks.size() == 1);
  245. expect (e.messages.size() == 1);
  246. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  247. expect (sumBlockLengths (e.blocks) == subBlockLength);
  248. expect (e.order == std::vector<CallbackKind> { CallbackKind::midi,
  249. CallbackKind::process });
  250. }
  251. {
  252. const auto e = processEvents (0, blockSize);
  253. expect (e.blocks.size() == blockSize);
  254. expect (e.messages.size() == blockSize);
  255. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  256. expect (sumBlockLengths (e.blocks) == blockSize);
  257. expect (e.order.front() == CallbackKind::midi);
  258. }
  259. }
  260. beginTest ("Subblocks respect their minimum size");
  261. {
  262. const int blockSize = 512;
  263. const auto midi = makeTestBuffer (blockSize);
  264. AudioBuffer<float> audio (1, blockSize);
  265. const auto blockLengthsAreValid = [] (const std::vector<StartAndLength>& info, int minLength, bool strict)
  266. {
  267. if (info.size() <= 1)
  268. return true;
  269. const auto lengthIsValid = [&] (const StartAndLength& s) { return minLength <= s.length; };
  270. const auto begin = strict ? info.begin() : std::next (info.begin());
  271. // The final block is allowed to be shorter than the minLength
  272. return std::all_of (begin, std::prev (info.end()), lengthIsValid);
  273. };
  274. for (auto strict : { false, true })
  275. {
  276. for (auto subblockSize : { 1, 16, 32, 64, 1024 })
  277. {
  278. MockSynthesiser synth;
  279. synth.setMinimumRenderingSubdivisionSize (subblockSize, strict);
  280. synth.setCurrentPlaybackSampleRate (44100);
  281. synth.renderNextBlock (audio, midi, 0, blockSize);
  282. const auto& e = synth.events;
  283. expectWithinAbsoluteError (float (e.blocks.size()),
  284. std::ceil ((float) blockSize / (float) subblockSize),
  285. 1.0f);
  286. expect (e.messages.size() == blockSize);
  287. expect (std::is_sorted (e.blocks.begin(), e.blocks.end()));
  288. expect (sumBlockLengths (e.blocks) == blockSize);
  289. expect (blockLengthsAreValid (e.blocks, subblockSize, strict));
  290. }
  291. }
  292. {
  293. MockSynthesiser synth;
  294. synth.setMinimumRenderingSubdivisionSize (32, true);
  295. synth.setCurrentPlaybackSampleRate (44100);
  296. synth.renderNextBlock (audio, MidiBuffer{}, 0, 16);
  297. expect (synth.events.blocks == std::vector<StartAndLength> { { 0, 16 } });
  298. expect (synth.events.order == std::vector<CallbackKind> { CallbackKind::process });
  299. expect (synth.events.messages.empty());
  300. }
  301. }
  302. }
  303. };
  304. MpeSynthesiserBaseTests mpeSynthesiserBaseTests;
  305. }
  306. #endif
  307. } // namespace juce