# HG changeset patch # User jwe # Date 1134420846 0 # Node ID c45cf76df06fa3abf700203fac513660bfb69460 # Parent 9fc532d861d4933b8b1ebbcf0b8ce813e697f71d [project @ 2005-12-12 20:54:06 by jwe] diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2005-12-12 Michael Zeising + + * audio/wavread.m, audio/wavwrite.m: + Correct scaling for 8-bit linear pcm samples. + Improve scaling for all other linear pcm resolutions. + 2005-12-07 John W. Eaton * statistics/base/moment.m: Don't save and restore warn_str_to_num. diff --git a/scripts/audio/wavread.m b/scripts/audio/wavread.m --- a/scripts/audio/wavread.m +++ b/scripts/audio/wavread.m @@ -36,6 +36,7 @@ ## @deftypefnx {Function File} {[@var{samples}, @var{channels}]} = wavread (@var{filename}, "size") ## Return the number of samples (@var{n}) and channels (@var{ch}) ## instead of the audio data. +## ## @end deftypefn ## ## @seealso{wavwrite} @@ -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 @@ -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 @@ -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)'; diff --git a/scripts/audio/wavwrite.m b/scripts/audio/wavwrite.m --- a/scripts/audio/wavwrite.m +++ b/scripts/audio/wavwrite.m @@ -58,7 +58,7 @@ ## determine sample format switch (bits_per_sample) case 8 - format = "int8"; + format = "uint8"; case 16 format = "int16"; case 32 @@ -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);