SFML community forums
Help => Audio => Topic started by: Aval on March 10, 2009, 11:48:43 pm
-
I cannot get LoadFromSamples to work.
I get samples in the form of a const sf::Int16 *. Since I can't change a const pointer directly, I copy all of the data to a buffer. I modify it, and then I try to load the buffer back to the sf::SoundBuffer.
const sf::Int16 * pSample = Buffer.GetSamples();
int max = Buffer.GetSamplesCount()-1;
int channels = Buffer.GetChannelsCount();
int sampleRate = Buffer.GetSampleRate();
sf::Int16 * myBuffer = new sf::Int16[max];
for( int i = 0; i < max; i++ )
{
myBuffer[i] = pSample[i];
}
//Modify myBuffer
Buffer.LoadFromSamples( myBuffer, max, channels, sampleRate );
I get the following error:
error C2662: 'sf::SoundBuffer::LoadFromSamples' : cannot convert 'this' pointer from 'const sf::SoundBuffer' to 'sf::SoundBuffer &' Conversion loses qualifiers
I declare Buffer as:
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
Maybe I'm doing something wrong with references?
-
I'm no expert but have you tried doing a const_cast? Because you have a problem as you declare Buffer as a const and later try to change it.
-
have you tried doing a const_cast?
Well, if the recorder returns a const buffer there's a reason ;)
Maybe I'm doing something wrong with references?
Yes, you try to modify the internal buffer of a recorder, which is obviously read-only. What are you trying to do?
-
have you tried doing a const_cast?
Well, if the recorder returns a const buffer there's a reason ;)
What about changing:const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
to:sf::SoundBuffer Buffer = Recorder.GetBuffer();
...no const_cast used here :P
-
I want to get the data recorded, stretch it by a factor, and play it back. I can access the data, and so I'm trying to load the buffer that has the modified sound back into the soundbuffer.
Declaring Buffer as a non-const doesn't work, because Recorder.GetBuffer() returns a const pointer.
-
Just don't try using the same buffer. The recorder's one is not meant to be changed, use your own copy.
// Get the source buffer
const sf::SoundBuffer& SourceBuffer = Recorder.GetBuffer();
// Copy and modify the samples
std::vector<sf::Int16> Samples;
...
// Load the modified samples
sf::SoundBuffer MyBuffer;
MyBuffer.LoadFromSamples(&Samples[0], Samples.size(), SourceBuffer.GetChannelsCount(), SourceBuffer.GetSampleRate());
// Play them
sf::Sound Sound(MyBuffer);
Sound.Play();
-
Ah! Thanks. Is there a way to manually delete a buffer from memory, or do you just make it go out of scope?
-
Just make it go out of scope :)