The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

206 lines
6.2KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. SamplerSound::SamplerSound (const String& soundName,
  22. AudioFormatReader& source,
  23. const BigInteger& notes,
  24. int midiNoteForNormalPitch,
  25. double attackTimeSecs,
  26. double releaseTimeSecs,
  27. double maxSampleLengthSeconds)
  28. : name (soundName),
  29. sourceSampleRate (source.sampleRate),
  30. midiNotes (notes),
  31. midiRootNote (midiNoteForNormalPitch)
  32. {
  33. if (sourceSampleRate > 0 && source.lengthInSamples > 0)
  34. {
  35. length = jmin ((int) source.lengthInSamples,
  36. (int) (maxSampleLengthSeconds * sourceSampleRate));
  37. data.reset (new AudioBuffer<float> (jmin (2, (int) source.numChannels), length + 4));
  38. source.read (data.get(), 0, length + 4, 0, true, true);
  39. attackSamples = roundToInt (attackTimeSecs * sourceSampleRate);
  40. releaseSamples = roundToInt (releaseTimeSecs * sourceSampleRate);
  41. }
  42. }
  43. SamplerSound::~SamplerSound()
  44. {
  45. }
  46. bool SamplerSound::appliesToNote (int midiNoteNumber)
  47. {
  48. return midiNotes[midiNoteNumber];
  49. }
  50. bool SamplerSound::appliesToChannel (int /*midiChannel*/)
  51. {
  52. return true;
  53. }
  54. //==============================================================================
  55. SamplerVoice::SamplerVoice() {}
  56. SamplerVoice::~SamplerVoice() {}
  57. bool SamplerVoice::canPlaySound (SynthesiserSound* sound)
  58. {
  59. return dynamic_cast<const SamplerSound*> (sound) != nullptr;
  60. }
  61. void SamplerVoice::startNote (int midiNoteNumber, float velocity, SynthesiserSound* s, int /*currentPitchWheelPosition*/)
  62. {
  63. if (auto* sound = dynamic_cast<const SamplerSound*> (s))
  64. {
  65. pitchRatio = std::pow (2.0, (midiNoteNumber - sound->midiRootNote) / 12.0)
  66. * sound->sourceSampleRate / getSampleRate();
  67. sourceSamplePosition = 0.0;
  68. lgain = velocity;
  69. rgain = velocity;
  70. isInAttack = (sound->attackSamples > 0);
  71. isInRelease = false;
  72. if (isInAttack)
  73. {
  74. attackReleaseLevel = 0.0f;
  75. attackDelta = (float) (pitchRatio / sound->attackSamples);
  76. }
  77. else
  78. {
  79. attackReleaseLevel = 1.0f;
  80. attackDelta = 0.0f;
  81. }
  82. if (sound->releaseSamples > 0)
  83. releaseDelta = (float) (-pitchRatio / sound->releaseSamples);
  84. else
  85. releaseDelta = -1.0f;
  86. }
  87. else
  88. {
  89. jassertfalse; // this object can only play SamplerSounds!
  90. }
  91. }
  92. void SamplerVoice::stopNote (float /*velocity*/, bool allowTailOff)
  93. {
  94. if (allowTailOff)
  95. {
  96. isInAttack = false;
  97. isInRelease = true;
  98. }
  99. else
  100. {
  101. clearCurrentNote();
  102. }
  103. }
  104. void SamplerVoice::pitchWheelMoved (int /*newValue*/) {}
  105. void SamplerVoice::controllerMoved (int /*controllerNumber*/, int /*newValue*/) {}
  106. //==============================================================================
  107. void SamplerVoice::renderNextBlock (AudioBuffer<float>& outputBuffer, int startSample, int numSamples)
  108. {
  109. if (auto* playingSound = static_cast<SamplerSound*> (getCurrentlyPlayingSound().get()))
  110. {
  111. auto& data = *playingSound->data;
  112. const float* const inL = data.getReadPointer (0);
  113. const float* const inR = data.getNumChannels() > 1 ? data.getReadPointer (1) : nullptr;
  114. float* outL = outputBuffer.getWritePointer (0, startSample);
  115. float* outR = outputBuffer.getNumChannels() > 1 ? outputBuffer.getWritePointer (1, startSample) : nullptr;
  116. while (--numSamples >= 0)
  117. {
  118. auto pos = (int) sourceSamplePosition;
  119. auto alpha = (float) (sourceSamplePosition - pos);
  120. auto invAlpha = 1.0f - alpha;
  121. // just using a very simple linear interpolation here..
  122. float l = (inL[pos] * invAlpha + inL[pos + 1] * alpha);
  123. float r = (inR != nullptr) ? (inR[pos] * invAlpha + inR[pos + 1] * alpha)
  124. : l;
  125. l *= lgain;
  126. r *= rgain;
  127. if (isInAttack)
  128. {
  129. l *= attackReleaseLevel;
  130. r *= attackReleaseLevel;
  131. attackReleaseLevel += attackDelta;
  132. if (attackReleaseLevel >= 1.0f)
  133. {
  134. attackReleaseLevel = 1.0f;
  135. isInAttack = false;
  136. }
  137. }
  138. else if (isInRelease)
  139. {
  140. l *= attackReleaseLevel;
  141. r *= attackReleaseLevel;
  142. attackReleaseLevel += releaseDelta;
  143. if (attackReleaseLevel <= 0.0f)
  144. {
  145. stopNote (0.0f, false);
  146. break;
  147. }
  148. }
  149. if (outR != nullptr)
  150. {
  151. *outL++ += l;
  152. *outR++ += r;
  153. }
  154. else
  155. {
  156. *outL++ += (l + r) * 0.5f;
  157. }
  158. sourceSamplePosition += pitchRatio;
  159. if (sourceSamplePosition > playingSound->length)
  160. {
  161. stopNote (0.0f, false);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. } // namespace juce