>Hi all,
>
>How to deal with audio files on CCS? To load data from audio file on to
>some buffer array,I have tried converting it into ASCII text file, but
it
>makes the resulting file quite bulky(150MB) and its taking long time to
add
>the header at the begining.
>
>any ideas?
>
>-aamer
>
CCS to Wav via Matlab and vice-versa
I've developped Matlab .m files that read CCS file to Matlab, and Matlab
to CCS files.
Here are 4 files below. You can thus create a wav file, read it with
Matlab, create a CCS file and then probe it in the CCS. Have fun.
wavwr16.m
===
function wavwr16(wavefile,waveData,format)
%WAVWRITE16 Saves Microsoft Windows 3.1 .WAV format sound files.
%
% WAVWR16(wavefile,y,format) saves a .WAV format file specified by
"wavefile".
%
% The input arguments for WAVWRITE16 are as follows:
%
% wavefile A string containing the name of the .WAV file to
create
% y The sampled data to save in floating point.
% Channel 0 Channel 1
% " "
% ... ...
% Note that these value will be truncat to 8 or 16 bits.
%
% format Format is a four(4) element vector of Wave file.
% [1] : Coding : PCM = 1, Mu-law = 2
% [2] : Channel: Mono = 1, Stereo = 2
% [3] : Sampling rate : 8000, 11025, 22050 or 44100 ( CD
quality )
% [4] : The resolution : 8 or 16 bit per sample.
%
% See also WAVREAD16.
% Copyright (c) 1984-93 by The MathWorks, Inc.
% Modify by Francois Caron, INRS-telecommunications
% Version 1.0, 7-7-94
%
if nargin~=3
error('WAVWRITE needs three arguments!');
end
if findstr(wavefile,'.')==[]
wavefile=[wavefile,'.wav'];
end
fid=fopen(wavefile,'wb');
if fid ~= -1
[m,n]=size(waveData);
nsamples = m;
if (n > format(2))
error('Can put a binaural vector into a Wave Mono sound file!');
end
if ~(format(3) == 8000 | format(3) == 11025 | format(3) == 22050 |
format(3) == 44100)
error('Sample frequency is not WAVE standard!');
end
if (format(4)+format(2) == 9)
Block = 1;
end
if (format(4)+format(2) == 10) | (format(4)+format(2) == 17)
Block = 2;
end
if (format(4)+format(2) == 18)
Block = 4;
end
Riffsize=36+(Block*nsamples);
% 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');
Average = (format(4)/8)*format(3)*format(2);
fwrite(fid,format(1),'ushort'); % PCM or X-law format
fwrite(fid,format(2),'ushort'); % x channel
fwrite(fid,format(3),'ulong'); % samples per second
fwrite(fid,Average,'ulong'); % average bytes per second
fwrite(fid,Block,'ushort'); % block alignment
fwrite(fid,format(4),'ushort'); % bits per sample
% write data sub-chunck
fwrite(fid,'data','uchar');
fwrite(fid,(nsamples*Block),'ulong');
if (format(4)+format(2) == 9)
fwrite(fid,waveData,'uchar');
end
if (format(4)+format(2) == 10)
for i=1:nsamples,
fwrite(fid,waveData(i,1),'uchar');
if n == 1
fwrite(fid,waveData(i,1),'uchar');
else
fwrite(fid,waveData(i,2),'uchar');
end
end
end
if (format(4)+format(2) == 17)
fwrite(fid,waveData,'short');
end
if (format(4)+format(2) == 18)
for i=1:nsamples,
fwrite(fid,waveData(i,1),'short');
if n == 1
fwrite(fid,waveData(i,1),'short');
else
fwrite(fid,waveData(i,2),'short');
end
end
end
end;
if fid == -1
error('Can''t open .WAV file for input!');
end;
fclose(fid);
wavrd16.m
====
function [L,R,format]=wavrd16(wavefile)
%WAVREAD16 Load Microsoft Windows 3.1 .WAV format sound files.
%
% [L,R,format]=WAVRD16(wavefile) loads a .WAV format file specified by
% "wavefile", returning the sampled data in variable L and R, and
the
% .WAV file format information in variable "format". The format
% information is returned as a 4 element vector with the following
% order:
%
% format(1) Data format (Always PCM).
% format(2) Number of channels
% format(3) Sample Rate (Fs)
% format(4) Bits per sample
%
% Note: If the Wave input file is mono, L and R will be the same.
%
% See also WAVWRITE16
%
% Copyright (c) 1984-93 by The MathWorks, Inc.
% Modified by Francois Caron, INRS-telecommunications
% Version 1.1 18-8-94
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');
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'); % Format
format(2)=fread(fid,1,'ushort'); % Channel
format(3)=fread(fid,1,'ulong'); % Samples per second
header=fread(fid,1,'ulong');
block=fread(fid,1,'ushort');
format(4)=fread(fid,1,'ushort'); % Bits per sample
% read data sub-chunck
header=fread(fid,4,'uchar');
nbyteforsamples=fread(fid,1,'ulong');
nsamples=nbyteforsamples/block;
if (format(4)+format(2) == 9)
L = fread(fid,nsamples,'char');
R = L;
end
if (format(4)+format(2) == 10)
y = fread(fid,[2,nsamples],'char');
L = y(1,:)';
R = y(2,:)';
end
if (format(4)+format(2) == 17)
L = fread(fid,nsamples,'short');
R = L;
end
if (format(4)+format(2) == 18)
y = fread(fid,[2,nsamples],'short');
L = y(1,:);
R = y(2,:);
end
end
if fid == -1
error('Can''t open .WAV file for input!');
end;
fclose(fid);
readcc.m
====
function data=ReadCC(filename)
% function data=ReadCC(filename)
%
% Read Code Composer file
%
% 16 bits signed supported only
%
%
% 13-4-2000
% François Caron, ing. M.Sc.
fid=fopen(filename,'r');
dummy=fscanf(fid,'%x\n',5); % Skip 5 first element (header)
temp=fscanf(fid,'%x\n'); % Read all the rest of data
fclose(fid);
for i=1:length(temp)
if ( temp(i) < 32768 )
data(i,1) = temp(i);
else
data(i,1) = temp(i) - 65536;
end
end
writecc.m
=====
function writeCC(filename, data)
% function WriteCC(filename, data)
%
% Write Code Composer file
%
% 16 bits signed supported only
%
%
% 13-4-2000
% François Caron, ing. M.Sc.
fid=fopen(filename,'w');
[n,m] = size(data);
if (n>1)
data = data';
end
datalength = length(data);
fprintf (fid,'1651 1 0 0 %d\n', datalength);
for i=1:datalength
if (data(i) >= 0)
fprintf(fid,'0x%x\n', data(i));
else
temp = data(i) + 65536;
fprintf(fid,'0x%x\n', temp);
end
end