OpenShot Audio Library | OpenShotAudio: juce_Sampler.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

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