Simple ARPEGGIO using Beep() (not looped)

// So I made this source code inspiration from a VST "CEDR" which its an arp vst :)


#include <iostream>
#include <windows.h>
#include <string>
#include <time.h>


using namespace std;
int main()
{




int c1, c2, c3, c4, c5, c6, c7, c8, c9, c10;
///This row gets the interger to load a beep sample
// cell 1
cout << "Enter 1st Key number: ";
cin >> c1 ;
cout << "\n";

// cell 2
cout << "Enter 2nd Key number: ";
cin >> c2;
cout << "\n";

//cell 3
cout << "Enter 3rd Key number: ";
cin >> c3;
cout << "\n";

//cell 4
cout << "Enter 4th Key number: ";
cin >> c4;
cout << "\n";

//cell 5
cout << "Enter 5th Key number: ";
cin >> c5;
cout << "\n";

//cell 6
cout << "Enter 6th Key number: ";
cin >> c6;
cout << "\n";
//cell 7
cout << "Enter 7th Key number: ";
cin >> c7;
cout << "\n";

//cell 8
cout << "Enter 8th Key number: ";
cin >> c8;
cout << "\n";

//cell 9
cout << "Enter 9th Key number: ";
cin >> c9;
cout << "\n";

//cell 10
cout << "Enter 10th Key number: ";
cin >> c10;
cout << "\n";

///aanddd this row is the couts of keys you entered.

cout << "Arp1 chord: "<< c1 << endl;

cout << "Arp2 chord: "<< c2 << endl;

cout << "Arp3 chord: "<< c3 << endl;

cout << "Arp4 chord: "<< c4 << endl;

cout << "Arp5 chord: "<< c5 << endl;

cout << "Arp6 chord: "<< c6 << endl;

cout << "Arp7 chord: "<< c7 << endl;

cout << "Arp8 chord: "<< c8 << endl;

cout << "Arp9 chord: "<< c9 << endl;

cout << "Arp10 chord: "<< c10 << endl;


///This row is the key selection, it finds the specific key pitch for the number you typed
{ // c1
if (c1 == 1)
{ // C
c1 = Beep(261,300);
}
if (c1 == 2)
{ // D
c1 = Beep(293,300);
}
if (c1 ==3)
{ // E
c1 = Beep(329,300);
}
if (c1 == 4)
{ // F
c1 = Beep(349,300);
}
if (c1 == 5)
{ // G
c1 = Beep(392,300);
}
if (c1 == 6)
{ // A
c1 = Beep(440,300);
}
if (c1 == 7)
{ // B
c1 = Beep(493,300);
}
}
{ // c2
if (c2 == 1)
{ // C
c2 = Beep(261,300);
}
if (c2 == 2)
{ // D
c2 = Beep(293,300);
}
if (c2 ==3)
{ // E
c2 = Beep(329,300);
}
if (c2 == 4)
{ // F
c2 = Beep(349,300);
}
if (c2 == 5)
{ // G
c2 = Beep(392,300);
}
if (c2 == 6)
{ // A
c2 = Beep(440,300);
}
if (c2 == 7)
{ // B
c2 = Beep(493,300);
}
}
{ // c3
if (c3 == 1)
{ // C
c3 = Beep(261,300);
}
if (c3 == 2)
{ // D
c3 = Beep(293,300);
}
if (c3 ==3)
{ // E
c3 = Beep(329,300);
}
if (c3 == 4)
{ // F
c3 = Beep(349,300);
}
if (c3 == 5)
{ // G
c3 = Beep(392,300);
}
if (c3 == 6)
{ // A
c3 = Beep(440,300);
}
if (c3 == 7)
{ // B
c3 = Beep(493,300);
}
}
{ // c4
if (c4 == 1)
{ // C
c4 = Beep(261,300);
}
if (c4 == 2)
{ // D
c4 = Beep(293,300);
}
if(c4 ==3)
{ // E
c4 = Beep(329,300);
}
if (c4 == 4)
{ // F
c4 = Beep(349,300);
}
if (c4 == 5)
{ // G
c4 = Beep(392,300);
}
if (c4 == 6)
{ // A
c4 = Beep(440,300);
}
if (c4 == 7)
{ // B
c4 = Beep(493,300);
}
}
{ // c5
if (c5 == 1)
{ // C
c5 = Beep(261,300);
}
if (c5 == 2)
{ // D
c5 = Beep(293,300);
}
if (c5 ==3)
{ // E
c5 = Beep(329,300);
}
if (c5 == 4)
{ // F
c5 = Beep(349,300);
}
if (c5 == 5)
{ // G
c5 = Beep(392,300);
}
if (c5 == 6)
{ // A
c5 = Beep(440,300);
}
if (c5 == 7)
{ // B
c5 = Beep(493,300);
}
}
{ // c6
if (c6 == 1)
{ // C
c6 = Beep(261,300);
}
if (c6 == 2)
{ // D
c6 = Beep(293,300);
}
if (c6 ==3)
{ // E
c6 = Beep(329,300);
}
if (c6 == 4)
{ // F
c6 = Beep(349,300);
}
if (c6 == 5)
{ // G
c6 = Beep(392,300);
}
if (c6 == 6)
{ // A
c6 = Beep(440,300);
}
if (c6 == 7)
{ // B
c6 = Beep(493,300);
}
}
{ // c7
if (c7 == 1)
{ // C
c7 = Beep(261,300);
}
if (c7 == 2)
{ // D
c7 = Beep(293,300);
}
if (c7 ==3)
{ // E
c7 = Beep(329,300);
}
if (c7 == 4)
{ // F
c7 = Beep(349,300);
}
if (c7 == 5)
{ // G
c7 = Beep(392,300);
}
if (c7 == 6)
{ // A
c7 = Beep(440,300);
}
if (c7 == 7)
{ // B
c7 = Beep(493,300);
}
}
{ // c8
if (c8 == 1)
{ // C
c8 = Beep(261,300);
}
if (c8 == 2)
{ // D
c8 = Beep(293,300);
}
if (c8 ==3)
{ // E
c8 = Beep(329,300);
}
if (c8 == 4)
{ // F
c8 = Beep(349,300);
}
if (c8 == 5)
{ // G
c8 = Beep(392,300);
}
if (c8 == 6)
{ // A
c8 = Beep(440,300);
}
if (c8 == 7)
{ // B
c8 = Beep(493,300);
}
}
{ // c9
if (c9 == 1)
{ // C
c9 = Beep(261,300);
}
if (c9 == 2)
{ // D
c9 = Beep(293,300);
}
if (c9 ==3)
{ // E
c9 = Beep(329,300);
}
if (c9 == 4)
{ // F
c9 = Beep(349,300);
}
if (c9 == 5)
{ // G
c9 = Beep(392,300);
}
if (c9 == 6)
{ // A
c9 = Beep(440,300);
}
if (c9 == 7)
{ // B
c9 = Beep(493,300);
}
}
{ // c10
if (c10 == 1)
{ // C
c10 = Beep(261,300);
}
if (c10 == 2)
{ // D
c10 = Beep(293,300);
}
if (c10 ==3)
{ // E
c10 = Beep(329,300);
}
if (c10 == 4)
{ // F
c10 = Beep(349,300);
}
if (c10 == 5)
{ // G
c10 = Beep(392,300);
}
if (c10 == 6)
{ // A
c10 = Beep(440,300);
}
if (c10 == 7)
{ // B
c10 = Beep(493,300);
}
}

/// Octave +1
{ // C1 + oct
if (c1 == 11)
{ // C
c1 = Beep(522,300);
}
if (c1 == 12)
{ // D
c1 = Beep(586,300);
}
if (c1 ==13)
{ // E
c1 = Beep(658,300);
}
if (c1 == 14)
{ // F
c1 = Beep(698,300);
}
if (c1 == 15)
{ // G
c1 = Beep(784,300);
}
if (c1 == 16)
{ // A
c1 = Beep(880,300);
}
if (c1 == 17)
{ // B
c1 = Beep(586,300);
}
}
{ // C2 + oct
if (c2 == 11)
{ // C
c2 = Beep(522,300);
}
if (c2 == 12)
{ // D
c2 = Beep(586,300);
}
if (c2 ==13)
{ // E
c2 = Beep(658,300);
}
if (c2 == 14)
{ // F
c2 = Beep(698,300);
}
if (c2 == 15)
{ // G
c2 = Beep(784,300);
}
if (c2 == 16)
{ // A
c2 = Beep(880,300);
}
if (c2 == 17)
{ // B
c2 = Beep(586,300);
}
}
{ // C3 + oct
if (c3 == 11)
{ // C
c3 = Beep(522,300);
}
if (c3 == 12)
{ // D
c3 = Beep(586,300);
}
if (c3 ==13)
{ // E
c3 = Beep(658,300);
}
if (c3 == 14)
{ // F
c3 = Beep(698,300);
}
if (c3 == 15)
{ // G
c3 = Beep(784,300);
}
if (c3 == 16)
{ // A
c3 = Beep(880,300);
}
if (c3 == 17)
{ // B
c3 = Beep(586,300);
}
}
{ // C4 + oct
if (c4 == 11)
{ // C
c4 = Beep(522,300);
}
if (c4 == 12)
{ // D
c4 = Beep(586,300);
}
if (c4 ==13)
{ // E
c4 = Beep(658,300);
}
if (c4 == 14)
{ // F
c4 = Beep(698,300);
}
if (c4 == 15)
{ // G
c4 = Beep(784,300);
}
if (c4 == 16)
{ // A
c4 = Beep(880,300);
}
if (c4 == 17)
{ // B
c4 = Beep(586,300);
}
}
{ // C5 + oct
if (c5 == 11)
{ // C
c5 = Beep(522,300);
}
if (c5 == 12)
{ // D
c5 = Beep(586,300);
}
if (c5 ==13)
{ // E
c5 = Beep(658,300);
}
if (c5 == 14)
{ // F
c5 = Beep(698,300);
}
if (c5 == 15)
{ // G
c5 = Beep(784,300);
}
if (c5 == 16)
{ // A
c5 = Beep(880,300);
}
if (c5 == 17)
{ // B
c5 = Beep(586,300);
}
}
{ // C6 + oct
if (c6 == 11)
{ // C
c6 = Beep(522,300);
}
if (c6 == 12)
{ // D
c6 = Beep(586,300);
}
if (c6 ==13)
{ // E
c6 = Beep(658,300);
}
if (c6 == 14)
{ // F
c6 = Beep(698,300);
}
if (c6 == 15)
{ // G
c6 = Beep(784,300);
}
if (c6 == 16)
{ // A
c6 = Beep(880,300);
}
if (c6 == 17)
{ // B
c6 = Beep(586,300);
}
}
{ // C7 + oct
if (c7 == 11)
{ // C
c7 = Beep(522,300);
}
if (c7 == 12)
{ // D
c7 = Beep(586,300);
}
if (c7 ==13)
{ // E
c7 = Beep(658,300);
}
if (c7 == 14)
{ // F
c7 = Beep(698,300);
}
if (c7 == 15)
{ // G
c7 = Beep(784,300);
}
if (c7 == 16)
{ // A
c7 = Beep(880,300);
}
if (c7 == 17)
{ // B
c7 = Beep(586,300);
}
}
{ // C8 + oct
if (c8 == 11)
{ // C
c8 = Beep(522,300);
}
if (c8 == 12)
{ // D
c8 = Beep(586,300);
}
if (c8 ==13)
{ // E
c8 = Beep(658,300);
}
if (c8 == 14)
{ // F
c8 = Beep(698,300);
}
if (c8 == 15)
{ // G
c8 = Beep(784,300);
}
if (c8 == 16)
{ // A
c8 = Beep(880,300);
}
if (c8 == 17)
{ // B
c8 = Beep(586,300);
}
}
{ // C9 + oct
if (c9 == 11)
{ // C
c9 = Beep(522,300);
}
if (c9 == 12)
{ // D
c9 = Beep(586,300);
}
if (c9 ==13)
{ // E
c9 = Beep(658,300);
}
if (c9 == 14)
{ // F
c9 = Beep(698,300);
}
if (c9 == 15)
{ // G
c9 = Beep(784,300);
}
if (c9 == 16)
{ // A
c9 = Beep(880,300);
}
if (c9 == 17)
{ // B
c9 = Beep(586,300);
}
}
{ // C10 + oct
if (c10 == 11)
{ // C
c10 = Beep(522,300);
}
if (c10 == 12)
{ // D
c10 = Beep(586,300);
}
if (c10 ==13)
{ // E
c10 = Beep(658,300);
}
if (c10 == 14)
{ // F
c10 = Beep(698,300);
}
if (c10 == 15)
{ // G
c10 = Beep(784,300);
}
if (c10 == 16)
{ // A
c10 = Beep(880,300);
}
if (c10 == 17)
{ // B
c10 = Beep(586,300);
}
}



cin.get();

return 0;
}
sorry If I didn't arranged the code :(
Does this play some music?
yeah using beep and pitch control it can play like piano keys
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
// 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( "180"
              "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 );
}


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


Yeah, picked out by ear, so it's the wrong key!

https://youtu.be/8q9RJiZZh8I?t=395
Topic archived. No new replies allowed.