]> Creatis software - CreaPhase.git/blob - octave_packages/m/audio/wavread.m
update packages
[CreaPhase.git] / octave_packages / m / audio / wavread.m
1 ## Copyright (C) 2005-2012 Michael Zeising
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
24 ##
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}).
28 ##
29 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n})
30 ## Read only the first @var{n} samples from each channel.
31 ##
32 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n1} @var{n2})
33 ## Read only samples @var{n1} through @var{n2} from each channel.
34 ##
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.
38 ## @seealso{wavwrite}
39 ## @end deftypefn
40
41 ## Author: Michael Zeising <michael@michaels-website.de>
42 ## Created: 06 December 2005
43
44 function [y, samples_per_sec, bits_per_sample] = wavread (filename, param)
45
46   FORMAT_PCM        = 0x0001;   # PCM (8/16/32 bit)
47   FORMAT_IEEE_FLOAT = 0x0003;   # IEEE float (32/64 bit)
48   BYTEORDER         = "ieee-le";
49
50   if (nargin < 1 || nargin > 2)
51     print_usage ();
52   endif
53
54   if (! ischar (filename))
55     error ("wavread: FILENAME must be a character string");
56   endif
57
58   fid = -1;
59
60   unwind_protect
61
62     [fid, msg] = fopen (filename, "rb");
63
64     if (fid < 0)
65       error ("wavread: %s", msg);
66     endif
67
68     ## Get file size.
69     fseek (fid, 0, "eof");
70     file_size = ftell (fid);
71     fseek (fid, 0, "bof");
72
73     ## Find RIFF chunk.
74     riff_size = find_chunk (fid, "RIFF", file_size);
75     riff_pos = ftell (fid);
76     if (riff_size == -1)
77       error ("wavread: file contains no RIFF chunk");
78     endif
79
80     riff_type = char (fread (fid, 4))';
81     if (! strcmp (riff_type, "WAVE"))
82       error ("wavread: file contains no WAVE signature");
83     endif
84     riff_pos = riff_pos + 4;
85     riff_size = riff_size - 4;
86
87     ## Find format chunk inside the RIFF chunk.
88     fseek (fid, riff_pos, "bof");
89     fmt_size = find_chunk (fid, "fmt ", riff_size);
90     fmt_pos = ftell(fid);
91     if (fmt_size == -1)
92       error ("wavread: file contains no format chunk");
93     endif
94
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);
100     if (data_size == -1)
101       error ("wavread: file contains no data chunk");
102     endif
103
104     ### Read format chunk.
105     fseek (fid, fmt_pos, "bof");
106
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);
111     endif
112
113     ## Number of interleaved channels.
114     channels = fread (fid, 1, "uint16", 0, BYTEORDER);
115
116     ## Sample rate.
117     samples_per_sec = fread (fid, 1, "uint32", 0, BYTEORDER);
118
119     ## Bits per sample.
120     fseek (fid, 6, "cof");
121     bits_per_sample = fread (fid, 1, "uint16", 0, BYTEORDER);
122
123     ### Read data chunk.
124     fseek (fid, data_pos, "bof");
125
126     ## Determine sample data type.
127     if (format_tag == FORMAT_PCM)
128       switch (bits_per_sample)
129         case 8
130           format = "uint8";
131         case 16
132           format = "int16";
133         case 24
134           format = "uint8";
135         case 32
136           format = "int32";
137         otherwise
138           error ("wavread: %d bits sample resolution is not supported with PCM",
139                  bits_per_sample);
140       endswitch
141     else
142       switch (bits_per_sample)
143         case 32
144           format = "float32";
145         case 64
146           format = "float64";
147         otherwise
148           error ("wavread: %d bits sample resolution is not supported with IEEE float",
149                  bits_per_sample);
150       endswitch
151     endif
152
153     ## Parse arguments.
154     if (nargin == 1)
155       length = idivide (8 * data_size, bits_per_sample);
156     else
157       nparams = numel (param);
158       if (nparams == 1)
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");
165         endif
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);
170         y = [tmp, channels];
171         return;
172       else
173         error ("wavread: invalid PARAM argument");
174       endif
175     endif
176
177     ## Read samples and close file.
178     if (bits_per_sample == 24)
179       length *= 3;
180     endif
181
182     [yi, n] = fread (fid, length, format, 0, BYTEORDER);
183
184   unwind_protect_cleanup
185
186     if (fid >= 0)
187       fclose (fid);
188     endif
189
190   end_unwind_protect
191
192   ## Check data.
193   if (mod (numel (yi), channels) != 0)
194     error ("wavread: data in %s doesn't match the number of channels",
195            filename);
196   endif
197
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];
202   endif
203
204   if (format_tag == FORMAT_PCM)
205     ## Normalize samples.
206     switch (bits_per_sample)
207       case 8
208         yi = (yi - 128)/128;
209       case 16
210         yi /= 32768;
211       case 24
212         yi /= 8388608;
213       case 32
214         yi /= 2147483648;
215     endswitch
216   endif
217
218   ## Deinterleave.
219   nr = numel (yi) / channels;
220   y = reshape (yi, channels, nr)';
221
222 endfunction
223
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.
228
229 function chunk_size = find_chunk (fid, chunk_id, size)
230   id = "";
231   offset = 8;
232   chunk_size = 0;
233
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;
241   endwhile
242   if (! strcmp (id, chunk_id))
243     chunk_size = -1;
244   endif
245 endfunction
246
247 ## Mark file as being tested.  Tests for wavread/wavwrite pair are in
248 ## wavwrite.m
249 %!assert(1)