Mercurial > octave-nkf
annotate scripts/audio/wavread.m @ 8506:bc982528de11
comment style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 13 Jan 2009 11:56:00 -0500 |
parents | da8e4aa06134 |
children | 9662dfb26652 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2005, 2006, 2007 Michael Zeising |
5565 | 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 | |
7117 | 41 ## Author: Michael Zeising <michael@michaels-website.de> |
5565 | 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 | |
8506 | 64 ## Check for RIFF/WAVE header. |
5567 | 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 | |
8506 | 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 |
8506 | 85 ## Format chunk size. |
6304 | 86 ck_size = fread (fid, 1, "uint32", 0, BYTEORDER); |
5565 | 87 |
8506 | 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 | |
8506 | 95 ## Number of interleaved channels. |
6304 | 96 channels = fread (fid, 1, "uint16", 0, BYTEORDER); |
5567 | 97 |
8506 | 98 ## Sample rate. |
6304 | 99 samples_per_sec = fread (fid, 1, "uint32", 0, BYTEORDER); |
5567 | 100 |
8506 | 101 ## Bits per sample. |
5565 | 102 fseek (fid, 6, SEEK_CUR); |
6304 | 103 bits_per_sample = fread (fid, 1, "uint16", 0, BYTEORDER); |
5567 | 104 |
8506 | 105 ## Ignore the rest of the chunk. |
5567 | 106 fseek (fid, ck_size-16, SEEK_CUR); |
5565 | 107 |
8506 | 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 |
7151 | 118 endwhile |
5567 | 119 |
8506 | 120 ## Data chunk size. |
6304 | 121 ck_size = fread (fid, 1, "uint32", 0, BYTEORDER); |
5565 | 122 |
8506 | 123 ## Determine sample data type. |
5567 | 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 | |
8506 | 152 ## Parse arguments. |
5567 | 153 if (nargin == 1) |
7461
da8e4aa06134
limit data read to data chunk in wavread.m
John W. Eaton <jwe@octave.org>
parents:
7151
diff
changeset
|
154 length = 8 * ck_size / bits_per_sample; |
5565 | 155 else |
5567 | 156 if (size (param, 2) == 1) |
8506 | 157 ## Number of samples is given. |
5565 | 158 length = param * channels; |
5567 | 159 elseif (size (param, 2) == 2) |
8506 | 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") |
8506 | 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 |
8506 | 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 |
8506 | 183 ## Check data. |
6304 | 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]; | |
7151 | 193 endif |
194 | |
5567 | 195 if (format_tag == FORMAT_PCM) |
8506 | 196 ## Normalize samples. |
5567 | 197 switch (bits_per_sample) |
5565 | 198 case 8 |
7117 | 199 yi = (yi - 128)/127; |
5572 | 200 case 16 |
7117 | 201 yi /= 32767; |
5839 | 202 case 24 |
7117 | 203 yi /= 8388607; |
5572 | 204 case 32 |
7117 | 205 yi /= 2147483647; |
5565 | 206 endswitch |
207 endif | |
208 | |
8506 | 209 ## Deinterleave. |
5567 | 210 nr = numel (yi) / channels; |
211 y = reshape (yi, channels, nr)'; | |
5565 | 212 |
213 endfunction |