SFML community forums

Help => Audio => Topic started by: alien8 on August 10, 2015, 12:55:56 pm

Title: sound /music loads successfully but no sound at all
Post by: alien8 on August 10, 2015, 12:55:56 pm
Hello to all and thanks for your time.
Having put all the sources provided in the tutorials in Code::Blocks 13.12, win XP
I'm with problems only at the audio part. My version of SFML is 2.3 and there is no necessity to put additional libraries in the .EXE folder (at least that says the last manual, the 2.3, only the opengl dll) to my knowledge, but the sound is unable to sound. The code is being executed with success (debugged) but no sound.
I don't put the code here because is simply the one provided in the 2.3 manual, having tried with the sound part and the music part.
So if you like me to put more data... I'm grateful for your help.
Thanks.
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 10, 2015, 01:46:50 pm
SFML has a manual?  :o

Are you linking dynamically?
You'll need: sfml-audio.dll, sfml-system.dll  (or their debugging equivalents: -d) and openal.dll
in the same folder as the executable file.

Which code are you trying exactly? Can you post it here? (If it's a tutorial code, it should be short and compilable already).

only the opengl dll
openal, but I assume that was just a typo  ;D
(gl means graphics library, al means audio library)


I really don't want to have to write this but I must ask: are you certain that your speakers are on etc.?
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 10, 2015, 02:39:51 pm
well, I must confess to be a little nervous because it's the first time I write on the forum , much respect to veterans around here, and the fact that I'm newcomer to the programming world (outside of many years of experience with C++ in DOS environments). The speakers no problem (desktop)
Another detail I forgot: the code is integrated in the code for the CodeBlocks setup, but I saw to be making correctly the instructions
Any detail more? I'm glad to be responsed
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 10, 2015, 03:34:39 pm
Hi, and welcome to the forum  :)

There doesn't seem to be any code that creates sound in the Code::Blocks tutorial (http://www.sfml-dev.org/tutorials/2.3/start-cb.php).

You haven't addressed anything I mentioned in my previous post.
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 10, 2015, 06:50:20 pm
Well, thanks for answer.
I didn't reply correctly for being in a hurry, no internet connection on the machine I code in  :-\

The "manual for me are the tutorials
The tests done are always dinamic
The code? I'll try to post it in a few hours. Reason: my first paragraph...but it's the C::B tuto code with the example code of sound+music tuto

For OpenGL... Well the familiarization with openAl is more recent, so I was confused.

On another point, I didn't well in saying to be newcomer...plenty of tests\personal manuals\partial games in C and C++ in DOS
Even complete games in VB6
Possible hint to be that the execution lasts less than half a second from the .exe itself but waits to be manually closed from the IDE
Hope it helps... Again thanks very much for the provided help and interest
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 10, 2015, 11:35:56 pm
it's the C::B tuto code with the example code of sound+music tuto
This (http://www.sfml-dev.org/tutorials/2.3/start-cb.php) and this (http://www.sfml-dev.org/tutorials/2.3/audio-sounds.php)?

Providing the exact combined code you used would help here.

Are you getting any errors in the console?
Make sure that the files can be found and loaded (the correct format).

Are you destroying the soundbuffer? It needs to stay alive as long as any sounds use it.

One other thing that is quite common:
Make sure that you're not just playing the sound in the main loop. This attempts to start the sounds every frame (play starts the sound from the beginning).
Solutions:
Start it based on an event,
Play it before the main loop,
or this:
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 11, 2015, 10:09:31 am
After setting up successfully C::B and tested with same success with the code in the first tutorial (http://www.sfml-dev.org/tutorials/2.3/start-cb.php), I was unable to get sound through the integration of the code in "Playing sounds and music tutorial" (http://www.sfml-dev.org/tutorials/2.2/audio-sounds.php).
I figured out the best place to put that rutine so it wouldn’t be ignored by the window routine, so the code itself is
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display(); // till here, code from lesson 1

sf::SoundBuffer buffer; // sound routines from "Playing sounds and music". First the sound
if (!buffer.loadFromFile("loose.wav"))
            return -1;
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();

sf::Music music; // Now the music routines
  if (!music.openFromFile("loose.wav"))
  return -1; // error
music.play();
    }

    return 0;
}


Important: this is the baseline code, customized afterwards with all kind of tricks to be able to hold music.play() and not be missed, like flags, if’s sentences and more.
Another important note is that I try to make the changes inside the event loop.

I linked dynamically.
In the tutos, it says to copy DLLs into the .EXE folder: libsndfile-1.dll and OpenAL32.dll in the 2.2 version, but only the openal one if you’re in 2.3
So I copied.

