[NFBCS] Real-time sound generation on Windows/Mac/Linux

Brian Buhrow buhrow at nfbcal.org
Thu Oct 19 21:44:10 UTC 2023


	hello Doug.  I'm not sure of the technique for doing what you want on Windows, but on BSD and
Linux systems, there are a number of things you can do.

1.  On BSD systems, there is a thing called the speaker or spkr(4) driver, which creates a
/dev/speaker device which accepts notes,durations and emits them to the system speaker, which
might be an internal speaker as in days of old, or might be a virtual tone generator on a sound
card attached to the computer.  This is how tones get generated on my desktop machine.  If you
want finer grain control of the sound, there is an ioctl interface that lets you build blocks
of sounds for generation.  At the bottom of this message is a simple shell script I wrote over 20
years ago that, if run at the top of the hour, serves as a chiming clock.  It still works today
on my current machine.
Here is a link to the man page for this device.
https://man.netbsd.org/speaker.4

2.  On Linux, there seems to be a moduled call: snd-pcsp, which looks like it provides the same
capabilities as the speaker driver  does on BSD systems.  I haven't tried it, but it looks like
it gives you the ability to send play strings to a speaker device or to use an ioctl interface
to generate tones.  I believe it hooks into the alsa sound system, but I'm guessing you don't
have to use alsa to use it.
Further reading suggests it also provides a PCM capable device for you to use, meaning you
could provide raw PCM audio to it for playing in addition to tone generation.


3.  For Windows it looks like you can do what you want through the use of power shell.  

Hope that helps.

-Brian

<Script to emulate a clock chime.>

#!/bin/sh
#$Id: chime.sh,v 1.2 2001/03/06 23:06:28 buhrow Exp $
#NAME: Brian Buhrow
#DATE: March 6, 2001
#
#This script sounds the speaker device and counts the current
#time.  If run at the top of the hour, it will act as a clock chime.

PATH=/bin:/usr/bin:/usr/local/bin; export PATH
DEVICE="/dev/speaker"
CHIME="<l6cp12>"


echo "l4c" > $DEVICE
echo $CHIME |awk  \
 'BEGIN { count = strftime("%l" ) ; start = 0 } { while (start + 0 < count + 0) { \
	print $0 ; start += 1 } } END { }' > $DEVICE

exit 0



More information about the NFBCS mailing list