I have a std::vector<float>
containing sound data. Without copying its data, I'd like to use this vector as input to the sonicChangeFloatSpeed function of the Sonic library. This method expects a float*
as first argument and mutates the input array. After completion, the pointer in first argument would point to the result data.
data
gives me access to the internal array of a C++ vector and with assign
, I can replace the vector's contents. Hence, I tried the following:
float* ptr = vec.data();
int num_samples = sonicChangeFloatSpeed(ptr, vec.size(), 1., 1.5, 1., 1., 0, 41000, 1);
vec.assign(ptr, ptr + num_samples);
But when I run this program, I get the error double free or corruption (!prev)
with a SIGABRT
at this location. What is the problem of this approach and how would this question be solved more appropriately?
As I mentioned, I solved this problem by not using sonicChangeFloatSpeed
at all, but the code within it. Before reading the results from the stream into vec
, I do vec.resize(numSamples)
:
sonicStream stream = sonicCreateStream(16000, 1);
sonicSetSpeed(stream, speed);
sonicSetPitch(stream, pitch);
sonicSetVolume(stream, volume);
sonicSetRate(stream, rate);
auto length = static_cast<int>(vec.size());
sonicWriteFloatToStream(stream, vec.data(), length);
sonicFlushStream(stream);
int numSamples = sonicSamplesAvailable(stream);
vec.resize(numSamples);
sonicReadFloatFromStream(stream, vec.data(), length);
sonicDestroyStream(stream);