7

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?

1
  • 1
    Have you tried with a wav file?
    – YoMismo
    Commented Jul 29, 2014 at 8:07

4 Answers 4

6

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.

4

The advanced bash scripting guide. Example 29.2:

#!/bin/b[as][1]h
# 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`
  do
    test $(( $t % $1 )) = 0 && echo -n $volume || echo -n $mute
  done
}

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...

4
  • 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. Commented Jul 29, 2014 at 7:48
  • 1
    @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
    Commented Apr 6, 2016 at 13:56
  • This makes no sound for me. I'm on Ubuntu 18.04. Commented Mar 12, 2022 at 7:08
  • This answer is out-of-date. Replace > /dev/dsp with | aplay and then it works for me! See Why won't Linux let me play with /dev/dsp?. In general: instead of doing cat music.wav > /dev/dsp, you must do cat music.wav | aplay, or just aplay music.wav. Commented Mar 12, 2022 at 7:29
3

At least on FreeBSD, /dev/dsp expects 8-bit unsigned mono audio at a sample rate of 8000 Hz. To convert an existing file, e.g. music.m4a, to this format with ffmpeg (as raw audio, no headers), do:

ffmpeg -i music.m4a -ar 8000 -ac 1 -c:a pcm_u8 -f u8 music.raw

The resulting music.raw file can then be played with:

cat music.raw > /dev/dsp
1

If you use ALSA, you can use aplay like:

cat foobar.wav | aplay

The output looks like:

Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .