From bug-request at octave dot org Mon Dec 12 09:47:55 2005 Subject: Update: wavread(), wavwrite() (diff files) From: Michael Zeising To: bug at octave dot org Date: Mon, 12 Dec 2005 07:31:13 -0600 There's an update for the RIFF/WAVE import/export functions wavread() and wavwrite(): wavread 1.4, wavwrite 1.3: o fixed wrong scaling for 8-bit linear pcm samples o improved scaling for all other linear pcm resolutions o further minimum adaptions to octave coding style This is a multi-part message in MIME format. --------------020109040708050103010203 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit --------------020109040708050103010203 Content-Type: text/x-patch; name="wavread.diff" Content-Transfer-Encoding: 8bit Content-Disposition: inline; filename="wavread.diff" --- wavread-1.3.m 2005-12-12 14:07:20.000000000 +0100 +++ wavread.m 2005-12-12 14:16:49.000000000 +0100 at @ -36,6 +36,7 @@ ## at deftypefnx {Function File} {[@var{samples}, @var{channels}]} = wavread (@var{filename}, "size") ## Return the number of samples ( at var{n}) and channels (@var{ch}) ## instead of the audio data. +## ## at end deftypefn ## ## at seealso{wavwrite} at @ -47,24 +48,20 @@ FORMAT_PCM = 0x0001; # PCM (8/16/32 bit) FORMAT_IEEE_FLOAT = 0x0003; # IEEE float (32/64 bit) - FORMAT_ALAW = 0x0006; # 8-bit ITU-T G.711 A-law (not yet supported) - FORMAT_MULAW = 0x0007; # 8-bit ITU-T G.711 -law (not yet supported) - FORMAT_IMA_ADPCM = 0x0011; # IMA/ADPCM 4:1 compression (not yet supported) BYTEORDER = "ieee-le"; if (nargin < 1 || nargin > 2) usage ("wavread (filename, param)"); endif - # open file for binary reading - if (! ischar (filename)) error ("wavwrite: expecting filename to be a character string"); endif + ## open file for binary reading [fid, msg] = fopen (filename, "rb"); if (fid < 0) - error ("wavread: %s", msg) + error ("wavread: %s", msg); endif ## check for RIFF/WAVE header at @ -130,7 +127,7 @@ if (format_tag == FORMAT_PCM) switch bits_per_sample case 8 - format = "int8"; + format = "uint8"; case 16 format = "int16"; case 32 at @ -175,27 +172,23 @@ endif endif - ## read samples + ## read samples and close file [yi, n] = fread (fid, length, format, 0, BYTEORDER); - fclose (fid); if (format_tag == FORMAT_PCM) ## normalize samples switch (bits_per_sample) case 8 - yi = (yi - 127)/127; # 8-bit samples are unsigned - case {16, 32} - yi = yi/((2 ** bits_per_sample) / 2 - 1); + yi = (yi - 127.5)/127.5; + case 16 + yi = yi/32768; + case 32 + yi = yi/2147483648; endswitch endif ## deinterleave - ## y = []; - ## for i = 1:channels - ## y = [y, yi(i:channels:n)]; - ## endfor - nr = numel (yi) / channels; y = reshape (yi, channels, nr)'; --------------020109040708050103010203 Content-Type: text/x-patch; name="wavwrite.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="wavwrite.diff" --- wavwrite-1.2.m 2005-12-12 14:08:14.000000000 +0100 +++ wavwrite.m 2005-12-11 22:06:54.000000000 +0100 at @ -58,7 +58,7 @@ ## determine sample format switch (bits_per_sample) case 8 - format = "int8"; + format = "uint8"; case 16 format = "int16"; case 32 at @ -121,22 +121,19 @@ error ("wavread: writing to file failed"); endif + ## interleave samples + yi = reshape (y', n*channels, 1); + ## scale samples switch (bits_per_sample) case 8 - y = floor (y*127 + 127); - case {16, 32} - y = floor (y*((2 ** bits_per_sample) / 2 - 1)); + yi = round (yi*127.5 + 127.5); + case 16 + yi = floor (yi*32767.5); + case 32 + yi = floor (yi*2147483647.5); endswitch - ## interleave samples - ## l = n*channels; - ## for i = 1:channels - ## yi(i:channels:l) = y(:,i); - ## endfor - - yi = reshape (y', n*channels, 1); - ## write to file c = fwrite (fid, yi, format, 0, BYTEORDER); --------------020109040708050103010203-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------