Professional Documents
Culture Documents
Matlab Music Synthesis
Matlab Music Synthesis
ELECTRONICS ENGINEERING
DIGITAL SIGNAL PROCESSING
Instructor: Engr. Edwin R. Arboleda, MENG-ECE
MUSIC SYNTHESIS USING MATLAB
1. Music Synthesis I. Raga Malkauns.
----------------------------------------------------------------------------------------------------------------Raga Malkauns: In Indian classical music, a raga is a musical composition based on
ascending and descending scale. The notes and their order from the musical alphabet
and grammar from which the performer construct musical passages, using only the
notes allowed. The performance of a Raga can last for a few minutes to an hour or
more! Raga Malkauns is a pentatonic raga( with five notes) and the following scales:
Ascending:
D
Bb
Bb
Descending:
C
The final note in each scale is held twice as long as the rest.
---------------------------------------------------------------------------------------------------------------A musical composition is a combination of notes, or signals, at various frequencies. An
octave covers a range of frequencies from f0 to 2f0. In the western musical scale, there
are 12 notes per octave, logarithmically equispaced . The frequencies of the notes from
f0 to 2f0 correspond to
f = 2 k/12 f0 (k = 0, 1, 2,., 11)
The 12 notes are as follows (the # stand for sharp and the
fair of notes in the parentheses has the same frequency:
A
(A# or B b)
(F# or Gb)
(C# or D b)
(G# or Ab)
>> d2 = 2*d;
Generate the sample sinusoid at these frequencies, using an appropriate sampling rate
(say 8192Hz); concatenate them, assuming silent passages between each note; play the
resulting signal, using the MATLAB command sound. Use the following MATLAB
code fragments as a guide.
>> ts=1/8192; %sampling interval
>> t=0:ts:0.4; %Time for each note (0.4 s)
>> s1 = 0*(0:ts:0.1); %Silent period (0.1s)
>> s2= 0*(0:ts:0.05); % Silent period (0.05s)
>> tl=0:ts:1;
% Time for last note in each scale
>> d1 =sin(2*pi*d*t);
>> f1 = sin(2*pi*f*t); g1=sin(2*pi*g*t);
>> bf1 = sin(2*pi*bf*t); c1 = sin(2*pi*c*t);
>> dl1=sin(2*pi*d2*tl); dl2=sin(2*pi*d*tl);
>> asc= [d1 s1 f1 s1 g1 s1 bf1 s1 c1 s2 dl1]; %creating ascending scale
>> dsc = [c1 s1 bf1 s1 g1 s1 f1 s1 dl2];
%creating descending scale
>> y = [asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1];
>> sound(y)
%malkauns scale (y)
Complete Codes for the Raga Malkauns: (It is easier to do the coding in Notepad
then copy-paste it in the MATLAB window.
>> f0= 340; d=f0; % pick a frequency and the note D
>> f= f0*(2^(3/12)); g=f0*(2^(5/12)); %The notes F and G
>> bf=f0*(2^(8/12)); c=f0*(2^(10/12)); %The notes B(flat) and C
>> d2 = 2*d;
>> ts=1/8192; %sampling interval
>> t=0:ts:0.4; %Time for each note (0.4 s)
>> s1 = 0*(0:ts:0.1); %Silent period (0.1s)
>> s2= 0*(0:ts:0.05); % Silent period (0.05s)
>> tl=0:ts:1;
% Time for last note in each scale
>> d1 =sin(2*pi*d*t);
>> f1 = sin(2*pi*f*t); g1=sin(2*pi*g*t);
>> bf1 = sin(2*pi*bf*t); c1 = sin(2*pi*c*t);
>> dl1=sin(2*pi*d2*tl); dl2=sin(2*pi*d*tl);
>> asc= [d1 s1 f1 s1 g1 s1 bf1 s1 c1 s2 dl1]; %creating ascending scale
>> dsc = [c1 s1 bf1 s1 g1 s1 f1 s1 dl2];
%creating descending scale
>> y = [asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1 asc s1 dsc s1];
>> sound(y)
%malkauns scale (y)
Lab.Exercises:
1. Big Ben .
Synthesize the following notes,
F# (0.3)
D(0.4)
E(0.4)
A(1)
A(0.4)
E(0.4)
F#(0.3)
D(1)
All notes cover one octave and the numbers in parentheses give a rough indication
of their duration.
2. Pictures of an Exhibition by Mussorgsky
A(3)
G*(1)
G(3)
E(3)
C(3)
C(3)
C(3)
E(3)
D (2)
All notes cover one octave except the note G* , which is an octave above G. The
numbers in parentheses give a rough indication of their relative duration of the notes.
Project:
Synthesize the notes of your favorite modern song.
Tip: Get a copy of song hits containing the lyrics and guitar chords of your favorite
song. Apply the principles discussed here.
Students are required to synthesize only the chorus of the song. However those who
can do the whole song will get an additional points. Also this project requires a
minimum of one song but those who can synthesize more than one will get an
additional grade.
Sample Program #1
>> % Open Arms
f0=130.82; c=f0;
d=f0*(2^(2/12)); e=f0*(2^(4/12));
f=f0*(2^(5/12)); g=f0*(2^(7/12));
a=f0*(2^(9/12)); b=f0*(2^(11/12));
c2=2*c; d2=2*d; e2=2*e; f2=2*f; g2=2*g; b2=2*b; a2=2*a;
ts=1/1892;
t=0:ts:0.5;
t1=0:ts:1;
t2=0:ts:2;
t3=0:ts:2.5;
t4=0:ts:3;
t5=0:ts:4;
t6=0:ts:4.5;
t7=0:ts:5;
t8=0:ts:6;
c2t=sin(2*pi*c2*t);%
c2t1=sin(2*pi*c2*t1);%
c2t2=sin(2*pi*c2*t2);%
c2t4=sin(2*pi*c2*t4);%
c2t5=sin(2*pi*c2*t5);%
c2t7=sin(2*pi*c2*t7);%
c2t8=sin(2*pi*c2*t8);%
d2t=sin(2*pi*d2*t);%
d2t1=sin(2*pi*d2*t1);%
d2t2=sin(2*pi*d2*t2);%
d2t4=sin(2*pi*d2*t4);%
d2t5=sin(2*pi*d2*t5);%
d2t6=sin(2*pi*d2*t6);%
d2t7=sin(2*pi*d2*t7);%
et2=sin(2*pi*e*t2);%
e2t1=sin(2*pi*e2*t1);%
e2t2=sin(2*pi*e2*t2);%
e2t3=sin(2*pi*e2*t3);%
e2t5=sin(2*pi*e2*t5);%
gt3=sin(2*pi*g*t3);%
gt4=sin(2*pi*g*t4);%
gt5=sin(2*pi*g*t5);%
gt7=sin(2*pi*g*t7);%
g2t1=sin(2*pi*g2*t1);%
g2t2=sin(2*pi*g2*t2);%
g2t3=sin(2*pi*g2*t3);%
g2t4=sin(2*pi*g2*t4);%
g2t5=sin(2*pi*g2*t5);%
g2t7=sin(2*pi*g2*t7);%
at1=sin(2*pi*a*t1);%
at2=sin(2*pi*a*t2);%
at4=sin(2*pi*a*t4);%
at5=sin(2*pi*a*t5);%
at7=sin(2*pi*a*t7);%
a2t2=sin(2*pi*a2*t2);%
a2t7=sin(2*pi*a2*t7);%
bt1= sin(2*pi*b*t1);%
bt2= sin(2*pi*b*t2);%
bt4= sin(2*pi*b*t4);%
bt5=sin(2*pi*b*t5);%
b2t1= sin(2*pi*b2*t1);%
f2t1= sin(2*pi*f2*t1);%
f2t4= sin(2*pi*f2*t4);%
f2t5= sin(2*pi*f2*t5);%
s1=0*(0:ts:1);
s2=0*(0:ts:2);
s3=0*(0:ts:3);
s4=0*(0:ts:4);
s5=0*(0:ts:5);
s6=0*(0:ts:6);
verse1=[gt3 d2t6 e2t1 c2t2 gt5 s2 gt5 d2t5 bt1 c2t4 s2 c2t4 bt5 c2t2 bt4 gt5 et2 at7 s4 gt3 d2t6 e2t1
c2t2 gt5 s2 gt5 d2t5 bt1 c2t4 s2 c2t4 bt5 c2t2 bt4 gt5 et2 at7 s4];%
verse2=[gt3 d2t6 e2t1 c2t2 gt5 s2 gt5 d2t5 bt1 c2t4 s2 c2t4 bt5 c2t2 bt4 gt5 et2 at7 s4 gt3 d2t6 e2t1
c2t2 gt5 s2 gt5 d2t5 bt1 c2t4 s2 c2t4 bt5 c2t2 bt4 gt5 et2 at7 s4];%
refrain=[d2t1 d2t1 d2t2 c2t1 d2t1 at2 s2 d2t2 e2t2 d2t2 c2t2 at2 s3 bt2 c2t5 c2t2 bt2 c2t2 bt1 at4 bt1
at5 s3];%
chorus=[c2t4 e2t5 f2t4 g2t7 e2t2 g2t7 c2t2 g2t4 a2t2 a2t2 g2t5 s2 g2t2 f2t4 e2t2 d2t4 s1 c2t2 g2t4
f2t4 e2t2 e2t2 d2t5 c2t2 c2t4 s1 g2t7 e2t2 g2t7 c2t2 g2t4 a2t2 a2t2 g2t5 s2 g2t2 f2t4 e2t2 d2t5 d2t2
c2t1 g2t3 f2t5 e2t2 d2t4 c2t5 s2 c2t1 bt4 c2t7 s5];%
y=[ verse1 refrain chorus verse2 refrain chorus];sound(y);
Sample # 2
%frequency of notes for the first octave
h1=69.3;
csharp1=h1;
e1=h1*(2^(3/12));
fsharp1=h1*(2^(5/12));
a1=h1*(2^(8/12));
asharp1=h1*(2^(9/12));
b1=h1*(2^(10/12));
%frequency of notes:second octave
h2=138.6;
csharp2=h2;
d2=h2*(2^(1/12));
dsharp2=h2*(2^(2/12));
e2=h2*(2^(3/12));
fsharp2=h2*(2^(5/12));
b2=h2*(2^(10/12));
c3=h2*(2^(11/12));
%frequency of notes:third octave
h3=277.2;
csharp3=h3;
d3=h3*(2^(1/12));
dsharp3=h3*(2^(2/12));
%duration of notes
ts=1/2300;
t1=0:ts:0.6;
t2=0:ts:1.2;
t3=0:ts:1.8;
t4=0:ts:3;
t5=0:ts:4;
%spaces
s=0*(0:ts:0.05);
s1=0*(0:ts:0.1);
s2=0*(0:ts:0.2);
s3=0*(0:ts:0.5);
s4=0*(0:ts:1);
s5=0*(0:ts:2.5);
s6=0*(0:ts:4);
%notes at t1
fsharp1t1=sin(2*pi*fsharp1*t1);
a1t1=sin(2*pi*a1*t1);
b1t1=sin(2*pi*b1*t1);
csharp2t1=sin(2*pi*csharp2*t1);
dsharp2t1=sin(2*pi*dsharp2*t1);
b2t1=sin(2*pi*b2*t1);
c3t1=sin(2*pi*c3*t1);
csharp3t1=sin(2*pi*csharp3*t1);
dsharp3t1=sin(2*pi*dsharp3*t1);
%notes at t2
csharp1t2=sin(2*pi*csharp1*t2);
e1t2=sin(2*pi*e1*t2);
fsharp1t2=sin(2*pi*fsharp1*t2);
a1t2=sin(2*pi*a1*t2);
b1t2=sin(2*pi*b1*t2);
csharp2t2=sin(2*pi*csharp2*t2);
d2t2=sin(2*pi*d2*t2);
e2t2=sin(2*pi*e2*t2);
fsharp2t2=sin(2*pi*fsharp2*t2);
b2t2=sin(2*pi*b2*t2);
c3t2=sin(2*pi*c3*t2);
dsharp3t2=sin(2*pi*dsharp3*t2);
%notes at t3
fsharp1t3=sin(2*pi*fsharp1*t3);
a1t3=sin(2*pi*a1*t3);
csharp2t3=sin(2*pi*csharp2*t3);
d2t3=sin(2*pi*d2*t3);
fsharp2t3=sin(2*pi*fsharp2*t3);
%notes at t4
a1t4=sin(2*pi*a1*t4);
%notes at t5
fsharp1t5=sin(2*pi*fsharp1*t5);
a1t5=sin(2*pi*a1*t5);
%the lazy song by bruno mars in matlab
line1=[a1t1 csharp2t2 csharp2t2 csharp2t2 csharp2t2 b1t2 a1t2 b1t2 csharp2t3 a1t1 fsharp1t5 s3];
whistle=[b2t1 csharp3t1 dsharp3t2 dsharp3t1 csharp3t1 b2t2];
line2=[csharp2t2 csharp2t2 csharp2t2 csharp2t2 b1t2 a1t2 b1t2 a1t2 fsharp1t5 s3];
line3=[a1t2 csharp2t2 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 fsharp2t3 a1t1 fsharp2t2 e2t2 csharp2t2
a1t2 a1t2 csharp2t2 fsharp1t2 a1t1 a1t1 csharp2t2 csharp2t2 csharp2t2 csharp2t2 b1t2 a1t2 b1t2
csharp2t3 a1t1 fsharp1t3 s6];
chorus1=[line1 whistle line2 whistle line3];
stanza1=[a1t2 a1t1 b1t1 csharp2t1 csharp2t1 csharp2t2 csharp2t2 csharp2t2 b1t1 b1t2 a1t1 csharp2t2
b1t2 b1t1 a1t1 b1t1 a1t1 b1t2 a1t1 a1t1 b1t2 a1t1 a1t1 csharp2t2 b1t2 csharp2t2 e2t2 e2t2 fsharp2t2
b1t2 b1t2 csharp2t2 a1t2 fsharp1t3 s5 a1t2 s5];
stanza2=[a1t1 b1t1 csharp2t1 b1t1 csharp2t1 b1t1 csharp2t2 csharp2t2 b1t1 a1t1 b1t1 a1t1 csharp2t1
b1t1 s4 b1t1 a1t1 b1t1 a1t1 b1t1 a1t1 b1t1 a1t1 b1t1 a1t1 b1t1 a1t1 csharp2t1 b1t2 a1t1 csharp2t2
e2t2 e2t2 fsharp2t2 a1t2 b1t2 a1t2 csharp2t2 a1t2 fsharp1t3 s6];
refrain1=[a1t2 csharp2t2 d2t2 d2t2 d2t3 csharp2t1 s4 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 e2t2
fsharp2t2 csharp2t2 dsharp2t1 csharp2t1 s5];
woohoo1=[d2t2 csharp2t2 b1t2 a1t4 fsharp1t2 e1t2 csharp1t2 e1t2 fsharp1t2 e1t2 csharp1t2 fsharp1t2
e1t2 csharp1t2 csharp1t2 d2t2 csharp2t2 b1t2 a1t4 fsharp1t2 e1t2 csharp1t2 e1t2 fsharp1t2 e1t2
csharp1t2 fsharp1t2 e1t2 csharp1t2 csharp1t2 s5];
stanza3=[a1t1 a1t1 b1t1 csharp2t2 csharp2t2 csharp2t2 csharp2t1 a1t1 b1t2 b1t1 a1t1 csharp2t2 b1t1
a1t1 b1t1 b1t1 csharp2t2 a1t2 b1t1 a1t1 b1t1 b1t1 csharp2t2 a1t3 a1t1 csharp2t2 e2t2 e2t2 fsharp2t2
b1t2 b1t2 csharp2t2 a1t2 fsharp1t3 s6 a1t2 s6];
stanza4=[b1t2 csharp2t2 csharp2t1 b1t1 csharp2t1 b1t1 csharp2t1 b1t1 b1t1 b1t2 a1t1 csharp2t2 a1t1
b1t1 a1t1 b1t2 b1t1 a1t1 b1t2 b1t2 b1t1 a1t1 csharp2t3 a1t1 csharp2t2 e2t2 e2t2 fsharp2t2 b1t2 b1t2
csharp2t2 a1t2 fsharp1t3 s5];
refrain2=[a1t2 b1t2 csharp2t2 d2t2 d2t2 d2t2 csharp2t1 s4 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 e2t2
fsharp2t2 csharp2t2 dsharp2t1 csharp2t1 s5];
line4=[fsharp2t2 e2t2 csharp2t2 fsharp2t2 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 d2t2 csharp2t3
fsharp1t1 a1t4 s4];
line5=[csharp2t2 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 fsharp2t2 csharp2t2 a1t5 s4];
line6=[a1t2 csharp2t2 e2t2 e2t2 csharp2t2 fsharp2t2 e2t2 fsharp2t3 a1t1 fsharp2t2 e2t2 csharp2t2
b1t2 b1t2 csharp2t2 fsharp1t2 a1t2 csharp2t2 csharp2t2 csharp2t2 a1t2 b1t2 a1t2 b1t2 csharp2t3 a1t2
fsharp1t2 s6];
chorus2=[line4 s5 line5 s5 line6];
woohoo2=[d2t2 csharp2t2 b1t2 a1t4 fsharp1t2 e1t2 csharp1t2 e1t2 fsharp1t2 e1t2 csharp1t2 fsharp1t2
e1t2 csharp1t2 csharp1t2 d2t2 csharp2t2 b1t2 a1t4 fsharp1t2 e1t2 csharp1t2 e1t2 fsharp1t2 e1t2
csharp1t2 fsharp1t2 e1t2 csharp1t2 csharp1t2 d2t2 csharp2t2 b1t2 a1t5];
thelazysong=[chorus1 stanza1 stanza2 refrain1 chorus1 woohoo1 stanza3 stanza4 refrain2 chorus1
chorus2 chorus2 woohoo2];
sound(thelazysong);
Sample # 3
Price Tag
f0=261.63;
c=f0
c0=f0*2*(2^(.48/12));
d=f0*(2^(2/12));
d1=f0*2*(2^(1.8/12));
e=f0*(2^(4/12));
f=f0*(2^(5/12));
f1=f0*(2^(6/12));
g=f0*(2^(7/12));
g1=f0*(2^(8/12));
a=f0*(2^(9/12));
b=f0*(2^(11/12));
bf=f0*(2^(10/12));
g2=f0*(2^(20/12));
ts=1/3300;
t1=0:ts:.5;
t11=0:ts:.8;
t12=0:ts:1;
t13=0:ts:.6;
t14=0:ts:1.2;
t15=0:ts:1.5;
t16=0:ts:1.4;
t2=0:ts:2;
t22=0:ts:2.8;
t23=0:ts:2.4;
t3=0:ts:3;
t4=0:ts:4;
t5=0:ts:5;
s1=0*(0:ts:2);
s2=0*(0:ts:0.8);
s3=0*(0:ts:.9);
s4=0*(0:ts:1.8);
s5=0*(0:ts:1);
s6=0*(0:ts:.5);
s7=0*(0:ts:1.2);
s8=0*(0:ts:2.3);
s9=0*(0:ts:.2);
s10=0*(0:ts:3);
ff=sin(2*pi*f*t2);
ff1=sin(2*pi*f*t12);
ff2=sin(2*pi*f*t1);
ff3=sin(2*pi*f*t13);
ff4=sin(2*pi*f*t11);
ff5=sin(2*pi*f*t15);
ff6=sin(2*pi*f*t22);
ff7=sin(2*pi*f*t16);
ff8=sin(2*pi*f*t14);
cc=sin(2*pi*c*t1);
cc1=sin(2*pi*c*t12);
aa=sin(2*pi*a*t1);
aa1=sin(2*pi*a*t12);
aa2=sin(2*pi*a*t14);
aa3=sin(2*pi*a*t11);
gg=sin(2*pi*g*t11);
gg1=sin(2*pi*g*t12);
gg2=sin(2*pi*g*t1);
gg3=sin(2*pi*g*t13);
gg4=sin(2*pi*g*t14);
dd=sin(2*pi*d*t1);
dd1=sin(2*pi*d*t12);
dd2=sin(2*pi*d*t2);
dd3=sin(2*pi*d*t23);
bbf=sin(2*pi*bf*t1);
bbf1=sin(2*pi*bf*t12);
c01=sin(2*pi*c0*t14);
c02=sin(2*pi*c0*t1);
c03=sin(2*pi*c0*t12);
d11=sin(2*pi*d1*t1);
ee=sin(2*pi*e*t1);
ee1=sin(2*pi*e*t12);
ee2=sin(2*pi*e*t15);
ee3=sin(2*pi*e*t14);
y=[ff s2 cc cc aa aa aa aa gg1 ff2 gg4 s1 gg2 gg2 gg2 gg2 gg2 aa1 ff1 ff1 s1 dd cc ff1 ff1 ff1 dd cc ff1
ff1 ff2 ff2 ff1 ff1 ff2 ff2 ff2 ff2 cc1 s2 gg1 aa2 s3 aa aa aa aa aa aa aa1 aa1 gg2 gg3 s4 gg2 gg2 gg1
gg1 gg1 gg1 ff2 ff3 s5 dd1 ff1 ff2 ff2 ff1 dd cc ff1 ff1 ff1 dd cc ff1 ff2 ff2 ff2 ff2 cc1 s2 gg1 aa2 s5
gg2 aa aa aa aa1 aa aa c03 s8 c02 d11 d11 d11 c03 aa gg2 aa1 s4 ff2 gg2 aa1 aa1 ff1 s6 aa bbf bbf
bbf1 aa1 ff2 ff1 s5 ff2 ff2 ff2 ff2 ff2 aa ff1 aa ff1 aa ff5 s5 ff2 ff2 ff2 ff2 gg2 ee1 gg2 ee1 gg2 ee2 ee
ee ee ee ee ee ff6 dd3 ee ee ee ee ee ff6 dd3 s2 ff2 ff2 ff2 ff1 s8 ff2 ff1 ff2 ff1 ff2 ff2 ff2 ff1 s8 ee ee1
ee ee1 ee ee ee ee ff6 dd3 ee ee ee ee ee ff6 dd2 s10 ff2 aa aa aa aa gg1 ff1 gg4 s1 gg2 gg2 gg2 gg2
gg2 aa1 ff1 ff1 s1 dd cc ff1 ff1 ff1 dd cc ff1 ff1 ff5 dd ff2 ff2 ff2 ff2 ff1 ff2 cc1 s2 gg1 aa2 s3 aa aa aa
aa aa aa aa1 aa1 gg2 gg3 s4 gg2 gg2 gg1 gg1 gg1 gg1 ff2 ff3 s5 dd1 ff1 ff2 ff2 ff1 dd cc ff1 ff1 ff1 dd
cc ff1 ff2 ff2 ff2 ff2 cc1 s2 gg1 aa2 s5 gg2 aa aa aa aa1 aa aa c03 s8 c02 d11 d11 d11 c03 aa gg2 aa1
s4 ff2 gg2 aa1 aa1 ff1 s6 aa bbf bbf bbf1 aa1 ff2 ff1 s5 ff2 ff2 ff2 ff2 ff2 aa ff1 aa ff1 aa ff5 s5 ff2 ff2
ff2 ff2 gg2 ee1 gg2 ee1 gg2 ee2 ee ee ee ee ee ee ff6 dd3 ee ee ee ee ee ff6 dd3 s2 ff2 ff2 ff2 ff1 s8
ff2 ff1 ff2 ff1 ff2 ff2 ff2 ff1 s8 ee ee1 ee ee1 ee ee ee ee ff6 dd3 ee ee ee ee ee ff6 dd2 s5 ff2 ff2 ff2
ff2 ff2 aa ff1 aa ff1 aa ff5 s5 ff2 ff2 ff2 ff2 gg2 ee1 gg2 ee1 gg2 ee2 ee ee ee ee ee ee ff6 dd3 ee ee ee
ee ee ff6 dd3 s2 ff2 ff2 ff2 ff1 s8 ff2 ff1 ff2 ff1 ff2 ff2 ff2 ff1 s8 ee ee1 ee ee1 ee ee ee ee ff6 dd3 ee
ee ee ee ee ff6 dd2] ;
sound(y);
c=
261.6300