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 -*- |
|
21 ## @deftypefn {Function File} {} @var{y} = wavread(@var{filename}) |
|
22 ## Load the RIFF/WAVE sound file @var{filename}, returning the samples in vector |
|
23 ## @var{y}. If the file contains multichannel data, then @var{y} is a matrix with the |
|
24 ## channels represented as columns. |
|
25 ## |
|
26 ## @deftypefnx {Function File} {} [@var{y},@var{Fs},@var{bits}] = wavread(@var{filename}) |
|
27 ## Additionally return the sample rate (@var{fs}) in Hz and the number of bits |
|
28 ## per sample (@var{bits}). |
|
29 ## |
|
30 ## @deftypefnx {Function File} {} [...] = wavread(@var{filename},@var{n}) |
|
31 ## Read only the first @var{n} samples from each channel. |
|
32 ## |
|
33 ## @deftypefnx {Function File} {} [...] = wavread(@var{filename},[@var{n1} @var{n2}]) |
|
34 ## Read only samples @var{n1} through @var{n2} from each channel. |
|
35 ## |
|
36 ## @deftypefnx {Function File} {} [@var{samples} @var{channels}] = wavread(@var{filename},'size') |
|
37 ## Return the number of samples (@var{n}) and channels (@var{ch}) instead of the |
|
38 ## audio data. |
|
39 ## |
|
40 ## @end deftypefn |
|
41 ## |
|
42 ## @seealso{wavwrite} |
|
43 |
|
44 ## Author: Michael Zeising <michael.zeising@stud.uni-erlangen.de> |
|
45 ## Created: 06 December 2005 |
|
46 |
|
47 function [y, samplesPerSec, bitsPerSample] = wavread (filename, param) |
|
48 FORMAT_PCM = 0x0001; # PCM (8/16/32 bit) |
|
49 FORMAT_IEEE_FLOAT = 0x0003; # IEEE float (32/64 bit) |
|
50 FORMAT_ALAW = 0x0006; # 8-bit ITU-T G.711 A-law (not yet supported) |
|
51 FORMAT_MULAW = 0x0007; # 8-bit ITU-T G.711 ยต-law (not yet supported) |
|
52 FORMAT_IMA_ADPCM = 0x0011; # IMA/ADPCM 4:1 compression (not yet supported) |
|
53 BYTEORDER = "ieee-le"; |
|
54 |
|
55 # open file for binary reading |
|
56 [fid, msg] = fopen (filename, "rb"); |
|
57 if (fid < 0) |
|
58 error ("wavread: %s", msg) |
|
59 endif |
|
60 |
|
61 # check for RIFF/WAVE header |
|
62 ckID = char (fread (fid, 4))'; # chunk ID: "RIFF" |
|
63 fseek (fid, 4, SEEK_CUR); |
|
64 WAVEID = char (fread (fid, 4))'; # WAVE ID: "WAVE" |
|
65 if ((ckID ~= "RIFF") || (WAVEID ~= "WAVE")) |
|
66 fclose (fid); |
|
67 error ("wavread: file contains no RIFF/WAVE signature"); |
|
68 endif |
|
69 |
|
70 # find format chunk within the next 256 (4*64) bytes |
|
71 i = 1; |
|
72 while 1 |
|
73 if (char (fread (fid, 4))' == "fmt ") |
|
74 break |
|
75 endif |
|
76 if (i++ == 64) |
|
77 fclose (fid); |
|
78 error ("wavread: file contains no format chunk") |
|
79 endif |
|
80 endwhile |
|
81 |
|
82 ckSize = fread (fid, 1, "ulong", 0, BYTEORDER); # format chunk size |
|
83 |
|
84 formatTag = fread (fid, 1, "short", 0, BYTEORDER); # sample format code |
|
85 if ((formatTag ~= FORMAT_PCM) && (formatTag ~= FORMAT_IEEE_FLOAT)) |
|
86 fclose (fid); |
|
87 error ("wavread: sample format %#x is not supported", formatTag) |
|
88 endif |
|
89 |
|
90 channels = fread (fid, 1, "short", 0, BYTEORDER); # number of interleaved channels |
|
91 samplesPerSec = fread (fid, 1, "ulong", 0, BYTEORDER); # sample rate |
|
92 fseek (fid, 6, SEEK_CUR); |
|
93 bitsPerSample = fread (fid, 1, "short", 0, BYTEORDER); # bits per sample |
|
94 # ignore the rest of the chunk |
|
95 fseek (fid, ckSize-16, SEEK_CUR); |
|
96 |
|
97 # find data chunk |
|
98 i = 1; |
|
99 while 1 |
|
100 if (char (fread(fid, 4))' == "data") |
|
101 break |
|
102 endif |
|
103 if (i++ == 64) |
|
104 fclose (fid); |
|
105 error ("wavread: file contains no data chunk") |
|
106 endif |
|
107 end |
|
108 |
|
109 ckSize = fread (fid, 1, "ulong", 0, BYTEORDER); # data chunk size |
|
110 |
|
111 # determine sample data type |
|
112 if (formatTag == FORMAT_PCM) |
|
113 switch bitsPerSample |
|
114 case 8 |
|
115 format = "int8"; |
|
116 case 16 |
|
117 format = "int16"; |
|
118 case 32 |
|
119 format = "int32"; |
|
120 otherwise |
|
121 fclose (fid); |
|
122 error ("wavread: %d bits sample resolution is not supported with PCM", bitsPerSample); |
|
123 endswitch |
|
124 else |
|
125 switch bitsPerSample |
|
126 case 32 |
|
127 format = "float32"; |
|
128 case 64 |
|
129 format = "float64"; |
|
130 otherwise |
|
131 fclose (fid); |
|
132 error ("wavread: %d bits sample resolution is not supported with IEEE float", bitsPerSample); |
|
133 endswitch |
|
134 endif |
|
135 |
|
136 # parse arguments |
|
137 if (exist ("param","var") < 1) |
|
138 length = inf; |
|
139 else |
|
140 if (size(param)(2) == 1) # number of samples is given |
|
141 length = param * channels; |
|
142 elseif (size(param)(2) == 2) # sample range is given |
|
143 if fseek(fid, param(1) * channels * (bitsPerSample/8), SEEK_CUR) < 0 |
|
144 warning ("wavread: seeking failed") |
|
145 endif |
|
146 length = (param(2)-param(1)) * channels; |
|
147 elseif ((size (param)(2) == 4) && (char(param) == "size")) # size of the file is requested |
|
148 fclose (fid); |
|
149 y = [ckSize/channels/bitsPerSample/8 channels]; |
|
150 return |
|
151 else |
|
152 fclose (fid); |
|
153 error ("wavread: invalid argument 2"); |
|
154 endif |
|
155 endif |
|
156 |
|
157 # read samples |
|
158 [yi, n] = fread (fid, length, format, 0, BYTEORDER); |
|
159 |
|
160 fclose (fid); |
|
161 |
|
162 if (formatTag == FORMAT_PCM) |
|
163 # normalize samples |
|
164 switch bitsPerSample |
|
165 case 8 |
|
166 yi = (yi - 127)/127; # 8-bit samples are unsigned |
|
167 case {16,32} |
|
168 yi = yi/((2 ** bitsPerSample) / 2 - 1); |
|
169 endswitch |
|
170 endif |
|
171 |
|
172 # deinterleave |
|
173 y = []; |
|
174 for (i = 1:channels) |
|
175 y = [y yi(i:channels:n)]; |
|
176 endfor |
|
177 |
|
178 endfunction |
|
179 ## Copyright (C) 2005 Michael Zeising |
|
180 ## |
|
181 ## This file is part of Octave. |
|
182 ## |
|
183 ## Octave is free software; you can redistribute it and/or modify it |
|
184 ## under the terms of the GNU General Public License as published by |
|
185 ## the Free Software Foundation; either version 2, or (at your option) |
|
186 ## any later version. |
|
187 ## |
|
188 ## Octave is distributed in the hope that it will be useful, but |
|
189 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
190 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
191 ## General Public License for more details. |
|
192 ## |
|
193 ## You should have received a copy of the GNU General Public License |
|
194 ## along with Octave; see the file COPYING. If not, write to the Free |
|
195 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
196 ## 02110-1301, USA. |
|
197 |
|
198 ## -*- texinfo -*- |
|
199 ## @deftypefn {Function File} {} wavwrite(@var{filename}, @var{y}) |
|
200 ## Write @var{y} to the canonical RIFF/WAVE sound file @var{filename}. A sample |
|
201 ## rate of 8000 Hz and 16-bit samples are assumed. Each column of the data |
|
202 ## represents a separate channel. |
|
203 ## |
|
204 ## @deftypefnx {Function File} {} wavwrite(@var{filename}, @var{y}, @var{fs}) |
|
205 ## Set the sample rate to @var{fs} Hz. |
|
206 ## |
|
207 ## @deftypefnx {Function File} {} wavwrite(@var{filename}, @var{y}, @var{fs}, @var{bits}) |
|
208 ## Set the sample rate to @var{fs} Hz and resolution to @var{bits} bits. |
|
209 ## |
|
210 ## @end deftypefn |
|
211 ## |
|
212 ## @seealso{wavread} |
|
213 |
|
214 ## Author: Michael Zeising <michael.zeising@stud.uni-erlangen.de> |
|
215 ## Created: 06 December 2005 |
|
216 |
|
217 function wavwrite (filename, y, samplesPerSec, bitsPerSample) |
|
218 BYTEORDER = "ieee-le"; |
|
219 |
|
220 # parse arguments |
|
221 if (exist ("samplesPerSec","var") < 1) |
|
222 warning ("wavwrite: sample rate set to 8000 Hz") |
|
223 samplesPerSec = 8000; |
|
224 endif |
|
225 if (exist ("bitsPerSample","var") < 1) |
|
226 warning ("wavwrite: sample resolution set to 16-bit") |
|
227 bitsPerSample = 16; |
|
228 endif |
|
229 |
|
230 # determine sample format |
|
231 switch bitsPerSample |
|
232 case 8 |
|
233 format = "int8"; |
|
234 case 16 |
|
235 format = "int16"; |
|
236 case 32 |
|
237 format = "int32"; |
|
238 otherwise |
|
239 fclose (fid); |
|
240 error ("wavread: sample resolution not supported"); |
|
241 endswitch |
|
242 |
|
243 # calculate filesize |
|
244 channels = size(y)(2); |
|
245 n = size(y)(1); |
|
246 |
|
247 ckSize = n*channels*(bitsPerSample/8); # size of data chunk |
|
248 |
|
249 # open file for writing binary |
|
250 [fid, msg] = fopen (filename, "wb"); |
|
251 if (fid < 0) |
|
252 error ("wavwrite: %s", msg) |
|
253 endif |
|
254 |
|
255 # write RIFF/WAVE header |
|
256 c = 0; |
|
257 c += fwrite (fid, "RIFF", "uchar"); |
|
258 c += fwrite (fid, ckSize + 36, "ulong", 0, BYTEORDER); # file size - 8 |
|
259 c += fwrite (fid, "WAVEfmt ", "uchar"); |
|
260 c += fwrite (fid, 16, "ulong", 0, BYTEORDER); # size of fmt chunk |
|
261 c += fwrite (fid, 0x0001, "short", 0, BYTEORDER); # sample format code (PCM) |
|
262 c += fwrite (fid, channels, "short", 0, BYTEORDER); # channels |
|
263 c += fwrite (fid, samplesPerSec, "ulong", 0, BYTEORDER); # sample rate |
|
264 c += fwrite (fid, samplesPerSec*channels*bitsPerSample/8, "ulong", 0, BYTEORDER); # bytes per second |
|
265 c += fwrite (fid, channels*bitsPerSample/8, "short", 0, BYTEORDER); # block align |
|
266 c += fwrite (fid, bitsPerSample, "short", 0, BYTEORDER); # bits/sample |
|
267 c += fwrite (fid, "data", "uchar"); |
|
268 c += fwrite (fid, ckSize, "ulong", 0, BYTEORDER); # size of data chunk |
|
269 |
|
270 if (c < 25) |
|
271 fclose (fid); |
|
272 error ("wavread: writing to file failed") |
|
273 endif |
|
274 |
|
275 # scale samples |
|
276 switch bitsPerSample |
|
277 case 8 |
|
278 y = floor (y*127 + 127); |
|
279 case {16,32} |
|
280 y = floor (y*((2 ** bitsPerSample) / 2 - 1)); |
|
281 endswitch |
|
282 |
|
283 # interleave samples |
|
284 l = n*channels; |
|
285 for (i = 1:channels) |
|
286 yi(i:channels:l) = y(:,i); |
|
287 endfor |
|
288 |
|
289 # write to file |
|
290 c = fwrite (fid, yi, format, 0, BYTEORDER); |
|
291 |
|
292 fclose (fid); |
|
293 |
|
294 endfunction |