from
MatLabPiano
by Alex Dytso
This is a simple code that generates music by pressing keys.
|
| MatLabPiano |
%Author: Alex Dytso
%Description: This is a simple code that generates music by pressing keys.
%The code uses a special function that avoids pressing enter after each key
%
function MatLabPiano
A=440;%press A
B=493.9;% press J
C=162.6; %press S
D=293.7; %press D
E=329.6; %press K
F=349.2; %press F
G=392.0; %press G
Fs=8000;
t=0:2.1/(Fs):1;
yA=cos(A*pi*t);
yB=cos(B*pi*t);
yC=cos(C*pi*t);
yD=cos(D*pi*t);
yE=cos(E*pi*t);
yF=cos(F*pi*t);
yG=cos(G*pi*t);
i=0;
while i<2 % infinit loop
K=getkey(1);
if K==97 % letter A was pressed corresponding to note A
sound(yA,Fs)
else
if K==106 % letter J was pressed correspondin to note B
sound(yB,Fs);
elseif K==115 % letter S was pressed corresponding to note C
sound(yC,Fs);
elseif K==100 % letter D was pressed correspondin to note D
sound(yD,Fs);
elseif K==107 % letter K was pressed correspondin to note E
sound(yE,Fs);
elseif K==102 % letter F was pressed correspondin to note F
sound(yF,Fs);
elseif K==103 % letter G was pressed correspondin to note G
sound(yG,Fs);
elseif K==113 % letter Q is pressed. Q is used to quite
break;
end
end
end
end
%using modified version getkey function written by Jos
function ch = getkey(N,nonascii)
nonascii = '' ;
N = 1 ;
callstr = 'set(gcbf,''Userdata'',double(get(gcbf,''Currentcharacter''))) ; uiresume ' ;
% Set up the figure
% May be the position property should be individually tweaked to avoid
% visibility
fh = figure(...
'keypressfcn',callstr, ...
'windowstyle','modal') ;
ch = cell(1,1) ;
% Wait for something to happen, usually a key press so uiresume is
% executed
uiwait ;
ch{1} = get(fh,'Userdata') ; % and the key itself
ch = ch{1} ; % return as a string
% return as a cell array of strings
% clean up the figure, if it still exists
close(fh) ;
end
|
|
Contact us