0% found this document useful (0 votes)
3 views12 pages

Lab 1

The document is a laboratory report for a music synthesis project conducted by students at the University of Da Nang. It details the process of synthesizing music using Matlab, including creating sound waves, applying ADSR envelope functions, and overlapping tones to improve sound quality. The final task involves synthesizing a favorite piece of music, specifically 'Jingle Bells,' and saving the results in a .m file.

Uploaded by

vdthanh6660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Lab 1

The document is a laboratory report for a music synthesis project conducted by students at the University of Da Nang. It details the process of synthesizing music using Matlab, including creating sound waves, applying ADSR envelope functions, and overlapping tones to improve sound quality. The final task involves synthesizing a favorite piece of music, specifically 'Jingle Bells,' and saving the results in a .m file.

Uploaded by

vdthanh6660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

THE UNIVERSITY OF DA NANG - UNIVERSITY SCIENCE AND TECHNOLOGY

FACULTY OF ADVANCED SCIENCE AND TECHNOLOGY


—-------------------------------------------------

LABORATORY
REPORT
Lab 1: Elementary Music Synthesis

INSTRUCTOR : Thai Vu Hien


CLASS : 22ES
SCHOOL YEAR : 2024-2025
STUDENTS : Van Duc Thanh
Nguyen Phi Song Toan
5.1 Music Synthesis
Synthesize the piece of music appearing in Figure 2 using only
information from Sections 2 and 3.
Play it back using the SOUND command in Matlab. Type HELP
SOUND for more information. Please specify the sampling rate = 8k
Hz in the playback.
Save the entire music synthesis in an .m file. Include this .m file in
your E-Submit.
1. Matlab script - file Bai1.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;
%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

5.2. Volume Variations


- Improve the quality of the sound with a volume window function
(see Section 4.1 above). Try concatenating different function to
model ADSR.
- 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 Bai2.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;
% 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

4. Favorite Music Synthesis


Please repeat those assignments above with your favorite
piece of music (around 15 notes). Save this created piece of
music in a new .m file. Include this .m file in your ESubmit.
1. Matlab script - file Bai2.m

% PHIEN BAN MOI - GIAI DIEU "Jingle Bells" (theo sheet


nhac da cho)
% ==== 1. Cai dat co ban ====
Fs = 16000; % Giam tan so lay mau de nhe tai
N = 4000; % Tang do dai moi not de toc do cham hon
% ==== 2. Bang tan so cua cac not ====
note_freq_map = containers.Map( ...
{'C4','D4','E4','F4','G4','A4','B4','C5','D5','E5','F5','
G5','R'}, ...
[261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88,
523.25, 587.33, 659.26, 698.46, 783.99, 0]);
% ==== 3. Ham tao song am cua not ====
note_wave = @(f, duration) sin(2*pi*(0:duration-1)/Fs *
f);
% ==== 4. ADSR envelope don gian ====
adsr_env = @(n) [linspace(0,1,floor(n*0.1)), ...
linspace(1,0.7,floor(n*0.1)), ...
0.7*ones(1,floor(n*0.6)), ...
linspace(0.7,0,floor(n*0.2))];
% ==== 5. Giai dieu "Jingle Bells" (doan dau 2 dong sheet
nhac) ====
melody = {
'E4', 1; 'E4', 1; 'E4', 2;
'E4', 1; 'E4', 1; 'E4', 2;
'E4', 1; 'G4', 1; 'C4', 1; 'D4', 1; 'E4', 2;
'F4', 1; 'F4', 1; 'F4', 1; 'F4', 1;
'F4', 1; 'E4', 1; 'E4', 1; 'E4', 0.5; 'E4', 0.5;
'E4', 1; 'D4', 1; 'D4', 1; 'E4', 1; 'D4', 2; 'G4', 2;
};
% ==== 6. Tong hop giai dieu ====
song = [];
prev_tone = [];
for i = 1:size(melody,1)
note = melody{i,1};
len = melody{i,2};
if strcmp(note, 'R')
tone = zeros(1, round(N*len));
else
f = note_freq_map(note);
tone = note_wave(f, round(N*len));
env = adsr_env(length(tone));
tone = tone .* env;
end
if isempty(prev_tone)
song = [song, tone];
else
overlap_len = floor(length(tone)*0.1);
mixed = prev_tone(end-overlap_len+1:end) +
tone(1:overlap_len);
mixed = mixed / max(abs(mixed));
song = [song(1:end-overlap_len), mixed,
tone(overlap_len+1:end)];
end
prev_tone = tone;
end
% ==== 7. Phat va luu bai hat ====
sound(song, Fs);
audiowrite('JingleBells.wav', song, Fs);
% ==== 8. Ve song am ====
t = (0:length(song)-1)/Fs;
figure;
plot(t, song);
xlabel('Thoi gian (giay)');
ylabel('Bien do');
title('Giai dieu "Jingle Bells"');
grid on;
2. Result

2.

You might also like