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 -*- |
5567
|
21 ## @deftypefn {Function File} {@var{y}} = wavread (@var{filename}) |
|
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 ## |
5567
|
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 ## |
5567
|
30 ## @deftypefnx {Function File} {[@dots{}]} = wavread (@var{filename}, @var{n}) |
5565
|
31 ## Read only the first @var{n} samples from each channel. |
|
32 ## |
5567
|
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 ## |
5567
|
36 ## @deftypefnx {Function File} {[@var{samples}, @var{channels}]} = wavread (@var{filename}, "size") |
|
37 ## Return the number of samples (@var{n}) and channels (@var{ch}) |
|
38 ## instead of the audio data. |
5565
|
39 ## @end deftypefn |
|
40 ## |
|
41 ## @seealso{wavwrite} |
|
42 |
|
43 ## Author: Michael Zeising <michael.zeising@stud.uni-erlangen.de> |
|
44 ## Created: 06 December 2005 |
|
45 |
5567
|
46 function [y, samples_per_sec, bits_per_sample] = wavread (filename, param) |
|
47 |
5565
|
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 |
5567
|
55 if (nargin < 1 || nargin > 2) |
|
56 usage ("wavread (filename, param)"); |
|
57 endif |
|
58 |
5565
|
59 # open file for binary reading |
5567
|
60 |
|
61 if (! ischar (filename)) |
|
62 error ("wavwrite: expecting filename to be a character string"); |
|
63 endif |
|
64 |
5565
|
65 [fid, msg] = fopen (filename, "rb"); |
|
66 if (fid < 0) |
|
67 error ("wavread: %s", msg) |
|
68 endif |
|
69 |
5567
|
70 ## check for RIFF/WAVE header |
|
71 ck_id = char (fread (fid, 4))'; |
5565
|
72 fseek (fid, 4, SEEK_CUR); |
5567
|
73 wave_id = char (fread (fid, 4))'; |
|
74 if (ck_id != "RIFF" || wave_id != "WAVE") |
5565
|
75 fclose (fid); |
|
76 error ("wavread: file contains no RIFF/WAVE signature"); |
|
77 endif |
|
78 |
5567
|
79 ## find format chunk within the next 256 (4*64) bytes |
5565
|
80 i = 1; |
5567
|
81 while (true) |
|
82 if (char (fread (fid, 4))' == "fmt "); |
|
83 break; |
5565
|
84 endif |
|
85 if (i++ == 64) |
|
86 fclose (fid); |
5567
|
87 error ("wavread: file contains no format chunk"); |
5565
|
88 endif |
|
89 endwhile |
5567
|
90 |
|
91 ## format chunk size |
|
92 ck_size = fread (fid, 1, "ulong", 0, BYTEORDER); |
5565
|
93 |
5567
|
94 ## sample format code |
|
95 format_tag = fread (fid, 1, "short", 0, BYTEORDER); |
|
96 if (format_tag != FORMAT_PCM && format_tag != FORMAT_IEEE_FLOAT) |
|
97 fclose (fid); |
|
98 error ("wavread: sample format %#x is not supported", format_tag); |
|
99 endif |
|
100 |
|
101 ## number of interleaved channels |
|
102 channels = fread (fid, 1, "short", 0, BYTEORDER); |
|
103 |
|
104 ## sample rate |
|
105 samples_per_sec = fread (fid, 1, "ulong", 0, BYTEORDER); |
|
106 |
|
107 ## bits per sample |
5565
|
108 fseek (fid, 6, SEEK_CUR); |
5567
|
109 bits_per_sample = fread (fid, 1, "short", 0, BYTEORDER); |
|
110 |
|
111 ## ignore the rest of the chunk |
|
112 fseek (fid, ck_size-16, SEEK_CUR); |
5565
|
113 |
5567
|
114 ## find data chunk |
5565
|
115 i = 1; |
5567
|
116 while (true) |
|
117 if (char (fread (fid, 4))' == "data") |
|
118 break; |
5565
|
119 endif |
|
120 if (i++ == 64) |
|
121 fclose (fid); |
5567
|
122 error ("wavread: file contains no data chunk"); |
5565
|
123 endif |
|
124 end |
5567
|
125 |
|
126 ## data chunk size |
|
127 ck_size = fread (fid, 1, "ulong", 0, BYTEORDER); |
5565
|
128 |
5567
|
129 ## determine sample data type |
|
130 if (format_tag == FORMAT_PCM) |
|
131 switch bits_per_sample |
5565
|
132 case 8 |
|
133 format = "int8"; |
|
134 case 16 |
|
135 format = "int16"; |
|
136 case 32 |
|
137 format = "int32"; |
|
138 otherwise |
|
139 fclose (fid); |
5567
|
140 error ("wavread: %d bits sample resolution is not supported with PCM", bits_per_sample); |
5565
|
141 endswitch |
|
142 else |
5567
|
143 switch (bits_per_sample) |
5565
|
144 case 32 |
|
145 format = "float32"; |
|
146 case 64 |
|
147 format = "float64"; |
|
148 otherwise |
|
149 fclose (fid); |
5567
|
150 error ("wavread: %d bits sample resolution is not supported with IEEE float", bits_per_sample); |
5565
|
151 endswitch |
|
152 endif |
|
153 |
5567
|
154 ## parse arguments |
|
155 if (nargin == 1) |
5565
|
156 length = inf; |
|
157 else |
5567
|
158 if (size (param, 2) == 1) |
|
159 ## number of samples is given |
5565
|
160 length = param * channels; |
5567
|
161 elseif (size (param, 2) == 2) |
|
162 ## sample range is given |
|
163 if (fseek (fid, param(1) * channels * (bits_per_sample/8), SEEK_CUR) < 0) |
|
164 warning ("wavread: seeking failed"); |
5565
|
165 endif |
|
166 length = (param(2)-param(1)) * channels; |
5567
|
167 elseif (size (param, 2) == 4 && char (param) == "size") |
|
168 ## size of the file is requested |
5565
|
169 fclose (fid); |
5567
|
170 y = [ck_size/channels/bits_per_sample/8, channels]; |
5565
|
171 return |
|
172 else |
|
173 fclose (fid); |
|
174 error ("wavread: invalid argument 2"); |
|
175 endif |
|
176 endif |
|
177 |
5567
|
178 ## read samples |
5565
|
179 [yi, n] = fread (fid, length, format, 0, BYTEORDER); |
|
180 |
|
181 fclose (fid); |
|
182 |
5567
|
183 if (format_tag == FORMAT_PCM) |
|
184 ## normalize samples |
|
185 switch (bits_per_sample) |
5565
|
186 case 8 |
|
187 yi = (yi - 127)/127; # 8-bit samples are unsigned |
5567
|
188 case {16, 32} |
|
189 yi = yi/((2 ** bits_per_sample) / 2 - 1); |
5565
|
190 endswitch |
|
191 endif |
|
192 |
5567
|
193 ## deinterleave |
|
194 ## y = []; |
|
195 ## for i = 1:channels |
|
196 ## y = [y, yi(i:channels:n)]; |
|
197 ## endfor |
|
198 |
|
199 nr = numel (yi) / channels; |
|
200 y = reshape (yi, channels, nr)'; |
5565
|
201 |
|
202 endfunction |