PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/juce_audio_basics/mpe/juce_MPESynthesiserBase.cpp

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