Sei sulla pagina 1di 3

Laboratory 4

Moses Abu

4 LAB EXERCISE: Synthesis of Musical Notes


function xx = key2note(keynum, dur, voice)
% KEY2NOTE Produce a sinusoidal waveform corresponding to a
% given piano key number
%
% usage: xx = key2note (keynum, dur)
%
% xx = the output sinusoidal waveform
% keynum = the piano keyboard number of the desired note
% dur = the duration (in seconds) of the output note
% voice = the voice level of the input file

fs = 11025;
tt = 0:(1/fs):dur;

if keynum==0
xx = zeros(1,length(tt));
else
fnote = 440*2^((keynum-49)/12);
if voice == 1,
f = fnote*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
elseif voice == 2
f = fnote*[1 3 5]';
A = 0.8*[1 1/2 1/4];
elseif voice == 3,
f = 1.5*fnote*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
end
xx = A * cos(2*pi*f*tt);

% Add the ADSR Profile to Improve Sound


if( dur>0.15 )
ee = interp1([0,0.05,0.1,dur-0.05,dur],[0,1,0.9,0.8,0],tt);
elseif( dur>0.10 )
ee = interp1([0,0.05,dur-0.05,dur],[0,1,0.8,0],tt);
else
ee = interp1([0,0.05,dur],[0,1,0],tt);
end
xx = xx.*ee;
end

The second m.file

clear; load bach_fugue.mat;


fs = 11025;
bpm = 120;
beats_per_second = bpm/60;
seconds_per_beat = 1/beats_per_second;
seconds_per_pulse = seconds_per_beat / 4;
xx = zeros(1,557203);

for i = 1:3
% Get number of notes in this "voice"
notes = length(theVoices(i).noteNumbers);
% Add the initial dead space
n1 = 1;
dead = key2note(0,theVoices(i).startPulses(1)*seconds_per_pulse,i);
n2 = n1 + length(dead) - 1;
xx(n1:n2) = xx(n1:n2) + dead;
n1 = n2 + 1;
for kk = 1:notes
keynum = theVoices(i).noteNumbers(kk);
dur = theVoices(i).durations(kk)*seconds_per_pulse;
startPulse = theVoices(i).startPulses(kk);
% Add the current key for the correct duration
tone = key2note(keynum,dur,i);
n2 = n1 + length(tone) - 1;
xx(n1:n2) = xx(n1:n2) + tone;
n1 = n2 + 1;

if kk < notes
% Add dead space is needed
if (theVoices(i).startPulses(kk+1)-startPulse) > dur
dead = key2note(0,(theVoices(i).startPulses(kk+1)-
startPulse)*seconds_per_pulse,i);
n2 = n1 + length(dead) - 1;
xx(n1:n2) = xx(n1:n2) + dead;
n1 = n2 + 1;
end
end
end
end
soundsc( xx, fs )
wavwrite(xx,11025,32,'moses_lab4.wav')

Spectrogram of the musical compilation displayed below:


5000

Frequency 4000

3000

2000

1000

0
5 10 15 20 25 30 35 40 45 50
Time

Potrebbero piacerti anche