5565
|
1 ## Copyright (C) 2005 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5565
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5565
|
18 |
|
19 ## -*- texinfo -*- |
6547
|
20 ## @deftypefn {Function File} {@var{y} =} wavread (@var{filename}) |
5567
|
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. |
5565
|
24 ## |
6547
|
25 ## @deftypefnx {Function File} {[@var{y}, @var{Fs}, @var{bits}] =} wavread (@var{filename}) |
5565
|
26 ## Additionally return the sample rate (@var{fs}) in Hz and the number of bits |
|
27 ## per sample (@var{bits}). |
|
28 ## |
6547
|
29 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n}) |
5565
|
30 ## Read only the first @var{n} samples from each channel. |
|
31 ## |
6549
|
32 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename},[@var{n1} @var{n2}]) |
5565
|
33 ## Read only samples @var{n1} through @var{n2} from each channel. |
|
34 ## |
6547
|
35 ## @deftypefnx {Function File} {[@var{samples}, @var{channels}] =} wavread (@var{filename}, "size") |
5567
|
36 ## Return the number of samples (@var{n}) and channels (@var{ch}) |
|
37 ## instead of the audio data. |
5642
|
38 ## @seealso{wavwrite} |
5565
|
39 ## @end deftypefn |
|
40 |
|
41 ## Author: Michael Zeising <michael.zeising@stud.uni-erlangen.de> |
|
42 ## Created: 06 December 2005 |
|
43 |
5567
|
44 function [y, samples_per_sec, bits_per_sample] = wavread (filename, param) |
|
45 |
5565
|
46 FORMAT_PCM = 0x0001; # PCM (8/16/32 bit) |
|
47 FORMAT_IEEE_FLOAT = 0x0003; # IEEE float (32/64 bit) |
|
48 BYTEORDER = "ieee-le"; |
|
49 |
5567
|
50 if (nargin < 1 || nargin > 2) |
6046
|
51 print_usage (); |
5567
|
52 endif |
|
53 |
|
54 if (! ischar (filename)) |
|
55 error ("wavwrite: expecting filename to be a character string"); |
|
56 endif |
|
57 |
5572
|
58 # open file for binary reading |
5565
|
59 [fid, msg] = fopen (filename, "rb"); |
|
60 if (fid < 0) |
5572
|
61 error ("wavread: %s", msg); |
5565
|
62 endif |
|
63 |
5567
|
64 ## check for RIFF/WAVE header |
|
65 ck_id = char (fread (fid, 4))'; |
5565
|
66 fseek (fid, 4, SEEK_CUR); |
5567
|
67 wave_id = char (fread (fid, 4))'; |
|
68 if (ck_id != "RIFF" || wave_id != "WAVE") |
5565
|
69 fclose (fid); |
|
70 error ("wavread: file contains no RIFF/WAVE signature"); |
|
71 endif |
|
72 |
5567
|
73 ## find format chunk within the next 256 (4*64) bytes |
5565
|
74 i = 1; |
5567
|
75 while (true) |
|
76 if (char (fread (fid, 4))' == "fmt "); |
|
77 break; |
5565
|
78 endif |
|
79 if (i++ == 64) |
|
80 fclose (fid); |
5567
|
81 error ("wavread: file contains no format chunk"); |
5565
|
82 endif |
|
83 endwhile |
5567
|
84 |
|
85 ## format chunk size |
6304
|
86 ck_size = fread (fid, 1, "uint32", 0, BYTEORDER); |
5565
|
87 |
5567
|
88 ## sample format code |
6304
|
89 format_tag = fread (fid, 1, "uint16", 0, BYTEORDER); |
5567
|
90 if (format_tag != FORMAT_PCM && format_tag != FORMAT_IEEE_FLOAT) |
|
91 fclose (fid); |
|
92 error ("wavread: sample format %#x is not supported", format_tag); |
|
93 endif |
|
94 |
|
95 ## number of interleaved channels |
6304
|
96 channels = fread (fid, 1, "uint16", 0, BYTEORDER); |
5567
|
97 |
|
98 ## sample rate |
6304
|
99 samples_per_sec = fread (fid, 1, "uint32", 0, BYTEORDER); |
5567
|
100 |
|
101 ## bits per sample |
5565
|
102 fseek (fid, 6, SEEK_CUR); |
6304
|
103 bits_per_sample = fread (fid, 1, "uint16", 0, BYTEORDER); |
5567
|
104 |
|
105 ## ignore the rest of the chunk |
|
106 fseek (fid, ck_size-16, SEEK_CUR); |
5565
|
107 |
5567
|
108 ## find data chunk |
5565
|
109 i = 1; |
5567
|
110 while (true) |
|
111 if (char (fread (fid, 4))' == "data") |
|
112 break; |
5565
|
113 endif |
|
114 if (i++ == 64) |
|
115 fclose (fid); |
5567
|
116 error ("wavread: file contains no data chunk"); |
5565
|
117 endif |
|
118 end |
5567
|
119 |
|
120 ## data chunk size |
6304
|
121 ck_size = fread (fid, 1, "uint32", 0, BYTEORDER); |
5565
|
122 |
5567
|
123 ## determine sample data type |
|
124 if (format_tag == FORMAT_PCM) |
5839
|
125 switch (bits_per_sample) |
5565
|
126 case 8 |
5572
|
127 format = "uint8"; |
5565
|
128 case 16 |
|
129 format = "int16"; |
5839
|
130 case 24 |
|
131 format = "uint8"; |
5565
|
132 case 32 |
|
133 format = "int32"; |
|
134 otherwise |
|
135 fclose (fid); |
6304
|
136 error ("wavread: %d bits sample resolution is not supported with PCM", |
|
137 bits_per_sample); |
5565
|
138 endswitch |
|
139 else |
5567
|
140 switch (bits_per_sample) |
5565
|
141 case 32 |
|
142 format = "float32"; |
|
143 case 64 |
|
144 format = "float64"; |
|
145 otherwise |
|
146 fclose (fid); |
6304
|
147 error ("wavread: %d bits sample resolution is not supported with IEEE float", |
|
148 bits_per_sample); |
5565
|
149 endswitch |
|
150 endif |
|
151 |
5567
|
152 ## parse arguments |
|
153 if (nargin == 1) |
5565
|
154 length = inf; |
|
155 else |
5567
|
156 if (size (param, 2) == 1) |
|
157 ## number of samples is given |
5565
|
158 length = param * channels; |
5567
|
159 elseif (size (param, 2) == 2) |
|
160 ## sample range is given |
5839
|
161 if (fseek (fid, (param(1)-1) * channels * (bits_per_sample/8), SEEK_CUR) < 0) |
5567
|
162 warning ("wavread: seeking failed"); |
5565
|
163 endif |
5839
|
164 length = (param(2)-param(1)+1) * channels; |
5567
|
165 elseif (size (param, 2) == 4 && char (param) == "size") |
|
166 ## size of the file is requested |
5565
|
167 fclose (fid); |
5937
|
168 y = [ck_size/channels/(bits_per_sample/8), channels]; |
5565
|
169 return |
|
170 else |
|
171 fclose (fid); |
|
172 error ("wavread: invalid argument 2"); |
|
173 endif |
|
174 endif |
5839
|
175 |
5572
|
176 ## read samples and close file |
5839
|
177 if (bits_per_sample == 24) |
|
178 length *= 3; |
|
179 endif |
5565
|
180 [yi, n] = fread (fid, length, format, 0, BYTEORDER); |
|
181 fclose (fid); |
5839
|
182 |
6304
|
183 ## check data |
|
184 if (mod (numel (yi), channels) != 0) |
|
185 error ("wavread: data in %s doesn't match the number of channels", |
|
186 filename); |
|
187 endif |
|
188 |
5839
|
189 if (bits_per_sample == 24) |
|
190 yi = reshape (yi, 3, rows(yi)/3)'; |
|
191 yi(yi(:,3) >= 128, 3) -= 256; |
|
192 yi = yi * [1; 256; 65536]; |
|
193 end |
5567
|
194 if (format_tag == FORMAT_PCM) |
|
195 ## normalize samples |
|
196 switch (bits_per_sample) |
5565
|
197 case 8 |
5572
|
198 yi = (yi - 127.5)/127.5; |
|
199 case 16 |
5839
|
200 yi /= 32768; |
|
201 case 24 |
|
202 yi /= 8388608; |
5572
|
203 case 32 |
5839
|
204 yi /= 2147483648; |
5565
|
205 endswitch |
|
206 endif |
|
207 |
5567
|
208 ## deinterleave |
|
209 nr = numel (yi) / channels; |
|
210 y = reshape (yi, channels, nr)'; |
5565
|
211 |
|
212 endfunction |