Translate a music sheet into MATLAB code
Mostrar comentarios más antiguos
I want to play a music sheet in MATLAB. I have seen some tutorials and threads but they only play one note at a time. How can I play 3 consecutive notes (a chord ex. C major composing notes C-E-G)? And also, how can I put rest in between notes? Can you give me sample code that I can use, or help me improve my code below?
% This will be on a separate file that define keys and duration
function tone = note(key,dur);
fs = 44100;
t = 0 : 1/fs : dur;
freq = 440 * 2 ^ ((key - 49) / 12);
wave = sin(2*pi*freq*t).* exp(-0.0004 * 2 * pi * freq * t) / 1;
if dur == 4
tone = wave.*(exp(-1.2*t));
elseif dur == 3
tone = wave.*(exp(-1.9*t));
elseif dur == 2.5
tone = wave.*(exp(-2.2*t));
elseif dur == 2.25
tone = wave.*(exp(-2.4*t));
elseif dur == 2
tone = wave.*(exp(-2.5*t));
elseif dur == 1.5
tone = wave.*(exp(-2.7*t));
elseif dur == 1.25
tone = wave.*(exp(-2.8*t));
elseif dur == 1
tone = wave.*(exp(-3*t));
elseif dur == 0.75
tone = wave.*(exp(-4*t));
elseif dur == 0.5
tone = wave.*(exp(-6*t));
elseif dur == 0.25
tone = wave.*(exp(-9*t));
end
end
I managed to play the 1st measure of the song of my choice, "When I was your man" by Bruno Mars.
%1st Measure
c10 = note(34,2) + note(37,2) + note(40,2);
c11 = note(33,2) + note(37,2) + note(40,2);
c12 = note(18,2) + note(30,2);
t01 = [c10 c11];
b01 = [c12 c12];
m1 = [t01 + b01];
soundsc(m1,44100);
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Audio and Video Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!