[Tutor] Beep sound
Phil
phil_lor at bigpond.com
Sun Mar 24 12:45:37 CET 2013
On 24/03/13 18:24, eryksun wrote:
<cut>
> PulseAudio also suggests that you're using Linux or BSD, though I
> think it does have ports for OS X and Windows.
>
> The ossaudiodev module exists on Linux/BSD, so try something
> relatively simple like outputting a square wave to /dev/dsp. Here's an
> example device configuration:
>
> format = ossaudiodev.AFMT_U8 # unsigned 8-bit
> channels = 1
> rate = 8000 # samples/second
> strict = True
>
> dsp = ossaudiodev.open('/dev/dsp', 'w')
> dsp.setparameters(format, channels, rate, strict)
>
> Say you want a 1000 Hz, 50% duty-cycle square wave. Given the rate is
> 8000 sample/s, that's 8 samples per cycle. In 8-bit mode, a cycle has
> four bytes at the given amplitude followed by four null bytes. For a
> 0.5 s beep, you need 0.5 * 1000 = 500 cycles.
>
> In general:
>
> amp = chr(amplitude) # bytes([amplitude]) in 3.x
> cycles = int(duration * frequency)
> nhalf = int(0.5 * rate / frequency)
> data = (amp * nhalf + b'\x00' * nhalf) * cycles
>
> Then just write the data. Make sure to close the device when you no
> longer need it.
>
> dsp.write(data)
> dsp.close()
>
> You can use threading to play the beep in the background without
> blocking the main thread. To me this is simpler than juggling partial
> writes in nonblock mode. However, you can't access the device
> concurrently. Instead you could make beep() a method. Then queue the
> requests, to be handled sequentially by a worker thread.
Thanks Erksun for your detailed reply. I've saved your reply for a rainy
day.
I had discovered some information about writing to the dsp device since
my original post. However, my experiments have been temporarily
curtailed by a wife who wants me to spent more time building our house
and less time playing with the computer.
It's amazing what's available to play with these days.
--
Regards,
Phil
More information about the Tutor
mailing list