Generating tones with SDL audio

Is it possible (or is there some library) to generate tones using SDL’s
audio functions?

I.e so I can generate a 100Hz tone that lasts 2 seconds, etc.–
I am not certified to remove asbestos

6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

While I don’t know of any code for this in a library, the person you
probably need to ask about this is John Hall, who might be convinced to
write a short function to emit a single tone at a time or so, maybe?

He reads this list, and is the person who taught me how to properly and
sanely mix audio, through his excellent book Programming Linux Games,
published by No Starch Press (not to be confused with the well-enough
written but poorly published Linux Game Programming from Prima…)

I highly recommend the book to any SDL programmer. It is light on some
details for Win32 (obviously it was not targetted for Win32) but it’s the
best thing I’ve found for teaching SDL to people. =)On Thu, May 16, 2002 at 06:36:02PM +0100, James wrote:

Is it possible (or is there some library) to generate tones using SDL’s
audio functions?

I.e so I can generate a 100Hz tone that lasts 2 seconds, etc.


Joseph Carter The guy with a rocket launcher

no BSD fans ?
Elric: it’s hard to be a gamer and a bsd fan :stuck_out_tongue:

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020516/062b8872/attachment.pgp

Is it possible (or is there some library) to generate tones using SDL’s
audio functions?
I.e so I can generate a 100Hz tone that lasts 2 seconds, etc.

While I don’t know of any code for this in a library, the person you
probably need to ask about this is John Hall, who might be convinced to
write a short function to emit a single tone at a time or so, maybe?

Well, a tone is just a sinewave, which you can generate with the sin()
function. Something like this might get you started:

float RenderSinewave(float sine_freq, float output_freq, float start_phase,
float *output, unsigned int length)
{
float phase;
unsigned int i;

for (i = 0, phase = start_phase; i < length; i++) {
output[i] = sin(phase);
phase += 2.0 * 3.141592654 * sine_freq / output_freq;
}
return phase;
}

This function returns the phase of the next sample that would be rendered,
which you should pass into the next RenderSinewave call to avoid glitches
between buffers.

Also note that this uses floating point samples. You’ll probably need to
convert it for 16-bit samples. A lookup table would be a good idea; sin() is
kind of expensive, and floating point conversions are even more so (I lifted
this function out of my (currently unreleased) mixing library, which uses
floating point for everything internally).

He reads this list, and is the person who taught me how to properly and
sanely mix audio, through his excellent book Programming Linux Games,
published by No Starch Press (not to be confused with the well-enough
written but poorly published Linux Game Programming from Prima…)
I highly recommend the book to any SDL programmer. It is light on some
details for Win32 (obviously it was not targetted for Win32) but it’s the
best thing I’ve found for teaching SDL to people. =)

Thanks for the plug. :slight_smile:

-JohnOn Thu, May 16, 2002 at 01:22:02PM -0700, Joseph Carter wrote:

On Thu, May 16, 2002 at 06:36:02PM +0100, James wrote:

“Tone”? What kind of timbre do you want? What kind of accuracy? Quality
requirements?

There’s a lot more to this than you may realize at first, but here’s some
code to play around with for starters:

http://olofson.net/mixed.html	(Look for "speaker.tar.gz".)

Obviously, the emulation is the interesting part. Who wants to drive a
real PC speaker these days? :slight_smile:

You can quite easily replace the highpass filtered square wave with saw
or triangle, and sine isn’t too hard either, unless you want it very
fast. (No point in worrying about that unless you want many voices.)

However, if you want high quality and anything but sine, you’ll have to
look into more advanced oscillators. One of the best
complexity/flexibility/performance tradeoffs seems to be wavetables with
some kind of interpolation. (Cubic/4-point works very well as long as
you’re careful with the highest octave of the wavetable data.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 16 May 2002 19:36, James wrote:

Is it possible (or is there some library) to generate tones using SDL’s
audio functions?

I.e so I can generate a 100Hz tone that lasts 2 seconds, etc.

| On Thursday 16 May 2002 19:36, James wrote:
| > Is it possible (or is there some library) to generate tones using SDL’s
| > audio functions?
| >
| > I.e so I can generate a 100Hz tone that lasts 2 seconds, etc.
|
| “Tone”? What kind of timbre do you want? What kind of accuracy? Quality
| requirements?
|
| There’s a lot more to this than you may realize at first, but here’s some
| code to play around with for starters:
|
| http://olofson.net/mixed.html (Look for “speaker.tar.gz”.)
|
| Obviously, the emulation is the interesting part. Who wants to drive a
| real PC speaker these days? :slight_smile:

That makes the kind of noises I’m after. Now to take it apart and see
what it does. I know it says “No, you’re not supposed to use this for
anything serious!”, but the API seems quite simple, so I might :slight_smile:

| However, if you want high quality and anything but sine, you’ll have to
| look into more advanced oscillators. One of the best

Oh no, I want sound that sounds “crap”, it fits the style of the game
I’m making.On Fri, May 17, 2002 at 02:53:24AM +0200, David Olofson wrote:


I will not Xerox my butt

6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

[…speaker.tar.gz…]

That makes the kind of noises I’m after. Now to take it apart and see
what it does. I know it says “No, you’re not supposed to use this for
anything serious!”, but the API seems quite simple, so I might :slight_smile:

Well, if it does what you need… :slight_smile:

The API is actually a direct port of the Pascal API I used in Project
Spitfire/DOS - and that’s where the “SFX engine” and the sound effects
come from as well. That’s also the reason for the 17 ms tick frequency -
the game used 60 Hz VGA Mode-X, and sound was driven from the main loop.

| However, if you want high quality and anything but sine, you’ll have
| to look into more advanced oscillators. One of the best

Oh no, I want sound that sounds “crap”, it fits the style of the game
I’m making.

Looks like that code could actually be of some use, then. :slight_smile:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Friday 17 May 2002 09:17, James wrote: