-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainplayer.ino
231 lines (207 loc) · 5 KB
/
mainplayer.ino
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
#define _console gb.display
#define _debug Serial
struct NoteEvent
{
uint32_t time;
uint8_t type;
uint8_t note;
};
uint8_t channelFreq[NUM_CHANNELS];
bool noteOnEvent(uint8_t note)
{
for (int ch = 0; ch < NUM_CHANNELS; ch++)
{
if (channelFreq[ch] == 0)
{
sound.playNote(ch, VOLUME_CHANNEL_MAX / 2, note - 9);
channelFreq[ch] = note;
return true;
}
}
return false;
}
void clearChannel(uint8_t channel)
{
sound.stopNote(channel);
channelFreq[channel] = 0;
}
void panic()
{
for (int ch = 0; ch < NUM_CHANNELS; ch++)
{
clearChannel(ch);
}
}
bool noteOffEvent(uint8_t note)
{
for (int ch = 0; ch < NUM_CHANNELS; ch++)
{
int chb = NUM_CHANNELS - 1 - ch;
if (channelFreq[chb] == note)
{
clearChannel(chb);
channelFreq[chb] = 0;
return true;
}
}
return false;
}
uint32_t get_time()
{
return micros();
//return sound.micros() / 1000;
}
inline void printlnOkFailed(bool test)
{
if (test)
{
_console.println(F("ok"));
} else {
_console.println(F("failed"));
}
}
uint16_t currentEvent;
uint32_t t;
uint32_t eventTime;
uint32_t nextEventTime = 0;
//Pulses per quarter note (PPQ)
uint32_t ppq; //https://www.recordingblogs.com/wiki/time-division-of-a-midi-file
//microseconds per beat
uint32_t uspb = 500000;
//microseconds per tick
uint32_t uspt;
void eventPlayer()
{
uspt = uspb / ppq;
midiEvent currentEventData;
t = get_time();
while (midi_file.findNextNoteEvent(file, currentEventData))
{
nextEventTime = nextEventTime + (currentEventData.delta * uspt);
//Serial.print(nextEventTime);
//Serial.print(F("-"));
//Serial.print(currentEventData.event_type);
//Serial.print(F("-"));
//Serial.println(currentEventData.data[0]);
while ((eventTime = get_time() - t) < nextEventTime)
{
_console.clear();
_console.println(sound.PWM_INTERRUPT_FREQUENCY);
gb.buttons.update();
if(gb.buttons.repeat(BTN_UP,2)) sound.PWM_INTERRUPT_FREQUENCY+=1;
if(gb.buttons.repeat(BTN_DOWN,2)) sound.PWM_INTERRUPT_FREQUENCY-=1;
//_console.println(eventTime);
//_console.println(nextEventTime);
for (int ch = 0; ch < NUM_CHANNELS; ch++)
{
_console.println(channelFreq[ch]);
}
_console.update();
}
uint8_t event_status_type = ((currentEventData.event_status >> 4) & 0xF);
if (event_status_type == midiNoteOn && currentEventData.data[1] == 0) event_status_type == midiNoteOff;
switch (event_status_type)
{
case midiNoteOff:
Serial.print("NoteOff");
Serial.println(currentEventData.data[0]);
noteOffEvent(currentEventData.data[0]);
break;
case midiNoteOn:
Serial.print("NoteOn");
Serial.println(currentEventData.data[0]);
noteOnEvent(currentEventData.data[0]);
break;
default:
break;
}
// for (int ch = 0; ch < NUM_CHANNELS; ch++)
// {
// Serial.print("\t");
// Serial.print(channelFreq[ch]);
// }
// Serial.println();
}
}
void executeEvents()
{
_console.print(F("loading "));
while (!gb.update()) {}
//file.printName(&(_console));
_console.println();
_console.print(F("midi "));
bool midi_ok = midi_file.loadFile(file);
printlnOkFailed(midi_ok);
_console.print(F("midi format "));
_console.println(midi_file.format());
_console.print(F("midi division "));
_console.println(midi_file.time_division());
ppq = midi_file.time_division();
_console.print(midi_file.readable_tracks());
_console.print(F("/"));
_console.print(midi_file.tracks());
_console.println(F(" tracks readable"));
while (!gb.update()) {}
do {
delay(50);
gb.buttons.update();
} while (!gb.buttons.pressed(BTN_A));
if (midi_ok)
{
//eventChecker();
eventPlayer();
}
Serial.println(F("midi done"));
_console.println(F("midi done"));
while (!gb.update()) {}
do {
delay(50);
gb.buttons.update();
} while (!gb.buttons.pressed(BTN_A));
//sound.test();
/*
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Hello, world!");
display.setTextColor(WHITE, BLACK); // 'inverted' text
display.println(3.141592);
display.setTextSize(2);
display.setTextColor(BLACK);
currentEvent = 0;
t = get_time();
while (true)
{
eventTime = get_time() - t;
//Serial.print(eventTime);
//Serial.print("\t");
//Serial.println(currentEvent);
NoteEvent thisEvent;
if (eventTime >= thisEvent.time)
{
switch (thisEvent.type)
{
case 0:
Serial.print("NoteOff");
noteOffEvent(thisEvent.note);
break;
case 1:
Serial.print("NoteOn");
noteOnEvent(thisEvent.note);
break;
}
if (++currentEvent >= num_events)
{
return;
}
for (int ch = 0; ch < NUM_CHANNELS; ch++)
{
Serial.print("\t");
Serial.print(channelFreq[ch]);
}
Serial.println();
}
}
*/
panic();
}