function [snd, srate, nbits] = wavin(infile,nframes); % [snd, srate, nbits] = wavin(infile,[nframes]) % % Read .wav file into snd (size (nchans,nsamples)). % srate = sampling rate in Hz. % nbits = bits per sample. % optional nframes = no. samples to read (all channels) % (default = all samples in file). % XXX FIXME XXX merge with auload % This program is public domain % Author: Julius O. Smith III % kpl replaced this line with following line: 10/24/05 % according to maintainers this fix should already be in the next octave-forge release % fpi = fopen(infile,'r'); fpi = fopen(infile,'rb','ieee-le'); riffId = 1179011410; % 'RIFF' waveId = 1163280727; % 'WAVE' fmtId = 544501094; % 'fmt ' dataId = 1635017060; % 'data' % read the file id - it must be 'RIFF' for this to be a .wav file id = fread(fpi, 1,'int32'); if id != riffId error(strcat(infile, ' is not a RIFF file')); end disp(strcat( infile, "is in RIFF format:")); nbytes = fread(fpi, 1,'int32'); disp(sprintf('\tTotal length = %d bytes',nbytes)); % read the 'WAVE' id subfmt = fread(fpi, 1,'int32'); if subfmt != waveId error(strcat('*** Cannot handle subformat %s',subfmt)); end % read the 'fmt ' id fmt = fread(fpi, 1,'int32'); %if (strcmp(fmt,'fmt ')!=0) if fmt != fmtId error(strcat("*** Expected 'fmt ' in header, but got '",fmt)); end cl = fread(fpi, 1,'int32'); if (cl ~= 16) error(sprintf('*** Expected format chunk length = 16 in soundfile header, but got %d',cl)); end % read the sample data format id tmps = fread(fpi,1,'int16'); if (tmps != 1) error(sprintf('*** Expected 01 in format chunk, but got %d',tmps)); end % read the number of audio channels nchans = fread(fpi,1,'int16'); % channels disp(sprintf('\tNumber of channels = %d',nchans)); srate = fread(fpi,1,'int32'); % sample rate disp(sprintf('\tSampling rate = %d Hz',srate)); tmpi = fread(fpi,1,'int32'); % bytes per sec % kpl: if (tmpi != 2*srate) if (tmpi != 2*srate*nchans) error(sprintf('*** %d bytes per second looks wrong. Expect %d', tmpi,2*srate)); end bps = fread(fpi,1,'int16'); % bytes per sample nbits = fread(fpi,1,'int16'); % bits per sample nbytes = 0; % seek to the 'data' chunk do id = fread(fpi,1,'int32'); % 'data'? nbytes = fread(fpi,1,'int32'); % chunk byte cnt if( id != dataId ) if( fseek(fpi,nbytes,'cof') == -1 ) error(strcat(infile," has no audio data.")); endif endif until( (id == dataId) || feof(fpi) ); %id = fread(fpi,1,'int32'); % 'data' %%if (strcmp(id,'data')!=0) %if id != dataId % error(sprintf("Did not find 'data' ID where expected")); %end % nbytes = fread(fpi,1,'int32'); % data chunk byte count if (mod(nbytes,2)==1) error(sprintf('*** Number of data bytes (%d) is ODD!',nbytes)); end nsamps = nbytes/2; disp(sprintf('\tData length = %d samples', nsamps)); if (mod(nsamps,nchans)~=0) error(sprintf(['*** Number of channels (%d) does not divide nsamps ' ... '(%d)!'],nchans,nsamps)); end if nargin < 2 nframes = nsamps/nchans; elseif (nframes=='size') && (nargout = 1) nspc = (nbytes/2)/nchannels; % no. samples per channel snd=[nspc nchannels]; % should first arg = total samples instead? return; end datafmt = sprintf('int%d',nbits'); snd = fread(fpi,[nchans,nframes],datafmt); snd = snd' / (2^(nbits-1)-1); % Transpose for Matlab compatibility