A Virtual Piano By:-- Anurag Pareek

anurag_pareek 0 Tallied Votes 198 Views Share

VIRTUAL PIANO
It's a very simple sound program , Input device is mouse.
Sound function is used to produce output.Working is very simple
based on seven natural freqencies.Move mouse over keys for sounds.
Change range and note the difference.

#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#define SHOW 1
#define HIDE 2

union REGS input,output;

class piano
{
 public:int BIGKEY,MIDKEY,back,border;
	piano()//init constructor
	{
		BIGKEY=15;
		MIDKEY=1;
		back=7;
		border=15;
	}
}color;
void drawpiano(int x,int y);
int check_xy(int x,int y);
void BOX(int c,int r,int c1,int r1,int col);
int initmouse();
void setupscreen();
void pointer(int on);
void restrictmouse(int x1,int y1,int x2,int y2);
void check_keys(int x,int y);
void getmouse(int *button,int *x,int *y);
float freq[7] = {130.81, 146.83, 164.81, 174.61,196, 220, 246.94 } ;
int a=4,backcolor=2,exitcode=1;
void showbar(int t)
{
 if(t>65) t=65;
 if(t<1) t=1;
 textcolor(15);
 for(int q=0;q<=t;t++)
 {
	gotoxy(3+q,4);
	cprintf("Û");
 }
}
void main()
{
 int b,x,y,key;
 char ch;
 if(initmouse()==-1)
 {
	clrscr();
	printf("\n\nNO MOUSE FOUND !\n\n");
	exit(1);
 }
 pointer(SHOW);
 setupscreen();
 exitcode=1;
 while(exitcode)
 {
	if(kbhit())
	{
	   ch=getch();
	   if(ch==27) break;
	   if(ch==75) a--;
	   if(ch==77) a++;
	   check_keys(x,y);
	   switch(ch)
	   {
		case 'a':case'A':key=0;break;
		case 's':case'S':key=1;break;
		case 'd':case'D':key=2;break;
		case 'f':case'F':key=3;break;
		case 'j':case'J':key=4;break;
		case 'k':case'K':key=5;break;
		case 'l':case'L':key=6;break;
	   }
	   sound(freq[key]*a);
	   delay(80);
	   nosound();
	}
	getmouse(&b,&x,&y);
	gotoxy(2,2);cprintf("%d,%d ",x,y);
	if(b==1)
	{
		while(b==1)
		{
			getmouse(&b,&x,&y);
			key=check_xy(x,y);
			if(key!=-1 && key<7)
			{
				sound(freq[key]*a);
			}
			else if(key>6)
			     {
				 sound(freq[12-key]*(a/2));
			     }
		}
		nosound();
		check_keys(x,y);
	}
 }
 textbackground(0);
 clrscr();
 _setcursortype(_NORMALCURSOR);
}
void setupscreen()
{
 _setcursortype(_NOCURSOR);
 textbackground(backcolor);
 clrscr();
 drawpiano(9,17);
 BOX(4,5,8,5,0);
 BOX(5,5,7,5,1);
 textcolor(15);
 gotoxy(70,5);
 cprintf("® Quit ¯");
 gotoxy(4,4);cprintf("Range");
 textbackground(0);
 gotoxy(8,5);cprintf("");
 gotoxy(4,5);cprintf("");
 gotoxy(6,5);cprintf("%d",a);
 textcolor(14);textbackground(backcolor+7);
 gotoxy(30,4);cprintf("VIRTUAL PIANO v1.0");
 gotoxy(33,6);textcolor(11);cprintf("DEVLOPED BY");
 gotoxy(32,7);textcolor(15);cprintf("ANURAG PAREEK");
 gotoxy(27,8);cprintf("pareek.anurag@gmail.com");
}
void pianokey(int x,int y)
{
 textbackground(color.MIDKEY);
 textcolor(color.BIGKEY);
 gotoxy(x,y);  cprintf("ÛÛÛ  ÛÛÛ");
 gotoxy(x,y+1);cprintf("ÛÛÛ  ÛÛÛ");
 gotoxy(x,y+2);cprintf("ÛÛÛ  ÛÛÛ");
 gotoxy(x,y+3);cprintf("ÛÛÛÛÞÛÛÛ");
 gotoxy(x,y+4);cprintf("ÛÛÛÛÞÛÛÛ");
 gotoxy(x,y+5);cprintf("ÛÛÛÛÞÛÛÛ");
 gotoxy(x,y+6);cprintf("ÛÛÛÛÞÛÛÛ");
 gotoxy(x,y);
}
void drawpiano(int x,int y)
{
 int t=9;
 BOX(x-5,y-3,75,y+8,color.border);
 BOX(x-4,y-2,74,y+7,color.back);
 pianokey(x,y);
 pianokey(x+t,y);
 pianokey(x+t*2,y);
 pianokey(x+t*3,y);
 pianokey(x+t*4,y);
 pianokey(x+t*5,y);
 pianokey(x+t*6,y);
}
void BOX(int c,int r,int c1,int r1,int col)
{
 int i,j;
 textcolor(col);
 for(i=r;i<=r1;i++)
 {
	for(j=c;j<=c1;j++)
	{
		gotoxy(j,i);
		cprintf("Û");
	}
 }
}
int initmouse()
{
 input.x.ax=0;
 int86(0x33,&input,&output);
 return(output.x.ax==0 ? -1 : 0);
}
void pointer(int on)
{
   input.x.ax=on;
   int86(0x33,&input,&output);
}
void restrictmouse(int x1,int y1,int x2,int y2)
{
   input.x.ax=7;
   input.x.cx=x1/8;
   input.x.dx=x2/8;
   int86(0x33,&input,&output);
   input.x.ax=8;
   input.x.cx=y1/8;
   input.x.dx=y2/8;
   int86(0x33,&input,&output);
}
void getmouse(int *button,int *x,int *y)
{
  input.x.ax=3;
  int86(0x33,&input,&output);
  *button=output.x.bx;
  *x=output.x.cx/8;
  *y=output.x.dx/8;
}
int check_xy(int x,int y)
{
  //mid keys
  if(x>=11 && y>=16 && x<=12 && y<=18)
  return 7;
  if(x>=20 && y>=16 && x<=21 && y<=18)
  return 8;
  if(x>=29 && y>=16 && x<=30 && y<=18)
  return 9;
  if(x>=38 && y>=16 && x<=39 && y<=18)
  return 10;
  if(x>=47 && y>=16 && x<=48 && y<=18)
  return 11;
  if(x>=56 && y>=16 && x<=57 && y<=18)
  return 0;
  if(x>=65 && y>=16 && x<=66 && y<=18)
  return 12;

  // big keys
  if(x>=8 && y>=16 && x<=15 && y<=22)
  return 0;
  if(x>=17 && y>=16 && x<=24 && y<=22)
  return 1;
  if(x>=26 && y>=16 && x<=33 && y<=22)
  return 2;
  if(x>=35 && y>=16 && x<=42 && y<=22)
  return 3;
  if(x>=44 && y>=16 && x<=51 && y<=22)
  return 4;
  if(x>=53 && y>=16 && x<=60 && y<=22)
  return 5;
  if(x>=62 && y>=16 && x<=69 && y<=22)
  return 6;

  return (-1);//no key pressed
}
void check_keys(int x,int y)
{
  if(x==7 && y==4)  a++;
  if(x==3 && y==4)  a--;
  if(a<1) a=1;else if(a>30) a=30;
  textcolor(15);textbackground(1);
  gotoxy(5,5);cprintf("   ");
  if(a<10) gotoxy(6,5);
  else gotoxy(5,5);
  cprintf("%d",a);
  if(x>=69 && y>=4 && x<=74 && y<=4) exitcode=0;
}
//***************************************************************************