wavwr.m
上传用户:m_sun_001
上传日期:2014-07-30
资源大小:1115k
文件大小:3k
- function wavwri(waveData,sRate,res,nchannel,wavefile,user_limit)
- %WAVWRI Saves Microsoft Windows .WAV format sound files.
- % WAVWRI(y,Fs,res,nchannel,wavefile,user_limit) saves a .WAV format file.
- %
- % The input arguments for WAVWRITE are as follows:
- %
- % y The sampled data to save
- % Fs The rate at which the data was sampled
- % res Resulution, 8 or 16 (bits/sample)
- % nchannel Number of channels, 1 or 2
- % wavefile A string containing the name of the .WAV file to create
- % user_limit OPTIONAL: if supplied, the data will be treated as if it had
- % maximum magnitude of user_limit. Otherwise the data will be
- % scaled so that its actual maximum magnitude goes to full scale.
- % So, if you want the actual values of your data to appear in the
- % WAV file, despite the fact that the max. mag. of your data may
- % not be full scale, for 16-bit you would supply a user_limit of
- % 32767.
- if (nargin~=5 & nargin~=6)
- error(['WAVWRI needs 5 or 6 arguments, got ' int2str(nargin)]);
- end
- limit=max(max(abs(waveData)));
- if nargin==6
- if limit <= user_limit
- limit = user_limit;
- else
- error(['a limit of ' num2str(user_limit) ' was passed in but actual limit is ' num2str(limit)]);
- end;
- end;
- waveData=waveData/limit*(2^(res-1) - 1);
- if res==8 waveData=waveData+128; end;
- fid=fopen(wavefile,'wb','l');
- if fid ~= -1
- [m,n]=size(waveData);
- nsamples=m*n;
- fac=res/8;
- riffsize=36+nsamples*fac;
- % 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');
- fwrite(fid,1,'ushort'); % PCM format
- fwrite(fid,nchannel,'ushort'); % 1 or 2 channel
- fwrite(fid,sRate,'ulong'); % samples per second
- fwrite(fid,fac*sRate*nchannel,'ulong'); % average bytes per second
- fwrite(fid,fac*nchannel,'ushort'); % block alignment
- fwrite(fid,fac*8,'ushort'); % bits per sample
- % write data sub-chunck
- fwrite(fid,'data','uchar');
- fwrite(fid,nsamples*fac,'ulong');
-
- if nchannel==2 waveData=waveData(:); end; % Wenn Stereo
-
- if fac==2
- fwrite(fid,waveData,'ushort'); % 16 bit
- else
- fwrite(fid,waveData,'uchar'); % 8 bit
- end;
- fclose(fid);
- else
- error(['Can''t open .WAV file ' wavefile ' for output!']);
- end;