How to generate sound of given frequency and duration?

Hi,

I am trying to port one old DOS game that just beeps its sounds to PC-Speaker or AdLib if it finds one. The frequency and 

duration is computed at run-time. What do I have to use in SDL to port this functionality ? May be there is some extension or
3rd-party library to achieve this ?

I am porting to SDL under Linux+X11 now, but I would prefer to know about portable functions for sound generation.

Thank you in advance for your replies.

Hi,

I am trying to port one old DOS game that just beeps its sounds to
PC-Speaker or AdLib if it finds one. The frequency and duration is
computed at run-time. What do I have to use in SDL to port this
functionality ?

There isn’t anything like it, really, so it has to be emulated on top
of the digital audio (“PCM”) API.

Many platforms don’t have anything remotely like the PC speaker in the
hardware, and most that do don’t offer any useful APIs for them. So,
there’s no point in having SDL support such things.

And who really wants the Real Thing anyway…? :slight_smile:

May be there is some extension or 3rd-party library
to achieve this ?

Actually, there is - and it’s even specifically meant to play sounds
created for a DOS game and the PC speaker:

http://olofson.net/mixed.html
(Look for speaker.tar.gz way down on that page.)

It’s even got a high pass filter to make it sound more like the Real
Thing, and a feedback delay to make it bearable with headphones. :slight_smile:

Seriously, it’s just a fun hack, but it might do the job for you.

You may want to throw the “sound effect sequencer” away if you have
your own code for that part. The actual speaker emulator can be used
on it’s own.

I am porting to SDL under Linux+X11 now, but I would prefer to
know about portable functions for sound generation.

Well, there are drivers for the Real Thing (the horrible PC speaker,
that is) through Linux console and X11, but those aren’t very
portable, and can’t offer the same control as programming the speaker
to the metal. I just hacked them to see if it would work - and it
does, sort of…

However, the normal output method is through the speaker emulator,
which uses the normal SDL audio API for output. That’s about as
portable as it gets.

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

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Wednesday 09 July 2003 16.51, Wladimir Mutel wrote:

I am trying to port one old DOS game that just beeps its sounds to
PC-Speaker or AdLib if it finds one. The frequency and duration is
computed at run-time. What do I have to use in SDL to port this
functionality ?

There isn’t anything like it, really, so it has to be emulated on top
of the digital audio (“PCM”) API.

Many platforms don’t have anything remotely like the PC speaker in the
hardware, and most that do don’t offer any useful APIs for them. So,
there’s no point in having SDL support such things.

And who really wants the Real Thing anyway…? :slight_smile:

PC speaker, yes, is mostly unusable, but why drop Adlib ?
Should the programmer really emulate its functionality and form waves manually in software ?
Don't like to feel that ... :< :>

May be there is some extension or 3rd-party library
to achieve this ?

Actually, there is - and it’s even specifically meant to play sounds
created for a DOS game and the PC speaker:

http://olofson.net/mixed.html
(Look for speaker.tar.gz way down on that page.)

It’s even got a high pass filter to make it sound more like the Real
Thing, and a feedback delay to make it bearable with headphones. :slight_smile:

Seriously, it’s just a fun hack, but it might do the job for you.

You may want to throw the “sound effect sequencer” away if you have
your own code for that part. The actual speaker emulator can be used
on it’s own.

Thank you very much, I certainly will look into this.
As I understand, its synthesis is purely in software.

I am porting to SDL under Linux+X11 now, but I would prefer to
know about portable functions for sound generation.

Well, there are drivers for the Real Thing (the horrible PC speaker,
that is) through Linux console and X11, but those aren’t very
portable, and can’t offer the same control as programming the speaker
to the metal. I just hacked them to see if it would work - and it
does, sort of…

However, the normal output method is through the speaker emulator,
which uses the normal SDL audio API for output. That’s about as
portable as it gets.

I found recently that SDL mixer contains support for native MIDI.
But for some reason (license?) it is not compiled into my Debian package.
May be I should go in that direction ?

[…]

And who really wants the Real Thing anyway…? :slight_smile:

PC speaker, yes, is mostly unusable, but why drop Adlib ?
Should the programmer really emulate its functionality and form
waves manually in software ? Don’t like to feel that … :< :>

Well, it’s just that you don’t have much choice. It seems like you
can get “direct” control over an Adlib compatible device through
the Linux drivers, but that’s an exception AFAIK. Normally, all you
get is a General MIDI driver with a hardcoded sound set. As if that
wasn’t bad enough, sound sets differ quite significantly between
drivers.

[…speaker.tar.gz…]

Thank you very much, I certainly will look into this.
As I understand, its synthesis is purely in software.

It includes support for two Linux “speaker APIs” as well, but yes, the
emulated mode is purely software.

[…]

I found recently that SDL mixer contains support for native MIDI.
But for some reason (license?)

Yes, it’s GPL code (not LGPL) so some projects may violate the GPL if
it’s compiled in.

it is not compiled into my Debian
package. May be I should go in that direction ?

Depends on what you want to do, and what kind of requirements you
have. Native MIDI effectively means General MIDI, and that, by
definition, means you have virtually no control whatsoever over
timbre and sound quality. Most songs do sound better on a
professional studio synth than an Adlib card, but when some
instruments are about 24 dB too loud, I’m not so sure… Better
sounds, but the mix gets totally f*cked up. :frowning: I really don’t like
GM, but that’s just my personal opinion, of course.

Anyway, any GM device definitely beats the PC speaker for music, but
what about sound effects? You could do sound effects on an Adlib with
"direct" control, but with MIDI, all you have is a drum kit and some
128 patches that may sound like pretty much anything but what you
expect.

BTW, I once implemented a MIDI driver for the sound effects included
with pcspeaker and it was, uhm… interesting. :slight_smile:

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

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Thursday 10 July 2003 10.13, Wladimir Mutel wrote: