-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLooper.h
More file actions
492 lines (356 loc) · 13.3 KB
/
Looper.h
File metadata and controls
492 lines (356 loc) · 13.3 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
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
// The Looper Machine
//
// The classes are broken up into "public" versions, available to the UI,
// and "implementation" versions that are only intended to be called in
// the loopMachine-loopTrack-loopClip hierarchy, while still maintaining
// some protection on the private members of the hiearchy (and a public
// API for use within it).
//
// The UI treats the (public) loopMachine as "readonly", except to
// issue commands() to it, or set the volume/mute of a clip or
// volume of a track.
//
// The machine is driven by the loopMachine::update() method, which
// is called by the audio system for every audio buffer.
#ifndef _loopMachine_h_
#define _loopMachine_h_
#include <audio/Audio.h>
#include "commonDefines.h"
// LOOPER_NUM_TRACKS and LAYERS, TRACK_STATES, LOOP_COMMANDS, and common CC's
#include "patches/RubberBandWrapper.h"
#define WITH_METERS 1
#define S32_MAX 32767
#define S32_MIN -32768
#if 0
#define LOOPER_LOG(f,...) pTheLoopMachine->LogUpdate(log_name,f,__VA_ARGS__)
#elif 0
#define LOOPER_LOG(f,...) CLogger::Get()->Write(log_name,LogDebug,f,__VA_ARGS__)
#else
#define LOOPER_LOG(f,...)
#endif
//------------------------------------------------------
// CONSTANTS AND STRUCTS
//------------------------------------------------------
// #define LOOPER_NUM_TRACKS 4
// #define LOOPER_NUM_LAYERS 3
// in common defines.h
#define LOOPER_NUM_CHANNELS 2
// the whole thing is stereo
#define CROSSFADE_BLOCKS 4
// The number of buffers (10 == approx 30ms) to continue recording
#define LOOP_TRACK_SECONDS (60 * LOOPER_NUM_TRACKS * LOOPER_NUM_LAYERS)
// Allowing for 60 seconds per clip per track overall, for example,
// equals 960 overall track seconds. A stero set of 128 s16 sample
// buffers is 512 bytes. Rounding up to 346 buffers per second at
// that's 177,152 bytes per second. 960 track seconds, therefore,
// is about 170M, There's still 600M+ of memory available on the pi
#define LOOP_HEAP_BYTES (LOOP_TRACK_SECONDS * AUDIO_BLOCK_BYTES * 2 * INTEGRAL_BLOCKS_PER_SECOND)
// 1440 track seconds, 496,800 blocks == 127,1800,00 bytes == approx 128M
#define INTEGRAL_BLOCKS_PER_SECOND ((AUDIO_SAMPLE_RATE + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES)
// 345
// meters
#define METER_INPUT 0
#define METER_LOOP 1
#define METER_THRU 2
#define METER_MIX 3
#define NUM_METERS 4
typedef struct
// the measurements across some number of samples
// for a single meter, bracketed by calls to getMeter()
{
s16 min_sample[LOOPER_NUM_CHANNELS];
s16 max_sample[LOOPER_NUM_CHANNELS];
} meter_t;
// controls
typedef struct // avoid byte sized structs
{
u16 value; // 0..127
u16 default_value; // 0..127
float scale; // 0..1.0 for my controls; unused for codec
int32_t multiplier; // for WITH_INT_VOLUMES
} controlDescriptor_t;
enum ClipState {
CS_IDLE,
CS_RECORDING,
CS_RECORDING_MAIN,
CS_RECORDING_TAIL,
CS_FINISHING,
CS_RECORDED,
CS_PLAYING,
CS_LOOPING,
CS_STOPPING
};
// An in memory log message
typedef struct logString_type
{
const char *lname;
CString *string;
logString_type *next;
} logString_t;
// forwards
class loopClip;
class loopTrack;
// static externs
extern CString *getClipStateName(ClipState state);
extern const char *getLoopStateName(u16 state);
extern const char *getLoopCommandName(u16 name);
extern CString *getTrackStateName(u16 track_state);
class loopBuffer
{
public:
loopBuffer(u32 size=LOOP_HEAP_BYTES);
~loopBuffer();
void init() { m_top = 0; }
s16 *getBuffer() { return &m_buffer[m_top]; }
u32 getSize() { return m_size; }
u32 getSizeBlocks() { return m_size / AUDIO_BLOCK_BYTES; }
u32 getFreeBytes() { return m_size - m_top; }
u32 getFreeBlocks() { return (m_size - m_top) / AUDIO_BLOCK_BYTES; }
u32 getUsedBytes() { return m_top; }
u32 getUsedBlocks() { return m_top / AUDIO_BLOCK_BYTES; }
void commitBytes(u32 bytes) { m_top += bytes; }
void commitBlocks(u32 blocks) { m_top += blocks * AUDIO_BLOCK_BYTES; }
private:
u32 m_top;
u32 m_size;
s16 *m_buffer;
};
class publicClip
{
public:
publicClip(u16 track_num, u16 clip_num)
{
m_track_num = track_num;
m_clip_num = clip_num;
init();
}
virtual ~publicClip() {}
u16 getClipNum() { return m_clip_num; }
u16 getTrackNum() { return m_track_num; }
ClipState getClipState() { return m_state; }
u32 getNumBlocks() { return m_num_blocks; }
u32 getMaxBlocks() { return m_max_blocks; }
u32 getPlayBlockNum() { return m_play_block; }
u32 getRecordBlockNum() { return m_record_block; }
u32 getCrossfadeBlockNum() { return m_crossfade_start + m_crossfade_offset; }
bool isMuted() { return m_mute; }
void setMute(bool mute) { m_mute = mute; }
int getVolume() { return m_volume * 100.00; }
void setVolume(int vol) { m_volume = ((float)vol)/100.00; }
protected:
void init()
{
m_state = CS_IDLE;
m_num_blocks = 0;
m_max_blocks = 0;
m_play_block = 0;
m_record_block = 0;
m_crossfade_start = 0;
m_crossfade_offset = 0;
m_origNumBlocks = 0;
m_quantizeTarget = 0;
m_quantizeWillPlay = false;
m_recordStartPhaseOffset = 0;
m_mute = false;
m_volume = 1.0;
m_mark_point = -1;
m_mark_point_active = false;
}
u16 m_track_num;
u16 m_clip_num;
ClipState m_state;
u32 m_num_blocks; // the number of blocks NOT including the crossfade blocks
u32 m_max_blocks; // the number of blocks available for recording
u32 m_play_block;
u32 m_record_block;
u32 m_crossfade_start;
u32 m_crossfade_offset;
u32 m_origNumBlocks;
u32 m_quantizeTarget;
bool m_quantizeWillPlay;
u32 m_recordStartPhaseOffset;
s32 m_mark_point;
bool m_mark_point_active;
bool m_mute;
float m_volume;
};
class loopClip : public publicClip
{
public:
loopClip(u16 clip_num, loopTrack* pTrack);
virtual ~loopClip();
void init();
inline s16 *getBlock(u32 block_num)
{
return &(m_buffer[block_num * AUDIO_BLOCK_SAMPLES * LOOPER_NUM_CHANNELS]);
}
void update(s32 *in, s32 *out);
void updateState(u16 cur_command);
void stopImmediate();
void setMarkPoint();
void clearMarkPoint();
void halveLength();
void doubleLength();
void setTempoRatio(float ratio) {}
RubberBandWrapper::DebugState getWrapperDebugState() const { return { 1.0f, 0, 0 }; }
private:
loopTrack *m_pLoopTrack;
s16 *m_buffer;
RubberBandWrapper m_wrapper;
void _startRecording();
void _startEndingRecording(u32 trimToBlocks, bool willPlay);
void _finishRecording();
void _startPlaying();
void _startFadeOut();
void _startCrossFade();
void _endFadeOut();
u32 _calcQuantizeTarget();
};
class publicTrack
{
public:
publicTrack(u16 track_num)
{
m_track_num = track_num;
init();
}
virtual ~publicTrack() {}
u16 getTrackNum() { return m_track_num; }
u16 getNumUsedClips() { return m_num_used_clips; }
u16 getNumRecordedClips() { return m_num_recorded_clips; }
u16 getNumRunningClips() { return m_num_running_clips; }
bool isSelected() { return m_selected; }
volatile u32 m_peakLevel;
virtual publicClip *getPublicClip(u16 clip_num) = 0;
virtual int getTrackState() = 0;
protected:
void init()
{
m_num_used_clips = 0;
m_num_recorded_clips = 0;
m_num_running_clips = 0;
m_selected = 0;
m_peakLevel = 0;
}
u16 m_track_num;
u16 m_num_used_clips;
u16 m_num_recorded_clips;
u16 m_num_running_clips;
bool m_selected;
};
class loopTrack : public publicTrack
{
public:
loopTrack(u16 track_num);
virtual ~loopTrack();
void init();
virtual int getTrackState();
loopClip *getClip(u16 num) { return m_clips[num]; }
void setSelected(bool selected) { m_selected = selected; }
void updateState(u16 cur_command);
void update(s32 *in, s32 *out);
void incDecRunning(int inc);
void incDecNumUsedClips(int inc);
void incDecNumRecordedClips(int inc);
void stopImmediate();
void setMarkPoint();
void clearMarkPoint();
void clearClip(int layer);
void halveLength();
void doubleLength();
private:
virtual publicClip *getPublicClip(u16 clip_num) { return (publicClip *) m_clips[clip_num]; }
loopClip *m_clips[LOOPER_NUM_LAYERS];
};
class publicLoopMachine : public AudioStream
{
public:
publicLoopMachine();
~publicLoopMachine();
virtual void command(u16 command) = 0;
u16 getRunning() { return m_running; }
u16 getPendingCommand() { return m_pending_command; }
int getSelectedTrackNum() { return m_selected_track_num; }
bool getDubMode() { return m_dub_mode; }
void setDubMode(bool dub) { m_dub_mode = dub; }
u32 m_masterLoopBlocks;
u32 m_masterPhase;
volatile u32 m_outputPeakLevel;
virtual publicTrack *getPublicTrack(u16 num) = 0;
float getMeter(u16 meter, u16 channel);
u8 getControlValue(u16 control);
u8 getControlDefault(u16 control);
void setControl(u16 control, u8 value);
logString_t *getNextLogString();
int getPendingLoopNotify()
{
if (m_pending_loop_notify)
return m_pending_loop_notify--;
return 0;
}
struct ClipWrapperDebug {
u16 track_num;
u16 clip_num;
RubberBandWrapper::DebugState state;
};
virtual int getWrapperDebugStates(ClipWrapperDebug *out, int maxCount) = 0;
protected:
virtual void update() = 0;
virtual const char *getName() { return "looper"; }
virtual u16 getType() { return AUDIO_DEVICE_OTHER; }
void init()
{
m_running = 0;
m_masterLoopBlocks = 0;
m_masterPhase = 0;
m_outputPeakLevel = 0;
m_pending_command = 0;
m_selected_track_num = -1;
m_dub_mode = false;
m_pending_loop_notify = 0;
}
AudioCodec *pCodec;
audio_block_t *inputQueueArray[LOOPER_NUM_CHANNELS];
int m_running;
u16 m_pending_command;
int m_selected_track_num;
bool m_dub_mode;
meter_t m_meter[NUM_METERS];
controlDescriptor_t m_control[LOOPER_NUM_CONTROLS];
logString_t *m_pFirstLogString;
logString_t *m_pLastLogString;
volatile int m_pending_loop_notify;
};
class loopMachine : public publicLoopMachine
{
public:
loopMachine();
~loopMachine();
loopTrack *getTrack(u16 num) { return m_tracks[num]; }
void incDecRunning(int inc);
void LogUpdate(const char *lname, const char *format, ...);
virtual int getWrapperDebugStates(ClipWrapperDebug *out, int maxCount);
private:
virtual void command(u16 command);
virtual publicTrack *getPublicTrack(u16 num) { return (publicTrack *) m_tracks[num]; }
virtual void update(void);
void init();
void updateState();
u16 m_cur_command;
int m_cur_track_num;
int m_mark_point_state;
volatile u16 m_track_pending[LOOPER_NUM_TRACKS];
u32 m_prevMasterLoopBlocks;
loopTrack *m_tracks[LOOPER_NUM_TRACKS];
static s32 m_input_buffer[ LOOPER_NUM_CHANNELS * AUDIO_BLOCK_SAMPLES ];
static s32 m_output_buffer[ LOOPER_NUM_CHANNELS * AUDIO_BLOCK_SAMPLES ];
};
//////////////////////////////////////////////////////
////////// STATIC GLOBAL ACCESSOR ////////////////////
//////////////////////////////////////////////////////
extern loopBuffer *pTheLoopBuffer;
// in loopBuffer.cpp
extern loopMachine *pTheLoopMachine;
extern publicLoopMachine *pTheLooper;
// in audio,cpp
#endif //!_loopMachine_h_