S&S midterm

1.piano keyboard

2.music simulation

------------------------piano_keyboard--------------

function y = gen_wave(freq,rythm)

Fs = 8192;

x = linspace(0,2*pi*rythm,floor(Fs * rythm));

y = sin(freq*x).*(1-x/(rythm*2*pi));

end

------------------piano_keyboard_GUI-----------

------------------buttom example

function A4_Callback(hObject, eventdata, handles)

y = gen_wave(440,0.5);

sound(y,8192);

-------------------music_simulation_star--------------------------------------------------------

function y = note(tone,rythm)

Fs = 8192;

freqs = [262,294,330,349,392,440,494];

x = linspace(0,2*pi*rythm,floor(Fs * rythm));

y = sin(freqs(tone)*x).*(1-x/(rythm*2*pi));

end

Fs=8192;

x1 = note(1,0.25);

x2 = note(1,0.25);

x3 = note(5,0.25);

x4 = note(5,0.25);

x5 = note(6,0.25);

x6 = note(6,0.25);

x7 = note(5,0.5);


y1 = [x1,x2,x3,x4,x5,x6,x7];


x1 = note(4,0.25);

x2 = note(4,0.25);

x3 = note(3,0.25);

x4 = note(3,0.25);

x5 = note(2,0.25);

x6 = note(2,0.25);

x7 = note(1,0.5);


y2 = [x1,x2,x3,x4,x5,x6,x7];


x1 = note(5,0.25);

x2 = note(5,0.25);

x3 = note(4,0.25);

x4 = note(4,0.25);

x5 = note(3,0.25);

x6 = note(3,0.25);

x7 = note(2,0.5);


y3 = [x1,x2,x3,x4,x5,x6,x7];

y4 = y3;

y5 = y1;

y6 = y2;

y=[y1,y2,y3,y4,y5,y6];


plot(y);

sound(y,Fs);