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 5.9KB

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