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

Author Topic: Sound fails on repeated play  (Read 8294 times)

0 Members and 1 Guest are viewing this topic.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Sound fails on repeated play
« on: April 07, 2011, 04:49:03 pm »
SFML 2.0
OS: XP

If I repeatedly play a sound it works for a couple seconds then the sound stops altogether. The music keeps going however.

"An internal OpenAL call failed in SoundSource.cpp(39) AL_INVALID_VALUE, a numeric argument is out of range"

"An internal OpenAL call failed in SoundSource.cpp(39) AL_INVALID_NAME, an unacceptable name has been specified"

"An internal OpenAL call failed in Sound.cpp(107) AL_INVALID_NAME, an unacceptable name has been specified"

"An internal OpenAL call failed in Sound.cpp(76) AL_INVALID_NAME, an unacceptable name has been specified"

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sound fails on repeated play
« Reply #1 on: April 07, 2011, 05:00:27 pm »
Can you show your code?
Laurent Gomila - SFML developer

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Sound fails on repeated play
« Reply #2 on: April 07, 2011, 05:02:19 pm »
I was dynamically allocating a sf::Sound object every frame. When I changed it to reuse one sf::Sound object it doesnt break. Is there a limit on the number of sf::Sound objects you can have?


EDIT:

I was essentially doing this every frame:

Code: [Select]

sf::Sound *sound = new sf::Sound()
sound->SetBuffer(ResourceManager->getSoundBuffer("sound.wav"));
soundList.push_back(sound);

for(int i = 0; i<soundList.size(); i++)
{
    soundList[i]->Play();
}
soundList.clear();

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Sound fails on repeated play
« Reply #3 on: April 07, 2011, 05:30:50 pm »
You're never deleting your sounds? Just calling clear won't help, thus the destructor isn't being called - you're probably filling up all the available voices.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Sound fails on repeated play
« Reply #4 on: April 07, 2011, 05:33:27 pm »
Oh, I was under the impression their destructors were called: http://www.cplusplus.com/reference/stl/vector/clear/

Is that not right?

Regardless, I also tried calling delete after each sound in the loop. Doing that, sound ceases to play at all.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Sound fails on repeated play
« Reply #5 on: April 07, 2011, 05:46:06 pm »
According to that text, it sure looks like I'm in error.

However, I'm not. The quote you refer to is only true for non-pointer objects.

Simple test:
Code: [Select]
#include <vector>

class Test
{
public:
Test() { };
~Test() { printf("Destructor called\n"); }
};

int main()
{
std::vector<Test> v;
Test test;
v.push_back(test);
v.clear();

return 0;
}


This will return "Destructor called" twice (once from the clear operation, and once for the local variable running out of scope).

However, replace it with:

Code: [Select]
#include <vector>

class Test
{
public:
Test() { };
~Test() { printf("Destructor called"); }
};

int main()
{
std::vector<Test*> v;
Test * test = new Test;
v.push_back(test);

v.clear();

return 0;
}


And it won't print anything at all.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Sound fails on repeated play
« Reply #6 on: April 07, 2011, 05:54:15 pm »
Oh, well you learn something new every day!  :)


This code causes no sound to be heard at all. Is that because the sound is deleted before it had the chance to be played?

Code: [Select]


sf::Sound *sound = new sf::Sound()
sound->SetBuffer(ResourceManager->getSoundBuffer("sound.wav"));
soundList.push_back(sound);

for(int i = 0; i<soundList.size(); i++)
{
    soundList[i]->Play();
    delete soundList[i];
}
soundList.clear();

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Sound fails on repeated play
« Reply #7 on: April 07, 2011, 06:15:20 pm »
Most definitely - yes.

Are you doing this per frame though? That seems largely wasteful. You'll want to load everything in during initialization (as much as possible) - reuse them every frame, and then delete during cleanup/finalization.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sound fails on repeated play
« Reply #8 on: April 07, 2011, 06:38:06 pm »
Quote
Oh, I was under the impression their destructors were called: http://www.cplusplus.com/reference/stl/vector/clear/

The destructor of the stored elements, yes. In this case the elements are pointers, and the destructor of a pointer does nothing at all just like any other primitive type.

Quote
This code causes no sound to be heard at all. Is that because the sound is deleted before it had the chance to be played?

Yes of course. The sound instances must remain alive as long as they are being played.

Quote
Is there a limit on the number of sf::Sound objects you can have?

Yes. It is platform dependent, but it will most likely be 512.
Laurent Gomila - SFML developer

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Sound fails on repeated play
« Reply #9 on: April 07, 2011, 08:05:41 pm »
Quote from: "Laurent"
Quote
Oh, I was under the impression their destructors were called: http://www.cplusplus.com/reference/stl/vector/clear/

The destructor of the stored elements, yes. In this case the elements are pointers, and the destructor of a pointer does nothing at all just like any other primitive type.


Haha, thats so obvious, how did i overlook that!  :roll: