SOUNDS FILES

Tune played at normal speed/pitch

Tune played @ 2X Speed

Tune played at higher pitch

MATLAB CODE

clc
clear

%The frequencies (Hz) shown below were found on the NYU CS website:
%http://www.cs.nyu.edu/courses/fall03/V22.0201-003/notes.htm

C = 262; CSharp = 227; D = 294; DSharp = 311; E = 330; 
F = 349; FSharp = 370; G = 392; GSharp = 415; A = 440;
ASharp = 466; B = 494;

%Note Lengths defined below:
QTR = 0.25; HLF = 0.50; ETH = 0.125;

%This is the array that makes up the notes played in the song:

noteOrder = [DSharp, F, G, GSharp, ASharp, C, C, 
             CSharp, CSharp, CSharp, GSharp, ASharp, 
             ASharp, C, C, C, ASharp, GSharp, ASharp, 
             C, C, ASharp, F, G, GSharp, G, F ,ASharp, ASharp];

noteLength = [HLF, QTR, QTR, QTR, ETH, QTR, QTR, QTR, 
              ETH, ETH, QTR, ETH, ETH, HLF, QTR, HLF, 
              QTR, QTR, ETH, QTR, QTR, QTR, ETH, ETH, 
              QTR, ETH, ETH, HLF, QTR];

numNotes = 29; %Number of notes to be played

delta = 0.00005; %Sampling Rate
x = 1; %Counter Value
count1 = 1; %Used as counter for while loop.

%PART A

while x<= numNotes;
    t1 = 0:delta:noteLength(x);     %Helps scale the length of notes
    p1 = sin(2*pi*noteOrder(x)*t1); %Sound function
 
    count2 = 1; %While loop counter
    while count2<length(p1);  %Builds the array.
        writeArray1(count1) = p1(count2);
        count1 = count1 + 1;
        count2 = count2 + 1;
    end
        
    x = x + 1; %Increments the loop counter
end
sound(writeArray1, 1/delta); %Plays sound file

x = 1; %Counter Value
count1 = 1; %Reinitializes the counter

%PART B

while x<= numNotes;
    t2 = 0:delta:0.5*noteLength(x);  %Helps scale the length of notes
    p2 = sin(2*pi*noteOrder(x)*t2);  %Sound function
    
    count2 = 1;
    while count2<length(p2); %Builds the array
        writeArray2(count1) = p2(count2);
        count1 = count1 + 1;
        count2 = count2 + 1;
    end
    
    x = x + 1;  %Increments the loop counter
end
sound(writeArray2, 1/delta); %Plays the sound file

x = 1; %Counter Value
count1 = 1; %Reinitializes the counter

%PART C

while x<= numNotes;
    t3 = 0:delta:noteLength(x);       %Helps scale the length of notes
    p3 = sin(2*pi*noteOrder(x)*2*t3); %Sound Function
    
    count2 = 1;
    while count2<length(p3); %Builds the array
        writeArray3(count1) = p3(count2);
        count1 = count1 + 1;
        count2 = count2 + 1;
    end
        
    x = x + 1; %Increments the loop counter
end
sound(writeArray3, 1/delta);  %Plays the sounds file

%Below are the matlab commands used to write the sound files to 3 separate
%files.  The files will be written to the same director as this ".m" file.
wavwrite(writeArray1,'fightSong_PartA.wav');
wavwrite(writeArray2,'fightSong_PartB.wav');
wavwrite(writeArray3,'fightSong_PartC.wav');


Results

This part of the HW was a lot of fun. I had no idea that Matlab could generate sound files. The project wasn't too hard, but it seemed to take a long time. It was a great "hands on" application of the material we've been learning in class.

Alumni Liaison

Recent Math PhD now doing a post-doctorate at UC Riverside.

Kuei-Nuan Lin