%Ajay Phadke (aap67@cornell.edu), Eashwar Rangarajan (er2942cornell.edu)
%ECE 4760 Spring 2012, Cornell University
%Electronic Dhol, Final Project

%This codes takes in a *.wav sound file and generates an equivalent 8-bit
%unsigned numerical array, and saves it to a text file. This array can then
%be stored in the Flash memory of our AtMega644 chip, and output these
%values as a Pulse-Width-Modulated signal to the speakers.
function audio_to_text(file)
y = wavread(file,'native');
y = y';

%%BEGIN ENVELOPE CODE
%at 16kHz sampling, 75 ms envelope on either side
%adding an envelope to the sound improves quality of sound and decreases
%the amount of shart static noises at the beginning and end of the sounds.
env_length = 275;
for i = 1:length(y)
    if (i <= env_length+1)
        envelope = 1/(env_length)*(i - 1);
        y2(i) = tilli(i)*envelope;
    elseif (i <= (length(y)-env_length)) 
        y2(i) = tilli(i);
    else    
        envelope = -1/env_length*(i - length(y));
        y2(i) = tilli(i)*envelope;
    end
end
%%END ENVELOPE CODE

%This stores the resulting 8-bit character array to a text file
vector = '';
for i = 1:length(y2)
    vector = [vector num2str(floor(y2(i))) ','];
end

[Name,Path] = uiputfile('music.txt','Save Text File');
fid = fopen([Path Name],'wt');

fprintf(fid,vector);
fclose(fid);
end
