| 1 |
--- |
| 2 |
layout: page |
| 3 |
--- |
| 4 |
|
| 5 |
# libsndfile and GNU Octave |
| 6 |
|
| 7 |
[GNU Octave](http://www.octave.org/) is a high-level interactive language for |
| 8 |
numerical computations. There are currently two development streams, a stable |
| 9 |
2.0.X series and a development 2.1.X series. Octave reads and writes data in |
| 10 |
binary formats that were originally developed for |
| 11 |
[MATLAB](http://www.mathworks.com/). Version 2.0.X of Octave uses binary data |
| 12 |
files compatible with MATLAB version 4.2 while Octave 2.1.X uses binary data |
| 13 |
files compatible with MATLAB version 5.0 as well as being able to read the older |
| 14 |
MATLAB 4.2 format. |
| 15 |
|
| 16 |
From version 1.0.1 of libsndfile onwards, libsndfile has the ability of reading |
| 17 |
and writing a small subset of the binary data files used by both versions of GNU |
| 18 |
Octave. This gives people using GNU Octave for audio based work an easy method |
| 19 |
of moving audio data between GNU Octave and other programs which use libsndfile. |
| 20 |
|
| 21 |
For instance it is now possible to do the following: |
| 22 |
|
| 23 |
* Load a WAV file into a sound file editor such as |
| 24 |
[Sweep](http://www.metadecks.org/software/sweep/). |
| 25 |
* Save it as a MAT4 file. |
| 26 |
* Load the data into Octave for manipulation. |
| 27 |
* Save the modified data. |
| 28 |
* Reload it in Sweep. |
| 29 |
|
| 30 |
Another example would be using the MAT4 or MAT5 file formats as a format which |
| 31 |
can be easily loaded into Octave for viewing/analyzing as well as a format which |
| 32 |
can be played with command line players such as the one included with |
| 33 |
libsndfile. |
| 34 |
|
| 35 |
## Details |
| 36 |
|
| 37 |
Octave, like most programming languages, uses variables to store data, and |
| 38 |
Octave variables can contain both arrays and matrices. It is also able to store |
| 39 |
one or more of these variables in a file. When reading Octave files, libsndfile |
| 40 |
expects a file to contain two variables and their associated data. The first |
| 41 |
variable should contain a variable holding the file sample rate while the second |
| 42 |
variable contains the audio data. |
| 43 |
|
| 44 |
For example, to generate a sine wave and store it as a binary file which is |
| 45 |
compatible with libsndfile, do the following: |
| 46 |
|
| 47 |
octave:1 > samplerate = 44100 ; |
| 48 |
octave:2 > wavedata = sin ((0:1023)*2*pi/1024) ; |
| 49 |
octave:3 > save sine.mat samplerate wavedata |
| 50 |
|
| 51 |
The process of reading and writing files compatible with libsndfile can be made |
| 52 |
easier by use of two Octave script files: |
| 53 |
|
| 54 |
octave:4 > [data fs] = sndfile_load ("sine.mat") ; |
| 55 |
octave:5 > sndfile_save ("sine2.mat", data, fs) ; |
| 56 |
|
| 57 |
In addition, libsndfile contains a command line program which which is able to |
| 58 |
play the correct types of Octave files. Using this command line player |
| 59 |
**sndfile-play** and a third Octave script file allows Octave data to be played |
| 60 |
from within Octave on any of the platforms which **sndfile-play** supports (at |
| 61 |
the moment: Linux, MacOS X, Solaris and Win32). |
| 62 |
|
| 63 |
octave:6 > sndfile_play (data, fs) ; |
| 64 |
|
| 65 |
These three Octave scripts are installed automatically in Octave's site script |
| 66 |
directory when libsndfile is installed (except on Win32) ie when libsndfile is |
| 67 |
being installed into /usr/local, the Octave scripts will be installed in |
| 68 |
/usr/local/share/octave/site/m/. |
| 69 |
|
| 70 |
There are some other Octave scripts for audio to be found |
| 71 |
[here](http://octave.sourceforge.net/audio/index.html). |