-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtracks.c
513 lines (424 loc) · 12 KB
/
tracks.c
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT license.
*/
#include <stdlib.h>
#include <math.h>
#include "dsp.h"
#include "note_macros.h"
#define CLIP_MAXVOICES 8 /* max voices in a clip */
#define DEFAULT_VOICES 4 /*default number of voices */
#define DB2LIN(A) pow(10, (double)(A)/20)
#define LIN2DB(A) (20*log10((double)A))
#define CLIP_MAXLEN 64
enum {
WHISPER_PLEASE_LAUNCH,
WHISPER_PLAYING,
WHISPER_PLAYING_AND_QUEUED,
WHISPER_NOT_PLAYING,
WHISPER_CLIP_ENDED
};
struct whisper_track {
void (*init)(int);
void (*tick)(SPFLOAT *sample);
void (*destroy)(void);
void (*set_note)(int, int);
void (*noteon)(int);
void (*noteoff)(int);
void *ud;
int mode;
int current_clip;
int next_clip;
whisper_clip *clip;
whisper_clip *clipspace[WHISPER_NCLIPS];
/* TODO: embed counter and ticker into layer above? */
int counter;
int ticker;
int loopmode;
SPFLOAT gain;
int row_id;
unsigned int timer;
unsigned char modflag;
};
static whisper_track tracks[WHISPER_NTRACKS];
static void nothing_param(int sr) {}
static void nothing_param2(int foo, int bar) {}
static void nothing_tick(SPFLOAT *sample) { *sample = 0.f; }
static void nothing_destroy(void) {}
void whisper_tracks_init(int sr)
{
int t, c;
for(t = 0; t < WHISPER_NTRACKS; t++) {
tracks[t].init = nothing_param;
tracks[t].tick = nothing_tick;
tracks[t].destroy = nothing_destroy;
tracks[t].set_note = nothing_param2;
tracks[t].noteon = nothing_param;
tracks[t].noteoff = nothing_param;
/* create clipspace */
for(c = 0; c < WHISPER_NCLIPS; c++) {
wclip_create(&tracks[t].clipspace[c], CLIP_MAXLEN, CLIP_MAXVOICES);
wclip_set_nvoices(tracks[t].clipspace[c], DEFAULT_VOICES);
}
tracks[t].mode = WHISPER_NOT_PLAYING;
tracks[t].next_clip = -2;
tracks[t].current_clip = -1;
tracks[t].counter = 0;
tracks[t].ticker = 0;
/* TODO: is this needed? */
/* tracks[t].init(sr); */
tracks[t].clip = tracks[t].clipspace[0];
tracks[t].loopmode = 0;
tracks[t].gain = 1.0;
tracks[t].row_id = -1.0;
tracks[t].timer = 0;
tracks[t].modflag = 0;
}
}
void whisper_tracks_init_instruments(int sr)
{
int t;
for(t = 0; t < WHISPER_NTRACKS; t++) {
tracks[t].init(sr);
}
}
void whisper_tracks_destroy(void)
{
int t, c;
for(t = 0; t < WHISPER_NTRACKS; t++) {
tracks[t].destroy();
for(c = 0; c < WHISPER_NCLIPS; c++) {
wclip_destroy(&tracks[t].clipspace[c]);
}
}
}
static void tracks_step(whisper_track *track)
{
int v;
int turnoff;
int arg;
int nvoices;
nvoices = wclip_get_nvoices(track->clip);
/* check for any noteoffs at tick time */
for(v = 0; v < nvoices; v++) {
arg = wclip_get_arg(track->clip, v, 2);
turnoff = (arg == track->ticker) &&
wclip_get_gate(track->clip, v) != 0 &&
arg > 0;
if(turnoff) {
track->noteoff(v);
}
}
if(track->ticker == 0) {
for(v = 0; v < nvoices; v++) {
if(wclip_get_gate(track->clip, v) == 1) {
track->set_note(v, wclip_get_note(track->clip, v));
if(wclip_get_raw_note(track->clip, v) != PREV_NOTE) {
track->noteon(v);
}
} else if(wclip_get_gate(track->clip, v) == 2) {
track->set_note(v,
wclip_get_note(track->clip, v));
} else {
track->noteoff(v);
}
}
if(wclip_step(track->clip)) {
track->timer = 0;
if(track->mode == WHISPER_PLAYING && track->loopmode == 0) {
track->mode = WHISPER_CLIP_ENDED;
} else if(track->mode == WHISPER_PLAYING_AND_QUEUED) {
track->mode = WHISPER_PLEASE_LAUNCH;
}
}
}
}
void whisper_tracks_tick_track(SPFLOAT *clock, SPFLOAT *sample, int tracknum)
{
SPFLOAT tmp;
int v;
int loop_check;
whisper_track *trk;
int nvoices;
tmp = 0.f;
*sample = 0.f;
trk = &tracks[tracknum];
if(*clock != 0.f) {
if(trk->ticker == 0) {
if(trk->mode == WHISPER_CLIP_ENDED) {
trk->mode = WHISPER_NOT_PLAYING;
/* kill switch: turn off all voices in current clip*/
nvoices = wclip_get_nvoices(trk->clip);
for(v = 0; v < nvoices; v++) trk->noteoff(v);
trk->current_clip = -1;
trk->next_clip = -2;
}
if(trk->mode == WHISPER_PLEASE_LAUNCH &&
trk->counter == 0) {
/* special condition: looping clips can be turned off if
* they are launched again */
loop_check = trk->loopmode &&
(trk->current_clip == trk->next_clip);
if(loop_check) {
trk->mode = WHISPER_CLIP_ENDED;
/* zero out next/current clips */
trk->current_clip = -1;
trk->next_clip = -2;
} else {
trk->current_clip = trk->next_clip;
/* zero out next clip */
trk->next_clip = -2;
trk->clip = trk->clipspace[trk->current_clip];
trk->mode = WHISPER_PLAYING;
wclip_rewind(trk->clip);
trk->timer = 0;
}
}
}
if(trk->mode == WHISPER_PLAYING ||
trk->mode == WHISPER_PLAYING_AND_QUEUED) {
trk->timer++;
tracks_step(trk);
}
/* TODO: put this under the bigger conditional */
if(trk->ticker == 0) {
trk->counter++;
trk->counter %= 16;
}
trk->ticker++;
trk->ticker %= WHISPER_NTICKS;
}
trk->tick(&tmp);
*sample += tmp * trk->gain;
}
void whisper_tracks_tick(SPFLOAT *clock, SPFLOAT *sample)
{
SPFLOAT tmp;
int t;
tmp = 0.f;
*sample = 0.f;
for(t = 0; t < WHISPER_NTRACKS; t++) {
whisper_tracks_tick_track(clock, &tmp, t);
*sample += tmp;
}
}
EXPORT void whisper_schedule(int track, int clip)
{
whisper_track *t;
t = &tracks[track];
if(t->mode == WHISPER_PLAYING || t->mode == WHISPER_PLAYING_AND_QUEUED) {
t->mode = WHISPER_PLAYING_AND_QUEUED;
} else {
t->mode = WHISPER_PLEASE_LAUNCH;
}
t->next_clip = clip;
/* set the currently played clip to be in the editor */
whisper_eyejam_edit_set_track(track);
whisper_eyejam_edit_set_clip(clip);
}
EXPORT void whisper_clip_set_note(int track, int clip, int pos, int voice, int nn)
{
whisper_track *t;
whisper_clip *c;
t = &tracks[track];
c = t->clipspace[clip];
wclip_set_note(c, pos, voice, (unsigned char)nn);
}
EXPORT void whisper_clip_set_gate(int track, int clip, int pos, int voice, int gate)
{
whisper_track *t;
whisper_clip *c;
t = &tracks[track];
c = t->clipspace[clip];
wclip_set_gate(c, pos, voice, (unsigned char)gate);
}
EXPORT void whisper_clip_set_arg(int track, int clip, int pos, int voice, int arg, int value)
{
whisper_track *t;
whisper_clip *c;
t = &tracks[track];
c = t->clipspace[clip];
whisper_clip_modified(track, clip);
wclip_set_arg(c, pos, voice, arg, (unsigned char)value);
}
EXPORT void whisper_clip_set_length(int track, int clip, int length)
{
whisper_track *t;
whisper_clip *c;
t = &tracks[track];
c = t->clipspace[clip];
if(wclip_get_length(c) != length) {
whisper_clip_modified(track, clip);
wclip_set_length(c, (unsigned int)length);
}
}
EXPORT void whisper_clip_set_nvoices(int track, int clip, int nvoices)
{
whisper_track *t;
whisper_clip *c;
t = &tracks[track];
c = t->clipspace[clip];
wclip_set_nvoices(c, (unsigned int)nvoices);
}
void whisper_tracks_set_init(int track, void (*fun)(int))
{
tracks[track].init = fun;
}
void whisper_tracks_set_tick(int track, void (*fun)(SPFLOAT *))
{
tracks[track].tick = fun;
}
void whisper_tracks_set_destroy(int track, void (*fun)(void))
{
tracks[track].destroy = fun;
}
void whisper_tracks_set_set_note(int track, void (*fun)(int, int))
{
tracks[track].set_note = fun;
}
void whisper_tracks_set_noteon(int track, void (*fun)(int))
{
tracks[track].noteon = fun;
}
void whisper_tracks_set_noteoff(int track, void (*fun)(int))
{
tracks[track].noteoff = fun;
}
EXPORT void whisper_tracks_loopmode(int track, int mode)
{
tracks[track].loopmode = mode;
}
EXPORT void whisper_tracks_gain(int track, SPFLOAT gain)
{
tracks[track].gain = DB2LIN(gain);
}
EXPORT int whisper_tracks_current_clip(int track)
{
return tracks[track].current_clip;
}
EXPORT int whisper_tracks_next_clip(int track)
{
return tracks[track].next_clip;
}
whisper_clip * whisper_clip_get(int track, int clip)
{
return tracks[track].clipspace[clip];
}
EXPORT int whisper_tracks_get_row_id(int track)
{
return tracks[track].row_id;
}
EXPORT void whisper_tracks_set_row_id(int track, int id)
{
tracks[track].row_id = id;
}
EXPORT int whisper_tracks_get_clip_row_id(int track, int clip)
{
return wclip_get_row_id(tracks[track].clipspace[clip]);
}
EXPORT void whisper_tracks_set_clip_row_id(int track, int clip, int id)
{
wclip_set_row_id(tracks[track].clipspace[clip], id);
}
whisper_track * whisper_tracks_global_data()
{
return tracks;
}
whisper_clip * wtrack_get_clip(whisper_track *track, int clip)
{
return track->clipspace[clip];
}
whisper_track * whisper_tracks_get_track(int track)
{
return &tracks[track];
}
SPFLOAT wtrack_get_gain(whisper_track *track)
{
return LIN2DB(track->gain);
}
int wtrack_get_loopmode(whisper_track *track)
{
return track->loopmode;
}
EXPORT void whisper_tracks_turnoff(int track)
{
tracks[track].mode = WHISPER_CLIP_ENDED;
}
EXPORT void whisper_clip_clear(int track, int clip)
{
whisper_clip_modified(track, clip);
wclip_clear(tracks[track].clipspace[clip]);
}
EXPORT void whisper_clip_clear_note(int track, int clip, int voice, int pos)
{
whisper_clip_modified(track, clip);
wclip_clear_note(tracks[track].clipspace[clip], voice, pos);
}
EXPORT double whisper_tracks_get_playhead(int track)
{
unsigned int pos;
unsigned int cliplen;
double val;
cliplen = wclip_get_length(tracks[track].clip) * WHISPER_NTICKS;
pos = tracks[track].timer;
val = (double) pos / cliplen;
return val;
}
EXPORT int whisper_tracks_am_i_alive(int track)
{
int mode = tracks[track].mode;
int rc;
mode = tracks[track].mode;
rc =
(mode == WHISPER_PLAYING) ||
(mode == WHISPER_PLAYING_AND_QUEUED) ||
(mode == WHISPER_PLEASE_LAUNCH);
return rc;
}
EXPORT void whisper_track_modified(int track)
{
wtrack_modified(&tracks[track]);
}
void wtrack_modified(whisper_track *track)
{
track->modflag = 1;
}
int wtrack_is_modified(whisper_track *track)
{
return track->modflag;
}
EXPORT int whisper_track_is_modified(int track)
{
return wtrack_is_modified(&tracks[track]);
}
EXPORT void whisper_clip_modified(int track, int clip)
{
whisper_clip *c;
c = whisper_clip_get(track, clip);
wclip_set_modflag(c);
whisper_track_modified(track);
}
EXPORT int whisper_tracks_get_loopmode(int track)
{
whisper_track *t;
t = whisper_tracks_get_track(track);
return wtrack_get_loopmode(t);
}
/* zeros out all whisper tracks data and mod flags. */
EXPORT void whisper_tracks_reset(void)
{
int t, c;
whisper_track *trk;
whisper_clip *clp;
for(t = 0; t < WHISPER_NTRACKS; t++) {
trk = &tracks[t];
/* set track modflag to 0 */
trk->modflag = 0;
/* unset modflags for all clips, and clear data */
for(c = 0; c < WHISPER_NCLIPS; c++) {
clp = trk->clipspace[c];
wclip_unset_modflag(clp);
wclip_clear(clp);
}
}
}