Files
2026-07-26 21:41:38 +02:00

71 lines
1.8 KiB
Matlab
Executable File

function wavw16(waveData,sRate,wavefile)
%WAVWRITE Saves Microsoft Windows 3.1 .WAV format sound files.
% WAVWRITE(y,Fs,wavefile) saves a .WAV format file specified by "wavefile".
%
% The input arguments for WAVWRITE are as follows:
%
% y The sampled data to save (8 bit max)
% Fs The rate at which the data was sampled
% wavefile A string containing the name of the .WAV file to create
%
% Note: WAVWRITE will create an 8-bit, simgle channel wave file. Non 8-bit
% sample data will be truncated.
%
% See also WAVREAD.
% Copyright (c) 1984-94 by The MathWorks, Inc.
if nargin~=3
error('WAVWRITE needs three arguments!');
end
if ischar(waveData) %old symtax, reorder args
tmp = waveData;
waveData = wavefile;
wavefile = sRate;
sRate = tmp;
end
if isempty(findstr(wavefile,'.'))
wavefile=[wavefile,'.wav'];
end
fid=fopen(wavefile,'wb','l');
if fid ~= -1
[m,n]=size(waveData);
nsamples=m*n;
riffsize=36+nsamples*2;
% 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,1,'ushort'); % 1 channel
fwrite(fid,sRate,'ulong'); % samples per second
fwrite(fid,2*sRate,'ulong'); % average bytes per second
fwrite(fid,2,'ushort'); % block alignment
fwrite(fid,16,'ushort'); % bits per sample
% write data sub-chunck
fwrite(fid,'data','uchar');
fwrite(fid,2*nsamples,'ulong');
fwrite(fid,waveData,'short');
if ((ceil(nsamples/2))~=(nsamples/2))
fwrite(fid,0,'short');
end;
fclose(fid);
end;
if fid == -1
error('Can''t open .WAV file for output!');
end;