I Need Help

This is a function in a program designed to be a very simple musical keyboard. This bit of code isn't working properly and I'm stumped. I'm also open to suggestions on how to do this in different styles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void Keys(){

    string a;
    cin >> a;
    if(a == "a" || a == "A"){
		
    int az = 20;
        cout << "\n\nPress Any Button To Stop\n\n";
		
		cin >> az;
		
		while(az == 20){
			
			long double sx = 1000000000000000000;
			
			Beep(220, sx);
			
			if(az != 20){
				break;
			}
		}
	}
}
I don't understand what you are trying to achieve here. You have defined az as variable equal to 20 and then you are using the same variable to take the value from the user...
.
I thought - but could be wrong - that the second argument to Beep was supposed to be an int?

You can try this if you are using Windows. have a look at function void play(), which calls the Beep function.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <windows.h>
using namespace std;

const double factorOctave = 2.0;
const double factorSemitone = 1.0594630943593;
map<string,double> noteMap;

// Default tune (guess!)
// First line is beats per minute; next triplets are:
//       base note (R is a rest);   octave relative to central octave (defined below);   number of beats;
string MUSIC( "150"
              "R 0 1   D  0 1   C  0 1   D  0 1   Eb 0 3   D  0 1   C  0 1   D  0 1   Bb 0 1.5   C  0 0.5   C  0 1   D  0 3"
              "R 0 1   D  0 1   C  0 1   D  0 1   Eb 0 3   D  0 1   C  0 1   D  0 1   Bb 0 1.5   C  0 0.5   D  0 4         "
              "R 0 1   F  0 1   F  0 1   F  0 1   G  0 3   D  0 1   D  0 1   F  0 1   F  0 1.5   C  0 0.5   C  0 1   Eb 0 3"
              "R 0 1   Eb 0 1   D  0 1   C  0 1   D  0 3   Bb 0 1   Bb 0 1   C  0 1   C  0 1.5   D  0 0.5   D  0 4         "
              "R 0 1   F  0 1   F  0 1   F  0 1   G  0 3   D  0 1   D  0 1   F  0 1   F  0 1.5   C  0 0.5   C  0 1   Eb 0 3"
              "R 0 1   Eb 0 1   D  0 1   C  0 1   D  0 3   Bb 0 1   Bb 0 1   C  0 1   C  0 1.5   Bb 0 0.5   Bb 0 4         " );


//======================================================================

class Note
{
public:
   string baseNote;
   int octave;
   double beats;

   Note( string bn = "A", int o = 0, double bt = 1.0 ) : baseNote( bn ), octave( o ), beats( bt ) {}
};

//----------

istream &operator >> ( istream &strm, Note &n )
{ 
   strm >> n.baseNote >> n.octave >> n.beats;
   return strm;
}

//----------

ostream &operator << ( ostream &strm, Note &n )
{ 
   strm << n.baseNote << " " << n.octave << " " << n.beats;
   return strm;
}

//----------

bool validNote( const Note &n )
{
   if ( noteMap.count( n.baseNote ) == 0 ) return false;
   if ( n.beats <= 0 ) return false;
   return true;
}

//======================================================================


void setMap()      // Maps notes to frequency (in Hz) for octave starting at A above middle C
{
   noteMap["A" ]                 = 440.00;       // tuning-fork A
   noteMap["A#"] = noteMap["Bb"] = 466.16;
   noteMap["B" ] = noteMap["Cb"] = 493.88;
   noteMap["B#"] = noteMap["C" ] = 523.25;
   noteMap["C#"] = noteMap["Db"] = 554.37;
   noteMap["D" ]                 = 587.33;
   noteMap["D#"] = noteMap["Eb"] = 622.25;
   noteMap["E" ]                 = 659.26;
   noteMap["E#"] = noteMap["F" ] = 698.46;
   noteMap["F#"] = noteMap["Gb"] = 739.99;
   noteMap["G" ]                 = 783.99;
   noteMap["G#"] = noteMap["Ab"] = 830.61;

   noteMap["R" ]                 = 1.0   ;       // rest (nominal frequency)
}


//======================================================================


void play( double freq, int ms )                 // Frequency in Hertz, duration in milliseconds
{
   int ifreq = freq + 0.5;
   Beep( ifreq, ms );                            // WINDOWS (but can be replaced by other libraries)
}


//======================================================================


void delay( int ms )
{
   Sleep( ms );                                  // WINDOWS (but can be replaced by other libraries)
}


//======================================================================


double transpose( double freq, int oct, int sem )
{
   while ( oct > 0 ) { freq *= factorOctave;    oct--; }
   while ( oct < 0 ) { freq /= factorOctave;    oct++; }
   while ( sem > 0 ) { freq *= factorSemitone;  sem--; }
   while ( sem < 0 ) { freq /= factorSemitone;  sem++; }
   return freq;
}


//======================================================================


void input( istream &in, vector<Note> &notes, double &bpm )
{
   Note n;

   in >> bpm;
   while ( in >> n ) if ( validNote( n ) ) notes.push_back( n );
}


//======================================================================


void playMusic( vector<Note> &notes, double bpm )
{
   double timeFactor = 1000.0 * 60.0 / bpm;
   double frequency;

   for ( Note n : notes )
   {
      int ms = timeFactor * n.beats + 0.5;
      if ( n.baseNote == "R" )                   // rest
      {
         delay( ms );
      }
      else                                       // play note
      {
         frequency = transpose( noteMap[n.baseNote], n.octave, 0 );
         play( frequency, ms );
      }
   }
}


//======================================================================


int main()
{
   vector<Note> notes;
   string filename = "music.in";
   double bpm;

   // Define basic note mapping for octave 0 (starts at A above middle C)
   setMap();

   // Read notes
// ifstream in( filename );                     // Read from file
   stringstream in( MUSIC );                    // Or from string
   input( in, notes, bpm );                     // Read notes

   // Play notes
   playMusic( notes, bpm );
}


//====================================================================== 


https://www.youtube.com/watch?v=5Q-hcUuRLcI&feature=youtu.be&t=281
Last edited on
I know that you're probably thinking about how terrible the code on top was, but the truth is that I made it terrible to see how the people on this forum would react. A lot of forums just have people on them that sit and wait for someone to screw up so they can attack them and I was seeing if this forum was like that. Thank you for being kind and offering constructive criticism.
Last edited on
Topic archived. No new replies allowed.