clear
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Channel_File_Creator2.m
% This program creates audio text files that can be
% used to program 24FC512 memory chips using
% "memoryProgrammer.c"
%
% The files can be sent to an 877A PIC Microcontroller
% using hyperterminal.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%
% The audio files created are the right and left channels
% of the desired wav file (8 chips per channel with 24FC512
% chips will produce about 16 seconds of audio at 16-bit,
% 16kHz).
%
% The audio data created is in 16-bit 2's complement
% format, which is used for a TI PCM56P Serial Digital
% to Analog Converter.
%%%%%

%%%%%%MUST BE SET TO DESIRED PATH OF OUTPUT FILES%%%%%%
Home_Path = 'C:\Documents and Settings\rhodesca\Desktop\Schoolwork\ECE 331\Final_Project\Matlab Code\';
kilobits_on_chip = 512;
bits_per_sample = 16;
samples_per_chip = kilobits_on_chip*1024/bits_per_sample;

%%%%%%ASSUMES WAV IS IN CURRENT MATLAB DIRECTORY%%%%%%
[y,fs] = wavread('Pick Up The Pieces.wav');

disp('Writing Right Channel...');

fid = fopen([Home_Path, 'Right_Audio0.txt'],'w');
for i=1:samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 0 Complete.');

fid = fopen([Home_Path, 'Right_Audio1.txt'],'w');
for i=(samples_per_chip+1):2*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 1 Complete.');

fid = fopen([Home_Path, 'Right_Audio2.txt'],'w');
for i=(2*samples_per_chip+1):3*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 2 Complete.');

fid = fopen([Home_Path, 'Right_Audio3.txt'],'w');
for i=(3*samples_per_chip+1):4*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 3 Complete.');

fid = fopen([Home_Path, 'Right_Audio4.txt'],'w');
for i=(4*samples_per_chip+1):5*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 4 Complete.');

fid = fopen([Home_Path, 'Right_Audio5.txt'],'w');
for i=(5*samples_per_chip+1):6*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 5 Complete.');

fid = fopen([Home_Path, 'Right_Audio6.txt'],'w');
for i=(6*samples_per_chip+1):7*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 6 Complete.');

fid = fopen([Home_Path, 'Right_Audio7.txt'],'w');
for i=(7*samples_per_chip+1):8*samples_per_chip
    hexAudio = getHexAudio(y(i,1));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Right Chip 7 Complete.');

disp('Finished Writing Right Channel');

disp('Writing Left Channel...');

fid = fopen([Home_Path, 'Left_Audio0.txt'],'w');
for i=1:samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 0 Complete.');

fid = fopen([Home_Path, 'Left_Audio1.txt'],'w');
for i=(samples_per_chip+1):2*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 1 Complete.');

fid = fopen([Home_Path, 'Left_Audio2.txt'],'w');
for i=(2*samples_per_chip+1):3*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 2 Complete.');

fid = fopen([Home_Path, 'Left_Audio3.txt'],'w');
for i=(3*samples_per_chip+1):4*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 3 Complete.');

fid = fopen([Home_Path, 'Left_Audio4.txt'],'w');
for i=(4*samples_per_chip+1):5*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 4 Complete.');

fid = fopen([Home_Path, 'Left_Audio5.txt'],'w');
for i=(5*samples_per_chip+1):6*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 5 Complete.');

fid = fopen([Home_Path, 'Left_Audio6.txt'],'w');
for i=(6*samples_per_chip+1):7*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 6 Complete.');

fid = fopen([Home_Path, 'Left_Audio7.txt'],'w');
for i=(7*samples_per_chip+1):8*samples_per_chip
    hexAudio = getHexAudio(y(i,2));
    hexAudioLength = size(hexAudio);
    switch(hexAudioLength(2))
        case 3
            hexAudio = ['0',hexAudio];
        case 2
            hexAudio = ['0','0',hexAudio];
        case 1
            hexAudio = ['0','0','0',hexAudio];
        otherwise
    end
    fprintf(fid,'%s',hexAudio);
end
fid = fclose(fid);
disp('Left Chip 7 Complete.');

disp('Finished Writing Left Channel');