- publishing free software manuals
 GNU Octave Manual Version 3 by John W. Eaton, David Bateman, Søren HaubergPaperback (6"x9"), 568 pagesISBN 095461206XRRP £24.95 (\$39.95)

# 31 Audio Processing

Octave provides a few functions for dealing with audio data. An audio sample' is a single output value from an A/D converter, i.e., a small integer number (usually 8 or 16 bits), and audio data is just a series of such samples. It can be characterized by three parameters: the sampling rate (measured in samples per second or Hz, e.g. 8000 or 44100), the number of bits per sample (e.g. 8 or 16), and the number of channels (1 for mono, 2 for stereo, etc.).

There are many different formats for representing such data. Currently, only the two most popular, linear encoding and mu-law encoding, are supported. Octave simply treats audio data as vectors of samples (non-mono data are not supported yet). It is assumed that audio files using linear encoding have one of the extensions ‘lin’ or ‘raw’, and that files holding data in mu-law encoding end in ‘au’, ‘mu’, or ‘snd’.

## 31.1 Audio Conversion Functions

Function File: lin2mu (x, n)
Converts audio data from linear to mu-law. Mu-law values use 8-bit unsigned integers. Linear values use n-bit signed integers or floating point values in the range -1 \leq x \leq 1 if n is 0. If n is not specified it defaults to 0, 8 or 16 depending on the range values in x.

Function File: mu2lin (x, bps)
Converts audio data from linear to mu-law. Mu-law values are 8-bit unsigned integers. Linear values use n-bit signed integers or floating point values in the range -1 \leq y \leq 1 if n is 0. If n is not specified it defaults to 8.

Function File: loadaudio (name, ext, bps)
Loads audio data from the file name.ext into the vector x.

The extension ext determines how the data in the audio file is interpreted; the extensions ‘lin’ (default) and ‘raw’ correspond to linear, the extensions ‘au’, ‘mu’, or ‘snd’ to mu-law encoding.

The argument bps can be either 8 (default) or 16, and specifies the number of bits per sample used in the audio file.

Function File: saveaudio (name, x, ext, bps)
Saves a vector x of audio data to the file name.ext. The optional parameters ext and bps determine the encoding and the number of bits per sample used in the audio file (see loadaudio`); defaults are ‘lin’ and 8, respectively.

The following functions for audio I/O require special A/D hardware and operating system support. It is assumed that audio data in linear encoding can be played and recorded by reading from and writing to ‘/dev/dsp’, and that similarly ‘/dev/audio’ is used for mu-law encoding. These file names are system-dependent. Improvements so that these functions will work without modification on a wide variety of hardware are welcome.

Function File: playaudio (name, ext)
Function File: playaudio (x)
Plays the audio file name.ext or the audio data stored in the vector x.

Function File: record (sec, sampling_rate)
Records sec seconds of audio input into the vector x. The default value for sampling_rate is 8000 samples per second, or 8kHz. The program waits until the user types RET and then immediately starts to record.

Function File: setaudio ([w_type [, value]])
Execute the shell command ‘mixer [w_type [, value]]’

Function File: y = wavread (filename)
Load the RIFF/WAVE sound file filename, and return the samples in vector y. If the file contains multichannel data, then y is a matrix with the channels represented as columns.

Function File: [y, Fs, bits] = wavread (filename)
Additionally return the sample rate (fs) in Hz and the number of bits per sample (bits).

Function File: [...] = wavread (filename, n)
Read only the first n samples from each channel.

Function File: [...] = wavread (filename,[n1 n2])
Read only samples n1 through n2 from each channel.

Function File: [samples, channels] = wavread (filename, "size")
Return the number of samples (n) and channels (ch) instead of the audio data.