-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetronome.cpp
More file actions
235 lines (218 loc) · 6.97 KB
/
metronome.cpp
File metadata and controls
235 lines (218 loc) · 6.97 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <vector>
#include <set>
#include <tonc.h>
// Initialize the metronome
void init_metro(int bpm)
{
int seconds_per_minute = 60;
// Based on common denominator of CPU clock speed
int ticks_per_second = 16384;
// Convert musical beat values to CPU tick values. (bpm actually stands for bars for minute here)
// Negative values cause timer to underflow
// The value of a bar in CPU ticks, negative because this causes timer to underflow at max value - beat
int beat = -1 * seconds_per_minute * ticks_per_second / bpm;
// Set timer settings to create a 6/8 time signature (2 timers used with different subdivisions)
// Subdivision for melody (8th notes)
REG_TM0D= beat / 6;
REG_TM0CNT= TM_FREQ_1024 | TM_ENABLE;
// Cascade causes timer 1 to increment after timer 0 overflows (i.e. after 1/6 of a bar has passed)
REG_TM1CNT= TM_ENABLE | TM_CASCADE;
// Subdivision for percussion (16th notes or 1/12 of a bar)
REG_TM2D= beat / 12;
REG_TM2CNT= TM_FREQ_1024 | TM_ENABLE;
// Cascade causes timer 3 to increment after timer 2 overflows (i.e. after 1/12 of a bar has passed)
REG_TM3CNT= TM_ENABLE | TM_CASCADE;
}
void init_sound()
{
// Setup and enable sound settings on the Gameboy with different timbres
REG_SNDSTAT= SSTAT_ENABLE;
REG_SNDDMGCNT= SDMG_BUILD(SDMG_SQR1 | SDMG_NOISE, SDMG_SQR1 | SDMG_SQR2, 7, 7);
REG_SNDDSCNT= SDS_DMG100;
REG_SND1SWEEP= SSW_BUILD(3, 1, 4);
REG_SND1CNT= SSQR_ENV_BUILD(7, 0, 5) | SSQR_DUTY1_2;
REG_SND2CNT= SSQR_ENV_BUILD(7, 0, 2) | SSQR_DUTY1_2;
REG_SND4CNT= SSQR_ENV_BUILD(4, 0, 4);
}
void set_kick_timbre(int timbre)
{
if (timbre == 1)
{
REG_SND1FREQ = SFREQ_RESET;
REG_SND1CNT= SSQR_ENV_BUILD(5, 0, 5) | SSQR_DUTY1_2;
}
else
{
REG_SND1CNT= SSQR_ENV_BUILD(7, 0, 5) | SSQR_DUTY1_2;
}
}
void init_display()
{
// Set the display mode to display a solid background
REG_DISPCNT= DCNT_MODE0 | DCNT_BG0;
tte_init_se_default(0, BG_CBB(0)|BG_SBB(31));
tte_init_con();
}
void play(int note, int octave) {
REG_SND2FREQ = SFREQ_RESET | SND_RATE(note, octave);
}
void play_beat(bool isPlaying) {
if (isPlaying)
{
REG_SND4FREQ = SFREQ_RESET | 0x8032;
}
}
void play_kick(bool isPlaying) {
if (isPlaying)
{
REG_SND1FREQ = SFREQ_RESET | SND_RATE(NOTE_C, -1);
}
}
template<typename S>
auto select_random(const S &s, size_t n) {
auto it = std::begin(s);
std::advance(it,n);
return it;
}
std::vector<int> generatePrimeZero(std::string phrase)
{
// Generate tone rows based on input string ascii values
// Fill the rest of the row with random values
std::vector<int> p_zero;
std::set<int> pitch_set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
for (int i=0; i<12; i++)
{
if (i < phrase.size() && pitch_set.contains(phrase[i] % 12))
{
p_zero.push_back(phrase[i] % 12);
pitch_set.erase(phrase[i] % 12);
}
else
{
if(pitch_set.size() == 0)
break;
auto r = rand() % pitch_set.size();
auto n = *select_random(pitch_set, r);
p_zero.push_back(n);
pitch_set.erase(n);
}
}
return p_zero;
}
void print_status(bool isInverted, bool isRetrograde, int row_index)
{
tte_erase_screen();
init_display();
std::cout << "\n\n\n\n\n\n";
std::cout << "Hello, world!" << "\n";
std::cout << (isInverted ? "Playing: Inverted Row " : "Playing: Prime Row ") << row_index << "\n" << "Retrograde: " << (isRetrograde ? "On" : "Off") << "\n";
}
int main() {
// First timer value (note melody)
int sec = -1;
// Second timer value (percussion)
int secc = -1;
// Note subdivision index
int index = 0;
// Indices and settings for current tone row being played
int row_index = 0;
bool isInverted = false;
bool isRetrograde = false;
// Percussion subdivision index
int indexx = 0;
// note array for the snare
int beat[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0};
// note array for the kick (2 hits)
int kick[12] = {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
// Second kick hit is a lower pitch (different timbre)
int kick_timbre[12] = {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
// Array for octaves of each beat, makes switching octaves convenient
int octaves[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
init_display();
init_metro(70);
init_sound();
std::vector<int> p_zero = generatePrimeZero("Hello world!");
int first_row[p_zero.size()];
int tone_row[p_zero.size()][p_zero.size()];
int i = 0;
int adding[p_zero.size()];
// Calculate the rest of the tone row matrix
// Similar to: https://www.musictheory.net/calculators/matrix
while(i < p_zero.size())
{
first_row[i] = p_zero.at(i);
adding[i] = first_row[0] - first_row[i];
i++;
}
for(int a=0;a<p_zero.size();a++)
{
for(int b=0;b<p_zero.size();b++)
{
if(first_row[b] < adding[a])
{
tone_row[a][b] = (first_row[b] + 12 + adding[a]) % 12;
}
else
{
tone_row[a][b] = (first_row[b] + adding[a]) % 12;
}
if(tone_row[a][b] < 0)
tone_row[a][b] += 12;
}
}
print_status(isInverted, isRetrograde, row_index);
while(1)
{
vid_vsync();
key_poll();
if(key_hit(KEY_A))
{
isInverted = true;
print_status(isInverted, isRetrograde, row_index);
}
if(key_hit(KEY_B))
{
isInverted = false;
print_status(isInverted, isRetrograde, row_index);
}
if(key_hit(KEY_LEFT) && row_index > 0)
{
row_index -= 1;
print_status(isInverted, isRetrograde, row_index);
}
if(key_hit(KEY_RIGHT) && row_index < 11)
{
row_index += 1;
print_status(isInverted, isRetrograde, row_index);
}
if(key_hit(KEY_UP))
{
isRetrograde = true;
print_status(isInverted, isRetrograde, row_index);
}
if(key_hit(KEY_DOWN))
{
isRetrograde = false;
print_status(isInverted, isRetrograde, row_index);
}
// When overflows happen (i.e. when the timer value changes), play each note accordingly
if(REG_TM1D != sec)
{
sec = REG_TM1D;
index = isRetrograde ? 11 - (sec % 12) : sec % p_zero.size();
play(tone_row[isInverted ? index : row_index][isInverted ? row_index : index], 1);
}
if(REG_TM3D != secc)
{
secc = REG_TM3D;
indexx = secc % p_zero.size();
bool hit = beat[indexx];
bool another_hit = kick[indexx];
set_kick_timbre(kick_timbre[indexx]);
play_beat(hit);
play_kick(another_hit);
}
}
return 0;
}