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.

189 lines
6.4KB

  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. MPESynthesiserBase::MPESynthesiserBase()
  24. : instrument (new MPEInstrument),
  25. sampleRate (0),
  26. minimumSubBlockSize (32),
  27. subBlockSubdivisionIsStrict (false)
  28. {
  29. instrument->addListener (this);
  30. }
  31. MPESynthesiserBase::MPESynthesiserBase (MPEInstrument* inst)
  32. : instrument (inst),
  33. sampleRate (0),
  34. minimumSubBlockSize (32)
  35. {
  36. jassert (instrument != nullptr);
  37. instrument->addListener (this);
  38. }
  39. //==============================================================================
  40. MPEZoneLayout MPESynthesiserBase::getZoneLayout() const noexcept
  41. {
  42. return instrument->getZoneLayout();
  43. }
  44. void MPESynthesiserBase::setZoneLayout (MPEZoneLayout newLayout)
  45. {
  46. instrument->setZoneLayout (newLayout);
  47. }
  48. //==============================================================================
  49. void MPESynthesiserBase::enableLegacyMode (int pitchbendRange, Range<int> channelRange)
  50. {
  51. instrument->enableLegacyMode (pitchbendRange, channelRange);
  52. }
  53. bool MPESynthesiserBase::isLegacyModeEnabled() const noexcept
  54. {
  55. return instrument->isLegacyModeEnabled();
  56. }
  57. Range<int> MPESynthesiserBase::getLegacyModeChannelRange() const noexcept
  58. {
  59. return instrument->getLegacyModeChannelRange();
  60. }
  61. void MPESynthesiserBase::setLegacyModeChannelRange (Range<int> channelRange)
  62. {
  63. instrument->setLegacyModeChannelRange (channelRange);
  64. }
  65. int MPESynthesiserBase::getLegacyModePitchbendRange() const noexcept
  66. {
  67. return instrument->getLegacyModePitchbendRange();
  68. }
  69. void MPESynthesiserBase::setLegacyModePitchbendRange (int pitchbendRange)
  70. {
  71. instrument->setLegacyModePitchbendRange (pitchbendRange);
  72. }
  73. //==============================================================================
  74. void MPESynthesiserBase::setPressureTrackingMode (TrackingMode modeToUse)
  75. {
  76. instrument->setPressureTrackingMode (modeToUse);
  77. }
  78. void MPESynthesiserBase::setPitchbendTrackingMode (TrackingMode modeToUse)
  79. {
  80. instrument->setPitchbendTrackingMode (modeToUse);
  81. }
  82. void MPESynthesiserBase::setTimbreTrackingMode (TrackingMode modeToUse)
  83. {
  84. instrument->setTimbreTrackingMode (modeToUse);
  85. }
  86. //==============================================================================
  87. void MPESynthesiserBase::handleMidiEvent (const MidiMessage& m)
  88. {
  89. instrument->processNextMidiEvent (m);
  90. }
  91. //==============================================================================
  92. template <typename floatType>
  93. void MPESynthesiserBase::renderNextBlock (AudioBuffer<floatType>& outputAudio,
  94. const MidiBuffer& inputMidi,
  95. int startSample,
  96. int numSamples)
  97. {
  98. // you must set the sample rate before using this!
  99. jassert (sampleRate != 0);
  100. MidiBuffer::Iterator midiIterator (inputMidi);
  101. midiIterator.setNextSamplePosition (startSample);
  102. bool firstEvent = true;
  103. int midiEventPos;
  104. MidiMessage m;
  105. const ScopedLock sl (noteStateLock);
  106. while (numSamples > 0)
  107. {
  108. if (! midiIterator.getNextEvent (m, midiEventPos))
  109. {
  110. renderNextSubBlock (outputAudio, startSample, numSamples);
  111. return;
  112. }
  113. const int samplesToNextMidiMessage = midiEventPos - startSample;
  114. if (samplesToNextMidiMessage >= numSamples)
  115. {
  116. renderNextSubBlock (outputAudio, startSample, numSamples);
  117. handleMidiEvent (m);
  118. break;
  119. }
  120. if (samplesToNextMidiMessage < ((firstEvent && ! subBlockSubdivisionIsStrict) ? 1 : minimumSubBlockSize))
  121. {
  122. handleMidiEvent (m);
  123. continue;
  124. }
  125. firstEvent = false;
  126. renderNextSubBlock (outputAudio, startSample, samplesToNextMidiMessage);
  127. handleMidiEvent (m);
  128. startSample += samplesToNextMidiMessage;
  129. numSamples -= samplesToNextMidiMessage;
  130. }
  131. while (midiIterator.getNextEvent (m, midiEventPos))
  132. handleMidiEvent (m);
  133. }
  134. // explicit instantiation for supported float types:
  135. template void MPESynthesiserBase::renderNextBlock<float> (AudioBuffer<float>&, const MidiBuffer&, int, int);
  136. template void MPESynthesiserBase::renderNextBlock<double> (AudioBuffer<double>&, const MidiBuffer&, int, int);
  137. //==============================================================================
  138. void MPESynthesiserBase::setCurrentPlaybackSampleRate (const double newRate)
  139. {
  140. if (sampleRate != newRate)
  141. {
  142. const ScopedLock sl (noteStateLock);
  143. instrument->releaseAllNotes();
  144. sampleRate = newRate;
  145. }
  146. }
  147. //==============================================================================
  148. void MPESynthesiserBase::setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict) noexcept
  149. {
  150. jassert (numSamples > 0); // it wouldn't make much sense for this to be less than 1
  151. minimumSubBlockSize = numSamples;
  152. subBlockSubdivisionIsStrict = shouldBeStrict;
  153. }