-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeensyTracks.h
More file actions
465 lines (424 loc) · 13.8 KB
/
Copy pathTeensyTracks.h
File metadata and controls
465 lines (424 loc) · 13.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* TeensyTracks
* Copyright (c) 2016, Colin Duffy, https://github.com/duff2013
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TeensyTracks_h_
#define TeensyTracks_h_
#include "Arduino.h"
#include "utility/thread.h"
enum keys {C, Cs, D, Ef, E, F, Fs, G, Gs, A, Bf, B};
class Track;
class MasterTrack;
#define WHOLE_BAR 0
#define HALF_BAR 1
#define QUARTER_BAR 2
#define EIGHTH_BAR 3
#define SIXTEENTH_BAR 4
#define THIRTY_SECOND_BAR 5
#define QUATER_TRIPLET_BAR 6
#define EIGHTH_TRIPLET_BAR 7
#define MASTER 0
#define ON_BEAT 1
#define ON_BAR 2
#define FREE_RUN 3
#define MASTER_TRACK Track &p
/**
* This is the delays that keep playing instruments or whatever in time with each other.
* User must supply the defined measure delays above WHOLE_BAR... etc.
*
* @param MASTER_TRACK MasterTrack class, this is so we can use the trackDelayStart and
* trackDelayEnd functions in the MasterTrack class.
* @param x WHOLE_BAR,HALF_BAR,QUARTER_BAR,EIGHTH_BAR,SIXTEENTH_BAR,THIRTY_SECOND_BAR
* QUATER_TRIPLET_BAR,EIGHTH_TRIPLET_BAR
*
* @return nothing
*/
#define TRACK_DELAY( MASTER_TRACK, x ) \
for ( uint8_t __ToDo = MASTER_TRACK.trackDelayStart( ); __ToDo; __ToDo = MASTER_TRACK.trackDelayEnd( x ) )
/**
* Only one Master Track can be used, all Track Classes inherit this class.
* TODO: make it a singleton!
*/
class MasterTrack {
public:
MasterTrack( uint16_t tempo, keys trackKey, uint8_t bars, uint8_t sigHigh, uint8_t sigLow ) {
beatTime = 60000000.0 / tempo;
timeSignatureHigh = sigHigh;
timeSignatureLow = sigLow;
key = trackKey;
measures = bars;
volume = 0;
currentBar = 0;
currentBeat = timeSignatureHigh - 1;
wholeBarMulitplier = beatTime * timeSignatureHigh;
halfBarMulitplier = wholeBarMulitplier/2;
quarterBarMulitplier = wholeBarMulitplier/4;
eighthBarMulitplier = wholeBarMulitplier/8;
sixteenthBarMulitplier = wholeBarMulitplier/16;
thritySecondBarMulitplier = wholeBarMulitplier/32;
quarterTripletBarMulitplier = quarterBarMulitplier * 0.66666666666667f;
eighthTripletBarMulitplier = eighthBarMulitplier * 0.66666666666667f;
count = 0;
isBeat = false;
isBar = false;
threadRunType = MASTER;
}
/**
* Start IntervalTimer and all tracks
*/
virtual void begin( void );
/**
* Call track threads at if configured for
* bar or beat run timing.
*
* @param arg none
*/
static void masterThread(void *arg);
/**
* Pause Master Track and Tracks Threads
*/
virtual void pause( void ) {
isBeat = false;
while ( !isBeat ) yield( );
__disable_irq( );
MasterTimer.end( );
__enable_irq( );
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->pause( );
}
}
/**
* Resume Master Track and Track Threads
*/
virtual void resume( void ) {
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->resume( );
}
MasterTimer.begin( BeatTrack, beatTime );
}
/**
* Restart IntervalTimer and Track Threads
*/
virtual void restart( void ) {
__disable_irq( );
MasterTimer.end( );
volume = 0;
currentBar = 0;
currentBeat = timeSignatureHigh - 1;
__enable_irq( );
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->restart( );
}
MasterTimer.begin( BeatTrack, beatTime );
}
/**
* Stop all threads - puts them in a returned state
*/
virtual void stop( void ) {
__disable_irq( );
MasterTimer.end( );
__enable_irq( );
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->stop( );
}
}
/**
* change tempo of the Master Track, all tracks will follow
*
* @param bpm beat per minute
*/
void tempo( uint16_t bpm ) {
isBeat = false;
while ( !isBeat ) yield( );
//__disable_irq( );
MasterTimer.end( );
beatTime = 60000000.0 / bpm;
wholeBarMulitplier = beatTime * timeSignatureHigh;
halfBarMulitplier = wholeBarMulitplier/2;
quarterBarMulitplier = wholeBarMulitplier/4;
eighthBarMulitplier = wholeBarMulitplier/8;
sixteenthBarMulitplier = wholeBarMulitplier/16;
thritySecondBarMulitplier = wholeBarMulitplier/32;
quarterTripletBarMulitplier = quarterBarMulitplier * 0.66666666666667f;
eighthTripletBarMulitplier = eighthBarMulitplier * 0.66666666666667f;
//__enable_irq( );
MasterTimer.begin( BeatTrack, beatTime );
}
/**
* Rewind all Tracks // Not implemented yet
*
* @param bars - Number of bars to rewind
*
* @return bar
*/
int16_t rewind( uint16_t bars = 0 ) {
return currentBar;
/*uint16_t cbeat;
int16_t cbar;
__disable_irq( );
MasterTimer.end( );
__enable_irq( );
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->isBeat = false;
p->isBar = false;
}
cbar = currentBar;
if ( bars == 0 ) {
cbeat = timeSignatureHigh - 1;
cbar -= 1;
if ( cbar <= 0 ) cbar = measures;
currentBeat = cbeat;
currentBar = cbar;
count = 16;
}
else {
cbeat = timeSignatureHigh - 1;
cbar -= bars;
if ( cbar <= 0 ) cbar = measures;
currentBeat = cbeat;
currentBar = cbar;
count = 16;
}
MasterTimer.begin( BeatTrack, beatTime/32 );
cbar += 1;
if ( cbar > measures ) {
cbar = 1;
}
return cbar;*/
}
/**
* Fastforward all Tracks // Not implemented yet
*
* @param bars - Number of bars to fastfoward
*
* @return new bar
*/
int16_t fastForward( uint16_t bars = 0 ) {
return currentBar;
/*uint16_t cbeat;
int16_t cbar;
__disable_irq( );
MasterTimer.end( );
__enable_irq( );
MasterTrack *p;
for ( p = MasterTrack::first_track; p; p = p->next_track ) {
p->isBeat = false;
p->isBar = false;
}
cbar = currentBar;
if ( bars == 0 ) {
cbeat = timeSignatureHigh - 1;
cbar += 1;
if ( cbar > measures ) cbar = 0;
currentBeat = cbeat;
currentBar = cbar;
count = 16;
}
else {
cbeat = timeSignatureHigh - 1;
cbar += bars;
if ( cbar > measures ) cbar = measures;
currentBeat = cbeat;
currentBar = cbar;
count = 16;
}
MasterTimer.begin( BeatTrack, beatTime/32 );
cbar += 1;
if ( cbar > measures ) {
cbar = 1;
}
return cbar;*/
}
/**
* Used by the TRACK_DELAY Macro, this starts the timer before any instrument is played.
*
* @return true for the Macro
*/
uint8_t trackDelayStart( void ) {
innerBeatTime = 0;
/*if ( currentBeat == 1 ) {
//Serial.println(barMicros);
//innerBeatTime = barMicros;
} else {
//innerBeatTime = 0;
}*/
return 1;
}
/**
* Used by the TRACK_DELAY Macro, called after instrument is played, this delays the next
* instrument so they play in time.
*
* @param duration how long to delay, usually use the 1/32, 1/16, 1/8, 1/4, 1/2, 1 beats
* of the measure.
*
* @return false for the Macro
*/
uint8_t trackDelayEnd( unsigned long duration ) {
unsigned long delayLength;
switch ( duration ) {
case WHOLE_BAR:
delayLength = wholeBarMulitplier;
break;
case HALF_BAR:
delayLength = halfBarMulitplier;
break;
case QUARTER_BAR:
delayLength = quarterBarMulitplier;
break;
case EIGHTH_BAR:
delayLength = eighthBarMulitplier;
break;
case SIXTEENTH_BAR:
delayLength = sixteenthBarMulitplier;
break;
case THIRTY_SECOND_BAR:
delayLength = thritySecondBarMulitplier;
break;
case QUATER_TRIPLET_BAR:
delayLength = quarterTripletBarMulitplier;
break;
case EIGHTH_TRIPLET_BAR:
delayLength = eighthTripletBarMulitplier;
break;
default:
return 0;
break;
}
while ( innerBeatTime < delayLength ) yield( );
return 0;
}
protected:
MasterTrack( void ) {
if (first_track == NULL) {
first_track = this;
} else {
MasterTrack *p;
for ( p = first_track; p->next_track; p = p->next_track ) ;
p->next_track = this;
}
next_track = NULL;
}
uint8_t threadRunType;
/**
* This sets up the Track Threads, the main thread is the loop function.
* Set at 2000 bytes but it might need to increase depending how many local
* variables are defined in the loop function.
*/
static Thread masterTrackThreadClass;// main task
static volatile uint16_t currentBar;
static volatile uint16_t currentBeat;
private:
MasterTrack *next_track;
static MasterTrack *first_track;
elapsedMicros innerBeatTime;
//static elapsedMicros beatMicros;
//static elapsedMicros barMicros;
static IntervalTimer MasterTimer;
static void BeatTrack( void );
static float volume;
static float beatTime;
static keys key;
static volatile uint8_t count;
static volatile uint8_t timeSignatureHigh;
static volatile uint8_t timeSignatureLow;
static volatile uint16_t measures;
static unsigned long wholeBarMulitplier;
static unsigned long halfBarMulitplier;
static unsigned long quarterBarMulitplier;
static unsigned long eighthBarMulitplier;
static unsigned long sixteenthBarMulitplier;
static unsigned long thritySecondBarMulitplier;
static unsigned long quarterTripletBarMulitplier;
static unsigned long eighthTripletBarMulitplier;
static volatile bool isBeat;
static volatile bool isBar;
};
// ****************************************************************************************** //
/**
* Inherits from the Master Track that does the time keeping.
*/
class Track : public MasterTrack {
private:
using MasterTrack::tempo;
using MasterTrack::rewind;
using MasterTrack::fastForward;
typedef void ( * task_func_t )( void *arg );
task_func_t trackThread;
uint16_t threadSize;
/**
* Restarts the Track thread from the beginning.
*/
void restart( void ) {
masterTrackThreadClass.restart( trackThread );
}
/**
* Returns the Track thread.
*/
void stop( void ) {
masterTrackThreadClass.stop( trackThread );
}
/**
* Creates and starts a Track Thread, these run independently of each other
*/
void begin( void ) {
masterTrackThreadClass.create( trackThread, threadSize, 0, 1 );
}
public:
/**
* Constructer to setup Track threads and their sizes.
*/
Track( task_func_t track, uint8_t runType, uint16_t size = 1000 ) {
threadRunType = runType;
trackThread = track;
threadSize = size;
}
/**
* Pauses the Track thread where it is.
*/
void pause( void ) {
masterTrackThreadClass.pause( trackThread );
}
/**
* Resumes the Track thread where it was paused.
*/
void resume( void ) {
masterTrackThreadClass.resume( trackThread );
}
/**
*
*/
uint8_t getBar( void ) {
uint16_t bar = currentBar;
return bar;
}
/**
*
*/
uint8_t getBeat( void ) {
uint16_t beat = currentBeat;
return beat;
}
};
#endif /* defined(TeensyTracks_h_) */