Real-time audio generation

Hi,

I am looking for a working example of an SDL audio application, where
you don’t read the data from a wave file, but generate it real-time.
So e.g. you press a button, and hear a sinus tone. I know how to do it
with ALSA and with JACK, however when using SDL_OpenAudio() etc. the
only result was distorted noise.

It would be convenient if the audio would run in it’s own thread,
because in a real application this always would be the case.

I searched the whole internet (with Google Code Search), but a simple
example I could not find.

W.Boeke

[…]

DT-42, maybe?

http://olofson.net/mixed.html

It’s a full, “usable” application, but still rather simple and
minimalistic. (Pretty close to what I actually intended, for
once! :wink:

There is also “simplemixer”:
http://olofson.net/examples.html

However, it’s a bit too simple when it comes to the main
application/audio callback interaction. I wouldn’t recommend
designing a real application that way.

Instead, use proper lock-free constructs for no risk of disturbing the
audio thread scheduling - or just use
SDL_LockAudio()/SDL_UnlockAudio() to keep the callback away when
messing with shared structures.

Either way is fine normally, but the latter is a bad idea if you want
rock solid low latency playback on a platform that supports real time
scheduling. (You effectively disable the real time scheduling as soon
as you block the audio callback from a non-real time thread - unless
you’re on QNX or some other OS with priority inheritance or similar
workaround, that is.)

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Friday 19 September 2008, W.Boeke wrote:

Hi,

I am looking for a working example of an SDL audio application,
where you don’t read the data from a wave file, but generate it
real-time.

Hi David,

A nice tool indeed, however it looks like you still read your sounds
from wave files. Or did I miss something? I want to generate waveforms
in real-time, using for instance a sinus generator!

Wouter

David Olofson wrote:> On Friday 19 September 2008, W.Boeke wrote:

Hi,

I am looking for a working example of an SDL audio application,
where you don’t read the data from a wave file, but generate it
real-time.
[…]

DT-42, maybe?

http://olofson.net/mixed.html

It’s a full, “usable” application, but still rather simple and
minimalistic. (Pretty close to what I actually intended, for
once! :wink:

Hi David,

A nice tool indeed, however it looks like you still read your sounds
from wave files. Or did I miss something? I want to generate waveforms
in real-time, using for instance a sinus generator!

Wouter

An audio buffer is an audio buffer. Just fill a given audio buffer with
your progmatically generated content and spit that at the audio backend. No
different than loading sections of a large audio file bits at a time into
the audio backend, except you substitute your generators in place of file
reading.

If you’re fishing for documents on how to generate different waveforms and
stuff, ya should ask more bluntly so :slight_smile:

-WillOn Fri, Sep 19, 2008 at 10:10 AM, W.Boeke <w.boeke at chello.nl> wrote:

Hi David,

A nice tool indeed, however it looks like you still read your sounds
from wave files.

Ah! Yes, the percussion sounds are indeed .WAV files.

Or did I miss something? I want to generate waveforms
in real-time, using for instance a sinus generator!

Well, there is a simple FM synth in DT-42. Just a trivial fun hack
using trig functions (not very efficient), but you get the idea.

For a “slightly” more advanced software synth, you could check out
Kobo Deluxe which, as of version 0.5.1, uses only algorithmically
generated sounds.

http://www.olofson.net/kobodl/

BTW, that sound engine is the precedessor of Audiality:

http://www.audiality.org/

No major functional differences - and both are still rather messy
beasts. There’ll be a major rewrite eventually, but until then, the
version in Kobo Deluxe is probably the best bet if you want to use it
for anything. The Audiality 0.1.x branch is a dead end.

Anyway, what exactly are you looking for? :slight_smile:

The audio source (loaded data or generated data) is irrelevant to how
you deal with the API - and the API is irrelevant if you already know
how to generate audio. Either way, you have to figure you how to get
the API to understand your data, and/or convert your data as needed.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Friday 19 September 2008, W.Boeke wrote:

Will Langford wrote:

An audio buffer is an audio buffer. Just fill a given audio buffer with
your progmatically generated content and spit that at the audio backend. No
different than loading sections of a large audio file bits at a time into
the audio backend, except you substitute your generators in place of file
reading.

That’s exactly what I did, however the result was horrible.

If you’re fishing for documents on how to generate different waveforms and
stuff, ya should ask more bluntly so :slight_smile:

-Will

I’m no fisher, and generating waveforms is my speciality :wink:

Please, please 1 small, working example …

Wouter

Really the callback provided to SDL_OpenAudio should be all you need.
I’m not sure what the problem is exactly.

Regardless, here’s some pretty much bare bones code, with comments. I
won’t promise that it does everything right though:
http://code.google.com/p/quake-gamecube/source/browse/trunk/quake/src/sdl/sound.cpp

Rip out the Quake specific parts and you’ll be left with an empty
callback function which you should complete to generate audio data.

Hope this helps,

Peter

2008/9/19 W.Boeke <w.boeke at chello.nl>:> Will Langford wrote:

An audio buffer is an audio buffer. Just fill a given audio buffer with
your progmatically generated content and spit that at the audio backend.
No
different than loading sections of a large audio file bits at a time into
the audio backend, except you substitute your generators in place of file
reading.

That’s exactly what I did, however the result was horrible.

If you’re fishing for documents on how to generate different waveforms and
stuff, ya should ask more bluntly so :slight_smile:

-Will

I’m no fisher, and generating waveforms is my speciality :wink:

Please, please 1 small, working example …

Wouter


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

Damnit! I’ve been thinking about ‘synth-only’ sounds as part of
libcrtxy, to get that cool retro effect. (Well, except for good old
"Use the Force, Luke!" from Star Wars arcade ;^) … but hey, that was
ahead of its time!)

Perhaps I’ll need to poke around. Any chance I could get the rights to
redistribute some derivative as LGPL?

Or I guess I should look at Audality, finally…

-bill!On Fri, Sep 19, 2008 at 05:44:00PM +0200, David Olofson wrote:

For a “slightly” more advanced software synth, you could check out
Kobo Deluxe which, as of version 0.5.1, uses only algorithmically
generated sounds.

http://www.olofson.net/kobodl/

Well, they’re basically the same, apart from the latter being packaged
as a proper library. Audiality introduces some minor changes, but I
backported the relevant parts to Kobo Deluxe for 0.5.1, as I wanted
to use the Audiality command line player when I created the new
sounds.

Most of the Kobo Deluxe code is LGPL anyway - basically everything
that isn’t based on the original XKobo source code. If you’re just
going to pick some of the DSP code or something, chances are you
won’t even see the little GPL code there is left in the Kobo Deluxe
sound engine.

The reason I’m suggesting the “old” Kobo Deluxe sound engine rather
than the “official” Audiality version is that the latter, in it’s
current form, is an abandoned project, whereas the one in Kobo Deluxe
is still maintained as part of the game.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Tuesday 23 September 2008, Bill Kendrick wrote:

On Fri, Sep 19, 2008 at 05:44:00PM +0200, David Olofson wrote:

For a “slightly” more advanced software synth, you could check out
Kobo Deluxe which, as of version 0.5.1, uses only algorithmically
generated sounds.

http://www.olofson.net/kobodl/

Damnit! I’ve been thinking about ‘synth-only’ sounds as part of
libcrtxy, to get that cool retro effect. (Well, except for good old
"Use the Force, Luke!" from Star Wars arcade ;^) … but hey, that
was ahead of its time!)

Perhaps I’ll need to poke around. Any chance I could get the rights
to redistribute some derivative as LGPL?

Or I guess I should look at Audality, finally…