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.

159 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. SamplerSound::SamplerSound (const String& soundName,
  16. AudioFormatReader& source,
  17. const BigInteger& notes,
  18. int midiNoteForNormalPitch,
  19. double attackTimeSecs,
  20. double releaseTimeSecs,
  21. double maxSampleLengthSeconds)
  22. : name (soundName),
  23. sourceSampleRate (source.sampleRate),
  24. midiNotes (notes),
  25. midiRootNote (midiNoteForNormalPitch)
  26. {
  27. if (sourceSampleRate > 0 && source.lengthInSamples > 0)
  28. {
  29. length = jmin ((int) source.lengthInSamples,
  30. (int) (maxSampleLengthSeconds * sourceSampleRate));
  31. data.reset (new AudioBuffer<float> (jmin (2, (int) source.numChannels), length + 4));
  32. source.read (data.get(), 0, length + 4, 0, true, true);
  33. params.attack = static_cast<float> (attackTimeSecs);
  34. params.release = static_cast<float> (releaseTimeSecs);
  35. }
  36. }
  37. SamplerSound::~SamplerSound()
  38. {
  39. }
  40. bool SamplerSound::appliesToNote (int midiNoteNumber)
  41. {
  42. return midiNotes[midiNoteNumber];
  43. }
  44. bool SamplerSound::appliesToChannel (int /*midiChannel*/)
  45. {
  46. return true;
  47. }
  48. //==============================================================================
  49. SamplerVoice::SamplerVoice() {}
  50. SamplerVoice::~SamplerVoice() {}
  51. bool SamplerVoice::canPlaySound (SynthesiserSound* sound)
  52. {
  53. return dynamic_cast<const SamplerSound*> (sound) != nullptr;
  54. }
  55. void SamplerVoice::startNote (int midiNoteNumber, float velocity, SynthesiserSound* s, int /*currentPitchWheelPosition*/)
  56. {
  57. if (auto* sound = dynamic_cast<const SamplerSound*> (s))
  58. {
  59. pitchRatio = std::pow (2.0, (midiNoteNumber - sound->midiRootNote) / 12.0)
  60. * sound->sourceSampleRate / getSampleRate();
  61. sourceSamplePosition = 0.0;
  62. lgain = velocity;
  63. rgain = velocity;
  64. adsr.setSampleRate (sound->sourceSampleRate);
  65. adsr.setParameters (sound->params);
  66. adsr.noteOn();
  67. }
  68. else
  69. {
  70. jassertfalse; // this object can only play SamplerSounds!
  71. }
  72. }
  73. void SamplerVoice::stopNote (float /*velocity*/, bool allowTailOff)
  74. {
  75. if (allowTailOff)
  76. {
  77. adsr.noteOff();
  78. }
  79. else
  80. {
  81. clearCurrentNote();
  82. adsr.reset();
  83. }
  84. }
  85. void SamplerVoice::pitchWheelMoved (int /*newValue*/) {}
  86. void SamplerVoice::controllerMoved (int /*controllerNumber*/, int /*newValue*/) {}
  87. //==============================================================================
  88. void SamplerVoice::renderNextBlock (AudioBuffer<float>& outputBuffer, int startSample, int numSamples)
  89. {
  90. if (auto* playingSound = static_cast<SamplerSound*> (getCurrentlyPlayingSound().get()))
  91. {
  92. auto& data = *playingSound->data;
  93. const float* const inL = data.getReadPointer (0);
  94. const float* const inR = data.getNumChannels() > 1 ? data.getReadPointer (1) : nullptr;
  95. float* outL = outputBuffer.getWritePointer (0, startSample);
  96. float* outR = outputBuffer.getNumChannels() > 1 ? outputBuffer.getWritePointer (1, startSample) : nullptr;
  97. while (--numSamples >= 0)
  98. {
  99. auto pos = (int) sourceSamplePosition;
  100. auto alpha = (float) (sourceSamplePosition - pos);
  101. auto invAlpha = 1.0f - alpha;
  102. // just using a very simple linear interpolation here..
  103. float l = (inL[pos] * invAlpha + inL[pos + 1] * alpha);
  104. float r = (inR != nullptr) ? (inR[pos] * invAlpha + inR[pos + 1] * alpha)
  105. : l;
  106. auto envelopeValue = adsr.getNextSample();
  107. l *= lgain * envelopeValue;
  108. r *= rgain * envelopeValue;
  109. if (outR != nullptr)
  110. {
  111. *outL++ += l;
  112. *outR++ += r;
  113. }
  114. else
  115. {
  116. *outL++ += (l + r) * 0.5f;
  117. }
  118. sourceSamplePosition += pitchRatio;
  119. if (sourceSamplePosition > playingSound->length)
  120. {
  121. stopNote (0.0f, false);
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. } // namespace juce