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

Author Topic: sf::Sound won't play content from a buffer declared in another class.  (Read 6025 times)

0 Members and 2 Guests are viewing this topic.

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Hi, I am new with audio programming and I am making a simple class for handling sounds in a game I'm working on. The purpose of this class is to load all the sounds needed into sf::SoundBuffer instances and feed this information to the actual sf::Sound instances that will be played in the gamestate.

My simple prototype works like this:
A SoundBuffer is created in my Manager class, the Manager class contains a simple getBuffer() function that I use to return the SoundBuffer instance to the game loop. Once it's there I simply feed the information to the relevant sf::Sound and play it.

I am pretty sure it's some technicality about the SoundBuffer class I am missing, but if that's not the case I could send you my source code.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #1 on: February 20, 2013, 03:54:16 pm »
You should provide a minimal and complete example that reproduces the problem, so we can tell you what you're doing wrong.

My guess is this:
the Manager class contains a simple getBuffer() function that I use to return the SoundBuffer instance

Are you passing it as a copy, reference or smart pointer?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #2 on: February 21, 2013, 10:30:16 am »
You should provide a minimal and complete example that reproduces the problem, so we can tell you what you're doing wrong.

My guess is this:
the Manager class contains a simple getBuffer() function that I use to return the SoundBuffer instance

Are you passing it as a copy, reference or smart pointer?
I tried both passing it as a reference and a copy. I started out with a reference, but then tried passing the whole thing, to be sure I wasn't some syntax thng wrong. At least one of them should have worked.

SoundManager:
Code: [Select]
void Manager::IntSounds()
{
buffer.loadFromFile("myfilename.ogg");

}
...

sf::SoundBuffer& Manager::getBuffer()
{

return buffer;
}
Th sound is loaded to main and played just before the game loop starts:
Code: [Select]
       Sound sound;
SoundBuffer buffer = manager.getBuffer();
sound.setBuffer(buffer);
        sound.play();
As long as I don't destroy the Manager instance before I call manager.getBuffer() this should, no?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #3 on: February 21, 2013, 10:34:42 am »
Please make use of the code=cpp tag, when posting code. ;)

SoundBuffer buffer = manager.getBuffer();
COPY! ;D

If you return a reference you don't need to crate a local variable, but you can directly use it.
sound.setBuffer(manager.getBuffer());

Or if you really want a local variable, you'll have to use a reference.
SoundBuffer& buffer = manager.getBuffer();
sound.setBuffer(buffer);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #4 on: February 21, 2013, 10:47:25 am »
Good tip, but it doesn't solve my problem. I still have the same problem. I realize know that using a reference like this won't work, as the SoundBuffer will fall out of scope before it's used. But neither passing the buffer as a reference, nor passing the actual data, will make a sound anyway  :o

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #5 on: February 21, 2013, 11:07:01 am »
Does the sound example that comes with SFML work?

If so, you'll have to provide a minimal and complete example that reproduces the problem.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #6 on: February 21, 2013, 11:41:49 am »
I can't find the source code for the example in my SFML folder, is it not supposed to be there? Anyhow, the .exe won't launch because of a missing .dll, but my code does not have this problem.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #7 on: February 21, 2013, 11:46:45 am »
I can't find the source code for the example in my SFML folder
The source is on GitHub.

Anyhow, the .exe won't launch because of a missing .dll.
Just copy the needed DLLs next to the example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #8 on: February 21, 2013, 11:58:02 am »
Thank you for your help so far, the code was certainly interesting, but it doesn't solve my issue. My problem is most likley due to memory loss while transferring the buffer from my manager class to my main loop.
If I do all the steps in one place it works.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #9 on: February 21, 2013, 12:41:27 pm »
If you had posted your complete code in your first post, I'd say you're problem would've been solved in the first answer. But since you're still not willing to share code, you'll probably never get a solution... :-\
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Strangled Bear

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #10 on: February 21, 2013, 02:58:10 pm »
I have decided to go for a different solution all together. The sound manager will now be responsible for loading and playing of sounds. Therefore I won't need to pass data around between the manager and the main code, all I need is to call a simple function that plays the sound I want to play.

Thanks for your support, but I will work around it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Sound won't play content from a buffer declared in another class.
« Reply #11 on: February 21, 2013, 03:00:27 pm »
Thanks for your support, but I will work around it.
A workaround should never be the solution to a problem, just to avoid a small learning curve or minimize some code to show to others. Sooner or later you'll be faced with the same problem again... :-\
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything