comparison scripts/audio/wavread.m @ 12701:de3e90a420e3 stable

Overhaul wavwrite, wavread and fix normalization problem (Bug #33420). * wavwrite.m: Remove ancient non-Matlab calling form of function. Update tests and add test to verify proper clipping of out-of-range values. * wavread.m: Use correct normalization constant to put values in range [-1,1). Add test to stop fntests.m from reporting this as untested function.
author Rik <octave@nomad.inbox5.com>
date Sat, 04 Jun 2011 07:20:42 -0700
parents c792872f8942
children e0b174e99449
comparison
equal deleted inserted replaced
12700:9843b3b055e0 12701:de3e90a420e3
20 ## @deftypefn {Function File} {@var{y} =} wavread (@var{filename}) 20 ## @deftypefn {Function File} {@var{y} =} wavread (@var{filename})
21 ## Load the RIFF/WAVE sound file @var{filename}, and return the samples 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 22 ## in vector @var{y}. If the file contains multichannel data, then
23 ## @var{y} is a matrix with the channels represented as columns. 23 ## @var{y} is a matrix with the channels represented as columns.
24 ## 24 ##
25 ## @deftypefnx {Function File} {[@var{y}, @var{Fs}, @var{bits}] =} wavread (@var{filename}) 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 26 ## Additionally return the sample rate (@var{fs}) in Hz and the number of bits
27 ## per sample (@var{bits}). 27 ## per sample (@var{bps}).
28 ## 28 ##
29 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n}) 29 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n})
30 ## Read only the first @var{n} samples from each channel. 30 ## Read only the first @var{n} samples from each channel.
31 ## 31 ##
32 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n1} @var{n2}) 32 ## @deftypefnx {Function File} {[@dots{}] =} wavread (@var{filename}, @var{n1} @var{n2})
73 fclose (fid); 73 fclose (fid);
74 error ("wavread: file contains no RIFF chunk"); 74 error ("wavread: file contains no RIFF chunk");
75 endif 75 endif
76 76
77 riff_type = char (fread (fid, 4))'; 77 riff_type = char (fread (fid, 4))';
78 if(! strcmp (riff_type, "WAVE")) 78 if (! strcmp (riff_type, "WAVE"))
79 fclose (fid); 79 fclose (fid);
80 error ("wavread: file contains no WAVE signature"); 80 error ("wavread: file contains no WAVE signature");
81 endif 81 endif
82 riff_pos = riff_pos + 4; 82 riff_pos = riff_pos + 4;
83 riff_size = riff_size - 4; 83 riff_size = riff_size - 4;
155 155
156 ## Parse arguments. 156 ## Parse arguments.
157 if (nargin == 1) 157 if (nargin == 1)
158 length = 8 * data_size / bits_per_sample; 158 length = 8 * data_size / bits_per_sample;
159 else 159 else
160 nparams = size (param, 2); 160 nparams = numel (param);
161 if (nparams == 1) 161 if (nparams == 1)
162 ## Number of samples is given. 162 ## Number of samples is given.
163 length = param * channels; 163 length = param * channels;
164 elseif (nparams == 2) 164 elseif (nparams == 2)
165 ## Sample range is given. 165 ## Sample range is given.
172 fclose (fid); 172 fclose (fid);
173 y = [data_size/channels/(bits_per_sample/8), channels]; 173 y = [data_size/channels/(bits_per_sample/8), channels];
174 return; 174 return;
175 else 175 else
176 fclose (fid); 176 fclose (fid);
177 error ("wavread: invalid argument 2"); 177 error ("wavread: invalid PARAM argument");
178 endif 178 endif
179 endif 179 endif
180 180
181 ## Read samples and close file. 181 ## Read samples and close file.
182 if (bits_per_sample == 24) 182 if (bits_per_sample == 24)
199 199
200 if (format_tag == FORMAT_PCM) 200 if (format_tag == FORMAT_PCM)
201 ## Normalize samples. 201 ## Normalize samples.
202 switch (bits_per_sample) 202 switch (bits_per_sample)
203 case 8 203 case 8
204 yi = (yi - 128)/127; 204 yi = (yi - 128)/128;
205 case 16 205 case 16
206 yi /= 32767; 206 yi /= 32768;
207 case 24 207 case 24
208 yi /= 8388607; 208 yi /= 8388608;
209 case 32 209 case 32
210 yi /= 2147483647; 210 yi /= 2147483648;
211 endswitch 211 endswitch
212 endif 212 endif
213 213
214 ## Deinterleave. 214 ## Deinterleave.
215 nr = numel (yi) / channels; 215 nr = numel (yi) / channels;
237 endwhile 237 endwhile
238 if (! strcmp (id, chunk_id)) 238 if (! strcmp (id, chunk_id))
239 chunk_size = -1; 239 chunk_size = -1;
240 endif 240 endif
241 endfunction 241 endfunction
242
243
244 %% Tests for wavread/wavwrite pair are in wavrite.m
245 %!assert(1) # stop fntests.m from reporting no tests for wavread
246