Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that it is possible to write arbitrary data into /dev/dsp and a noise will come up from the speakers. But whatever data I pass to this file the noise is the same. I wonder is it possible to generate some meaningful sound just by writing to /dev/dsp?

share|improve this question
Have you tried with a wav file? – YoMismo Jul 29 '14 at 8:07

It must be possible, though I haven't come across any program to generate a file in this type of raw format.

If you are just interested in how it works and what must be written do the device to have sound output, you can look into the source code of sox. This program can be used to transcode a given input file and play it by outputing to /dev/dsp. (Run the program with -t oss option).

If you are just looking for the simplest to play a sound from the console, you'd be probably much better off using ALSA instead of OSS. Use aplay that is part of the alsa-utils. You'll most probably just have it installed by default.

share|improve this answer

The advanced bash scripting guide. Example 29.2:

# music.sh

# Music without external files

# Author: Antonio Macchi
# Used in ABS Guide with permission.

#  /dev/dsp default = 8000 frames per second, 8 bits per frame (1 byte),
#+ 1 channel (mono)

duration=2000       # If 8000 bytes = 1 second, then 2000 = 1/4 second.
volume=$'\xc0'      # Max volume = \xff (or \x00).
mute=$'\x80'        # No volume = \x80 (the middle).

function mknote ()  # $1=Note Hz in bytes (e.g. A = 440Hz ::
{                   #+ 8000 fps / 440 = 16 :: A = 16 bytes per second)
  for t in `seq 0 $duration`
    test $(( $t % $1 )) = 0 && echo -n $volume || echo -n $mute

e=`mknote 49`
g=`mknote 41`
a=`mknote 36`
b=`mknote 32`
c=`mknote 30`
cis=`mknote 29`
d=`mknote 27`
e2=`mknote 24`
n=`mknote 32767`
# European notation.

echo -n "$g$e2$d$c$d$c$a$g$n$g$e$n$g$e2$d$c$c$b$c$cis$n$cis$d \
$n$g$e2$d$c$d$c$a$g$n$g$e$n$g$a$d$c$b$a$b$c" > /dev/dsp
# dsp = Digital Signal Processor

exit      # A "bonny" example of an elegant shell script!

Has the calculations commented...

share|improve this answer
This is a good catch and the linked resource looks quite relevant, but we tend to prefer you excerpt the relevant parts of linked pages here - links can break later on, and then the answer's no help any more. You can edit your answer (the link is just below it) and add more details. – Michael Homer Jul 29 '14 at 7:48
@MichaelHomer Next time, perhaps you would consider not only leaving a comment but submitting an edit yourself. This guarantees it gets done before the link breaks while still educating the OP of the answer how to correctly include relevant details from the link. In this case, I've already submitted the edit, but next time, I hand the baton to you. :) – Dan Apr 6 at 13:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.