Stefano Lanzavecchia wrote:
SL> Ian, as far as I know, under most Uni*s you can simply write binary data to
SL> /dev/audio and it will play

That's the spirit, Stefano!  See below how three lines of J will do the job.

SL> Then there's /dev/dsp (http://www.opensound.com/pguide/audio.html) and I
SL> don't know enough to tell you the difference between /dev/audio and /dev/dsp

As your link explains, /dev/audio will expect samples at 8kHz in
8-bit U-Law encoding (as originally devised by SUN), whereas /dev/dsp
takes the simpler linear encoding.  (The original SUN /dev/audio
from the 80ies and similar devices from other Unix workstation
vendors were for very simple DAC/ADC hardware and pretty fixed.
The OSS OpenSoundSystem driver is highly configurable using
ioctl(2), and different /dev/whatever devices provide just
different pre-configurations.)

SL> I don't know if MacOSX is Uni* enough to come equipped with those
SL> devices in its filesystem...

I have OSX running here, too, and so far it appears to me that Apple
has done away with those marvellously simple device files and
replaced them with some huge API.  If Ian has the XCode Developer
kit installed, he can point his browser to the result of

        locate CoreAudio/index.html

and waste some weeks on reading C and Java APIs for sound.
(I didn't know about afplay(1), it certainly isn't standard
across Unix systems.  Thanks for making me aware of it.
It didn't show up with a "man -k audio" -- booo, Apple!)

Having said all this, the following works on my Linux and *BSD
systems:

NB. How to beep at 194.18 Hz for one second,
NB. using J, the OSS /dev/dsp device,
NB. and no freaking J/system library whatsoever.

NB. /dev/dsp uses a default sample rate of 8 kHz.
NB. That is, for a one second second beep, we need to
NB. compute 8000 samples.

NB.  We'll sample a sine wave at 194.18 Hz over 1 second.
NB.  Or in other words: our x axis covers 194.18 radians,
NB.  and we'll choose our 8000 sample points from that range:

x =: (194.18 * 2p1) * (%~ i.)  8000

NB. /dev/dsp expects an "8-bit unsigned linear" encoding of
NB. the signal.
NB. That is, we'll have to map our _1 to 1 sine wave amplitudes
NB. into the 0..255 range (for maximum blast):

samples =: <. 128 + 127 * 1 o. x

NB.  Lastly, we convert the shifted values into bytes (characters)
NB.  and write those to the dsp device:

(samples { a.) 1!:2 <'/dev/dsp'

NB. Done.                                       Martin Neitzel
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to