- refactored
This commit is contained in:
Executable
+67
@@ -0,0 +1,67 @@
|
||||
function [y,Fs,Format]=wavr16(wavefile)
|
||||
%WAVREAD Load Microsoft Windows 3.1 .WAV format sound files.
|
||||
% [y]=WAVREAD(wavefile) loads a .WAV format file specified by "wavefile",
|
||||
% returning the sampled data in variable "y". The .WAV extension
|
||||
% in the filename is optional.
|
||||
%
|
||||
% [y,Fs]=WAVREAD(wavefile) loads a .WAV format file specified by
|
||||
% "wavefile", returning the sampled data in variable "y" and the
|
||||
% sample rate in variable "Fs".
|
||||
%
|
||||
% [y,Fs,Format]=WAVREAD(wavefile) loads a .WAV format file specified by
|
||||
% "wavefile",returning the sampled data in variable "y", the sample
|
||||
% rate in variable "Fs", and the .WAV file format information in
|
||||
% variable "Format". The format information is returned as a 6 element
|
||||
% vector with the following order:
|
||||
%
|
||||
% Format(1) Data format (always PCM)
|
||||
% Format(2) Number of channels
|
||||
% Format(3) Sample Rate (Fs)
|
||||
% Format(4) Average bytes per second (sampled)
|
||||
% Format(5) Block alignment of data
|
||||
% Format(6) Bits per sample
|
||||
%
|
||||
% Note: WAVREAD currently supports only 8-bit single channel data.
|
||||
%
|
||||
% See also WAVWRITE.
|
||||
|
||||
% Copyright (c) 1984-94 by The MathWorks, Inc.
|
||||
|
||||
if nargin~=1
|
||||
error('WAVREAD takes one argument, which is the name of the .WAV file');
|
||||
end
|
||||
|
||||
if isempty(findstr(wavefile,'.'))
|
||||
wavefile=[wavefile,'.wav'];
|
||||
end
|
||||
|
||||
fid=fopen(wavefile,'rb','l');
|
||||
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'); % PCM format
|
||||
Format(2)=fread(fid,1,'ushort'); % 1 channel
|
||||
Fs=fread(fid,1,'ulong'); % samples per second
|
||||
Format(3)=Fs;
|
||||
Format(4)=fread(fid,1,'ulong'); % average bytes per second
|
||||
Format(5)=fread(fid,1,'ushort'); % block alignment
|
||||
Format(6)=fread(fid,1,'ushort'); % bits per sample
|
||||
|
||||
|
||||
% read data sub-chunck
|
||||
header=fread(fid,4,'uchar');
|
||||
nsamples=fread(fid,1,'ulong');
|
||||
y=fread(fid,nsamples,'short');
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
if fid == -1
|
||||
error('Can''t open .WAV file for input!');
|
||||
end;
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
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 isstr(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;
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
function [y,Fs,Format]=wr16(wavefile)
|
||||
%WAVREAD Load Microsoft Windows 3.1 .WAV format sound files.
|
||||
% [y]=WAVREAD(wavefile) loads a .WAV format file specified by "wavefile",
|
||||
% returning the sampled data in variable "y". The .WAV extension
|
||||
% in the filename is optional.
|
||||
%
|
||||
% [y,Fs]=WAVREAD(wavefile) loads a .WAV format file specified by
|
||||
% "wavefile", returning the sampled data in variable "y" and the
|
||||
% sample rate in variable "Fs".
|
||||
%
|
||||
% [y,Fs,Format]=WAVREAD(wavefile) loads a .WAV format file specified by
|
||||
% "wavefile",returning the sampled data in variable "y", the sample
|
||||
% rate in variable "Fs", and the .WAV file format information in
|
||||
% variable "Format". The format information is returned as a 6 element
|
||||
% vector with the following order:
|
||||
%
|
||||
% Format(1) Data format (always PCM)
|
||||
% Format(2) Number of channels
|
||||
% Format(3) Sample Rate (Fs)
|
||||
% Format(4) Average bytes per second (sampled)
|
||||
% Format(5) Block alignment of data
|
||||
% Format(6) Bits per sample
|
||||
%
|
||||
% Note: WAVREAD currently supports only 8-bit single channel data.
|
||||
%
|
||||
% See also WAVWRITE.
|
||||
|
||||
% Copyright (c) 1984-94 by The MathWorks, Inc.
|
||||
|
||||
if nargin~=1
|
||||
error('WAVREAD takes one argument, which is the name of the .WAV file');
|
||||
end
|
||||
|
||||
if findstr(wavefile,'.')==[ ]
|
||||
wavefile=[wavefile,'.wav'];
|
||||
end
|
||||
|
||||
fid=fopen(wavefile,'rb','l');
|
||||
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'); % PCM format
|
||||
Format(2)=fread(fid,1,'ushort'); % 1 channel
|
||||
Fs=fread(fid,1,'ulong'); % samples per second
|
||||
Format(3)=Fs;
|
||||
Format(4)=fread(fid,1,'ulong'); % average bytes per second
|
||||
Format(5)=fread(fid,1,'ushort'); % block alignment
|
||||
Format(6)=fread(fid,1,'ushort'); % bits per sample
|
||||
|
||||
|
||||
% read data sub-chunck
|
||||
header=fread(fid,4,'uchar');
|
||||
nsamples=fread(fid,1,'ulong');
|
||||
y=fread(fid,nsamples,'short');
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
if fid == -1
|
||||
error('Can''t open .WAV file for input!');
|
||||
end;
|
||||
Reference in New Issue
Block a user