Mercurial > hg > octave-lyh
comparison scripts/audio/wavwrite.m @ 15529:9a7f73fc304f
Fix wavwrite to accept a row vector input (bug #37540)
* wavwrite.m: Accept a row vector as a single audio channel. Update
docstring and add a test case.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Mon, 15 Oct 2012 21:02:42 -0400 |
parents | 72c96de7a403 |
children | 221e71d2aef0 |
comparison
equal
deleted
inserted
replaced
15528:8d2b3db8b5b0 | 15529:9a7f73fc304f |
---|---|
21 ## @deftypefnx {Function File} {} wavwrite (@var{y}, @var{Fs}, @var{filename}) | 21 ## @deftypefnx {Function File} {} wavwrite (@var{y}, @var{Fs}, @var{filename}) |
22 ## @deftypefnx {Function File} {} wavwrite (@var{y}, @var{Fs}, @var{bps}, @var{filename}) | 22 ## @deftypefnx {Function File} {} wavwrite (@var{y}, @var{Fs}, @var{bps}, @var{filename}) |
23 ## Write @var{y} to the canonical RIFF/WAVE sound file @var{filename} | 23 ## Write @var{y} to the canonical RIFF/WAVE sound file @var{filename} |
24 ## with sample rate @var{Fs} and bits per sample @var{bps}. The | 24 ## with sample rate @var{Fs} and bits per sample @var{bps}. The |
25 ## default sample rate is 8000 Hz with 16-bits per sample. Each column | 25 ## default sample rate is 8000 Hz with 16-bits per sample. Each column |
26 ## of the data represents a separate channel. | 26 ## of the data represents a separate channel. If @var{y} is either a |
27 ## row vector or a column vector, it is written as a single channel. | |
27 ## @seealso{wavread} | 28 ## @seealso{wavread} |
28 ## @end deftypefn | 29 ## @end deftypefn |
29 | 30 |
30 ## Author: Michael Zeising <michael@michaels-website.de> | 31 ## Author: Michael Zeising <michael@michaels-website.de> |
31 ## Created: 06 December 2005 | 32 ## Created: 06 December 2005 |
48 if (nargin > 3) | 49 if (nargin > 3) |
49 bits_per_sample = varargin{2}; | 50 bits_per_sample = varargin{2}; |
50 endif | 51 endif |
51 endif | 52 endif |
52 | 53 |
54 ## calculate filesize | |
55 [n, channels] = size (y); | |
56 | |
57 ## allow y to be a row vector | |
58 if (n == 1) | |
59 n = channels; | |
60 channels = 1; | |
61 endif | |
62 | |
53 ## test arguments | 63 ## test arguments |
54 if (columns (y) < 1) | 64 if (channels < 1) |
55 error ("wavwrite: Y must have at least one column"); | 65 error ("wavwrite: Y must have at least one column"); |
56 endif | 66 endif |
57 if (columns (y) > 0x7FFF) | 67 if (channels > 0x7FFF) |
58 error ("wavwrite: Y has more than 32767 columns (too many for a WAV-file)"); | 68 error ("wavwrite: Y has more than 32767 columns (too many for a WAV-file)"); |
59 endif | 69 endif |
60 | 70 |
61 ## determine sample format | 71 ## determine sample format |
62 switch (bits_per_sample) | 72 switch (bits_per_sample) |
67 case 32 | 77 case 32 |
68 format = "int32"; | 78 format = "int32"; |
69 otherwise | 79 otherwise |
70 error ("wavwrite: sample resolution not supported"); | 80 error ("wavwrite: sample resolution not supported"); |
71 endswitch | 81 endswitch |
72 | |
73 ## calculate filesize | |
74 [n, channels] = size (y); | |
75 | 82 |
76 ## size of data chunk | 83 ## size of data chunk |
77 ck_size = n*channels*(bits_per_sample/8); | 84 ck_size = n*channels*(bits_per_sample/8); |
78 | 85 |
79 if (! ischar (filename)) | 86 if (! ischar (filename)) |
172 %! assert (samples_per_sec, 4000); | 179 %! assert (samples_per_sec, 4000); |
173 %! assert (bits_per_sample, 8); | 180 %! assert (bits_per_sample, 8); |
174 %! unlink (fname); | 181 %! unlink (fname); |
175 % | 182 % |
176 %!test | 183 %!test |
177 %! A = [-2:2]; | 184 %! A = [-2:2]'; |
178 %! wavwrite (A, fname); | 185 %! wavwrite (A, fname); |
179 %! B = wavread (fname); | 186 %! B = wavread (fname); |
180 %! B *= 32768; | 187 %! B *= 32768; |
181 %! assert (B, [-32768 -32768 0 32767 32767]); | 188 %! assert (B, [-32768 -32768 0 32767 32767]'); |
189 %! unlink (fname); | |
190 % | |
191 %!test | |
192 %! A = [-1:0.1:1]; | |
193 %! wavwrite (A, fname); | |
194 %! [B, samples_per_sec, bits_per_sample] = wavread (fname); | |
195 %! assert (A', B, 1/2^15); | |
196 %! assert (samples_per_sec, 8000); | |
197 %! assert (bits_per_sample, 16); | |
182 %! unlink (fname); | 198 %! unlink (fname); |
183 | 199 |