Spaces:
Paused
Paused
File size: 3,233 Bytes
8c39918 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include "SamplerVoice.h"
SamplerVoice::SamplerVoice() = default;
SamplerVoice::~SamplerVoice()
{
clear();
}
void SamplerVoice::prepareToPlay(double sampleRate, int)
{
this->sampleRate = sampleRate;
}
void SamplerVoice::releaseResources()
{
isVoiceActive = false;
}
void SamplerVoice::noteOn(int noteNumber, int velocity)
{
if (sampleData == nullptr || sampleData->getNumSamples() == 0)
return;
this->noteNumber = noteNumber;
this->velocity = static_cast<float>(velocity) / 127.0f;
this->isVoiceActive = true;
this->samplePosition = 0;
// Calculate pitch ratio based on MIDI note (middle C = 60)
pitchRatio = std::pow(2.0, (noteNumber - 60) / 12.0);
}
void SamplerVoice::noteOff()
{
if (!loopMode)
{
isVoiceActive = false;
}
}
void SamplerVoice::renderNextBlock(juce::AudioBuffer<float>& outputBuffer,
int startSample,
int numSamples)
{
if (!isVoiceActive || sampleData == nullptr)
return;
const int numChannels = juce::jmin(outputBuffer.getNumChannels(),
sampleData->getNumChannels());
const int sampleLength = sampleData->getNumSamples();
for (int sample = 0; sample < numSamples; ++sample)
{
if (samplePosition >= sampleLength)
{
if (loopMode)
{
samplePosition = 0;
}
else
{
isVoiceActive = false;
break;
}
}
for (int channel = 0; channel < numChannels; ++channel)
{
auto* destData = outputBuffer.getWritePointer(channel);
const auto* srcData = sampleData->getReadPointer(channel);
destData[startSample + sample] +=
srcData[samplePosition] * velocity * volume;
}
samplePosition += static_cast<juce::int64>(pitchRatio);
}
}
bool SamplerVoice::loadSample(const juce::String& filePath)
{
juce::File file(filePath);
if (!file.existsAsFile())
{
jassertfalse;
return false;
}
juce::AudioFormatManager formatManager;
formatManager.registerBasicFormats();
std::unique_ptr<juce::AudioFormatReader> reader(
formatManager.createReaderFor(file));
if (reader == nullptr)
return false;
auto newBuffer = std::make_unique<juce::AudioBuffer<float>>(
reader->numChannels,
static_cast<int>(reader->lengthInSamples));
reader->read(newBuffer.get(), 0, reader->lengthInSamples, 0, true, true);
sampleData = newBuffer.release();
sampleRate = reader->sampleRate;
return true;
}
void SamplerVoice::clear()
{
delete sampleData;
sampleData = nullptr;
isVoiceActive = false;
samplePosition = 0;
}
void SamplerVoice::setLoopMode(bool shouldLoop)
{
loopMode = shouldLoop;
}
void SamplerVoice::setVolume(float vol)
{
volume = juce::jlimit(0.0f, 1.0f, vol);
}
void SamplerVoice::setTempo(double bpm)
{
currentBpm = bpm;
// Could adjust playback speed based on tempo for time-stretching
}
|