I'm trying to make filters and other audio effects that can be applied in real time for a game engine. I've found a simple lowpass filter formula that works just fine. I have a function that processes the sf::SoundBuffer and reloads it to the sf::Sound. The problem is, I cannot seem to get the buffer to retain its processed samples. More specifically, it goes out of scope in my function.
Maybe I should let the code do the talking:
CSoundController::CSoundController(sf::Sound* target)
{
mySound = target; // mySound's type is sf::Sound*
myBuffer = mySound->getBuffer(); // myBuffer's type is const sf::SoundBuffer*
}
void
CSoundController::applyFilter(float val)
{
if (mySound != NULL)
{
if (val <= 1.0f && val >= 0.0f)
{
if (mySound->getStatus() == sf::Sound::Status::Playing)
{
mySound->stop();
}
const sf::Int16* drySamples = myBuffer->getSamples();
int numSamples = myBuffer->getSampleCount();
int channels = myBuffer->getChannelCount();
int sampleRate = myBuffer->getSampleRate();
std::vector<sf::Int16> wetSamples;
wetSamples.reserve(numSamples);
float b = 1.0f - val;
float z = 0;
for (int n = 0; n < numSamples; n++)
{
z = (drySamples[n] * b) + (z * val);
wetSamples.push_back(z);
}
sf::SoundBuffer* wetBuffer = new sf::SoundBuffer();
wetBuffer->loadFromSamples(&wetSamples[0], wetSamples.size(), channels, numSamples);
myBuffer = wetBuffer;
mySound->setBuffer(*myBuffer);
delete wetBuffer;
}
}
}
When I run this function, the buffer is processed fine and is just peachy until wetBuffer gets deleted and the data it contained is deleted. Next time I run the function, the buffer's filled with garbage.
I can't seem to figure out a solution for this. I would appreciate some suggestions.
I found this post related to loading buffers, but I feel like my case is a little different:
http://en.sfml-dev.org/forums/index.php?topic=1098.msg6998#msg6998Also, if there is a better way to modify the buffer data without swapping vectors and buffers around, I would love to hear it. I'm quite new to this sort of thing.
Also also, it was a pain for me to find this filter algorithm and get it functioning, so for the sake of those who are pursuing the same as me, here is the link to the algorithm:
http://www.musicdsp.org/archive.php?classid=3#257I'm sure this is probably as lame of a filter as you could possibly get, but it does exactly what I want it to do, and I can actually comprehend it, so it works for me
Thanks for any and all help