Coding Project

Piano Project In C

In C Language by Baqir AliLeave a Comment

This project is designed and developed using c language. In this project we have used graphics.h library to draw piano keys and accessed speaker ports to produce sound of different frequencies. There are few prerequisites to this project.

  • You should be using Windows XP or below version to run Turbo c in full screen mode
  • If you are using some latest windows you should install Dos Box to be able to run this project
Piano Project in C Language
Piano Project in C Language
//--------------------------- Header Files 
#include<stdio.h>
 #include<graphics.h>
 #define ON (1)
 #define OFF  (0)
 void NoSound(void);
 void key(int,int);
 void createkey(void);
 void move(int a, int b);
 void *buff;
//-----------------------------
void ChangeSpeaker( int status )     
 {
     int portval;
     portval = inportb( 0x61 );
     if ( status==ON )
         portval |= 0x03;
     else
     portval &=~ 0x03;
     outportb( 0x61, portval );
 } /*--ChangeSpeaker( )----------*/
void Sound( int hertz )
 {
     unsigned divisor = 989898L / hertz;
     ChangeSpeaker( ON );
     outportb(0x43, 0xB6 );
     outportb(0x42, divisor & 0xFF     ) ;
     outportb(0x42, divisor >> 8 ) ;
     delay(70);
     NoSound();
 } /*--Sound()-----*/
void NoSound( void )
 {
     ChangeSpeaker( OFF );
 } /*--NoSound()------*/
void main(void)
 {
     int driver, mode;
     char ch;
     clrscr();
     driver=VGA; mode=VGAHI;
     initgraph(&driver,&mode,"c:\tc\bgi");
     settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
     moveto(160,60);
     outtext("SOFTNAT PIANO");
     settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
     moveto(400,400);
     outtext("DEVELOPED BY: BAQIR ALI");
     createkey();
     //putpixel(130,300,RED);
     while( (ch=getch())!='\x1b')
     {
         switch(ch)
         {
             case 'a':
                 move(130,160);
                 Sound(100);
                 free(buff);
                 break;
             case 's':
                 move(170,200);
                 Sound(500);
                 free(buff);
                 break;
             case 'd':
                 move(210,240);
                 Sound(900);
                 free(buff);
                 break;
             case 'f':
                 move(250,280);
                 Sound(1300);
                 free(buff);
                 break;
             case 'g':
                 move(290,320);
                 Sound(1700);
                 free(buff);
                 break;
             case 'h':
                 move(330,360);
                 Sound(2100);
                 free(buff);
                 break;
             case 'j':
                 move(370,400);
                 Sound(2500);
                 free(buff);
                 break;
             case 'k':
                 move(410,440);
                 Sound(2900);
                 free(buff);
                 break;
             case 'l':
                 move(450,480);
                 Sound(3400);
                 free(buff);
                 break;
         }
     }
     closegraph();
 }
void move(int a,int b)
{
    unsigned size;     
    a-=5;     
    b+=5;         
    size=imagesize(a,195,b,300);         
    buff=(void *)malloc(size);         
    getimage(a,190,b,300,buff);         
    putimage(a,190+4,buff,XOR_PUT);         
    putimage(a,190+4,buff,COPY_PUT);        
    delay(70);         
    size=imagesize(a,190+4,b,300+10);         
    getimage(a,190+4,b,300+10,buff);         
    putimage(a,190,buff,XOR_PUT);               
    putimage(a,190,buff,COPY_PUT);
 }/* -- moves the key down when clicked a buttton --*/
void createkey(void)
{
     setcolor(GREEN);
     setfillstyle(SOLID_FILL,WHITE);
     key(30,60);
     setfillstyle(SOLID_FILL,BLACK);
     key(70,100);
     setfillstyle(SOLID_FILL,WHITE);
     key(110,140);
     setfillstyle(SOLID_FILL,BLACK);
     key(150,180);
     setfillstyle(SOLID_FILL,WHITE);
     key(190,220);
     setfillstyle(SOLID_FILL,BLACK);
     key(230,260);
     setfillstyle(SOLID_FILL,WHITE);
     key(270,300);
     setfillstyle(SOLID_FILL,BLACK);
     key(310,340);
     setfillstyle(SOLID_FILL,WHITE);
     key(350,380);
}
void key(int x,int y)
{
     bar3d(x+100,200,y+100,300,3,3);
}

Leave a Comment