Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: LoadFromSamples Function  (Read 8144 times)

0 Members and 1 Guest are viewing this topic.

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
LoadFromSamples Function
« 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?

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
LoadFromSamples Function
« Reply #1 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
LoadFromSamples Function
« Reply #2 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?
Laurent Gomila - SFML developer

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
LoadFromSamples Function
« Reply #3 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

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
LoadFromSamples Function
« Reply #4 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
LoadFromSamples Function
« Reply #5 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();
Laurent Gomila - SFML developer

Aval

  • Newbie
  • *
  • Posts: 34
    • View Profile
    • Email
LoadFromSamples Function
« Reply #6 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
LoadFromSamples Function
« Reply #7 on: March 14, 2009, 10:14:04 am »
Just make it go out of scope :)
Laurent Gomila - SFML developer

 

anything