140
D. Rocchesso: Sound Processing
in order to start working is really limited to the basic concepts and can be
condensed in a two-hours lecture.
Processing in Octave/Matlab usually proceeds using monophonic sounds, as
stereo sounds are simply seen as couples of vectors. It is necessary to make clear
what the sound sample rate is at each step, i.e., how many samples are needed
to produce one second of sound.
Let us give an example of how we can create a 440Hz sinusoidal sound,
lasting 2 seconds, and using the sample rate F
s
= 44100Hz:
f = 440; % pitch in Hz
Fs= 44100; % sample rate in Hz
l = 2;
% soundlength in seconds
Y = sin(2*pi*f/Fs*[0:Fs*l]); % sound vector
The sound is simply defined by application of the function sin() to a vector
of Fs*l + 1 elements (namely, 88200 elements) containing an increasing ramp,
suitably scaled so that f cycles are represented in F s samples.
Once the sound vector has been defined, one may like to listen to it. On
this point, Matlab and Octave present different behaviors, also dependent on
the machine and operating system where they are running. Matlab offers the
function sound() that receives as input the vector containing the sound and,
optionally, a second parameter indicating the sample rate. Without the second
parameter, the default sample rate is 8192Hz. Up to version 4.2 of Matlab, the
number of reproduction bits was 8 on a Intel-compatible machine. More recent
versions of Matlab reproduce sound vectors using 16 bits of sample resolution.
In order to reproduce the sound that we have produced with the above script
we should write
sound(Y, Fc);
Up to now, in the core Octave distribution the function that allows to pro-
duce sounds from the Octave interpreter is playaudio(), that can receive "file-
name" and "extension" as the first and second argument, respectively. The
extension contains information about the audio file format, but so far only the
formats raw data linear and mu-law are supported. Alternatively, the argument
of playaudio can be a vector name, such as Y in our example. The reproduction
is done at 8 bits and 8192 Hz, but it would be easy to modify the function so
that it can use better quantizations and sample rates. Fortunately, there is the
octave-forge project
2
that contains useful functions for Octave which are not in
the main distribution. In the audio section we notice the following interesting
functions (quoting from the help lines):
sound(x [, fs]) Play the signal through the speakers. Data is a matrix with
one column per channel. Rate fs defaults to 8000 Hz. The signal is clipped
to [-1, 1].
soundsc(x, fs, limit) or soundsc(x, fs, [ lo, hi ]) Scale the signal so
that [min(x), max(x)] [-1, 1], then play it through the speakers at 8000
Hz sampling rate. The signal has one column per channel.
2
http://www.sourceforge.net
Next Page >>
<< Previous Page