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.
-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: