Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

In the olden days, you used to be able to open /dev/dsp for reading and writing, now, with PulseAudio this doesn't work anymore.

I thought you could do it with padsp, but this code doesn't run:

import ossaudiodev

f ="w")
fmt, channels, rate = dsp.setparameters(fmt, channels, rate)

(running it via padsp python

The error I get is No such file or directory: '/dev/dsp'.

What am I missing? How do I read and write raw bytes from and to a sound device, and how do I select which one I want to use?

share|improve this question
up vote 3 down vote accepted

This script is a thin wrapper around PulseAudio's Simple API. It allows you to write samples to the default output:

#!/usr/bin/env python3

import random
import ctypes
import struct

class NoiseMaker (object):

    PA_SAMPLE_U8 = 0

    def __init__(self, rate, channels):
        pat_sample_spec = ctypes.c_buffer(struct.pack("LLB",
            self.PA_SAMPLE_U8, rate, channels)) = ctypes.cdll.LoadLibrary("")
        self.s =, "App", self.PA_STREAM_PLAYBACK,
            0, "App Noise", ctypes.byref(pat_sample_spec), 0, 0,0)

    def write(self, data):, data, len(data), 0)

    def __del__(self):

one_second_noise = bytes(random.randint(0, 255) for i in range(44100*2))
NoiseMaker(44100, 2).write(one_second_noise)

The rate (44100) defines how many samples there are in a second of real time. It expects bytes in the range of 0-255 inclusive, which is defined by PA_SAMPLE_U8 (equivalent to 'Unsigned 8-Bit' as shown by Audacity, for example). If you have more than one channel, it expects alternating samples for each of them. So, one second of noise has 44100 samples per channel = 88200 samples, where the (modulo 2 = 0) ones are for the left channel.

Please note that this script runs only on Python 3000 .

share|improve this answer
As an side note, if you're interested in using sound manipulation with the terminal, you can write or read raw audio using the pacat tool. – eltommo Dec 14 '12 at 15:50

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.