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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. {
  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. auto midiIterator = inputMidi.findNextSamplePosition (startSample);
  92. bool firstEvent = true;
  93. const ScopedLock sl (noteStateLock);
  94. for (; numSamples > 0; ++midiIterator)
  95. {
  96. if (midiIterator == inputMidi.cend())
  97. {
  98. renderNextSubBlock (outputAudio, startSample, numSamples);
  99. return;
  100. }
  101. const auto metadata = *midiIterator;
  102. auto samplesToNextMidiMessage = metadata.samplePosition - startSample;
  103. if (samplesToNextMidiMessage >= numSamples)
  104. {
  105. renderNextSubBlock (outputAudio, startSample, numSamples);
  106. handleMidiEvent (metadata.getMessage());
  107. break;
  108. }
  109. if (samplesToNextMidiMessage < ((firstEvent && ! subBlockSubdivisionIsStrict) ? 1 : minimumSubBlockSize))
  110. {
  111. handleMidiEvent (metadata.getMessage());
  112. continue;
  113. }
  114. firstEvent = false;
  115. renderNextSubBlock (outputAudio, startSample, samplesToNextMidiMessage);
  116. handleMidiEvent (metadata.getMessage());
  117. startSample += samplesToNextMidiMessage;
  118. numSamples -= samplesToNextMidiMessage;
  119. }
  120. std::for_each (midiIterator,
  121. inputMidi.cend(),
  122. [&] (const MidiMessageMetadata& meta) { handleMidiEvent (meta.getMessage()); });
  123. }
  124. // explicit instantiation for supported float types:
  125. template void MPESynthesiserBase::renderNextBlock<float> (AudioBuffer<float>&, const MidiBuffer&, int, int);
  126. template void MPESynthesiserBase::renderNextBlock<double> (AudioBuffer<double>&, const MidiBuffer&, int, int);
  127. //==============================================================================
  128. void MPESynthesiserBase::setCurrentPlaybackSampleRate (const double newRate)
  129. {
  130. if (sampleRate != newRate)
  131. {
  132. const ScopedLock sl (noteStateLock);
  133. instrument->releaseAllNotes();
  134. sampleRate = newRate;
  135. }
  136. }
  137. //==============================================================================
  138. void MPESynthesiserBase::setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict) noexcept
  139. {
  140. jassert (numSamples > 0); // it wouldn't make much sense for this to be less than 1
  141. minimumSubBlockSize = numSamples;
  142. subBlockSubdivisionIsStrict = shouldBeStrict;
  143. }
  144. } // namespace juce