% A program to read the au file
%

% file : /home/sb/sb1/speech_assign/assign5/auread.m
%

function y = auread(name)
%AUREAD Read mu-law encoded audio file.
%       Y = AUREAD('filename') reads the audio file in 'filename'
%       and converts the data from mu-law encoded bytes to a signal
%       in the range -1 <= Y <= 1.
%
%       Copyright (c) 1984-94 by The MathWorks, Inc.
%       $Revision: 1.8 $ 
%
%       modified: Matt Grund 1996  matt@sardine.whoi.edu
%
%                Script modified to read header block completely, 
%                and can handle several new formats: 
%
%                               - 8 bit linear
%                               - 16 bit linear
%                               - 32 bit linear
%
%               Also, if the header indicates that the data is multichannel,
%               the output matrix is reshaped appropriately. In general,
%               the format is one column per channel, one row per sample
%               interval.
%
fp = fopen(name,'rb');
if (fp == -1), error(['File ',name,' not found.']); end
[rawhead,len] = fread(fp,24,'uchar');

if ( len ~=24), error('incomplete header.'); end

%parse the header "big-endian"
magic=rawhead(4)+256*rawhead(3)+256*256*rawhead(2)+256*256*256*rawhead(1);
location=rawhead(8)+256*rawhead(7)+256*256*rawhead(6)+256*256*256*rawhead(5);
bytes=rawhead(12)+256*rawhead(11)+256*256*rawhead(10)+256*256*256*rawhead(9);
format=rawhead(16)+256*rawhead(15)+256*256*rawhead(14)+256*256*256*rawhead(13);
rate=rawhead(20)+256*rawhead(19)+256*256*rawhead(18)+256*256*256*rawhead(17);
channels=rawhead(24)+256*rawhead(23)+256*256*rawhead(22)+256*256*256*rawhead(21);

if (format==1)
        formstring='8 bit mu-law';
        bytespersample=1;
end;

if (format==2)
        formstring='8 bit linear';
        bytespersample=1;
end;

if (format==3)
        formstring='16 bit linear';
        bytespersample=2;
end;

if (format==5)
        formstring='32 bit linear';
        bytespersample=4;
end;

if ((format~=5)&(format~=1)&(format~=2)&(format~=3))
        formstring='UNRECOGNIZED';
        bytespersample=1;
end;

fid = fopen('temp.bin','wb');

% print header report
fprintf(fid,'\nauread:\n');
fprintf(fid,'       file:         "%s"\n',name);
fprintf(fid,'       format:        %s\n',formstring);
fprintf(fid,'       channels:      %d\n',channels);
fprintf(fid,'       size:          %d bytes\n',bytes);
fprintf(fid,'       sample rate:   %d Hz\n',rate);
duration=bytes/bytespersample/channels/rate;
fprintf(fid,'       duration:      %06.2f seconds\n\n',duration);

% skip comments
[dummy,len] = fread(fp,(location - 24),'uchar');
if ( len ~=(location-24)), error('header incorrect: cant find data.'); end
clear dummy;
clear rawhead;
clear y;
chunk=1024;
if (format==1)
        [mu,len]=fread(fp,inf,'uchar');
        y = mu2lin(mu);
        clear mu;
end;

if (format==2)
        [y,len]=fread(fp,inf,'int8');
end;

if (format==3)
        [y,len]=fread(fp,inf,'int16');
end;

if (format==5)
        [y,len]=fread(fp,inf,'int32');
end;

if ((format~=5)&(format~=1)&(format~=2)&(format~=3))
        [y,len]=fread(fp,inf,'schar');
end;
l=length(y);

%reshape the data
if (channels~=1),
        y=reshape(y(1:channels*floor(l/channels)),channels,floor(l/channels))';
end


