-
Notifications
You must be signed in to change notification settings - Fork 6
/
NonBlockingRtttl.cpp
342 lines (285 loc) · 7.05 KB
/
NonBlockingRtttl.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// ---------------------------------------------------------------------------
// Created by Antoine Beauchamp based on the code from the ToneLibrary
//
// See "NonBlockingRtttl.h" for purpose, syntax, version history, links, and more.
// ---------------------------------------------------------------------------
#include "Arduino.h"
#include "NonBlockingRtttl.h"
/*********************************************************
* RTTTL Library data
*********************************************************/
namespace rtttl
{
const int notes[] = { 0,
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
//#define isdigit(n) (n >= '0' && n <= '9')
#define OCTAVE_OFFSET 0
const char * buffer = "";
int bufferIndex = -32760;
byte default_dur = 4;
byte default_oct = 5;
int bpm = 63;
long wholenote;
byte pin = -1;
unsigned long noteDelay = 0; //m will always be after which means the last note is done playing
bool playing = false;
//pre-declaration
void nextnote();
// tone() and noTone() are not implemented for Arduino core for the ESP32
// See https://github.com/espressif/arduino-esp32/issues/980
// and https://github.com/espressif/arduino-esp32/issues/1720
#if defined(ESP32)
void noTone(){
ledcWrite(0, 0); // channel, volume
}
void noTone(int pin){
noTone();
}
void tone(int frq) {
ledcWriteTone(0, frq); // channel, freq
ledcWrite(0, 255); // channel, volume
}
void tone(int pin, int frq, int duration){
tone(frq);
}
#endif
void begin(byte iPin, const char * iSongBuffer)
{
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.print("playing: ");
Serial.println(iSongBuffer);
#endif
//init values
pin = iPin;
#if defined(ESP32)
ledcSetup(0, 1000, 10); // resolution always seems to be 10bit, no matter what is given
ledcAttachPin(pin, 0);
#endif
buffer = iSongBuffer;
bufferIndex = 0;
default_dur = 4;
default_oct = 6;
bpm=63;
playing = true;
noteDelay = 0;
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.print("noteDelay=");
Serial.println(noteDelay);
#endif
//stop current note
noTone(pin);
//read buffer until first note
int num;
// format: d=N,o=N,b=NNN:
// find the start (skip name, etc)
while(*buffer != ':') buffer++; // ignore name
buffer++; // skip ':'
// get default duration
if(*buffer == 'd')
{
buffer++; buffer++; // skip "d="
num = 0;
while(isdigit(*buffer))
{
num = (num * 10) + (*buffer++ - '0');
}
if(num > 0) default_dur = num;
buffer++; // skip comma
}
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("ddur: "); Serial.println(default_dur, 10);
#endif
// get default octave
if(*buffer == 'o')
{
buffer++; buffer++; // skip "o="
num = *buffer++ - '0';
if(num >= 3 && num <=7) default_oct = num;
buffer++; // skip comma
}
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("doct: "); Serial.println(default_oct, 10);
#endif
// get BPM
if(*buffer == 'b')
{
buffer++; buffer++; // skip "b="
num = 0;
while(isdigit(*buffer))
{
num = (num * 10) + (*buffer++ - '0');
}
bpm = num;
buffer++; // skip colon
}
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("bpm: "); Serial.println(bpm, 10);
#endif
// BPM usually expresses the number of quarter notes per minute
wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds)
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("wn: "); Serial.println(wholenote, 10);
#endif
}
void nextnote()
{
long duration;
byte note;
byte scale;
//stop current note
noTone(pin);
// first, get note duration, if available
int num = 0;
while(isdigit(*buffer))
{
num = (num * 10) + (*buffer++ - '0');
}
if(num) duration = wholenote / num;
else duration = wholenote / default_dur; // we will need to check if we are a dotted note after
// now get the note
note = 0;
switch(*buffer)
{
case 'c':
note = 1;
break;
case 'd':
note = 3;
break;
case 'e':
note = 5;
break;
case 'f':
note = 6;
break;
case 'g':
note = 8;
break;
case 'a':
note = 10;
break;
case 'b':
note = 12;
break;
case 'p':
default:
note = 0;
}
buffer++;
// now, get optional '#' sharp
if(*buffer == '#')
{
note++;
buffer++;
}
// now, get optional '.' dotted note
if(*buffer == '.')
{
duration += duration/2;
buffer++;
}
// now, get scale
if(isdigit(*buffer))
{
scale = *buffer - '0';
buffer++;
}
else
{
scale = default_oct;
}
scale += OCTAVE_OFFSET;
if(*buffer == ',')
buffer++; // skip comma for next note (or we may be at the end)
// now play the note
if(note)
{
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("Playing: ");
Serial.print(scale, 10); Serial.print(' ');
Serial.print(note, 10); Serial.print(" (");
Serial.print(notes[(scale - 4) * 12 + note], 10);
Serial.print(") ");
Serial.println(duration, 10);
#endif
tone(pin, notes[(scale - 4) * 12 + note], duration);
noteDelay = millis() + (duration+1);
}
else
{
#ifdef RTTTL_NONBLOCKING_INFO
Serial.print("Pausing: ");
Serial.println(duration, 10);
#endif
noteDelay = millis() + (duration);
}
}
void play()
{
//if done playing the song, return
if (!playing)
{
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.println("done playing...");
#endif
return;
}
//are we still playing a note ?
unsigned long m = millis();
if (m < noteDelay)
{
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.println("still playing a note...");
#endif
//wait until the note is completed
return;
}
//ready to play the next note
if (*buffer == '\0')
{
//no more notes. Reached the end of the last note
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.println("end of note...");
#endif
//issue #6: Bug for ESP8266 environment - noTone() not called at end of sound.
//disable sound at the end of a normal playback.
stop();
return; //end of the song
}
else
{
//more notes to play...
#ifdef RTTTL_NONBLOCKING_DEBUG
Serial.println("next note...");
#endif
nextnote();
}
}
void stop()
{
if (playing)
{
//increase song buffer until the end
while (*buffer != '\0')
{
buffer++;
}
//issue #6: Bug for ESP8266 environment - noTone() not called at end of sound.
//disable sound if one abort playback using the stop() command.
noTone(pin);
playing = false;
}
}
bool done()
{
return !playing;
}
bool isPlaying()
{
return playing;
}
}; //namespace