Lab 1
Lab 1
LABORATORY
REPORT
Lab 1: Elementary Music Synthesis
% Sampling rate
Fs = 8000;
%Frequency of each note
A = 220;
B = 220 * 2^(2 / 12);
C = 220 * 2^(3 / 12);
E = 220 * 2^(7 / 12);
%Time duration
tt1 = 0:1 / Fs:0.5 - 1 / Fs;
tt2 = 0:1 / Fs:1 - 1 / Fs;
tt4 = 0:1 / Fs:2 - 1 / Fs;
%Notes
A2 = sin(2*pi*tt2*A);
A1 = sin(2*pi*tt1*A);
E1 = sin(2*pi*tt1*E);
B1 = sin(2*pi*tt1*B);
C1 = sin(2*pi*tt1*C);
A4 = sin(2*pi*tt4*A);
p = zeros(1, 1000);
y = [A2, p, A1, p, E1, p, E1, p, E1, p, B1, p, C1, p, B1,
p, A4];
plot(y);
title("Music synthesis: Van Duc Thanh - Nguyen Phi Song
Toan");
sound(y, Fs);
2. Result
%Sampling rate
Fs = 8000;
%Frequency of each note
A = 220;
B = 220 * 2^(2 / 12);
C = 220 * 2^(3 / 12);
E = 220 * 2^(7 / 12);
%Time duration
tt1 = 0:1 / Fs:0.5 - 1 / Fs;
tt2 = 0:1 / Fs:1 - 1 / Fs;
tt4 = 0:1 / Fs:2 - 1 / Fs;
% Generate the adsr envelop for each note duration
adsr_whole = adsr_gen(16000);
adsr_half = adsr_gen(8000);
adsr_fourth = adsr_gen(4000);
%Notes
A2 = sin(2*pi*tt2*A);
A1 = sin(2*pi*tt1*A);
E1 = sin(2*pi*tt1*E);
B1 = sin(2*pi*tt1*B);
C1 = sin(2*pi*tt1*C);
A4 = sin(2*pi*tt4*A);
p = zeros(1, 1000);
y = [adsr_half .* A2, adsr_fourth .* A1, adsr_fourth .*
E1, adsr_fourth .* E1, adsr_fourth .* E1, adsr_fourth .*
B1, adsr_fourth .* C1, adsr_fourth .* B1, adsr_whole .*
A4];
plot(y);
title("Music synthesis: Van Duc Thanh - Nguyen Phi Song
Toan ");
sound(y, Fs);
%Function to generate the ADSR envelop
function a = adsr_gen(freq)
Fs = freq;
a = zeros(1, Fs);
durationFourth = [453; 453; 1735; 1359];
durationHalf = [905; 905; 3471; 2719];
durationWhole = [1811; 1811; 6943; 5435];
coeffAttack = 24000;
coeffDecay = 96000;
if freq == 4000
duration = durationFourth;
elseif freq == 8000
duration = durationHalf;
coeffAttack = coeffAttack * 2;
coeffDecay = coeffDecay * 2;
elseif freq == 16000
duration = durationWhole;
coeffAttack = coeffAttack * 4;
coeffDecay = coeffDecay * 4;
end
% Attack phase
start = 1;
stop = duration(1);
for n = start:stop
a(1, n) = 53 / coeffAttack * n;
end
% Decay phase
start = stop + 1;
stop = start + duration(2);
for n = start:stop
a(1, n) = -53 / coeffDecay * n + 5 / 4;
tempVal = a(n);
end
% Sustain phase
start = stop + 1;
stop = start + duration(3);
for n = start:stop
a(1, n) = tempVal;
end
%Relase phase
start = stop + 1;
stop = Fs;
for n = start:stop
a(1, n) = -53 / coeffDecay * n + 53 / 24;
end
end
2. Result
3. Tone Overlapping
- As explained in Section 4.2, allow the decaying notes (i.e. with the
windowing function) to overlap slightly in time. Decide the overlap
duration yourself.
- Were you able to improve the sound quality? Save the modified
music synthesis in a new .m file. Include this .m file in your
E-Submit.
1. Matlab script - file Bai3.m
%Sampling rate
Fs = 8000;
%Frequency of each note
A = 220;
B = 220 * 2^(2 / 12);
C = 220 * 2^(3 / 12);
E = 220 * 2^(7 / 12);
%Time duration
tt1 = 0:1 / Fs:0.5 - 1 / Fs;
tt2 = 0:1 / Fs:1 - 1 / Fs;
tt4 = 0:1 / Fs:2 - 1 / Fs;
adsr_whole = adsr_gen(16000);
adsr_half = adsr_gen(8000);
adsr_fourth = adsr_gen(4000);
rightCutIndexFourth = 3000;
rightCutIndexHalf = 7000;
% Find the index of the left cut of the note
leftCutIndexHalfFourth =
find_left_cut_index(rightCutIndexHalf, adsr_half,
adsr_fourth);
leftCutIndexFourthFourth =
find_left_cut_index(rightCutIndexFourth, adsr_fourth,
adsr_fourth);
leftCutIndexFourthWhole =
find_left_cut_index(rightCutIndexFourth, adsr_fourth,
adsr_whole);
%Notes
A2 = sin(2*pi*tt2*A);
A1 = sin(2*pi*tt1*A);
E1 = sin(2*pi*tt1*E);
B1 = sin(2*pi*tt1*B);
C1 = sin(2*pi*tt1*C);
A4 = sin(2*pi*tt4*A);
A2_Cut = adsr_half .* A2;
A1_Cut = adsr_fourth .* A1;
E1_Cut = adsr_fourth .* E1;
B1_Cut = adsr_fourth .* B1;
C1_Cut = adsr_fourth .* C1;
A4_Cut = adsr_whole .* A4;
p = zeros(1, 1000);
y = [A2_Cut(1:7000), A1_Cut(leftCutIndexHalfFourth:3000),
E1_Cut(leftCutIndexFourthFourth:3000),
E1_Cut(leftCutIndexFourthFourth:3000),
E1_Cut(leftCutIndexFourthFourth:3000),
B1_Cut(leftCutIndexFourthFourth:3000),
C1_Cut(leftCutIndexFourthFourth:3000),
B1_Cut(leftCutIndexFourthFourth:3000),
A4_Cut(leftCutIndexFourthWhole:end)];
plot(y);
title("Music synthesis: Van Duc Thanh - Nguyen Phi Song
Toan ");
sound(y, Fs);
function a = adsr_gen(freq)
Fs = freq;
a = zeros(1, Fs);
durationFourth = [453; 453; 1735; 1359];
durationHalf = [905; 905; 3471; 2719];
durationWhole = [1811; 1811; 6943; 5435];
coeffAttack = 24000;
coeffDecay = 96000;
if freq == 4000
duration = durationFourth;
elseif freq == 8000
duration = durationHalf;
coeffAttack = coeffAttack * 2;
coeffDecay = coeffDecay * 2;
elseif freq == 16000
duration = durationWhole;
coeffAttack = coeffAttack * 4;
coeffDecay = coeffDecay * 4;
end
% Attack phase
start = 1;
stop = duration(1);
for n = start:stop
a(1, n) = 53 / coeffAttack * n;
end
% Decay phase
start = stop + 1;
stop = start + duration(2);
for n = start:stop
a(1, n) = -53 / coeffDecay * n + 5 / 4;
tempVal = a(n);
end
% Sustain phase
start = stop + 1;
stop = start + duration(3);
for n = start:stop
a(1, n) = tempVal;
end
start = stop + 1;
stop = Fs;
for n = start:stop
a(1, n) = -53 / coeffDecay * n + 53 / 24;
end
end
function out = find_left_cut_index(leftFreq,
leftAdsrValue, rightAdsrValue)
out = 0;
for i = 1:leftFreq;
out = out + 1;
if (rightAdsrValue(i) > leftAdsrValue(leftFreq+1)) &&
(rightAdsrValue(i)) < leftAdsrValue(leftFreq-1)
break;
end
end
end
2. Result
2.