clear all
% Convert WAV files to C header files
% Read WAV file which you wish to convert
[s, Fs] = audioread('C:\Users\Ben Francis\Downloads\Gnarls_Barkley_Crazy.wav');

% Only use one channel
s = s(:,1);

% Drop the sample rate to about 11 kHz
s = resample(s,1,4);

% now scale for the 12-bit DAC
min_s = min(s);
max_s = max(s);
s = s - min_s;
s = fix(s * (127/(max_s-min_s)));
s = s + 128;

% make a textfile with PIC XC32 source code in it.
fname='Crazy.h';
fid = fopen(fname,'w');
% write samples to header file
for i=1:length(s)-1
    fprintf(fid,'%5d\r',s(i));
end
fprintf(fid,' %5d\r',s(end));
fclose(fid);