Blogger news

Sunday, 22 June 2014

MATLAB code for electronic piano

function keys

clear all
close all
clc
% MISC PARAMETERS
fontsize=[10];
timekeycolor=[0.7,0.0,0.0];
soundkeycolor=[0.0,0.0,0.7];
keycolor1=[1.0,1.0,1.0];
keycolor2=[1.0,1.0,1.0];
keycolor3=[0.0,0.0,0.0];
keycolor4=[0.0,0.0,0.0];

% FIGURE
hFigure=figure(...
    'Units','Pixels',...
    'Position',[90 400 831 350],...
    'Color','k',...
    'MenuBar','none',...
    'Toolbar','none',...
    'NumberTitle','off',...
    'Name','KEYS');

% TIME BUTTONS
hbuttonGroup=uibuttongroup(...
    'Parent',hFigure,...
    'Units','Pixels',...
    'Position',[0 301 831 25],...
    'BackgroundColor',[0,0,0],...
    'SelectionChangeFcn',@timeSelection_callback);

time1=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup,...
    'Units','Pixels',...
    'Position',[ 3 3 164 15],...
    'String','T I M E 1',...
    'FontSize',8,...
    'ForegroundColor',timekeycolor,...
    'BackgroundColor',timekeycolor);

time2=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup,...
    'Units','Pixels',...
    'Position',[168 3 164 15],...
    'String','T I M E 2',...
    'FontSize',8,...
    'ForegroundColor',timekeycolor,...
    'BackgroundColor',timekeycolor);

time3=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup,...
    'Units','Pixels',...
    'Position',[333 3 164 15],...
    'String','T I M E 3',...
    'FontSize',8,...
    'ForegroundColor',timekeycolor,...
    'BackgroundColor',timekeycolor);

time4=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup,...
    'Units','Pixels',...
    'Position',[498 3 164 15],...
    'String','T I M E 4',...
    'FontSize',8,...
    'ForegroundColor',timekeycolor,...
    'BackgroundColor',timekeycolor);

tuner=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup,...
    'Units','Pixels',...
    'Position',[663 3 164 15],...
    'String','T U N E R  T I M E',...
    'FontSize',8,...
    'ForegroundColor',timekeycolor,...
    'BackgroundColor',timekeycolor);

% SOUND BUTTONS

hbuttonGroup2=uibuttongroup(...
    'Parent',hFigure,...
    'Units','Pixels',...
    'Position',[0 326 831 25],...
    'BackgroundColor',[0,0,0],...
    'SelectionChangeFcn',@fsoundSelection_callback);

snd1=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup2,...
    'Units','Pixels',...
    'Position',[3 3 164 15],...
    'String','S O U N D  1',...
    'FontSize',8,...
    'ForegroundColor',soundkeycolor,...
    'BackgroundColor',soundkeycolor);

snd2=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup2,...
    'Units','Pixels',...
    'Position',[168 3 164 15],...
    'String','S O U N D  2',...
    'FontSize',8,...
    'ForegroundColor',soundkeycolor,...
    'BackgroundColor',soundkeycolor);

snd3=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup2,...
    'Units','Pixels',...
    'Position',[333 3 164 15],...
    'String','S O U N D  3',...
    'FontSize',8,...
    'ForegroundColor',soundkeycolor,...
    'BackgroundColor',soundkeycolor);

snd4=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup2,...
    'Units','Pixels',...
    'Position',[498 3 164 15],...
    'String','S O U N D  4',...
    'FontSize',8,...
    'ForegroundColor',soundkeycolor,...
    'BackgroundColor',soundkeycolor);

snd5=uicontrol(...
    'Style','togglebutton',...
    'Parent',hbuttonGroup2,...
    'Units','Pixels',...
    'Position',[663 3 164 15],...
    'String','S O U N D  5',...
    'FontSize',8,...
    'ForegroundColor',soundkeycolor,...
    'BackgroundColor',soundkeycolor)

% KEYS
hButtonGroup=uibuttongroup(...
    'Parent',hFigure,...
    'Units','Pixels',...
    'Position',[0 0 831 300],...
    'BackgroundColor',[0,0,0],...
    'SelectionChangeFcn',@noteSelection_callback);

% WHITE KEYS (t1 - t15)
t1=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[ 0 0 60 300],...
    'String','C3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t2=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[55 0 60 300],...
    'String','D3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t3=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[110 0 60 300],...
    'String','E3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t4=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[165 0 60 300],...
    'String','F3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t5=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[220 0 60 300],...
    'String','G3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t6=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[275 0 60 300],...
    'String','A3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t7=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[330 0 60 300],...
    'String','B3',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t8=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[385 0 60 300],...
    'String','C4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t9=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[440 0 60 300],...
    'String','D4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t10=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[495 0 60 300],...
    'String','E4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t11=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[550 0 60 300],...
    'String','F4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t12=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[605 0 60 300],...
    'String','G4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t13=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[660 0 60 300],...
    'String','A4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t14=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[715 0 60 300],...
    'String','B4',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

t15=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[770 0 60 300],...
    'String','C5',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor1,...
    'BackgroundColor',keycolor2);