But the best I got was very scarce broken or partial sounds (indeed, with no changes to the code you see), I think due to put the sentences in the whiles and failing to correctly controlling music+sound (I also made use of the several methods for these two).
Well, this is till now the best I can
Note: it's a laptop, and this post is written offline, unaware of changes
Title: Re: sound /music loads successfully but no sound at all
Post by: shadowmouse on August 11, 2015, 10:19:18 am
That while loop is just going to keep repeating an repeating and the sound and music are going out of scope and then being recreated. Create them before the while loop.
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 11, 2015, 10:37:59 am
I take note
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 11, 2015, 12:19:38 pm
well, my instict said to me to be chance of problems due to that while (now I know is the game/main loop)
But I read well the tutorial(s) and I thought finally not to be problems derived by the existence of that while because play was to start , not for replay. At least that was that I understood. I'll fix it. Many thanks!
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 11, 2015, 04:48:57 pm
first tutorial (http://www.sfml-dev.org/tutorials/2.3/start-cb.php)
[...]
"Playing sounds and music tutorial" (http://www.sfml-dev.org/tutorials/2.2/audio-sounds.php).
I don't know why you're using one tutorial for 2.3 and one for 2.2. If you're using 2.3, use the 2.3 tutorials (there may be no differences but at least you can be reasonably sure that it'll match the version you are using if there are) as linked in my above post.

One other thing that is quite common:
Make sure that you're not just playing the sound in the main loop. This attempts to start the sounds every frame (play starts the sound from the beginning).
Solutions:
Start it based on an event,
Play it before the main loop,
or this:
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
It looks like your code does exactly this. It tried to start the sound from the beginning every frame.
However, as shadowmouse pointed out, it's much worse here. You're actually creating the soundbuffers and loading the sounds in from the file every frame/cycle/tick.

It loads creates the soundbuffer, loads the sample, creates a sound, starts the sound playing and then immediately destroys all of those objects and does it all over again..

If you'd like to have a look at a (simple) working game with (few) sounds and music, feel free to have a look at my entry for the last game jam. Here's the main.cpp (https://github.com/Hapaxia/BringItBack/blob/master/main.cpp). It may be quite long and uses a separate class to load into the sound buffer (the sound is called "activate") but you should be able to see that it does all of the loading completely before the main loop. A sound can then just be play()-ed when necessary.
Title: Re: sound /music loads successfully but no sound at all
Post by: Jesper Juhl on August 11, 2015, 04:52:02 pm
My Jukebox class (https://github.com/SFML/SFML/wiki/Source:-Jukebox) from the wiki may also be inspirational :)
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 11, 2015, 04:59:23 pm
I wanted to show something smaller and simpler but I realised that it was the only thing I had available with sound :(
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 12, 2015, 01:32:40 pm
Well, the first thing I want to say today is I' amazed for the quality of this community and the software it supports.
I'm discovering it with joy day after day
I did research in other threads, and I saw 'no exact match' of my problem, so I posted this.

And my last research is about the proper and correct integration of "my code" into the 'skeletal' code provided by the several tutorials.
So I had the problem as correctly pointed out by shadowmouse and Hapax:
creating in the main loop the different sounds, time after time
I was mistaken by this paragraph in the tutos:
Quote
To control playback, the following functions are available:

    play starts or resumes playback
I incorrectly interpretd it as if the play starts once and it isn't necessarily as the play in a mp3 player
And just like you pointed me out, worse when I create buffer just in the loop.
Disastrous results
So, now my doubts acquires a more strategic approach:
Do we need to put in the main loop only code for the window realted stuff?
I was in the belief that the event loop integrated itself into the main loop doubles as 'supervisor', or the loop that helps create the passing by of the game.
Hopefully, with your hints and code, I'll be able to identify that part of my many doubts
The testings showed me to play fine outside the main loop, now I want to integrate it (with every portion of any program) inthe correct loop. Is the event loop only for the management of the events itself? and how is it different from the main one?
Many thanks and I understand that this post may falls under another module
Title: Re: sound /music loads successfully but no sound at all
Post by: Hapax on August 12, 2015, 08:09:40 pm
I was mistaken by this paragraph in the tutos:
Quote
To control playback, the following functions are available:
    play starts or resumes playback
Resume playback here does not mean continue the playback (necessary to play the sound) but to resume from being paused.
Play starts the sound playing and it will continue to play until finished, paused, stopped, destroyed, or its buffer is changed.

Do we need to put in the main loop only code for the window realted stuff?
I was in the belief that the event loop integrated itself into the main loop doubles as 'supervisor', or the loop that helps create the passing by of the game.
Hopefully, with your hints and code, I'll be able to identify that part of my many doubts
The testings showed me to play fine outside the main loop, now I want to integrate it (with every portion of any program) inthe correct loop. Is the event loop only for the management of the events itself? and how is it different from the main one?
Many thanks and I understand that this post may falls under another module
This is off-topic for the SFML forum so I'll try to be brief.

The main loop is for code that continuously runs, not for things that exist during that time.
Generally, all objects should be created before the loop, modified in the main loop and then destroyed after the main loop (this is usually automatic).

Generally...
In the main loop, you need three general things: input, update, render.
The event loop is a way of processing the input given to your window. That's all that should be in the event loop - processing the events.
Then, update everything. Text, graphics, physics, hp, score, colour of background etc.. The way this is updated may changed depending on events that may have occurred. Also, in some cases, this update may be looped. This is a timestep thing and might be a little advanced but you can read an article about this: http://gafferongames.com/game-physics/fix-your-timestep/
After the input has been processed, then everything is updated to be what it should, you render. That is, you clear the window, draw the objects required, and then display the window.

If things are sometimes shown but sometimes not shown, just don't draw them; it's likely that you don't need to destroy it if it'll be re-created or it will re-appear.

So, generally, create everything before the loop.
There are, of course, exceptions to this. Things like bullets that are dynamically created can be...created dynamically within the main loop. They are still only created once (per bullet, for example); once they are created, they are then modified while needed and then finally destroyed when they are no longer needed.

For information about events, you should read, read, re-read, and read again this tutorial (http://www.sfml-dev.org/tutorials/2.3/window-events.php) until you fully understand it.

You should probably google about this topic too. "Game loop", "event loop", "message loop" and "time step" are all probably good starting points for your searches.

I guess I was not as brief as I had hoped...  :-X
Title: Re: sound /music loads successfully but no sound at all
Post by: alien8 on August 13, 2015, 01:07:40 pm
I apologize if this post appears to not consider your last response, Hapax, you're so kind but I must paste prewritten posts on the run...OK I know my next stop is the system (window maybe more) module.


The biggest error was to be mistakenly convinced that due to the presence of the 'if (!buffer.loadFromFile("..."))' the buffer of the sound wouldn't be not only damaged but also repeated or born from zero.
That is precisely the reason
"
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
"
that brillantly was suggested gets its purpose defeated

I don't know why I was using 2,2 tuto but the only difference to 2.3 is that it doensn't specify the list of supported formats

Having read the system module, etc. I tend to think that the position of the main loop is for facilitate, among other things, space for events (through the event loop itself) and that's the reason you must not integrate the bulk of the code into the main loop.
But then it arises the question of what about the supervisor ( ...is that the name for the loop in wich the course of the game takes place? ).

Anyways, I want to think that if the buffers are not destroyed, the sound variables are available, and not only that, if there is playing any music, it arrives to the end if no method interrupts it. Another question is if you include all this in a thread and that thread is affected by something.
Am I in the right track?.


..so many thanks, I hope this solves initial doubts of mine and can focus on the provided links by Hapax
Title: Re: sound /music loads successfully but no sound at all
Post by: Jesper Juhl on August 13, 2015, 09:56:27 pm
The biggest error was to be mistakenly convinced that due to the presence of the 'if (!buffer.loadFromFile("..."))' the buffer of the sound wouldn't be not only damaged but also repeated or born from zero.
That is precisely the reason
"
if (sound.getStatus() != sf::Sound::Playing)
    sound.play();
"
that brillantly was suggested gets its purpose defeated
Not really.
Your biggest error (to use your own words) here is to not realize a very basic C++ fact; that objects created in a scope, on the stack, get destroyed when that scope (in this case, the game loop) ends.  This is very fundamental C++ knowledge and, had you had that knowledge, it would/should have made it obvious that the "sound" object would not live for more than a single frame. The if statement testing whether or not the sound is currently playing is completely irrelevant in that context.

Having read the system module, etc. I tend to think that the position of the main loop is for facilitate, among other things, space for events (through the event loop itself) and that's the reason you must not integrate the bulk of the code into the main loop.
But then it arises the question of what about the supervisor ( ...is that the name for the loop in wich the course of the game takes place? ).
The main "game" loop is what (in most games) drives everything. It is responsible for processing events, updating game state and rendering the current state of the world. It's what makes the game "tick".
The main game loop repeats over and over - every frame - and it's where you do all your processing. But, that doesn't mean that you need to create all objects within the loop. Long lived objects usually get created before the loop and persist for the entire lifetime of the application. Objects that need to be dynamically created (but need to live more than a single frame) are usually created on the heap and stored as std::unique_ptr or std::shared_ptr objects - commonly in STL containers like std::vector or similar.

Anyways, I want to think that if the buffers are not destroyed, the sound variables are available, and not only that, if there is playing any music, it arrives to the end if no method interrupts it.
You may think what you want. But that's just not good enough. You need to know how the lifetime rules for objects in C++ work.
As long as your sound object is alive and there's something to play in its buffer and play() has been called on it, it will play. But if you destroy the object (for example; by letting it go out of scope) or destroy the buffer it's playing from, then - surprise, surprise - it will stop playing.

Another question is if you include all this in a thread and that thread is affected by something.
Music already plays in its own thread. You don't need to worry about that...  Please, please don't even consider threads until you have a much firmer grasp of C++ (I'd say at least 5 more years of experience or so).

Am I in the right track?.
Personally, I'd say no.
You seem to be guessing randomly and making assumptions that you have no basis for.
You should attempt to get a firmer grasp of the C++ language before proceeding or you'll only get yourself into trouble that you'll have no idea how to dig yourself out of.