Biano- beep piano

Dec 19, 2009 at 5:35pm
Hei all!

I am trying to make a sort of piano that plays music through pc's internal speaker using beeps.

this is what i have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

int main(int argc, char *argv[])
{
  while(true){
              cout<<"Input note: ";
              char note = getch();
              
              
              //do re mi fa sol la si do re mi fa sol
              if(note == 'a'){
                      Beep(261,100);
                      }
              if(note == 's'){
                      Beep(293,100);
                      }
              if(note == 'd'){
                      Beep(329,100);
                      }
              if(note == 'f'){
                      Beep(349,100);
                      }
              if(note == 'g'){
                      Beep(392,100);
                      }
              if(note == 'h'){
                      Beep(440,100);
                      }
              if(note == 'j'){
                      Beep(493,100);
                      }                      
              if(note == 'k'){
                      Beep(523,100);
                      }                      
              if(note == 'l'){
                      Beep(587,100);
                      }
              if(note == ';'){
                      Beep(659,100);
                      }  
              if(note == '\''){
                      Beep(698,100);
                      } 
               if(note == '\\'){
                      Beep(784,100);
                      } 
                      
               //rebemol mibemol solbemol labemol sibemol rebemol mibemol solbemol
               if(note == 'w'){
                      Beep(277,100);
                      } 
                      if(note == 'e'){
                      Beep(311,100);
                      }
                      if(note == 't'){
                      Beep(370,100);
                      }
                      if(note == 'y'){
                      Beep(415,100);
                      }
                      if(note == 'u'){
                      Beep(466,100);
                      }
                      if(note == 'o'){
                      Beep(554,100);
                      }
                      if(note == 'p'){
                      Beep(622,100);
                      }
                      if(note == ']'){
                      Beep(740,100);
                      }     
                      
            system("cls");          
}  
    
    return EXIT_SUCCESS;
}



Ok so what i am trying to do now is that the program would recognize for how long the person is holding a key, so it would beep without stop.

Can anyone help me with it? I dont seem to think of a way
Dec 19, 2009 at 5:40pm
Instead of getch you can try with something like GetAsyncKeyState http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx

eg:
1
2
while ( GetAsyncKeyState ( 'P' ) )
    Beep(622,100);
Dec 19, 2009 at 5:47pm
doesnt work, maybe it is becouse i am a noob

Is there any other way to do it?
Last edited on Dec 19, 2009 at 6:02pm
Dec 19, 2009 at 10:18pm
Dec 20, 2009 at 12:26am
you can make two variables that they type in for note and length.
1
2
3
4
5
6
7
8
9
cout<<"Input note: ";
              char note;
              int length;
              cin >> note;
              cin >> length;
              if(note == 'a'){
                   if (length == 100) {
                      Beep(261,100);
                      }


You can change this up and do whatever you want with it, but this is the most basic way to do it. It takes, and believe me when I say this, it takes A LOT of if statements. You will go mad trying to do all of those if statements, so I would only use this way as a last resort. Try to find another way. haha
Dec 20, 2009 at 12:51am
It takes, and believe me when I say this, it takes A LOT of if statements.


You're suggesting doing something like this, right?
1
2
3
4
if (note == 'a' and length == 100){Beep(261, 100);}
if (note == 'a' and length == 101){Beep(261, 101);}
if (note == 'a' and length == 102){Beep(261, 102);}
if (note == 'a' and length == 103){Beep(261, 103);}

Why not just do this?
 
if (note == 'a'){Beep(261, length);}


Then you need just one "if" statement per note. Or you could shorten it to a single "if" for the entire program:
1
2
3
4
5
6
char notes[5] = {"a", "b", "c", "d", "e"};
int frequencies[5] = {261,130,75,32,16}; //these aren't accurate numbers but whatever
for(int i = 0; i < 5; i++){
 if (note == notes[i]){Beep(frequencies[i], length)}
}
//a map would be better but I don't feel like implementing it. The concept is the same. 
Dec 20, 2009 at 12:55am
WOW! LOL! I didn't even think about doing that. I'm still relatively new to programming, so I'm not good at thinking this stuff out yet. That's the best idea I've seen though. haha
Topic archived. No new replies allowed.