SFML community forums

Help => Audio => Topic started by: WSPSNIPER on March 08, 2010, 02:51:50 am

Title: sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
Post by: WSPSNIPER on March 08, 2010, 02:51:50 am
when i play a sound about 1000 times it gives me the error and the sound goes away

my code



Code: [Select]

sf::Sound m_sound;

//this returns the sound buffer for the hit sound
m_sound.SetBuffer(soundMgr->GetSoundBuffer(SoundManager::kHit));

/*
 * the function above
 * sf::SoundBuffer &SoundManager::GetSoundBuffer(int id)
 * {
 * return m_buffers[id];  // an array of buffers for the sounds
 * }
*/

//when added to weapon manager
WeaponManager::Add(Entity *weapon)
{
    weapon->PlaySound();
    m_weaponList.push_back(weapon);
}



this is a sound that is located in a knife class that gets added to a vector in my weapon manager when throwen and will play the kife's sound when it is added

so if there is anything i can do
Title: better solution?
Post by: WSPSNIPER on March 08, 2010, 03:21:50 am
should i have a sound buffer for every sound or 1 sound buffer for 1 type of sound

example :

Code: [Select]


SoundBuffer hit("hit.wav");

Sound hitSound(hit);
Sound hit2Sound(hit);
Sound hit3Sound(hit); exc



or

Code: [Select]

class MySound{
public:
    SoundBuffer;
    Sound;
}



just examples code may be off but you get the point
Title: sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
Post by: Laurent on March 08, 2010, 08:38:38 am
Quote
when i play a sound about 1000 times it gives me the error and the sound goes away

Is it 1024 times? Do you properly destroy unused sounds, or reuse them (ie. you have no leak)?

Quote
should i have a sound buffer for every sound or 1 sound buffer for 1 type of sound

One sound buffer for one type of sound. This is why sf::Sound and sf::SoundBuffer are two separate classes: load the audio data once, play it multiple times.
Title: i think that may be the reason, i will check for a leak
Post by: WSPSNIPER on March 09, 2010, 02:05:18 am
thanks for pointing that out, i dont know exactly how a vector takes care of memory allocation/deallocation, but here is how i did it

Code: [Select]
std::vector<Weapon*> m_weapons;

void Add(Weapon *weapon)
{
    weapon->PlaySound();
    m_weapons.pushback(weapon);
}

then i to use the Add function for a knife for example i wound do

Code: [Select]
weaponMgr->Add(new Knife());

then i did no cleanup, if you could give me any tips on cleaning up the vector it would help

ps. i have a GarbageCollect function that will erase weapons that dont exist anymore

Code: [Select]
for(int i = 0; i < m_weapons.size(); i++)
{
if(m_weapons[i]->IsDead())
{
m_weapons.erase(m_weapons.begin() + i);
}
}


should this work for getting rid of the sounds ( that are in the weapon class )

or is there a different thing i should be doing to clean it up
Title: Found Solution for memory problem
Post by: WSPSNIPER on March 09, 2010, 02:12:43 am
the vector dosent delete the memory so you have to do this your self


new garbage collector


Code: [Select]
for(int i = 0; i < m_weapons.size(); i++)
{
if(m_weapons[i]->IsDead())
{
delete m_weapons[i];
m_weapons.erase(m_weapons.begin() + i);
}
}


btw thanks for pointing out the memory thing i wouldent have thought of it ill test it real quick and get back to you on the results, cross my fingers
Title: Solved
Post by: WSPSNIPER on March 09, 2010, 02:16:10 am
Thanks so much, i love the support that SFML has for the users great job.
Title: sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
Post by: Laurent on March 09, 2010, 07:59:00 am
There's a mistake in your GC loop, it will skip one element every time one is erased.

Here is a corrected code
Code: [Select]
for (std::vector<Weapon*>::iterator it = m_weapons.begin(); it != m_weapons.end(); )
{
   if ((*it)->IsDead())
   {
      delete *it;
      it = m_weapons.erase(it);
   }
   else
   {
      ++it;
   }
}