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

Author Topic: Reusing sound buffers with new data  (Read 4205 times)

0 Members and 2 Guests are viewing this topic.

tsiegel

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.softcon.com/mac/
Reusing sound buffers with new data
« on: July 28, 2010, 05:39:04 am »
Not for me, but someone has asked me how to reuse buffers in sfml, and as far as I can tell, it's not doable.  Could someone confirm/deny this for me.
The scenario is this:
1. Load a audio file (works)
2 Assign it to a buffer (works)
3 Play said audio snippet (works)
4 deallocate memory used for buffer (I.E. set buffer to null)
5 load new sound
6 assign to previously used buffer w/o making a new one (I think Sound.SetBuffer() does this)

Basically, I think the question is whether or not buffer content can be deallocated until reinitialized at a later time.
The purpose (I think) is to load audio files, play them, then move to new levels, and reload the buffers with new sounds without using any additional memory.
Is there something other than SetBuffer that can be used to nullify a buffer until it's services are required again?
For my purposes, simply reassigning the buffer seems to be perfectly acceptable, but I'm being asked how to free memory used by a buffer w/o loosing the buffer itself.
Dunno how to do that (yet) so if anyone has ideas/suggestions/hard facts, I sure would be happy to pass them on. :)
Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Reusing sound buffers with new data
« Reply #1 on: July 28, 2010, 08:25:32 am »
You talk about buffers (sf::SoundBuffer), but it looks like you mean "sounds" (sf::Sound). So what exactly are you talking about?
Laurent Gomila - SFML developer

albertoguerere

  • Newbie
  • *
  • Posts: 2
    • View Profile
Reusing sound buffers with new data
« Reply #2 on: July 28, 2010, 04:53:32 pm »
Quote from: "Laurent"
You talk about buffers (sf::SoundBuffer), but it looks like you mean "sounds" (sf::Sound). So what exactly are you talking about?


I have a project that must run 24x7 and am currently having some issues with either sf::SoundBuffer or sf::Sound. They load and play perfectly using wav files, but over time, like 12 hours after initialization the sounds get really noisy. You can still hear it but it sounds badly with lots of clics and noise.

I thought about reloading the sound files at some point but became concerned about wasting memory by not reusing the already allocated space for the files that were loaded initially.

How can we reuse them? (I am currently using SFML 1.6)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Reusing sound buffers with new data
« Reply #3 on: July 28, 2010, 04:55:58 pm »
If you create new sounds/buffers, the previous ones will be deallocated so there's no wasted memory. Unless you badly manage your objects (ie. create new instances, but the previous ones still exist in memory).
Laurent Gomila - SFML developer

tsiegel

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.softcon.com/mac/
Reusing sound buffers with new data
« Reply #4 on: July 28, 2010, 05:04:37 pm »
Quote from: "Laurent"
You talk about buffers (sf::SoundBuffer), but it looks like you mean "sounds" (sf::Sound). So what exactly are you talking about?


Fair enough, so same question, but with the sounds themselves instead of their buffers.
Basically, how to release memory used by one sound, (whether that be buffer or sound) until needed later.
I.E. load sound1, play it, unload sound 1, then some time later (a minute, an ahour, whatever) load a new sound and play it, but in the in between time, release the memory used by the first sound so there's no wasted memory during the interval.
Does the buffer actually use memory? or is it just a pointer to the sound itself?
And, if the buffer is truly only a pointer, then how to reinit the sound to eliminate it's memory footprint. (unload the sound I guess)
Apparently, these are rather large sounds, but streaming them is too slow, so loading one or two at a time is all the memory that he wants to use.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Reusing sound buffers with new data
« Reply #5 on: July 28, 2010, 05:13:15 pm »
Quote
Basically, how to release memory used by one sound, (whether that be buffer or sound) until needed later.

You destroy the corresponding sf::Sound instance, or you assign it an empty one if you want to keep the same instance
Code: [Select]
sound = sf::Sound();
Same for sf::SounfBuffer, by the way.

Quote
Does the buffer actually use memory? or is it just a pointer to the sound itself?

The buffer holds the audio data, so this is the heavy one.
Basically, sf:Sound instances are lightweight but use audio resources (you can't have more than a fixed number of sf::Sound instances at the same time).
sf::SoundBuffer is heavy, it stores all the sound data.

Maybe you forget to destroy unused sf::SoundBuffer objects (new but never delete), and you run out of memory? That would explain the artifacts after 12 hours (the memory starts swapping and becomes damn slow).
Laurent Gomila - SFML developer