Thread: c++ program for a virtual piano.

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    5

    c++ program for a virtual piano.

    I started with c++ 6 months back in school and have a grasp over the basics,selection statements,iterations,arrays,functions.
    I have no knowledge of graphics,structures.
    My desire is to create a virtual piano using c++.I don't want the whole code or anything, but I would like some help to start.I don't exactly know the areas where i should be familiar with to create this.I am willing to learn but need guidance as to what to learn.
    I've tried to use the help section in c++ to learn a little graphics.

    The main objectives and problems as per my conclusion are:
    1) drawing the piano using graphics.h and coloring it.
    2) connecting the keyboard to the program.
    3) playing the required frequencies when the keys are pressed.(I think the sound() function will help?)
    4) naming the keys( A,B,C)as pert he notes and also turning them a different shade of color when the respective key is pressed.
    5)To make the duration of each sound for as long as the user is pressing the key. The sound must stop when the user removes his finger from the key.
    6) What happens when you press more than one key ie. multiple sounds(I think this is not required as my piano will be quite primitive anyway)

    I have absolutely no idea how to proceed.I think drawing the piano and naming the keys are relatively easier,but the rest I am blank.
    Our teachers will not help us and have asked us to learn ourselves and we have about 2 weeks as the time limit.Because of the limited time and my approaching exams, I just want to know what functions to use so I can go read up about them and what is the extent of things c++can do, which is turning out to be very hard to find out myself. It doesn't have to be a perfect piano as there are better programming languages to do that, but since this is our syllabus,I am expected to produce the best c++ can do.
    I am using the Turboc3 version for windows 7.
    I desperately need help. And excuse me for the seemingly stupidity but my knowledge is poor although I am eager to learn.Please give me guidance and some sort of direction.
    Last edited by takando12; 01-16-2016 at 05:35 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by takando12 View Post
    I am using the Turboc3 version for windows 7.
    OMG, please don't! Choose a real IDE such as CodeBlocks, Netbeans, Eclipse or even Orwell Dev-C++.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    5
    Well, the problem is the school lab uses Turboc3 and that's what all the students are asked to use. Nevertheless, I will try to convince my teacher somehow and use dev c++(Never used it before,I hope it's the same).
    Can I get some help as to the program now? Please?

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    5
    Quote Originally Posted by GReaper View Post
    OMG, please don't! Choose a real IDE such as CodeBlocks, Netbeans, Eclipse or even Orwell Dev-C++.
    Well, the problem is the school lab uses Turboc3 and that's what all the students are asked to use. Nevertheless, I will try to convince my teacher somehow and use dev c++(Never used it before,I hope it's the same).
    Can I get some help as to the program now? Please?






  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by takando12 View Post
    [COLOR=#333333]Well, the problem is the school lab uses Turboc3 and that's what all the students are asked to use.
    Then dump your school and go somewhere else. It has proven itself worthless. I'm sorry, but I'm not even joking...
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    5
    Quote Originally Posted by GReaper View Post
    Then dump your school and go somewhere else. It has proven itself worthless. I'm sorry, but I'm not even joking...
    Oh it proved itself worthless ages ago. My only comfort is that I'll be out in a year.
    I will get Dev c++ installed soon and will work with it. But I still need help.
    I've been experimenting with the sound() today and have tried to play what sounds like "happy birthday". What I just don't understand how to do is play a sound till the user has got his fingers on a key.
    Code:
     
    #include<iostream.h>
    #include<dos.h>
    #include<conio.h>
    
    
    #define Key_a 97
    
    
    int main()
    { clrscr();
      int ch=0;
      while(ch!=65)
      { ch=getch();
        if(kbhit())
           { switch(ch)
              { case Key_a:  {sound(356);
                                      delay(500);
                                   }
              }
           }
        else
        nosound();
    
      }
      return(0);
    }
    I found that there is something called the kbhit() and am trying to use it. This program is supposed to play that frequency as long as the user presses 'a' and exit if the user presses 'A', but it's wrong and it's not working at all.It does exit when 'A' is pressed but doesn't play the frequency when 'a' is pressed. I'm a beginner so please bear with me.
    What am I doing wrong and how can I perfect it to achieve this?
    Thank you for your consideration.
    Last edited by takando12; 01-17-2016 at 05:34 AM.

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    5
    Please do help. I just want to know how to play a specified frequency as long as the user presses the specified key.Should i use kbhit() and how? Or perhaps there is a better way to do it?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you looked into using a C++ midi library?

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You probably want to look into the non-standard function called getch() that will work only in turbo-c and MS-DOS (but since that's true of all turbo C program I guess it doesn't matter)

    See: conio.h - Digital Mars

    For example, something like the snippet below might work...

    Code:
    int ch;
    while ((ch = getch()) != ' ') /* Loop until use presses space key */
    {
        switch (ch)
        {
        case 'a'
            sound(356);
            delay(500);
            break;
        default:
            nosound();
            break;
    }
    
    Edit: If you want the sound to play continuously until the user lifts their finger off the key you'll need some other method... this might help, http://www.digitalmars.com/rtl/bios.html#_bios_keybrd but I think you'll be on your own after you start using that Edit 2: That probably won't work actually. You might have to write your own keyboard interrupt handler
    Last edited by Hodor; 01-18-2016 at 07:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Talented piano player
    By happyclown in forum General Discussions
    Replies: 1
    Last Post: 07-18-2010, 06:16 PM
  2. Piano window questions
    By cdave in forum Windows Programming
    Replies: 6
    Last Post: 10-26-2005, 11:01 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. virtual memory program
    By funkylunch in forum Windows Programming
    Replies: 14
    Last Post: 12-04-2003, 11:15 AM
  5. really cool piano solo
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-26-2003, 11:11 AM

Tags for this Thread