SFML community forums

Help => Audio => Topic started by: Aval on March 10, 2009, 11:48:43 pm

Title: LoadFromSamples Function
Post 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.

Code: [Select]
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:
Code: [Select]
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();

Maybe I'm doing something wrong with references?
Title: LoadFromSamples Function
Post by: dabo on March 11, 2009, 01:51:36 am
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.
Title: LoadFromSamples Function
Post by: Laurent on March 11, 2009, 07:53:11 am
Quote
have you tried doing a const_cast?

Well, if the recorder returns a const buffer there's a reason ;)

Quote
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?
Title: LoadFromSamples Function
Post by: dabo on March 11, 2009, 12:31:26 pm
Quote from: "Laurent"
Quote
have you tried doing a const_cast?

Well, if the recorder returns a const buffer there's a reason ;)


What about changing:
Code: [Select]
const sf::SoundBuffer& Buffer = Recorder.GetBuffer();
to:
Code: [Select]
sf::SoundBuffer Buffer = Recorder.GetBuffer();

...no const_cast used here :P
Title: LoadFromSamples Function
Post by: Aval on March 13, 2009, 02:27:54 am
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.
Title: LoadFromSamples Function
Post by: Laurent on March 13, 2009, 07:57:15 am
Just don't try using the same buffer. The recorder's one is not meant to be changed, use your own copy.
Code: [Select]
// 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();
Title: LoadFromSamples Function
Post by: Aval on March 14, 2009, 01:17:01 am
Ah! Thanks. Is there a way to manually delete a buffer from memory, or do you just make it go out of scope?
Title: LoadFromSamples Function
Post by: Laurent on March 14, 2009, 10:14:04 am
Just make it go out of scope :)