### Author Topic: sound /music loads successfully but no sound at all  (Read 14178 times)

0 Members and 1 Guest are viewing this topic.

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### sound /music loads successfully but no sound at all
« 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.
Alien 8

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #1 on: August 10, 2015, 01:46:50 pm »
SFML has a manual?

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
(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.?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #2 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
Alien 8

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #3 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.

You haven't addressed anything I mentioned in my previous post.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #4 on: August 10, 2015, 06:50:20 pm »
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
« Last Edit: August 10, 2015, 06:52:12 pm by alien8 »
Alien 8

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #5 on: August 10, 2015, 11:35:56 pm »
it's the C::B tuto code with the example code of sound+music tuto
This and this?

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();
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #6 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.

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
« Last Edit: August 11, 2015, 10:14:15 am by alien8 »
Alien 8

• Sr. Member
• Posts: 302
##### Re: sound /music loads successfully but no sound at all
« Reply #7 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.

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #8 on: August 11, 2015, 10:37:59 am »
I take note
Alien 8

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #9 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!
Alien 8

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #10 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. 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### Jesper Juhl

• Hero Member
• Posts: 1405
##### Re: sound /music loads successfully but no sound at all
« Reply #11 on: August 11, 2015, 04:52:02 pm »
My Jukebox class from the wiki may also be inspirational

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #12 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
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### alien8

• Newbie
• Posts: 16
• 8 bits rocks!
##### Re: sound /music loads successfully but no sound at all
« Reply #13 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
Alien 8

#### Hapax

• Hero Member
• Posts: 3100
• My number of posts is shown in hexadecimal.
##### Re: sound /music loads successfully but no sound at all
« Reply #14 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.

I guess I was not as brief as I had hoped...
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision