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

Author Topic: sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]  (Read 4615 times)

0 Members and 1 Guest are viewing this topic.

WSPSNIPER

  • Newbie
  • *
  • Posts: 28
    • View Profile
sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
« 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

WSPSNIPER

  • Newbie
  • *
  • Posts: 28
    • View Profile
better solution?
« Reply #1 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
« Reply #2 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.
Laurent Gomila - SFML developer

WSPSNIPER

  • Newbie
  • *
  • Posts: 28
    • View Profile
i think that may be the reason, i will check for a leak
« Reply #3 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

WSPSNIPER

  • Newbie
  • *
  • Posts: 28
    • View Profile
Found Solution for memory problem
« Reply #4 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

WSPSNIPER

  • Newbie
  • *
  • Posts: 28
    • View Profile
Solved
« Reply #5 on: March 09, 2010, 02:16:10 am »
Thanks so much, i love the support that SFML has for the users great job.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Sound internal OpenAL error in soundbuffer.cpp [Solved]
« Reply #6 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;
   }
}
Laurent Gomila - SFML developer