-
Notifications
You must be signed in to change notification settings - Fork 26
/
sound.ino
265 lines (252 loc) · 5.27 KB
/
sound.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "settings.h"
#define NEXT_CHAR rtttl.startposition++; c = (char)readMem(rtttl.startposition); if(c == 0) return 0;
#define NEXT_CHAR_IN_P rtttl.position++; c = (char)readMem(rtttl.startposition + rtttl.position); if(c == 0) return 50;
#define OCTAVE_OFFSET 0
#pragma GCC optimize ("-O2")
#pragma GCC push_options
const int notes[] PROGMEM = { 0,
262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976,
2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951
};
unsigned int millisec = (unsigned int)millis();
struct RTTTL {
uint16_t address;
uint16_t position;
uint16_t startposition;
uint8_t loop;
uint8_t play;
uint8_t default_dur;
uint8_t default_oct;
uint16_t bpm;
uint32_t wholenote;
uint16_t this_tone;
int16_t delay;
uint8_t isPlayed;
};
struct PLAY_TONE {
uint16_t freq;
int16_t time;
};
struct RTTTL rtttl;
struct PLAY_TONE play_tone;
inline void addTone(uint16_t f, uint16_t t){
play_tone.freq = f;
play_tone.time = t;
}
uint8_t loadRtttl(){
uint16_t num;
char c;
rtttl.default_dur = 4;
rtttl.default_oct = 6;
rtttl.bpm = 63;
rtttl.startposition = rtttl.address;
c = readMem(rtttl.startposition);
while(c != ':'){
// ignore name
NEXT_CHAR
}
NEXT_CHAR // skip ':'
// get default duration
if(c == 'd'){
NEXT_CHAR
NEXT_CHAR// skip "d="
num = 0;
while(isdigit(c)){
num = (num * 10) + (c - '0');
NEXT_CHAR
}
if(num > 0)
rtttl.default_dur = num;
NEXT_CHAR // skip comma
}
// get default octave
if(c == 'o'){
NEXT_CHAR
NEXT_CHAR// skip "o="
num = c - '0';
NEXT_CHAR
if(num >= 3 && num <=7)
rtttl.default_oct = num;
NEXT_CHAR // skip comma
}
// get BPM
if(c == 'b'){
NEXT_CHAR
NEXT_CHAR// skip "b="
num = 0;
while(isdigit(c)){
num = (num * 10) + (c - '0');
NEXT_CHAR
}
rtttl.bpm = num;
}
rtttl.wholenote = (60 * 1000 / rtttl.bpm) * 4;
NEXT_CHAR
rtttl.position = 0;
rtttl.delay = 0;
return 1;
}
void setRtttlAddress(uint16_t adr){
rtttl.address = adr;
loadRtttl();
}
void setRtttlLoop(uint8_t loop){
rtttl.loop = loop;
}
void setRtttlPlay(uint8_t play){
if(play == 0){
rtttl.play = 0;
noTone(SOUNDPIN);
}
else if(play == 1){
rtttl.play = 1;
}
else{
rtttl.play = 0;
rtttl.position = 0;
noTone(SOUNDPIN);
}
rtttl.isPlayed = 0;
rtttl.delay = 0;
}
inline void updateRtttl(){
if(rtttl.delay > 0)
rtttl.delay--;
if(play_tone.time > 0)
play_tone.time--;
//play single tone
if(play_tone.time > 0){
if(rtttl.delay <= 0){
tone(SOUNDPIN, play_tone.freq, 128);
rtttl.isPlayed = 0;
return;
}
if(play_tone.time & 1){
tone(SOUNDPIN, play_tone.freq, 128);
rtttl.isPlayed = 0;
return;
}
}
//player
if(rtttl.play == 0){
return;
}
if(rtttl.delay > 0){
if(!rtttl.isPlayed){
rtttl.isPlayed = 1;
tone(SOUNDPIN, rtttl.this_tone, rtttl.delay);
}
return;
}
}
int playRtttl(){
uint16_t num;
uint32_t duration;
uint8_t note;
uint8_t scale;
char c;
//first, get note duration, if available
noTone(SOUNDPIN);
if(rtttl.play == 0 || rtttl.startposition == 0){
return 50;
}
num = 0;
c = (char)readMem(rtttl.startposition + rtttl.position);
if(c == 0){
if(!rtttl.loop){
rtttl.play = 0;
rtttl.isPlayed = 0;
rtttl.this_tone = 0;
rtttl.delay = 0;
}
rtttl.position = 0;
c = (char)readMem(rtttl.startposition + rtttl.position);
}
while(isdigit(c)){
num = (num * 10) + (c - '0');
NEXT_CHAR_IN_P
}
if(num)
duration = rtttl.wholenote / num;
else
duration = rtttl.wholenote / rtttl.default_dur; // we will need to check if we are a dotted note after
//now get the note
note = 0;
switch(c){
case 'c':
case 'C':
note = 1;
break;
case 'd':
case 'D':
note = 3;
break;
case 'e':
case 'E':
note = 5;
break;
case 'f':
case 'F':
note = 6;
break;
case 'g':
case 'G':
note = 8;
break;
case 'a':
case 'A':
note = 10;
break;
case 'b':
case 'B':
note = 12;
break;
case 'p':
case 'P':
default:
note = 0;
}
NEXT_CHAR_IN_P
// now, get optional '#' sharp
if(c == '#'){
note++;
NEXT_CHAR_IN_P
}
// now, get optional '.' dotted note
if(c == '.'){
duration += duration/2;
NEXT_CHAR_IN_P
}
else if(c == ';'){
duration += duration;
NEXT_CHAR_IN_P
}
else if(c == '&'){
duration += duration / 2 * 3;
NEXT_CHAR_IN_P
}
// now, get scale
if(isdigit(c)){
scale = c - '0';
NEXT_CHAR_IN_P
}
else{
scale = rtttl.default_oct;
}
scale += OCTAVE_OFFSET;
if(c == ',')
NEXT_CHAR_IN_P // skip comma for next note (or we may be at the end)
// now play the note
rtttl.delay = duration;
if(note){
rtttl.this_tone = pgm_read_word(¬es[note + ((scale - 4) * 12)]);
tone(SOUNDPIN, rtttl.this_tone, rtttl.delay);
}
else{
rtttl.this_tone = 0;
}
rtttl.isPlayed = 1;
return (duration > 8)?duration - 8 : 0;
}