Some help with Audio Playback

Hi all !

I am new with audio in general and SDL audio in particular, so I need some advice even in what could be a trivial case.
I need to play a very short WAV with the sound of a ball hit. It is important to avoid any blocking function call, because the movement of the balls looks discontinuous.

So I try to put the sequence of : SDL_LoadWAV(), SDL_OpenAudio(), SDL_PauseAudio(0),… in a new thread. But when SDL_OpenAudio() or SDL_OpenAudioDevice() is called from the new thread (different from the main thread) they fail with the error message of not possible to open audio device.

If I start the audio in the same thread of the physics and animation sound plays well but I can not avoid a perceptible halt in the balls animation.

Any idea ?

Thanks to all of you.------------------------
Armando Alaminos Bouza

Well if you’re using it a lot, just cache it. Load it once (maybe during
loading screen), and keep it in memory.On Wed, Nov 20, 2013 at 6:58 PM, alabouza wrote:

Hi all !

I am new with audio in general and SDL audio in particular, so I need some
advice even in what could be a trivial case.
I need to play a very short WAV with the sound of a ball hit. It is
important to avoid any blocking function call, because the movement of the
balls looks discontinuous.

So I try to put the sequence of : SDL_LoadWAV(), SDL_OpenAudio(),
SDL_PauseAudio(0),… in a new thread. But when SDL_OpenAudio() or
SDL_OpenAudioDevice() is called from the new thread (different from the
main thread) they fail with the error message of not possible to open audio
device.

If I start the audio in the same thread of the physics and animation sound
plays well but I can not avoid a perceptible halt in the balls animation.

Any idea ?

Thanks to all of you.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Wait a second… are you initializing the sound hardware every time
the sound plays
? Yeah, no wonder. You’re supposed to do that once
when the program starts and keep it around until the program closes.

I don’t know why the behavior changes depending on the thread, though
(I don’t even know why your method works from the main thread, in
fact, you should be able to open each device only once unless you
close them).

[…]

So I try to put the sequence of : SDL_LoadWAV(), SDL_OpenAudio(),
SDL_PauseAudio(0),… in a new thread. But when SDL_OpenAudio() or
SDL_OpenAudioDevice() is called from the new thread (different from the main
thread) they fail with the error message of not possible to open audio
device.
[…]

The SDL audio API is a very low level one, and isn’t intended to be
used like this. You’re supposed to call SDL_OpenAudio() once,
initialize some kind of “audio engine” (load sounds, create structures
to manage the number of voices or similar that you need etc), activate
it all with SDL_PauseAudio(0), and then have this “engine” generate a
continuous stream of audio, until it’s time to close.

If you want to do it the hard way; rolling your own, and learning a
bit about audio processing in the process, my ancient "simplemixer"
example might be a starting point:
http://olofson.net/examples.html

There are probably other, better ones out there. (Maybe I should write
a proper one…?) This one doesn’t even do resampling, which is pretty
much required in any real application. Also, this is for SDL 1.2, so
it might need some changes for 2.0.

The easiest way is probably to use an existing mixer/engine of some
sort. The most obvious alternative would be SDL_mixer:
http://www.libsdl.org/projects/SDL_mixer/

SDL_sound may also be of interest. Originally, this dealt only with
loading/decoding of audio files, but there is/was apparently a mixer
in the works as well for 2.0, though I don’t know what happened with
that:
http://www.icculus.org/SDL_sound/

Of course, there is Audiality 2 that I'm working on now, and Audiality "1" (the sound engine of Kobo Deluxe), but although either would do the job, the old one is abandoned, the new one would still need SDL_sound or something to load anything but raw audio files (I suppose you could use SDL_LoadWAV() with it...) - and both are kind of odd, complex beasts, more focused on synthesis, procedural sound and stuff. A2 will become nicer, get authoring tools and stuff eventually, but as it is, you'll need to learn a small scripting language to do anything much with it. :-) On Wed, Nov 20, 2013 at 5:58 PM, alabouza wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

The idea of Mr. Rubinson is working for me. I loaded the WAV once, at the beginning, and using SDL_PauseAudio(1) and SDL_PauseAudio(0) the sound plays almost synchronously with the hit , without animation discontinuity. It is not perfect yet but I hope to solve the details.
Thanks to all.------------------------
Armando Alaminos Bouza

Please, just Ivan. You make me sound like some professor at Harvard or MIT.
I’m just an 18 years old virgin locked up in a cave with a PC, somehow
magically connected to the world wide web.

As others have mentioned, you might want to do the initialization and
deinitialization of the library once, and not every time you play a sound.On Thu, Nov 21, 2013 at 1:05 AM, alabouza wrote:

The idea of Mr. Rubinson is working for me. I loaded the WAV once, at
the beginning, and using SDL_PauseAudio(1) and SDL_PauseAudio(0) the sound
plays almost synchronously with the hit , without animation discontinuity.
It is not perfect yet but I hope to solve the details.
Thanks to all.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

soloud audio engine could be your answer. it looks easier to use. I never
wanted to use anything other than sdl mixer since I found it years ago but
soloud is now a fav. besides you could compare the two with your issue with
very little effort.On Wed, Nov 20, 2013 at 10:58 AM, alabouza wrote:

Hi all !

I am new with audio in general and SDL audio in particular, so I need some
advice even in what could be a trivial case.
I need to play a very short WAV with the sound of a ball hit. It is
important to avoid any blocking function call, because the movement of the
balls looks discontinuous.

So I try to put the sequence of : SDL_LoadWAV(), SDL_OpenAudio(),
SDL_PauseAudio(0),… in a new thread. But when SDL_OpenAudio() or
SDL_OpenAudioDevice() is called from the new thread (different from the
main thread) they fail with the error message of not possible to open audio
device.

If I start the audio in the same thread of the physics and animation sound
plays well but I can not avoid a perceptible halt in the balls animation.

Any idea ?

Thanks to all of you.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I solved my particular problem with the idea posted by Ivan: loading wav only once , opening de audio device and playing or pausing when needed. I also had to trim the wav file to the relevant sound of the hit and play it just till the end to avoid undeflow.

But any way, I will check SoLoud to learn something more about audio, this is absolute new for me. There is some problem with SoLoud for me: there is no C interface ( ? ), only found C++ .
Thanks.

[quote=“R Manard”]soloud audio engine could be your answer. it looks easier to use. I never wanted to use anything other than sdl mixer since I found it years ago but soloud is now a fav. besides you could compare the two with your issue with very little effort.------------------------
Armando Alaminos Bouza

If you look and see what it takes to use the library in your C program
you’ll find it’s really nothing at all of a change . SDL is written in C
and C++ and you can use it on so many platforms and with so many languages
. I probably shouldn’t mention it here but I will… I saw a review of
Mobile Phone development tools written on October 15th and saw that 15 of
them were listed with no mention of sdl, pissed me off. Some of them were
crap compared to sdl2On Nov 22, 2013 7:11 AM, “alabouza” wrote:

I solved my particular problem with the idea posted by Ivan: loading wav
only once , opening de audio device and playing or pausing when needed. I
also had to trim the wav file to the relevant sound of the hit and play it
just till the end to avoid undeflow.

But any way, I will check SoLoud to learn something more about audio, this
is absolute new for me. There is some problem with SoLoud for me: there is
no C interface ( ? ), only found C++ .
Thanks.

[quote=“R Manard”]soloud audio engine could be your answer. it looks
easier to use. I never wanted to use anything other than sdl mixer since I
found it years ago but soloud is now a fav. besides you could compare the
two with your issue with very little effort.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org