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

  1. function [y,Fs,Format]=loadwave(wavefile)
  2. %LOADWAVE  Load Microsoft Windows 3.1 .WAV format sound files.
  3. %   [y]=LOADWAVE(wavefile) loads a .WAV format file specified by "wavefile", 
  4. %       returning the sampled data in variable "y". The .WAV extension 
  5. %       in the filename is optional.
  6. %
  7. %   [y,Fs]=LOADWAVE(wavefile) loads a .WAV format file specified by  
  8. %       "wavefile", returning the sampled data in variable "y" and the 
  9. %       sample rate in variable "Fs".
  10. %   
  11. %   [y,Fs,Format]=LOADWAVE(wavefile) loads a .WAV format file specified by 
  12. %       "wavefile",returning the sampled data in variable "y", the sample 
  13. %       rate in variable "Fs", and the .WAV file format information in 
  14. %       variable "Format". The format information is returned as a 6 element
  15. %       vector with the following order:
  16. %
  17. %               Format(1)       Data format (always PCM) 
  18. %               Format(2)       Number of channels
  19. %               Format(3)       Sample Rate (Fs)
  20. %               Format(4)       Average bytes per second (sampled)
  21. %               Format(5)       Block alignment of data
  22. %               Format(6)       Bits per sample
  23. %
  24. %   Note: LOADWAVE currently supports only 8-bit single channel data.
  25. %
  26. %   See also, SAVEWAVE .
  27.  
  28. %       Copyright (c) 1984-93 by The MathWorks, Inc.
  29.  
  30. if nargin~=1
  31.     error('LOADWAVE takes one argument, which is the name of the .WAV file');
  32. end
  33.  
  34. if findstr(wavefile,'.')==[]
  35.     wavefile=[wavefile,'.wav'];
  36. end
  37.  
  38. fid=fopen(wavefile,'rb');
  39. if fid ~= -1 
  40.     % read riff chunk
  41.     header=fread(fid,4,'uchar');
  42.     header=fread(fid,1,'ulong');
  43.     header=fread(fid,4,'uchar');
  44.     
  45.     % read format sub-chunk
  46.     header=fread(fid,4,'uchar');
  47.     header=fread(fid,1,'ulong');
  48.     
  49.     Format(1)=fread(fid,1,'ushort');                % PCM format 
  50.     Format(2)=fread(fid,1,'ushort');                % 1 channel
  51.     Fs=fread(fid,1,'ulong');        % samples per second
  52.     Format(3)=Fs;
  53.     Format(4)=fread(fid,1,'ulong'); % average bytes per second
  54.     Format(5)=fread(fid,1,'ushort');                % block alignment
  55.     Format(6)=fread(fid,1,'ushort');                % bits per sample
  56.     
  57.     
  58.     % read data sub-chunck
  59.     header=fread(fid,4,'uchar');
  60.     nsamples=fread(fid,1,'ulong');
  61.     y=fread(fid,nsamples,'uchar');
  62.     fclose(fid);
  63. end     
  64.  
  65. if fid == -1
  66.     error('Can''t open .WAV file for input!');
  67. end;
  68.