<html lang="en"> <head> <title>Audio Processing - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="prev" href="Image-Processing.html#Image-Processing" title="Image Processing"> <link rel="next" href="Object-Oriented-Programming.html#Object-Oriented-Programming" title="Object Oriented Programming"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Audio-Processing"></a> <p> Next: <a rel="next" accesskey="n" href="Object-Oriented-Programming.html#Object-Oriented-Programming">Object Oriented Programming</a>, Previous: <a rel="previous" accesskey="p" href="Image-Processing.html#Image-Processing">Image Processing</a>, Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> <hr> </div> <h2 class="chapter">33 Audio Processing</h2> <p>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.). <p>There are many different formats for representing such data. Currently, only the two most popular, <em>linear encoding</em> and <em>mu-law encoding</em>, are supported by Octave. There is an excellent FAQ on audio formats by Guido van Rossum <a href="mailto:guido@cwi.nl">guido@cwi.nl</a> which can be found at any FAQ ftp site, in particular in the directory <samp><span class="file">/pub/usenet/news.answers/audio-fmts</span></samp> of the archive site <code>rtfm.mit.edu</code>. <p>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 <samp><span class="file">lin</span></samp> or <samp><span class="file">raw</span></samp>, and that files holding data in mu-law encoding end in <samp><span class="file">au</span></samp>, <samp><span class="file">mu</span></samp>, or <samp><span class="file">snd</span></samp>. <!-- lin2mu scripts/audio/lin2mu.m --> <p><a name="doc_002dlin2mu"></a> <div class="defun"> — Function File: <b>lin2mu</b> (<var>x, n</var>)<var><a name="index-lin2mu-2970"></a></var><br> <blockquote><p>Convert audio data from linear to mu-law. Mu-law values use 8-bit unsigned integers. Linear values use <var>n</var>-bit signed integers or floating point values in the range -1 ≤ <var>x</var> ≤ 1 if <var>n</var> is 0. <p>If <var>n</var> is not specified it defaults to 0, 8, or 16 depending on the range of values in <var>x</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmu2lin.html#doc_002dmu2lin">mu2lin</a>, <a href="doc_002dloadaudio.html#doc_002dloadaudio">loadaudio</a>, <a href="doc_002dsaveaudio.html#doc_002dsaveaudio">saveaudio</a>. </p></blockquote></div> <!-- mu2lin scripts/audio/mu2lin.m --> <p><a name="doc_002dmu2lin"></a> <div class="defun"> — Function File: <b>mu2lin</b> (<var>x, n</var>)<var><a name="index-mu2lin-2971"></a></var><br> <blockquote><p>Convert audio data from mu-law to linear. Mu-law values are 8-bit unsigned integers. Linear values use <var>n</var>-bit signed integers or floating point values in the range -1≤y≤1 if <var>n</var> is 0. <p>If <var>n</var> is not specified it defaults to 0. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlin2mu.html#doc_002dlin2mu">lin2mu</a>, <a href="doc_002dloadaudio.html#doc_002dloadaudio">loadaudio</a>, <a href="doc_002dsaveaudio.html#doc_002dsaveaudio">saveaudio</a>. </p></blockquote></div> <!-- loadaudio scripts/audio/loadaudio.m --> <p><a name="doc_002dloadaudio"></a> <div class="defun"> — Function File: <b>loadaudio</b> (<var>name, ext, bps</var>)<var><a name="index-loadaudio-2972"></a></var><br> <blockquote><p>Load audio data from the file <samp><var>name</var><span class="file">.</span><var>ext</var></samp> into the vector <var>x</var>. <p>The extension <var>ext</var> determines how the data in the audio file is interpreted; the extensions <samp><span class="file">lin</span></samp> (default) and <samp><span class="file">raw</span></samp> correspond to linear, the extensions <samp><span class="file">au</span></samp>, <samp><span class="file">mu</span></samp>, or <samp><span class="file">snd</span></samp> to mu-law encoding. <p>The argument <var>bps</var> can be either 8 (default) or 16, and specifies the number of bits per sample used in the audio file. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlin2mu.html#doc_002dlin2mu">lin2mu</a>, <a href="doc_002dmu2lin.html#doc_002dmu2lin">mu2lin</a>, <a href="doc_002dsaveaudio.html#doc_002dsaveaudio">saveaudio</a>, <a href="doc_002dplayaudio.html#doc_002dplayaudio">playaudio</a>, <a href="doc_002dsetaudio.html#doc_002dsetaudio">setaudio</a>, <a href="doc_002drecord.html#doc_002drecord">record</a>. </p></blockquote></div> <!-- saveaudio scripts/audio/saveaudio.m --> <p><a name="doc_002dsaveaudio"></a> <div class="defun"> — Function File: <b>saveaudio</b> (<var>name, x, ext, bps</var>)<var><a name="index-saveaudio-2973"></a></var><br> <blockquote><p>Save a vector <var>x</var> of audio data to the file <samp><var>name</var><span class="file">.</span><var>ext</var></samp>. The optional parameters <var>ext</var> and <var>bps</var> determine the encoding and the number of bits per sample used in the audio file (see <code>loadaudio</code>); defaults are <samp><span class="file">lin</span></samp> and 8, respectively. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlin2mu.html#doc_002dlin2mu">lin2mu</a>, <a href="doc_002dmu2lin.html#doc_002dmu2lin">mu2lin</a>, <a href="doc_002dloadaudio.html#doc_002dloadaudio">loadaudio</a>, <a href="doc_002dplayaudio.html#doc_002dplayaudio">playaudio</a>, <a href="doc_002dsetaudio.html#doc_002dsetaudio">setaudio</a>, <a href="doc_002drecord.html#doc_002drecord">record</a>. </p></blockquote></div> <p>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 <samp><span class="file">/dev/dsp</span></samp>, and that similarly <samp><span class="file">/dev/audio</span></samp> 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. <!-- playaudio scripts/audio/playaudio.m --> <p><a name="doc_002dplayaudio"></a> <div class="defun"> — Function File: <b>playaudio</b> (<var>name, ext</var>)<var><a name="index-playaudio-2974"></a></var><br> — Function File: <b>playaudio</b> (<var>x</var>)<var><a name="index-playaudio-2975"></a></var><br> <blockquote><p>Play the audio file <samp><var>name</var><span class="file">.</span><var>ext</var></samp> or the audio data stored in the vector <var>x</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlin2mu.html#doc_002dlin2mu">lin2mu</a>, <a href="doc_002dmu2lin.html#doc_002dmu2lin">mu2lin</a>, <a href="doc_002dloadaudio.html#doc_002dloadaudio">loadaudio</a>, <a href="doc_002dsaveaudio.html#doc_002dsaveaudio">saveaudio</a>, <a href="doc_002dsetaudio.html#doc_002dsetaudio">setaudio</a>, <a href="doc_002drecord.html#doc_002drecord">record</a>. </p></blockquote></div> <!-- record scripts/audio/record.m --> <p><a name="doc_002drecord"></a> <div class="defun"> — Function File: <b>record</b> (<var>sec, sampling_rate</var>)<var><a name="index-record-2976"></a></var><br> <blockquote><p>Record <var>sec</var> seconds of audio input into the vector <var>x</var>. The default value for <var>sampling_rate</var> is 8000 samples per second, or 8kHz. The program waits until the user types <RET> and then immediately starts to record. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlin2mu.html#doc_002dlin2mu">lin2mu</a>, <a href="doc_002dmu2lin.html#doc_002dmu2lin">mu2lin</a>, <a href="doc_002dloadaudio.html#doc_002dloadaudio">loadaudio</a>, <a href="doc_002dsaveaudio.html#doc_002dsaveaudio">saveaudio</a>, <a href="doc_002dplayaudio.html#doc_002dplayaudio">playaudio</a>, <a href="doc_002dsetaudio.html#doc_002dsetaudio">setaudio</a>. </p></blockquote></div> <!-- setaudio scripts/audio/setaudio.m --> <p><a name="doc_002dsetaudio"></a> <div class="defun"> — Function File: <b>setaudio</b> ()<var><a name="index-setaudio-2977"></a></var><br> — Function File: <b>setaudio</b> (<var>w_type</var>)<var><a name="index-setaudio-2978"></a></var><br> — Function File: <b>setaudio</b> (<var>w_type, value</var>)<var><a name="index-setaudio-2979"></a></var><br> <blockquote><p>Execute the shell command ‘<samp><span class="samp">mixer</span></samp>’, possibly with optional arguments <var>w_type</var> and <var>value</var>. </p></blockquote></div> <!-- wavread scripts/audio/wavread.m --> <p><a name="doc_002dwavread"></a> <div class="defun"> — Function File: <var>y</var> = <b>wavread</b> (<var>filename</var>)<var><a name="index-wavread-2980"></a></var><br> <blockquote><p>Load the RIFF/WAVE sound file <var>filename</var>, and return the samples in vector <var>y</var>. If the file contains multichannel data, then <var>y</var> is a matrix with the channels represented as columns. — Function File: [<var>y</var>, <var>Fs</var>, <var>bps</var>] = <b>wavread</b> (<var>filename</var>)<var><a name="index-wavread-2981"></a></var><br> <blockquote><p>Additionally return the sample rate (<var>fs</var>) in Hz and the number of bits per sample (<var>bps</var>). — Function File: [<small class="dots">...</small>] = <b>wavread</b> (<var>filename, n</var>)<var><a name="index-wavread-2982"></a></var><br> <blockquote><p>Read only the first <var>n</var> samples from each channel. — Function File: [<small class="dots">...</small>] = <b>wavread</b> (<var>filename, n1 n2</var>)<var><a name="index-wavread-2983"></a></var><br> <blockquote><p>Read only samples <var>n1</var> through <var>n2</var> from each channel. — Function File: [<var>samples</var>, <var>channels</var>] = <b>wavread</b> (<var>filename, "size"</var>)<var><a name="index-wavread-2984"></a></var><br> <blockquote><p>Return the number of samples (<var>n</var>) and channels (<var>ch</var>) instead of the audio data. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dwavwrite.html#doc_002dwavwrite">wavwrite</a>. </p></blockquote></div> <!-- wavwrite scripts/audio/wavwrite.m --> <p><a name="doc_002dwavwrite"></a> <div class="defun"> — Function File: <b>wavwrite</b> (<var>y, filename</var>)<var><a name="index-wavwrite-2985"></a></var><br> — Function File: <b>wavwrite</b> (<var>y, Fs, filename</var>)<var><a name="index-wavwrite-2986"></a></var><br> — Function File: <b>wavwrite</b> (<var>y, Fs, bps, filename</var>)<var><a name="index-wavwrite-2987"></a></var><br> <blockquote><p>Write <var>y</var> to the canonical RIFF/WAVE sound file <var>filename</var> with sample rate <var>Fs</var> and bits per sample <var>bps</var>. The default sample rate is 8000 Hz with 16-bits per sample. Each column of the data represents a separate channel. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dwavread.html#doc_002dwavread">wavread</a>. </p></blockquote></div> <!-- DO NOT EDIT! Generated automatically by munge-texi.pl. --> <!-- Copyright (C) 2008-2012 David Bateman --> <!-- Copyright (C) 2009 VZLU Prague --> <!-- This file is part of Octave. --> <!-- Octave is free software; you can redistribute it and/or modify it --> <!-- under the terms of the GNU General Public License as published by the --> <!-- Free Software Foundation; either version 3 of the License, or (at --> <!-- your option) any later version. --> <!-- Octave is distributed in the hope that it will be useful, but WITHOUT --> <!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or --> <!-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --> <!-- for more details. --> <!-- You should have received a copy of the GNU General Public License --> <!-- along with Octave; see the file COPYING. If not, see --> <!-- <http://www.gnu.org/licenses/>. --> <!-- FIXME --> <!-- For now can't include "@" character in the path name, and so name --> <!-- the example directory without the "@"!! --> </body></html>