2

I have a vector of float

vector<float>nFloats;

I would like to pass it to the following function:

/* This is a non-stream oriented interface to just change the speed of a sound sample */
int sonicChangeFloatSpeed(
    float *samples,
    int numSamples,
    float speed,
    float pitch,
    float rate,
    float volume,
    int useChordPitch,
    int sampleRate,
    int numChannels)
{
    sonicStream stream = sonicCreateStream(sampleRate, numChannels);

    sonicSetSpeed(stream, speed);
    sonicSetPitch(stream, pitch);
    sonicSetRate(stream, rate);
    sonicSetVolume(stream, volume);
    sonicSetChordPitch(stream, useChordPitch);
    sonicWriteFloatToStream(stream, samples, numSamples);
    sonicFlushStream(stream);
    numSamples = sonicSamplesAvailable(stream);
    sonicReadFloatFromStream(stream, samples, numSamples);
    sonicDestroyStream(stream);
    return numSamples;
}

Can anybody tell me how to do it correctly?

My attempts

 1) int iRet = sonicChangeFloatSpeed(&nFloats[0], *num, 1, 0.5, 1, 1, 1, 48000, 1);

 2) int iRet = sonicChangeFloatSpeed(nFloats[0], *num, 1, 0.5, 1, 1, 1, 48000, 1);

 3) int iRet = sonicChangeFloatSpeed(*nFloats, *num, 1, 0.5, 1, 1, 1, 48000, 1);

all don't work.

I'm sure such a question has already been asked, but I couldn't find any exact duplicate.

Can anybody help?

Thank you.

3
  • It looks like the pointer is used as output of the function. You must resize() the vector before using it to an appropriate buffer size. Commented Jan 29, 2017 at 11:26
  • The semantics of the function are unclear, because it takes numSamples and returns it. Commented Jan 29, 2017 at 11:43
  • Is the function supposed to add floats to the vector? And what do you mean by all don't work ? Doesn't work in which way? The first method looks fine to me. Commented Jan 29, 2017 at 11:45

2 Answers 2

4

I think your first version should work, because the vectors use contiguous storage locations for their elements.

int iRet = sonicChangeFloatSpeed(&nFloats[0], nFloats.size(), 1, 0.5, 1, 1, 1, 48000, 1);

In c++11 you can use

float* p = nFloats.data();
int iRet = sonicChangeFloatSpeed(p, nFloats.size(), 1, 0.5, 1, 1, 1, 48000, 1);
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I can tell, your approach is problematic.

Your function seems to do 2 things:

1) Write a number of floats to a stream

2) Read a number of floats from a stream

In both cases you want the floats to be in a vector.

For number 1 it is fine to use a vector but for number 2 it is very problematic.

You can't pass a pointer to the data held by a vector and then change the number of elements. The original vector will keep its original size. If you attempt to read more floats from the stream than currently held by the vector, the behavior is undefined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.