Logo Computer scientist,
engineer, and educator
• Articles• Articles about computing• Utility corner

r2d2-voice — an R2D2 sound simulator for Linux

Version 0.1

What is this?

r2d2-voice is a simple utility for generating whistles and squeaks and random pitch sequences, rather like the little droid of Star Wars fame. I wrote it for a Raspberry Pi robotics demonstration, in which various sounds were used to give audible feedback of the "thinking" process. For simplicity the utility writes directly to the ALSA audio API, so it probably won't work without a good deal of fiddling on desktop-type Linux systems, where ALSA devices are seized by Jack or Pulse or what-not.


$ r2d2-voice [--device=alsa_device] {sound_spec}
For example, to generate a string of short random pitches:
$ r2d2-voice r 800 80 200 6000 

Sound spec

A sound specification is made up of any number of groups of five arguments of this form:

sound_type duration t f1 f2

The duration parameter will always be a total duration in msec; the other parameters have varying interpretations according to the sound_type parameter.

Sound types

g Glissando — sinewave pitch varies smoothly between f1 and f2

r Random — random sinewave pitches between f1 and f2, each of length t msec

b Buzz — random squarewave pitches between f1 and f2, each of length t msec

s Silence — only the duration argument is interpreted

n Noise — a burst of white noise. Only the duration argument is interpreted

A constant tone can be obtained by specifying a glissando with the same values of f1 and f2.


  • The "buzz" sound works the same way as "random," but with a squarewave profile. It is really designed for producing low buzzing sounds for alerts. The f1 and f2 parameters should probably be the same.
  • Periods of random tones of varying base pitch interspersed with short silences can sound quite authentic. Try this:
    $ r2d2-voice r 300 80 200 2000  s 200 0 0 0 r 600 100 80 500 s 200 0 0 0 r 500 80 500 1000
    Similarly, mix random tones with whistles:
    $ r2d2-voice r 800 80 200 2000 g 200 0 2000 1000 


  • Because the same set of parameters is used for different sounds, do not assume that all combinations of parameters will produce useful output, or even complete without the program crashing. It is not practicable to check the parameters in sufficient detail. A certain amount of trial and error will probably be required to figure out what works and what does not.
  • r2d2-voice attempts to buffer two seconds' worth of audio before starting playback. There is no other protection for buffer underruns, so trying to generate longer sound sequences to two seconds might behave badly if the system is under heavy load.
  • The shortest sound that can be created is about 800msec. If that is too long, trying padding the sound spec out with silence until it is at least one second in total.
  • If the sound spec is unworkable for some reason, the playback might not even start. This means that the logic the program uses to tidy up at the end of playback may fail to wait for the output buffer to flush. There is a five-second timeout to prevent that — if you hear no sound and the program hangs for five seconds, trying changing the sound spec.
  • Even allowing for the above, the shortest sound that can be generated is one buffer unit's worth, which is about 100 msec.

Author and legal

r2d2voice is, for what it's worth, copyright (c)2016 Kevin Boone, and distributed according to the GNU Public Licence v3.0. In essence that means that you can do with this software whatever you wish, so long as the original author is acknowledged.

I wrote r2d2voice over a couple of evenings for a very specific purpose. I'm publishing it in the hope that somebody might find it useful, but there is no warranty of any kind.


Source code bundle
Copyright © 1994-2015 Kevin Boone. Updated Feb 29 2016