0

A fun little program that uses your internal motherboard speaker or "Beep" speaker and your keyboard to make a musical instument very similar to a musical KeyBoard.

// Program by #include<DAN.h> 
#include <iostream> 
#include <windows.h> // WinApi header 
#include <conio.h>
#include <defkeynum.h> // a header file I made to define the key numbers into names

using namespace std;

char KeyStroke; int octave = 0;

int main() 
{
     while (KeyStroke != 27) // 27 = Escape key
     {
          KeyStroke = getch ();
          //KeyStroke = getch ();
          switch (KeyStroke)
          {
                 case UP_ARROW:
                         cout << "UP ARROW" << endl;
                         if (octave < 8){octave = octave + 1;}
                         cout << "OCTAVE " << octave << endl;
				 break;
	             case DOWN_ARROW:
                         cout << "DOWN ARROW" << endl;
                         if (octave > 0){octave = octave - 1;}
                         cout << "OCTAVE " << octave << endl;
				 break;
				 case 'a': {cout << "C" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(16.35,100);
                                 break;
                                 case 1: Beep(32.70,100);
                                 break;
                                 case 2: Beep(65.41,100);
                                 break;
                                 case 3: Beep(130.81,100);
                                 break;
                                 case 4: Beep(261.63,100);
                                 break;
                                 case 5: Beep(523.25,100);
                                 break;
                                 case 6: Beep(1046.50,100);
                                 break;
                                 case 7: Beep(2093.00,100);
                                 break;
                                 case 8: Beep(4186.01,100);
                                 break;
                            }
                           }
                 break;
                 case 'w': {cout << "C#" << octave << " / Db" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(17.32,100);
                                 break;
                                 case 1: Beep(34.65,100);
                                 break;
                                 case 2: Beep(69.30,100);
                                 break;
                                 case 3: Beep(138.59,100);
                                 break;
                                 case 4: Beep(277.18,100);
                                 break;
                                 case 5: Beep(554.37,100);
                                 break;
                                 case 6: Beep(1108.73,100);
                                 break;
                                 case 7: Beep(2217.46,100);
                                 break;
                                 case 8: Beep(4434.92,100);
                                 break;
                            }
                           }
                 break;
                 case 's': {cout << "D" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(18.35,100);
                                 break;
                                 case 1: Beep(36.71,100);
                                 break;
                                 case 2: Beep(73.42,100);
                                 break;
                                 case 3: Beep(146.83,100);
                                 break;
                                 case 4: Beep(293.66,100);
                                 break;
                                 case 5: Beep(587.33,100);
                                 break;
                                 case 6: Beep(1174.66,100);
                                 break;
                                 case 7: Beep(2349.32,100);
                                 break;
                                 case 8: Beep(4698.64,100);
                                 break;
                            }
                           }
                 break;
                 case 'e': {cout << "D#" << octave << " / Eb" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(19.45,100);
                                 break;
                                 case 1: Beep(38.89,100);
                                 break;
                                 case 2: Beep(77.78,100);
                                 break;
                                 case 3: Beep(155.56,100);
                                 break;
                                 case 4: Beep(311.13,100);
                                 break;
                                 case 5: Beep(622.25,100);
                                 break;
                                 case 6: Beep(1244.51,100);
                                 break;
                                 case 7: Beep(2489.02,100);
                                 break;
                                 case 8: Beep(4978.03,100);
                                 break;
                            }
                           }
                 break;
                 case 'd': {cout << "E" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(20.60,100);
                                 break;
                                 case 1: Beep(41.20,100);
                                 break;
                                 case 2: Beep(82.41,100);
                                 break;
                                 case 3: Beep(164.81,100);
                                 break;
                                 case 4: Beep(329.63,100);
                                 break;
                                 case 5: Beep(659.26,100);
                                 break;
                                 case 6: Beep(1318.51,100);
                                 break;
                                 case 7: Beep(2637.02,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'f': {cout << "F" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(21.83,100);
                                 break;
                                 case 1: Beep(43.65,100);
                                 break;
                                 case 2: Beep(87.31,100);
                                 break;
                                 case 3: Beep(174.61,100);
                                 break;
                                 case 4: Beep(349.23,100);
                                 break;
                                 case 5: Beep(698.46,100);
                                 break;
                                 case 6: Beep(1396.91,100);
                                 break;
                                 case 7: Beep(2793.83,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 't': {cout << "F#" << octave << " / Gb" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(23.12,100);
                                 break;
                                 case 1: Beep(46.25,100);
                                 break;
                                 case 2: Beep(92.50,100);
                                 break;
                                 case 3: Beep(185.00,100);
                                 break;
                                 case 4: Beep(369.99,100);
                                 break;
                                 case 5: Beep(739.99,100);
                                 break;
                                 case 6: Beep(1479.98,100);
                                 break;
                                 case 7: Beep(2959.96,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'g': {cout << "G" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(24.50,100);
                                 break;
                                 case 1: Beep(49.00,100);
                                 break;
                                 case 2: Beep(98.00,100);
                                 break;
                                 case 3: Beep(196.00,100);
                                 break;
                                 case 4: Beep(392.00,100);
                                 break;
                                 case 5: Beep(783.99,100);
                                 break;
                                 case 6: Beep(1567.98,100);
                                 break;
                                 case 7: Beep(3135.96,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'y': {cout << "G#" << octave << " / Ab" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(25.96,100);
                                 break;
                                 case 1: Beep(51.91,100);
                                 break;
                                 case 2: Beep(103.83,100);
                                 break;
                                 case 3: Beep(207.65,100);
                                 break;
                                 case 4: Beep(415.30,100);
                                 break;
                                 case 5: Beep(830.61,100);
                                 break;
                                 case 6: Beep(1661.22,100);
                                 break;
                                 case 7: Beep(3322.44,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'h': {cout << "A" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(27.50,100);
                                 break;
                                 case 1: Beep(55.00,100);
                                 break;
                                 case 2: Beep(110.00,100);
                                 break;
                                 case 3: Beep(220.00,100);
                                 break;
                                 case 4: Beep(440.00,100);
                                 break;
                                 case 5: Beep(880.00,100);
                                 break;
                                 case 6: Beep(1760.00,100);
                                 break;
                                 case 7: Beep(3520.00,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'u': {cout << "A#" << octave << " / Bb" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(29.14,100);
                                 break;
                                 case 1: Beep(58.27,100);
                                 break;
                                 case 2: Beep(116.54,100);
                                 break;
                                 case 3: Beep(233.08,100);
                                 break;
                                 case 4: Beep(466.16,100);
                                 break;
                                 case 5: Beep(932.33,100);
                                 break;
                                 case 6: Beep(1864.66,100);
                                 break;
                                 case 7: Beep(3729.31,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'j': {cout << "B" << octave << endl;
                            switch (octave)
                            {
                                 case 0: Beep(30.87,100);
                                 break;
                                 case 1: Beep(61.74,100);
                                 break;
                                 case 2: Beep(123.47,100);
                                 break;
                                 case 3: Beep(246.94,100);
                                 break;
                                 case 4: Beep(493.88,100);
                                 break;
                                 case 5: Beep(987.77,100);
                                 break;
                                 case 6: Beep(1975.53,100);
                                 break;
                                 case 7: Beep(3951.07,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'k': {cout << "C" << (octave + 1) << endl;
                            switch (octave)
                            {
                                 case 0: Beep(32.70,100);
                                 break;
                                 case 1: Beep(65.41,100);
                                 break;
                                 case 2: Beep(130.81,100);
                                 break;
                                 case 3: Beep(261.63,100);
                                 break;
                                 case 4: Beep(523.25,100);
                                 break;
                                 case 5: Beep(1046.50,100);
                                 break;
                                 case 6: Beep(2093.00,100);
                                 break;
                                 case 7: Beep(4186.01,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'o': {cout << "C#" << (octave + 1) << " / Db" << (octave + 1) << endl;
                            switch (octave)
                            {
                                 case 0: Beep(34.65,100);
                                 break;
                                 case 1: Beep(69.30,100);
                                 break;
                                 case 2: Beep(138.59,100);
                                 break;
                                 case 3: Beep(277.18,100);
                                 break;
                                 case 4: Beep(554.37,100);
                                 break;
                                 case 5: Beep(1108.73,100);
                                 break;
                                 case 6: Beep(2217.46,100);
                                 break;
                                 case 7: Beep(4434.92,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'l': {cout << "D" << (octave + 1) << endl;
                            switch (octave)
                            {
                                 case 0: Beep(36.71,100);
                                 break;
                                 case 1: Beep(73.42,100);
                                 break;
                                 case 2: Beep(146.83,100);
                                 break;
                                 case 3: Beep(293.66,100);
                                 break;
                                 case 4: Beep(587.33,100);
                                 break;
                                 case 5: Beep(1174.66,100);
                                 break;
                                 case 6: Beep(2349.32,100);
                                 break;
                                 case 7: Beep(4698.64,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case 'p': {cout << "D#" << (octave + 1) << " / Eb" << (octave + 1) << endl;
                            switch (octave)
                            {
                                 case 0: Beep(38.89,100);
                                 break;
                                 case 1: Beep(77.78,100);
                                 break;
                                 case 2: Beep(155.56,100);
                                 break;
                                 case 3: Beep(311.13,100);
                                 break;
                                 case 4: Beep(622.25,100);
                                 break;
                                 case 5: Beep(1244.51,100);
                                 break;
                                 case 6: Beep(2489.02,100);
                                 break;
                                 case 7: Beep(4978.03,100);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case ';': {cout << "E" << (octave + 1) << endl;
                            switch (octave)
                            {
                                 case 0: Beep(41.20,100);
                                 break;
                                 case 1: Beep(82.41,100);
                                 break;
                                 case 2: Beep(164.81,100);
                                 break;
                                 case 3: Beep(329.63,100);
                                 break;
                                 case 4: Beep(659.26,100);
                                 break;
                                 case 5: Beep(1318.51,100);
                                 break;
                                 case 6: Beep(2637.02,100);
                                 break;
                                 case 7: Beep(1000,10);
                                 break;
                                 case 8: Beep(1000,10);
                                 break;
                            }
                           }
                 break;
                 case ESC:
                         exit(1);
                 break;
                
                 default:
                        cout << "Some other key." << endl;
                 break;
          }
     }
system("pause"); // wait 
return 0; 
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by KumarUtkarsh
0

To many branches using 'switch', you should have used a function to calculate the pitches based on the current octave. Note how C grows by the power-of-two, but has a gain added each time(16, 33, 65, etc.).

Plus Beep, and "pause" are not portable. Try including cstdlib, and use _beep(), and getchar().
Your variables should be in main(), not global.

edit:
Hope that helps you a bit, in simplifying it, and making it easier on yourself; instead of manually typing all of those pitch tables out.

0

Hey #include<DAN.h>
Please sent its .exe file @ [email snipped]
I will make it original looking Keyboard fully functional Musical Keyboard.

Edited by WaltP: Never post your EMAIL address in the forums

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.