33 int midiNoteForNormalPitch,
34 double attackTimeSecs,
35 double releaseTimeSecs,
36 double maxSampleLengthSeconds)
38 sourceSampleRate (source.sampleRate),
40 midiRootNote (midiNoteForNormalPitch)
45 (
int) (maxSampleLengthSeconds * sourceSampleRate));
49 source.
read (data.get(), 0, length + 4, 0,
true,
true);
51 params.
attack =
static_cast<float> (attackTimeSecs);
52 params.
release =
static_cast<float> (releaseTimeSecs);
62 return midiNotes[midiNoteNumber];
76 return dynamic_cast<const SamplerSound*
> (sound) !=
nullptr;
81 if (
auto* sound = dynamic_cast<const SamplerSound*> (s))
83 pitchRatio = std::pow (2.0, (midiNoteNumber - sound->midiRootNote) / 12.0)
84 * sound->sourceSampleRate / getSampleRate();
86 sourceSamplePosition = 0.0;
90 adsr.setSampleRate (sound->sourceSampleRate);
91 adsr.setParameters (sound->params);
120 if (
auto* playingSound = static_cast<SamplerSound*> (getCurrentlyPlayingSound().
get()))
122 auto& data = *playingSound->data;
123 const float*
const inL = data.getReadPointer (0);
124 const float*
const inR = data.getNumChannels() > 1 ? data.getReadPointer (1) :
nullptr;
129 while (--numSamples >= 0)
131 auto pos = (int) sourceSamplePosition;
132 auto alpha = (float) (sourceSamplePosition - pos);
133 auto invAlpha = 1.0f - alpha;
136 float l = (inL[pos] * invAlpha + inL[pos + 1] * alpha);
137 float r = (inR !=
nullptr) ? (inR[pos] * invAlpha + inR[pos + 1] * alpha)
140 auto envelopeValue = adsr.getNextSample();
142 l *= lgain * envelopeValue;
143 r *= rgain * envelopeValue;
152 *outL++ += (l + r) * 0.5f;
155 sourceSamplePosition += pitchRatio;
157 if (sourceSamplePosition > playingSound->length)
159 stopNote (0.0f,
false);
SamplerSound(const String &name, AudioFormatReader &source, const BigInteger &midiNotes, int midiNoteForNormalPitch, double attackTimeSecs, double releaseTimeSecs, double maxSampleLengthSeconds)
bool canPlaySound(SynthesiserSound *) override
void pitchWheelMoved(int newValue) override
void stopNote(float velocity, bool allowTailOff) override
int getNumChannels() const noexcept
void controllerMoved(int controllerNumber, int newValue) override
void startNote(int midiNoteNumber, float velocity, SynthesiserSound *, int pitchWheel) override
bool appliesToChannel(int midiChannel) override
bool appliesToNote(int midiNoteNumber) override
Type * getWritePointer(int channelNumber) noexcept
void renderNextBlock(AudioBuffer< float > &, int startSample, int numSamples) override