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 136 137 138 139 140 141 142 143 144 145
|
/*
==============================================================================
This file is part of the IEM plug-in suite.
Author: Stefan Riedel
Copyright (c) 2022 - Institute of Electronic Music and Acoustics (IEM)
https://iem.at
The IEM plug-in suite is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The IEM plug-in suite is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <https://www.gnu.org/licenses/>.
==============================================================================
*/
#include "Grain.h"
Grain::Grain()
{
_isActive = false;
_currentIndex = 0;
_blockCounter = 0;
}
void Grain::setBlockSize (int numSampOutBuffer)
{
_outputBuffer.setSize (1, numSampOutBuffer);
_outputBuffer.clear();
}
void Grain::startGrain (const GrainJobParameters& grainParameters) // Type envelopeType)
{
_params = grainParameters;
_isActive = true;
_currentIndex = 0;
_blockCounter = 0;
_outputBuffer.clear();
}
void Grain::processBlock (juce::AudioBuffer<float>& buffer,
juce::AudioBuffer<float>& circularBuffer)
{
if (! _isActive)
return;
int numSampCircBuffer = circularBuffer.getNumSamples();
int numSampOutBuffer = buffer.getNumSamples();
jassert (_outputBuffer.getNumSamples() == numSampOutBuffer);
_outputBuffer.clear();
int nChOut = buffer.getNumChannels();
const float* circularLeftChannel = circularBuffer.getReadPointer (0);
const float* circularRightChannel = circularBuffer.getReadPointer (1);
const float* circularBuffToSeed;
if (_params.seedFromLeftCircBuffer)
circularBuffToSeed = circularLeftChannel;
else
circularBuffToSeed = circularRightChannel;
const float* window_ptr = _params.windowBuffer.getReadPointer (0);
const int windowNumSamples = _params.windowBuffer.getNumSamples();
float* outputBufferWritePtr = _outputBuffer.getWritePointer (0);
int outStart;
if (_blockCounter == 0)
{
outStart = _params.startOffsetInBlock;
}
else
{
outStart = 0;
}
// Write samples to internal mono buffer
for (int i = outStart; i < numSampOutBuffer; i++)
{
if (_currentIndex < _params.grainLengthSamples) // grain still needs samples
{
// Linear interpolation of buffer samples
float readIndex =
_params.startPositionCircBuffer + (_currentIndex * _params.pitchReadFactor);
int readIndexInt = static_cast<int> (readIndex);
int readIndexIntNext = readIndexInt + 1;
float sampleFracWeight = readIndex - readIndexInt;
readIndexInt %= numSampCircBuffer;
readIndexIntNext %= numSampCircBuffer;
float sampleIntPart = circularBuffToSeed[readIndexInt];
float sampleFracPart = circularBuffToSeed[readIndexIntNext] - sampleIntPart;
float sampleValue = sampleIntPart + sampleFracWeight * sampleFracPart;
// Linear interpolation for grain window function
float windowIndex = static_cast<float> (_currentIndex)
/ static_cast<float> (_params.grainLengthSamples)
* (windowNumSamples - 1);
int windowIndexInt = static_cast<int> (windowIndex);
jassert (windowIndexInt >= 0 && windowIndexInt < (windowNumSamples - 1));
int windowIndexIntNext = windowIndexInt + 1;
float windowFracWeight = windowIndex - windowIndexInt;
float windowIntPart = window_ptr[windowIndexInt];
float windFracPart = window_ptr[windowIndexIntNext] - windowIntPart;
float windowValue = windowIntPart + windowFracWeight * windFracPart;
jassert (windowValue >= 0.0f && windowValue <= 1.0f);
outputBufferWritePtr[i] = sampleValue * windowValue;
_currentIndex++;
}
else
{
_isActive = false;
break;
}
}
const float* outputBufferReadPtr = _outputBuffer.getReadPointer (0);
for (int ch = 0; ch < nChOut; ++ch)
{
buffer.addFrom (ch,
0,
outputBufferReadPtr,
buffer.getNumSamples(),
_params.channelWeights[ch] * _params.gainFactor);
}
_blockCounter++;
};
bool Grain::isActive() const
{
return _isActive;
};
|