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