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