1 ## Copyright (C) 2005-2012 Michael Zeising
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{y} =} wavread (@var{filename})
21 ## Load the RIFF/WAVE sound file @var{filename}, and return the samples
22 ## in vector @var{y}. If the file contains multichannel data, then
23 ## @var{y} is a matrix with the channels represented as columns.
25 ## @deftypefnx {Function File} {[@var{y}, @var{Fs}, @var{bps}] =} wavread (@var{filename})
26 ## Additionally return the sample rate (@var{fs}) in Hz and the number of bits
27 ## per sample (@var{bps}).
29 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n})
30 ## Read only the first @var{n} samples from each channel.
32 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n1} @var{n2})
33 ## Read only samples @var{n1} through @var{n2} from each channel.
35 ## @deftypefnx {Function File} {[@var{samples}, @var{channels}] =} wavread (@var{filename}, "size")
36 ## Return the number of samples (@var{n}) and channels (@var{ch})
37 ## instead of the audio data.
41 ## Author: Michael Zeising <michael@michaels-website.de>
42 ## Created: 06 December 2005
44 function [y, samples_per_sec, bits_per_sample] = wavread (filename, param)
46 FORMAT_PCM = 0x0001; # PCM (8/16/32 bit)
47 FORMAT_IEEE_FLOAT = 0x0003; # IEEE float (32/64 bit)
48 BYTEORDER = "ieee-le";
50 if (nargin < 1 || nargin > 2)
54 if (! ischar (filename))
55 error ("wavread: FILENAME must be a character string");
62 [fid, msg] = fopen (filename, "rb");
65 error ("wavread: %s", msg);
69 fseek (fid, 0, "eof");
70 file_size = ftell (fid);
71 fseek (fid, 0, "bof");
74 riff_size = find_chunk (fid, "RIFF", file_size);
75 riff_pos = ftell (fid);
77 error ("wavread: file contains no RIFF chunk");
80 riff_type = char (fread (fid, 4))';
81 if (! strcmp (riff_type, "WAVE"))
82 error ("wavread: file contains no WAVE signature");
84 riff_pos = riff_pos + 4;
85 riff_size = riff_size - 4;
87 ## Find format chunk inside the RIFF chunk.
88 fseek (fid, riff_pos, "bof");
89 fmt_size = find_chunk (fid, "fmt ", riff_size);
92 error ("wavread: file contains no format chunk");
95 ## Find data chunk inside the RIFF chunk.
96 ## We don't assume that it comes after the format chunk.
97 fseek (fid, riff_pos, "bof");
98 data_size = find_chunk (fid, "data", riff_size);
99 data_pos = ftell (fid);
101 error ("wavread: file contains no data chunk");
104 ### Read format chunk.
105 fseek (fid, fmt_pos, "bof");
107 ## Sample format code.
108 format_tag = fread (fid, 1, "uint16", 0, BYTEORDER);
109 if (format_tag != FORMAT_PCM && format_tag != FORMAT_IEEE_FLOAT)
110 error ("wavread: sample format %#x is not supported", format_tag);
113 ## Number of interleaved channels.
114 channels = fread (fid, 1, "uint16", 0, BYTEORDER);
117 samples_per_sec = fread (fid, 1, "uint32", 0, BYTEORDER);
120 fseek (fid, 6, "cof");
121 bits_per_sample = fread (fid, 1, "uint16", 0, BYTEORDER);
124 fseek (fid, data_pos, "bof");
126 ## Determine sample data type.
127 if (format_tag == FORMAT_PCM)
128 switch (bits_per_sample)
138 error ("wavread: %d bits sample resolution is not supported with PCM",
142 switch (bits_per_sample)
148 error ("wavread: %d bits sample resolution is not supported with IEEE float",
155 length = idivide (8 * data_size, bits_per_sample);
157 nparams = numel (param);
159 ## Number of samples is given.
160 length = param * channels;
161 elseif (nparams == 2)
162 ## Sample range is given.
163 if (fseek (fid, (param(1)-1) * channels * (bits_per_sample/8), "cof") < 0)
164 warning ("wavread: seeking failed");
166 length = (param(2)-param(1)+1) * channels;
167 elseif (nparams == 4 && char (param) == "size")
168 ## Size of the file is requested.
169 tmp = idivide (8 * data_size, channels * bits_per_sample);
173 error ("wavread: invalid PARAM argument");
177 ## Read samples and close file.
178 if (bits_per_sample == 24)
182 [yi, n] = fread (fid, length, format, 0, BYTEORDER);
184 unwind_protect_cleanup
193 if (mod (numel (yi), channels) != 0)
194 error ("wavread: data in %s doesn't match the number of channels",
198 if (bits_per_sample == 24)
199 yi = reshape (yi, 3, rows(yi)/3)';
200 yi(yi(:,3) >= 128, 3) -= 256;
201 yi = yi * [1; 256; 65536];
204 if (format_tag == FORMAT_PCM)
205 ## Normalize samples.
206 switch (bits_per_sample)
219 nr = numel (yi) / channels;
220 y = reshape (yi, channels, nr)';
224 ## Given a chunk_id, scan through chunks from the current file position
225 ## though at most size bytes. Return the size of the found chunk, with
226 ## file position pointing to the start of the chunk data. Return -1 for
227 ## size if chunk is not found.
229 function chunk_size = find_chunk (fid, chunk_id, size)
234 while (! strcmp (id, chunk_id) && (offset < size))
235 fseek (fid, chunk_size, "cof");
236 id = char (fread (fid, 4))';
237 chunk_size = fread (fid, 1, "uint32", 0, "ieee-le");
238 ## Chunk sizes must be word-aligned (2 byte)
239 chunk_size += rem (chunk_size, 2);
240 offset = offset + 8 + chunk_size;
242 if (! strcmp (id, chunk_id))
247 ## Mark file as being tested. Tests for wavread/wavwrite pair are in