home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / SOUNDS.DI$ / SAVEWAVE.M < prev    next >
Encoding:
Text File  |  1993-03-21  |  1.7 KB  |  65 lines

  1. function savewave(wavefile,waveData,sRate)
  2. %SAVEWAVE Saves Microsoft Windows 3.1 .WAV format sound files.
  3. %   SAVEWAVE(wavefile,y,Fs) saves a .WAV format file specified by "wavefile".
  4. %
  5. %   The input arguments for SAVEWAVE are as follows:
  6. %
  7. %       wavefile    A string containing the name of the .WAV file to create
  8. %       y           The sampled data to save (8 bit max)
  9. %       Fs          The rate at which the data was sampled
  10. %               
  11. %   Note: SAVEWAVE will create an 8-bit, simgle channel wave file. Non 8-bit
  12. %   sample data will be truncated.
  13. %
  14. %   See also, LOADWAVE .
  15.  
  16. %       Copyright (c) 1984-93 by The MathWorks, Inc.
  17.  
  18. if nargin~=3
  19.     error('SAVEWAVE needs three arguments!');
  20. end
  21.  
  22. if findstr(wavefile,'.')==[]
  23.     wavefile=[wavefile,'.wav'];
  24. end
  25.  
  26. fid=fopen(wavefile,'wb');
  27.  
  28. if fid ~= -1
  29.     [m,n]=size(waveData);
  30.     nsamples=m*n;
  31.  
  32.     riffsize=36+nsamples;
  33.  
  34.     % write riff chunk
  35.     fwrite(fid,'RIFF','uchar');
  36.     fwrite(fid,riffsize,'ulong');
  37.     fwrite(fid,'WAVE','uchar');
  38.  
  39.     % write format sub-chunk
  40.     fwrite(fid,'fmt ','uchar');
  41.     fwrite(fid,16,'ulong');
  42.  
  43.     fwrite(fid,1,'ushort');         % PCM format 
  44.     fwrite(fid,1,'ushort');         % 1 channel
  45.     fwrite(fid,sRate,'ulong');      % samples per second
  46.     fwrite(fid,sRate,'ulong');      % average bytes per second
  47.     fwrite(fid,1,'ushort');         % block alignment
  48.     fwrite(fid,8,'ushort');         % bits per sample
  49.  
  50.  
  51.     % write data sub-chunck
  52.     fwrite(fid,'data','uchar');
  53.     fwrite(fid,nsamples,'ulong');
  54.     fwrite(fid,waveData,'uchar');
  55.  
  56.     if ((ceil(nsamples/2))~=(nsamples/2)) 
  57.         fwrite(fid,0,'uchar');
  58.     fclose(fid);
  59.     end
  60. end;
  61.  
  62. if fid == -1
  63.     error('Can''t open .WAV file for input!');
  64. end;
  65.