1 ## Copyright (C) 1999 Paul Kienzle
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {[@var{x},@var{fs},@var{sampleformat}] =} auload (@var{filename})
19 ## Reads an audio waveform from a file given by the string @var{filename}.
20 ## Returns the audio samples in data, one column per channel, one row per
21 ## time slice. Also returns the sample rate and stored format (one of ulaw,
22 ## alaw, char, int16, int24, int32, float, double). The sample value will be
23 ## normalized to the range [-1,1] regardless of the stored format.
26 ## [x, fs] = auload(file_in_loadpath("sample.wav"));
30 ## Note that translating the asymmetric range [-2^n,2^n-1] into the
31 ## symmetric range [-1,1] requires a DC offset of 2/2^n. The inverse
32 ## process used by ausave requires a DC offset of -2/2^n, so loading and
33 ## saving a file will not change the contents. Other applications may
34 ## compensate for the asymmetry in a different way (including previous
35 ## versions of auload/ausave) so you may find small differences in
36 ## calculated DC offsets for the same file.
39 ## 2001-09-04 Paul Kienzle <pkienzle@users.sf.net>
40 ## * skip unknown blocks in WAVE format.
41 ## 2001-09-05 Paul Kienzle <pkienzle@users.sf.net>
42 ## * remove debugging stuff from AIFF format.
43 ## * use data length if it is given rather than reading to the end of file.
44 ## 2001-12-11 Paul Kienzle <pkienzle@users.sf.net>
45 ## * use closed interval [-1,1] rather than open interval [-1,1) internally
47 function [data, rate, sampleformat] = auload(path)
50 usage("[x, fs, sampleformat] = auload('filename.ext')");
52 data = []; # if error then read nothing
54 sampleformat = 'ulaw';
55 ext = rindex(path, '.');
57 usage('x = auload(filename.ext)');
59 ext = tolower(substr(path, ext+1, length(path)-ext));
61 [file, msg] = fopen(path, 'rb');
63 error([ msg, ": ", path]);
66 msg = sprintf('Invalid audio header: %s', path);
67 ## Microsoft .wav format
70 ## Header format obtained from sox/wav.c
72 ## Copyright 1992 Rick Richardson
73 ## Copyright 1991 Lance Norskog And Sundry Contributors
74 ## This source code is freely redistributable and may be used for
75 ## any purpose. This copyright notice must be maintained.
76 ## Lance Norskog And Sundry Contributors are not responsible for
77 ## the consequences of using this software.
79 ## check the file magic header bytes
81 str = char(fread(file, 4, 'char')');
82 if !strcmp(str, 'RIFF')
85 len = fread(file, 1, 'int32', 0, arch);
86 str = char(fread(file, 4, 'char')');
87 if !strcmp(str, 'WAVE')
91 ## skip to the "fmt " section, ignoring everything else
96 str = char(fread(file, 4, 'char')');
97 len = fread(file, 1, 'int32', 0, arch);
98 if strcmp(str, 'fmt ')
101 fseek(file, len, SEEK_CUR);
104 ## read the "fmt " section
105 formatid = fread(file, 1, 'int16', 0, arch);
106 channels = fread(file, 1, 'int16', 0, arch);
107 rate = fread(file, 1, 'int32', 0, arch);
108 fread(file, 1, 'int32', 0, arch);
109 fread(file, 1, 'int16', 0, arch);
110 bits = fread(file, 1, 'int16', 0, arch);
111 fseek(file, len-16, SEEK_CUR);
113 ## skip to the "data" section, ignoring everything else
118 str = char(fread(file, 4, 'char')');
119 len = fread(file, 1, 'int32', 0, arch);
120 if strcmp(str, 'data')
123 fseek(file, len, SEEK_CUR);
128 sampleformat = 'uchar';
132 sampleformat = 'int16';
136 sampleformat = 'int24';
140 sampleformat = 'int32';
146 elseif (formatid == 3)
148 sampleformat = 'float';
152 sampleformat = 'double';
153 precision = 'double';
158 elseif (formatid == 6 && bits == 8)
159 sampleformat = 'alaw';
162 elseif (formatid == 7 && bits == 8)
163 sampleformat = 'ulaw';
172 elseif strcmp(ext, 'au')
174 ## Header format obtained from sox/au.c
175 ## September 25, 1991
176 ## Copyright 1991 Guido van Rossum And Sundry Contributors
177 ## This source code is freely redistributable and may be used for
178 ## any purpose. This copyright notice must be maintained.
179 ## Guido van Rossum And Sundry Contributors are not responsible for
180 ## the consequences of using this software.
182 str = char(fread(file, 4, 'char')');
186 invmagic(1) = char(0);
187 if strcmp(str, 'dns.') || strcmp(str, magic)
189 elseif strcmp(str, '.snd') || strcmp(str, invmagic)
194 header = fread(file, 1, 'int32', 0, 'ieee-be');
195 len = fread(file, 1, 'int32', 0, 'ieee-be');
196 formatid = fread(file, 1, 'int32', 0, 'ieee-be');
197 rate = fread(file, 1, 'int32', 0, 'ieee-be');
198 channels = fread(file, 1, 'int32', 0, 'ieee-be');
199 fseek(file, header-24, SEEK_CUR); % skip file comment
201 ## interpret the sample format
203 sampleformat = 'ulaw';
208 sampleformat = 'uchar';
213 sampleformat = 'int16';
218 sampleformat = 'int32';
223 sampleformat = 'float';
228 sampleformat = 'double';
229 precision = 'double';
236 ## Apple/SGI .aiff format
237 elseif strcmp(ext,'aiff') || strcmp(ext,'aif')
239 ## Header format obtained from sox/aiff.c
240 ## September 25, 1991
241 ## Copyright 1991 Guido van Rossum And Sundry Contributors
242 ## This source code is freely redistributable and may be used for
243 ## any purpose. This copyright notice must be maintained.
244 ## Guido van Rossum And Sundry Contributors are not responsible for
245 ## the consequences of using this software.
247 ## IEEE 80-bit float I/O taken from
248 ## ftp://ftp.mathworks.com/pub/contrib/signal/osprey.tar
249 ## David K. Mellinger
253 ## Monterey Bay Aquarium Research Institute
254 ## 7700 Sandholdt Road
256 ## check the file magic header bytes
258 str = char(fread(file, 4, 'char')');
259 if !strcmp(str, 'FORM')
262 len = fread(file, 1, 'int32', 0, arch);
263 str = char(fread(file, 4, 'char')');
264 if !strcmp(str, 'AIFF')
268 ## skip to the "COMM" section, ignoring everything else
273 str = char(fread(file, 4, 'char')');
274 len = fread(file, 1, 'int32', 0, arch);
275 if strcmp(str, 'COMM')
278 fseek(file, len, SEEK_CUR);
281 ## read the "COMM" section
282 channels = fread(file, 1, 'int16', 0, arch);
283 frames = fread(file, 1, 'int32', 0, arch);
284 bits = fread(file, 1, 'int16', 0, arch);
285 exp = fread(file, 1, 'uint16', 0, arch); % read a 10-byte float
286 mant = fread(file, 2, 'uint32', 0, arch);
287 mant = mant(1) / 2^31 + mant(2) / 2^63;
288 if (exp >= 32768), mant = -mant; exp = exp - 32768; end
291 fseek(file, len-18, SEEK_CUR);
293 ## skip to the "SSND" section, ignoring everything else
298 str = char(fread(file, 4, 'char')');
299 len = fread(file, 1, 'int32', 0, arch);
300 if strcmp(str, 'SSND')
303 fseek(file, len, SEEK_CUR);
305 offset = fread(file, 1, 'int32', 0, arch);
306 fread(file, 1, 'int32', 0, arch);
307 fseek(file, offset, SEEK_CUR);
311 sampleformat = 'uchar';
315 sampleformat = 'int16';
316 samples = (len - 8)/2;
319 sampleformat = 'int32';
320 samples = (len - 8)/4;
325 ## file extension unknown
327 error('auload(filename.ext) understands .wav .au and .aiff only');
330 ## suck in all the samples
331 if (samples <= 0) samples = Inf; end
332 if (precision == 'int24')
333 data = fread(file, 3*samples, 'uint8', 0, arch);
334 if (arch == 'ieee-le')
335 data = data(1:3:end) + data(2:3:end) * 2^8 + cast(typecast(cast(data(3:3:end), 'uint8'), 'int8'), 'double') * 2^16;
337 data = data(3:3:end) + data(2:3:end) * 2^8 + cast(typecast(cast(data(1:3:end), 'uint8'), 'int8'), 'double') * 2^16;
340 data = fread(file, samples, precision, 0, arch);
344 ## convert samples into range [-1, 1)
345 if strcmp(sampleformat, 'alaw')
347 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, \
348 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, \
349 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, \
350 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, \
351 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944, \
352 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, \
353 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472, \
354 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, \
355 -344, -328, -376, -360, -280, -264, -312, -296, \
356 -472, -456, -504, -488, -408, -392, -440, -424, \
357 -88, -72, -120, -104, -24, -8, -56, -40, \
358 -216, -200, -248, -232, -152, -136, -184, -168, \
359 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, \
360 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, \
361 -688, -656, -752, -720, -560, -528, -624, -592, \
362 -944, -912, -1008, -976, -816, -784, -880, -848 ];
363 alaw = ([ alaw,-alaw]+0.5)/32767.5;
365 elseif strcmp(sampleformat, 'ulaw')
366 data = mu2lin(data, 0);
367 elseif strcmp(sampleformat, 'uchar')
368 ## [ 0, 255 ] -> [ -1, 1 ]
369 data = data/127.5 - 1;
370 elseif strcmp(sampleformat, 'int16')
371 ## [ -32768, 32767 ] -> [ -1, 1 ]
372 data = (data+0.5)/32767.5;
373 elseif strcmp(sampleformat, 'int32')
374 ## [ -2^31, 2^31-1 ] -> [ -1, 1 ]
375 data = (data+0.5)/(2^31-0.5);
377 data = reshape(data, channels, length(data)/channels)';
382 %! [x, fs] = auload(file_in_loadpath("sample.wav"));