(→Matlab Code) |
(→Sound File) |
||
Line 148: | Line 148: | ||
== Sound File == | == Sound File == | ||
+ | |||
+ | [[Media:Hail_Purdue_ECE301Fall2008mboutin.wav|here!]] |
Latest revision as of 16:18, 4 September 2008
Matlab Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kathleen Schremser %
% ECE 301 %
% Homework 1, due Friday, September 5, 2008 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clear the screen
clear
clc
% Define delta as used in class
delta = 0.00005;
% Define the tempo you wish to use (Number of Quarter notes per minute)
% Note: The time signature for this piece is taken to be 2/4
% Also, create a blank Song array to store the completed song (to be used later)
Tempo = 152;
Song = [];
% Define the length of each Eighth, Quarter, Dotted Quarter, Half, and Tied
% Note (A Tied note consists of 3 Quarter notes)
E = 1/2 * 60/Tempo;
Q = 60/Tempo;
DQ = 1.5 * 60/Tempo;
H = 2 * 60/Tempo;
T = 3/2 * 60/Tempo;
% Define the frequency of each note
% The key of Hail Purdue is D flat
Ef = 311.13;
F = 349.23;
G = 392.00;
Af = 415.30;
Bf = 466.16;
B = 493.88;
C = 523.25;
Df = 554.36;
% The chorus of "Hail Purdue" consists of four strains. The four strains
% and corresponding notes are set into arrays here:
Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ;
Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ;
Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ;
Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af];
% The associated length of each note is set into 3 different arrays here:
Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2
Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T];
Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T];
% Because the song is going to have to be played a total of 3 times,
% putting the playing part in a loop would make the code a lot shorter. We
% can use the counter (j) to tell it to change the things we want to change
% each time as well.
for j = 1:3
% The second time, the tempo has to increase if j == 2 E = E/2; Q = Q/2; DQ = DQ/2; H = H/2; T = T/2; Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; end % Now make each note a full scale higher for the third run if j == 3 Ef = Ef*2; F = F*2; G = G*2; Af = Af*2; Bf = Bf*2; B = B*2; C = C*2; Df = Df*2; Strain1 = [Ef, F, G, Af, Bf, C, C, Df, Df, Df, Af, Bf, B, C] ; Strain2 = [C, C, Bf, Af, Bf, C, C, Bf, F, G, Af, G, F, Bf] ; Strain3 = [Ef, Ef, F, G, Af, Bf, C, C, C, Df, Df, Af, Bf, C] ; Strain4 = [F, G, Af, F, Ef, Af, C, Ef, F, C, Bf, Af, Af]; end
% For each strain, a loop creates the length of time needed for the note % and then plays it
for i = 1:14 time = 0 : delta : Time1(i);
sound(sin(2*pi*Strain1(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain1(i)*time)]; end
% A quarter rest separates each strain
Rest = ones(Q/delta, 1); sound(Rest, 1/delta) Song = [Song, Rest];
% Second strain
for i = 1:14 time = 0 : delta : Time1(i); sound(sin(2*pi*Strain2(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain2(i)*time)]; end
% Quarter Rest
sound(Rest, 1/delta) Song = [Song, Rest];
% Third Strain
for i = 1:14 time = 0 : delta : Time2(i); sound(sin(2*pi*Strain3(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain3(i)*time)]; end
% Quarter Rest
sound(Rest, 1/delta) Song = [Song, Rest];
% Last Strain
for i = 1:13 time = 0 : delta : Time3(i); sound(sin(2*pi*Strain4(i)*time), 1/delta) Song = [Song, sin(2*pi*Strain4(i)*time)]; end
% If it's the second time, recreate the original tempo if j == 2 E = E*2; Q = Q*2; DQ = DQ*2; H = H*2; T = T*2; Time1 = [H, Q, Q, DQ, E, Q, Q, Q, E, E, Q, E, E, T] ; % Used for strains 1 and 2 Time2 = [DQ, E, Q, Q, DQ, E, Q, E, E, Q, Q, Q, Q, T]; Time3 = [DQ, E, Q, Q, Q, Q, Q, Q, DQ, E, DQ, E, T]; end Song = [Song, Rest, Rest, Rest, Rest, Rest, Rest];
end
% Use the completed Song array that has been building with each additional
% note and write to a .wav file
wavwrite(Song, 44100, 'Hail_Purdue.wav')