% BLACK KEYS (t16-t25)
t16=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[30 140 40 160],...
    'String','C3s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t17=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[100 140 40 160],...
    'String','D3s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t18=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[195 140 40 160],...
    'String','F3s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);
t19=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[258 140 40 160],...
    'String','G3s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t20=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[320 140 40 160],...
    'String','A3s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t21=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[423 140 40 160],...
    'String','C4s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t22=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[485 140 40 160],...
    'String','D4s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t23=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[580 140 40 160],...
    'String','F4s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t24=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[642 140 40 160],...
    'String','G4s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

t25=uicontrol(...
    'Style','togglebutton',...
    'Parent',hButtonGroup,...
    'Units','Pixels',...
    'Position',[705 140 40 160],...
    'String','A4s',...
    'FontSize',fontsize,...
    'ForegroundColor',keycolor3,...
    'BackgroundColor',keycolor4);

% FUNCTIONS
t=0.6;
    function timeSelection_callback(hObject,eventdata)
        T=get(eventdata.NewValue,'String');

        switch T
            case 'T I M E 1'
                t=0.2;
            case 'T I M E 2'
                t=0.4;
            case 'T I M E 3'
                t=0.6;
            case 'T I M E 4'
                t=1.0;
            case 'T U N E R  T I M E'
                t=10.0;
        end
    end % END OF TIME FUNCTION

f=0.0;
    function fsoundSelection_callback(hObject,eventdata)
        F=get(eventdata.NewValue,'String');

        switch F
            case 'S O U N D  1'
                f=0.0;
            case 'S O U N D  2'
                f=0.2;
            case 'S O U N D  3'
                f=0.3;
            case 'S O U N D  4'
                f=0.4;
            case 'S O U N D  5'
                f=0.8;
        end
    end % END OF FSOUND FUNCTION

    function noteSelection_callback(hObject,eventdata)
        N=get(eventdata.NewValue,'String');
        x=linspace(0,t*30*pi,t*5000);

        switch N
            case 'C3'
                y=sin(0.5*10.65*x)+f*sin(0.5*(10.65+f)*x);
                sound(y)
            case 'D3'
                y=sin(0.5*11.95*x)+f*sin(0.5*(11.95+f)*x);
                sound(y)
            case 'E3'
                y=sin(0.5*13.4*x)+f*sin(0.5*(13.4+f)*x);
                sound(y)
            case 'F3'
                y=sin(0.5*14.2*x)+f*sin(0.5*(14.2+f)*x);
                sound(y)
            case 'G3'
                y=sin(0.5*15.95*x)+f*sin(0.5*(15.95+f)*x);
                sound(y)
            case 'A3'
                y=sin(8.95*x)+f*sin((8.95+f)*x);
                sound(y)
            case 'B3'
                y=sin(10.05*x)+f*sin((10.05+f)*x);
                sound(y)
            case 'C4'
                y=sin(10.65*x)+f*sin((10.65+f)*x);
                sound(y)
            case 'D4'
                y=sin(11.95*x)+f*sin((11.95+f)*x);
                sound(y)
            case 'E4'
                y=sin(13.4*x)+f*sin((13.4+f)*x);
                sound(y)
            case 'F4'
                y=sin(14.2*x)+f*sin((14.2+f)*x);
                sound(y)
            case 'G4'
                y=sin(15.95*x)+f*sin((15.95+f)*x);
                sound(y)
            case 'A4'
                y=sin(2*8.95*x)+f*sin(2*(8.95+f)*x);
                sound(y)
            case 'B4'
                y=sin(2*10.05*x)+f*sin(2*(10.65+f)*x);
                sound(y)
            case 'C5'
                y=sin(2*10.65*x)+f*sin(2*(10.65+f)*x);
                sound(y)
            case 'C3s'
                y=sin(0.5*11.27*x)+f*sin(0.5*(11.27+f)*x);
                sound(y)
            case 'D3s'
                y=sin(0.5*12.65*x)+f*sin(0.5*(12.65+f)*x);
                sound(y)
            case 'F3s'
                y=sin(0.5*15.05*x)+f*sin(0.5*(15.05+f)*x);
                sound(y)
            case 'G3s'
                y=sin(0.5*16.88*x)+f*sin(0.5*(16.88+f)*x);
                sound(y)
            case 'A3s'
                y=sin(9.48*x)+f*sin(0.5*(9.48+f)*x);
                sound(y)
            case 'C4s'
                y=sin(11.275*x)+f*sin(0.5*(11.275+f)*x);
                sound(y)
            case 'D4s'
                y=sin(12.66*x)+f*sin(0.5*(12.66+f)*x);
                sound(y)
            case 'F4s'
                y=sin(15.05*x)+f*sin(0.5*(15.05+f)*x);
                sound(y)
            case 'G4s'
                y=sin(16.88*x)+f*sin(0.5*(16.88+f)*x);
                sound(y)
            case 'A4s'
                y=sin(2*9.48*x)+f*sin(2*(9.48+f)*x);
                sound(y)
        end
    end
end % END OF NOTE FUNCTION

No comments:

Post a Comment