Date: Wed, 4 Oct 1995 17:22:43 -0400 (EDT) To: caron@inrs-telecom.uquebec.ca (Francky goes to Hollywood) cc: dpwe@media.mit.edu From: Francky goes to Hollywood Subject: Re: WAVREAD and WAVWRITE and also AUWRITE & AUREAD Allo! Here are the .m files that are very helpfull. WAVREAD16.m WAVWRITE16.m AUWRITEALL.m AUREADALL.m For AIFF .m file, I heard that MIT media lab made them. -- Francois Caron //// //// //// caron@inrs-telecom.uquebec.ca INRS-telecommunications //// //// //// @ (514)-761-8603 16 Place du Commerce //// //// //// @ (514)-362-9015 Ile des Soeurs, Montreal //// @ (514)-761-8501 Fax CANADA H3E 1H6 ******************************************************************************* ------------------------------------------------------------------------------- function [L,R,format]=wavread16(wavefile) %WAVREAD16 Load Microsoft Windows 3.1 .WAV format sound files. % % [L,R,format]=WAVREAD16(wavefile) loads a .WAV format file specified by % "wavefile", returning the sampled data in variable L and R, and the % .WAV file format information in variable "format". The format % information is returned as a 4 element vector with the following % order: % % format(1) Data format (Only PCM for now is known). % format(2) Number of channels % format(3) Sample Rate (Fs) % format(4) Bits per sample % % Note: If the Wave input file is mono, L and R will be the same. % % See also WAVWRITE16 % % Copyright (c) 1984-93 by The MathWorks, Inc. % Modified by Francois Caron, INRS-telecommunications, Quebec % 18-8-94 if nargin~=1 error('WAVREAD16 takes one argument, which is the name of the .WAV file.'); end if findstr(wavefile,'.')==[] wavefile=[wavefile,'.wav']; end fid=fopen(wavefile,'rb'); if fid ~= -1 % read riff chunk header=fread(fid,4,'uchar'); header=fread(fid,1,'ulong'); header=fread(fid,4,'uchar'); % read format sub-chunk header=fread(fid,4,'uchar'); header=fread(fid,1,'ulong'); format(1)=fread(fid,1,'ushort'); % Format format(2)=fread(fid,1,'ushort'); % Channel format(3)=fread(fid,1,'ulong'); % Samples per second header=fread(fid,1,'ulong'); block=fread(fid,1,'ushort'); format(4)=fread(fid,1,'ushort'); % Bits per sample % read data sub-chunck header=fread(fid,4,'uchar'); nbyteforsamples=fread(fid,1,'ulong'); nsamples=nbyteforsamples/block; if (format(4)+format(2) == 9) L = fread(fid,nsamples,'char'); R = L; end if (format(4)+format(2) == 10) y = fread(fid,[2,nsamples],'char'); L = y(1,:)'; R = y(2,:)'; end if (format(4)+format(2) == 17) L = fread(fid,nsamples,'short'); R = L; end if (format(4)+format(2) == 18) y = fread(fid,[2,nsamples],'short'); L = y(1,:); R = y(2,:); end end if fid == -1 error('Can''t open .WAV file for input!'); end; ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- function wavwrite16(wavefile,waveData,format) %WAVWRITE16 Saves Microsoft Windows 3.1 .WAV format sound files. % % WAVWRITE16(wavefile,y,format) saves a .WAV format file specified by "wavefile ". % % The input arguments for WAVWRITE16 are as follows: % % wavefile A string containing the name of the .WAV file to create % y The sampled data to save in floating point. % Channel 0 Channel 1 % " " % ... ... % Note that these value will be truncat to 8 or 16 bits. % % format Format is a four(4) element vector of Wave file. % [1] : Coding : PCM = 1, Mu-law = 2 % [2] : Channel: Mono = 1, Stereo = 2 % [3] : Sampling rate : 11025, 22050 or 44100 ( CD quality ) % [4] : The resolution : 8 or 16 bit per sample. % % See also WAVREAD16. % % Copyright (c) 1984-93 by The MathWorks, Inc. % Modify by Francois Caron, INRS-telecommunications, Quebec % 7-7-94 if nargin~=3 error('WAVWRITE16 needs three arguments!'); end if findstr(wavefile,'.')==[] wavefile=[wavefile,'.wav']; end fid=fopen(wavefile,'wb'); if fid ~= -1 [m,n]=size(waveData); nsamples = m; if (n > format(2)) error('Can put a binaural vector into a Wave Mono sound file!'); end if ~(format(3) == 11025 | format(3) == 22050 | format(3) == 44100) error('Sample frequency is not WAVE standard!'); end if (format(4)+format(2) == 9) Block = 1; end if (format(4)+format(2) == 10) | (format(4)+format(2) == 17) Block = 2; end if (format(4)+format(2) == 18) Block = 4; end Riffsize=36+(Block*nsamples); % write riff chunk fwrite(fid,'RIFF','uchar'); fwrite(fid,Riffsize,'ulong'); fwrite(fid,'WAVE','uchar'); % write format sub-chunk fwrite(fid,'fmt ','uchar'); fwrite(fid,16,'ulong'); Average = (format(4)/8)*format(3)*format(2); fwrite(fid,format(1),'ushort'); % PCM or X-law format fwrite(fid,format(2),'ushort'); % x channel fwrite(fid,format(3),'ulong'); % samples per second fwrite(fid,Average,'ulong'); % average bytes per second fwrite(fid,Block,'ushort'); % block alignment fwrite(fid,format(4),'ushort'); % bits per sample % write data sub-chunck fwrite(fid,'data','uchar'); fwrite(fid,(nsamples*Block),'ulong'); if (format(4)+format(2) == 9) fwrite(fid,waveData,'uchar'); end if (format(4)+format(2) == 10) for i=1:nsamples, fwrite(fid,waveData(i,1),'uchar'); if n == 1 fwrite(fid,waveData(i,1),'uchar'); else fwrite(fid,waveData(i,2),'uchar'); end end end if (format(4)+format(2) == 17) fwrite(fid,waveData,'ushort'); end if (format(4)+format(2) == 18) for i=1:nsamples, fwrite(fid,waveData(i,1),'ushort'); if n == 1 fwrite(fid,waveData(i,1),'ushort'); else fwrite(fid,waveData(i,2),'ushort'); end end end end; if fid == -1 error('Can''t open .WAV file for input!'); end; ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- function auwriteall(aufile,data,format) %AUWRITEALL Read all kind of .au audio file. % % AUWRITEALL('filename',data,format) creates the .AU audio file. % % data: The data vector. % % format(1) = Data format % format(2) = Data sampling rate % format(3) = Data number of channel % % Note: Only for Monophonique sound for now. % Only for format type 1 and 3. There are the most popular. % % 1: 8-bits U-law % 3: 16-bits linear % % rf: audio-fmts (part2) % http://www.lib.ox.ac.uk/internet/news/faq/by_category.audio-fmts.html (1K) % % Copyright (c) 1984-94 by The MathWorks, Inc. % $Revision: 1.8 $ % Modified by Francois Caron -- INRS-telecommunications, Quebec % 4-6-95 if nargin~=3 error('AUWRITEALL needs three arguments!'); end if findstr(aufile,'.')==[] aufile=[aufile,'.au']; end [m,n]=size(data); if (n>m) data=data'; a=m; m=n; end nsamples = m; if (a > format(3)) error('Can put a binaural vector into a Mono sound file!'); error('Stereo file not available for now, sorry'); end clear a m n; fid = fopen(aufile,'wb'); if fid ~= -1 % Writing to the file fwrite(fid,'.snd','uchar'); if (format(1) == 3) % 3 -> 16 bits linear nsamples = nsamples*2; end fwrite(fid,28,'ulong'); fwrite(fid,nsamples,'ulong'); fwrite(fid,format(1),'ulong'); fwrite(fid,format(2),'ulong'); fwrite(fid,format(3),'ulong'); fwrite(fid,'INRS','uchar'); % Put any ID you want o f 4 char if (format(1) == 1) % 1 -> 8 bits mu-law data = lin2mu(data); fwrite(fid,data,'char'); end if (format(1) == 3) % 3 -> 16 bits linear fwrite(fid,data,'short'); else error('Unknow format'); end end if fid == -1 error('Can''t create .AU file for writing!'); end end; ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- function [L,format] = aureadall(aufile) %AUREADALL Read all kind of .au audio file. % % [L, format] = AUREADALL('filename') reads the audio file. % % L: The left signal % % format(1) = Data format % format(2) = Data sampling rate % format(3) = Data number of channel % % Only for Monophonique sound for now. % Only for format type 1 and 3. % % Copyright (c) 1984-94 by The MathWorks, Inc. % $Revision: 1.8 $ % Modified by Francois Caron -- INRS-telecommunications % 27-6-1995 if findstr(aufile,'.')==[] aufile=[aufile,'.au']; end fid = fopen(aufile,'rb'); if fid ~= -1 header=fread(fid,4,'uchar'); % ID = .snd datalocation=fread(fid,1,'ulong'); nsamples =fread(fid,1,'ulong'); % Data size in byte format(1)=fread(fid,1,'ulong'); % Data format format(2)=fread(fid,1,'ulong'); % Sampling rate format(3)=fread(fid,1,'ulong'); % Channel header=fread(fid,datalocation-24,'uchar'); % Skip to data if (format(1)+format(3) == 2) % 1 -> 8 bits mu-law LL = fread(fid,nsamples,'uchar'); L = 33425*mu2lin(LL); % Resize to a short clear LL; end if (format(1)+format(3) == 4) % 3 -> 16 bit linear nsamples = nsamples/2; L = fread(fid,nsamples,'short'); end end if fid == -1 error('Can''t open .AU file for input!'); end; -----------------------------------------------------------------------------