-
Notifications
You must be signed in to change notification settings - Fork 7
/
demotune.cpp
219 lines (179 loc) · 5.84 KB
/
demotune.cpp
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
/**
* This is a demonstration sketch for the OPL2 Audio Board. It demonstrates playing a little tune on 3 channels using
* a piano from the MIDI instrument defenitions.
*
* Code by Maarten Janssen (maarten@cheerful.nl) 2016-04-13
* Most recent version of the library can be found at my GitHub: https://github.com/DhrBaksteen/ArduinoOPL2
*/
/**
* Hacked for a OPL2LPT test program by pdewacht@gmail.com.
*/
#include "OPL2.h"
#include "midi_instruments.h"
#include "timer.h"
void parseTune(struct Tune *tune);
void parseNote(struct Tune *tune);
int parseDuration(struct Tune *tune);
int parseNumber(struct Tune *tune);
const byte noteDefs[21] = {
NOTE_A, NOTE_A - 1, NOTE_A + 1,
NOTE_B, NOTE_B - 1, NOTE_B + 1,
NOTE_C, 0/*FIXME*/, NOTE_C + 1,
NOTE_D, NOTE_D - 1, NOTE_D + 1,
NOTE_E, NOTE_E - 1, NOTE_E + 1,
NOTE_F, NOTE_F - 1, NOTE_F + 1,
NOTE_G, NOTE_G - 1, NOTE_G + 1
};
unsigned tempo;
struct Tune {
const char *data;
int channel;
int octave;
int noteDuration;
int noteLength;
unsigned long nextNoteTime;
unsigned long releaseTime;
int index;
};
const char *tuneData[3] = {
"t150m200o5l8egredgrdcerc<b>er<ba>a<a>agdefefedr4.regredgrdcerc<b>er<ba>a<a>agdedcr4.c<g>cea>cr<ag>cr<gfarfearedgrdcfrc<bagab>cdfegredgrdcerc<b>er<ba>a<a>agdedcr4.cro3c2",
"m85o3l8crer<br>dr<ar>cr<grbrfr>cr<grbr>crer<gb>dgcrer<br>dr<ar>cr<grbrfr>cr<grbr>ceger4.rfrafergedrfdcrec<br>d<bar>c<agrgd<gr4.o4crer<br>dr<ar>cr<grbrfr>cr<grbr>cege",
"m85o3l8r4gr4.gr4.er4.err4fr4.gr4.gr4.grr4gr4.er4.er4.frr4gr4>ccr4ccr4<aarraar4ggr4ffr4.ro4gab>dr4.r<gr4.gr4.err4er4.fr4.g"
};
OPL2 opl2;
struct Tune music[3];
void music_setup() {
tempo = 120;
// Initialize 3 channels of the tune.
for (int i = 0; i < 3; i ++) {
struct Tune channel;
channel.data = tuneData[i];
channel.channel = i;
channel.octave = 4;
channel.noteDuration = 4;
channel.noteLength = 85;
channel.releaseTime = 0;
channel.nextNoteTime = timer_get();
channel.index = 0;
music[i] = channel;
}
// Setup channels 0, 1 and 2.
opl2.setInstrument(0, PIANO1);
opl2.setBlock (0, 5);
opl2.setInstrument(1, PIANO1);
opl2.setBlock (1, 4);
opl2.setInstrument(2, PIANO1);
opl2.setBlock (2, 4);
timer_setup(100);
}
void music_shutdown() {
timer_shutdown();
for (int i = 0; i < 3; i ++) {
if (opl2.getKeyOn(music[i].channel)) {
opl2.setKeyOn(music[i].channel, false);
}
}
}
void music_loop() {
bool busy = false;
for (int i = 0; i < 3; i ++) {
if (timer_get() >= music[i].releaseTime && opl2.getKeyOn(music[i].channel)) {
opl2.setKeyOn(music[i].channel, false);
}
if (timer_get() >= music[i].nextNoteTime && music[i].data[music[i].index] != 0) {
parseTune(&music[i]);
}
if (music[i].data[music[i].index] != 0 || timer_get() < music[i].nextNoteTime) {
busy = true;
}
}
if (!busy) {
for (int i = 0; i < 3; i ++) {
music[i].index = 0;
music[i].nextNoteTime = timer_get() + 500;
}
}
hlt();
}
void parseTune(struct Tune *tune) {
while (tune->data[tune->index] != 0) {
// Decrease octave if greater than 1.
if (tune->data[tune->index] == '<' && tune->octave > 1) {
tune->octave --;
// Increase octave if less than 7.
} else if (tune->data[tune->index] == '>' && tune->octave < 7) {
tune->octave ++;
// Set octave.
} else if (tune->data[tune->index] == 'o' && tune->data[tune->index + 1] >= '1' && tune->data[tune->index + 1] <= '7') {
tune->octave = tune->data[++ tune->index] - 48;
// Set default note duration.
} else if (tune->data[tune->index] == 'l') {
tune->index ++;
int duration = parseNumber(tune);
if (duration != 0) tune->noteDuration = duration;
// Set note length in percent.
} else if (tune->data[tune->index] == 'm') {
tune->index ++;
tune->noteLength = parseNumber(tune);
// Set song tempo.
} else if (tune->data[tune->index] == 't') {
tune->index ++;
tempo = parseNumber(tune);
// Pause.
} else if (tune->data[tune->index] == 'p' || tune->data[tune->index] == 'r') {
tune->index ++;
tune->nextNoteTime = timer_get() + parseDuration(tune);
break;
// Next character is a note A..G so play it.
} else if (tune->data[tune->index] >= 'a' && tune->data[tune->index] <= 'g') {
parseNote(tune);
break;
}
tune->index ++;
}
}
void parseNote(struct Tune *tune) {
// Get index of note in base frequenct table.
unsigned char note = (tune->data[tune->index ++] - 97) * 3;
if (tune->data[tune->index] == '-') {
note ++;
tune->index ++;
} else if (tune->data[tune->index] == '+') {
note += 2;
tune->index ++;
}
// Get duration, set delay and play note.
int duration = parseDuration(tune);
tune->nextNoteTime = timer_get() + duration;
tune->releaseTime = timer_get() + ((long)duration * tune->noteLength / 100);
opl2.setKeyOn(tune->channel, false);
opl2.setFrequency(tune->channel, opl2.getNoteFrequency(tune->channel, tune->octave, noteDefs[note]));
opl2.setKeyOn(tune->channel, true);
}
int parseDuration(struct Tune *tune) {
unsigned char duration = parseNumber(tune);
if (duration == 0) {
duration = tune->noteDuration;
}
// See whether we need to double the duration
unsigned char base;
if (tune->data[tune->index + 1] == '.') {
++ tune->index;
base = 6;
} else {
base = 4;
}
// Calculate note duration in timer ticks (0.01s)
int ticks = 6000U * base / duration / tempo;
return ticks;
}
int parseNumber(struct Tune *tune) {
int number = 0;
if (tune->data[tune->index] != 0 && tune->data[tune->index] >= '0' && tune->data[tune->index] <= '9') {
while (tune->data[tune->index] != 0 && tune->data[tune->index] >= '0' && tune->data[tune->index] <= '9') {
number = number * 10 + (tune->data[tune->index ++] - 48);
}
tune->index --;
}
return number;
}