leaf-sampling.h
1 /*==============================================================================
2 
3  leaf-sampling.h
4  Created: 23 Jan 2019 11:22:09am
5  Author: Mike Mulshine
6 
7  ==============================================================================*/
8 
9 
10 #ifndef LEAF_SAMPLING_H_INCLUDED
11 #define LEAF_SAMPLING_H_INCLUDED
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17  //==============================================================================
18 
19 #include "leaf-global.h"
20 #include "leaf-math.h"
21 #include "leaf-mempool.h"
22 #include "leaf-envelopes.h"
23 #include "leaf-mempool.h"
24 #include "leaf-analysis.h"
25 
34  //==============================================================================
35 
125  typedef enum RecordMode
126  {
127  RecordOneShot = 0,
128  RecordLoop,
129  RecordModeNil
130  } RecordMode;
131 
132  typedef struct _tBuffer
133  {
134 
135  tMempool mempool;
136 
137  float* buff;
138 
139  uint32_t idx;
140  uint32_t bufferLength;
141  uint32_t recordedLength;
142  uint32_t channels;
143  uint32_t sampleRate;
144  RecordMode mode;
145 
146  int active;
147  } _tBuffer;
148 
149  typedef _tBuffer* tBuffer;
150 
151  void tBuffer_init (tBuffer* const, uint32_t length, LEAF* const leaf);
152  void tBuffer_initToPool (tBuffer* const sb, uint32_t length, tMempool* const mp);
153  void tBuffer_free (tBuffer* const);
154 
155  void tBuffer_tick (tBuffer* const, float sample);
156  void tBuffer_read (tBuffer* const, float* buff, uint32_t len);
157  float tBuffer_get (tBuffer* const, int idx);
158  void tBuffer_record (tBuffer* const);
159  void tBuffer_stop (tBuffer* const);
160  void tBuffer_setBuffer (tBuffer* const sb, float* externalBuffer, int length, int channels, int sampleRate);
161  int tBuffer_getRecordPosition (tBuffer* const);
162  void tBuffer_setRecordPosition (tBuffer* const, int pos);
163  void tBuffer_setRecordMode (tBuffer* const, RecordMode mode);
164  void tBuffer_clear (tBuffer* const);
165  uint32_t tBuffer_getBufferLength (tBuffer* const);
166  uint32_t tBuffer_getRecordedLength (tBuffer* const sb);
167  void tBuffer_setRecordedLength (tBuffer* const sb, int length);
168  int tBuffer_isActive (tBuffer* const sb);
169 
170  //==============================================================================
171 
236  typedef enum PlayMode
237  {
238  PlayNormal,
239  PlayLoop,
240  PlayBackAndForth,
241  PlayModeNil
242  } PlayMode;
243 
244  typedef struct _tSampler
245  {
246 
247  tMempool mempool;
248 
249  tBuffer samp;
250 
251  float leafInvSampleRate;
252  float leafSampleRate;
253  float ticksPerSevenMs;
254  float rateFactor;
255  uint32_t channels;
256 
257  tRamp gain;
258 
259  float idx;
260  float inc;
261  float last;
262  float iinc;
263  int8_t dir;
264  int8_t flip;
265  int8_t bnf;
266 
267  int32_t start, targetstart;
268  int32_t end, targetend;
269 
270  uint32_t len;
271  uint32_t cfxlen;
272  float numticks;
273  PlayMode mode;
274  int retrigger;
275 
276  int active;
277 
278  uint8_t inCrossfade;
279 
280  float flipStart;
281  float flipIdx;
282  } _tSampler;
283 
284  typedef _tSampler* tSampler;
285 
286  void tSampler_init (tSampler* const, tBuffer* const, LEAF* const leaf);
287  void tSampler_initToPool (tSampler* const sp, tBuffer* const b, tMempool* const mp, LEAF* const leaf);
288  void tSampler_free (tSampler* const);
289 
290  float tSampler_tick (tSampler* const);
291  float tSampler_tickStereo (tSampler* const sp, float* outputArray);
292  void tSampler_setSample (tSampler* const, tBuffer* const);
293  void tSampler_setMode (tSampler* const, PlayMode mode);
294  void tSampler_play (tSampler* const);
295  void tSampler_stop (tSampler* const);
296  void tSampler_setStart (tSampler* const, int32_t start);
297  void tSampler_setEnd (tSampler* const, int32_t end);
298  void tSampler_setLength (tSampler* const, int32_t length);
299  void tSampler_setCrossfadeLength (tSampler* const sp, uint32_t length);
300  void tSampler_setRate (tSampler* const, float rate);
301 
302  //==============================================================================
303 
362  typedef struct _tAutoSampler
363  {
364 
365  tMempool mempool;
366  tSampler sampler;
367  tEnvelopeFollower ef;
368  uint32_t windowSize;
369  float threshold;
370  float previousPower;
371  uint32_t sampleCounter;
372  uint32_t powerCounter;
373  uint8_t sampleTriggered;
374  } _tAutoSampler;
375 
376  typedef _tAutoSampler* tAutoSampler;
377 
378  void tAutoSampler_init (tAutoSampler* const, tBuffer* const, LEAF* const leaf);
379  void tAutoSampler_initToPool (tAutoSampler* const as, tBuffer* const b, tMempool* const mp, LEAF* const leaf);
380  void tAutoSampler_free (tAutoSampler* const);
381 
382  float tAutoSampler_tick (tAutoSampler* const, float input);
383  void tAutoSampler_setBuffer (tAutoSampler* const, tBuffer* const);
384  void tAutoSampler_setMode (tAutoSampler* const, PlayMode mode);
385  void tAutoSampler_play (tAutoSampler* const);
386  void tAutoSampler_stop (tAutoSampler* const);
387  void tAutoSampler_setThreshold (tAutoSampler* const, float thresh);
388  void tAutoSampler_setWindowSize (tAutoSampler* const, uint32_t size);
389  void tAutoSampler_setCrossfadeLength (tAutoSampler* const, uint32_t length);
390  void tAutoSampler_setRate (tAutoSampler* const, float rate);
391 
392 
455 #define FILLEN 256
456 
457  typedef struct _tMBSampler
458  {
459 
460  tMempool mempool;
461 
462  tBuffer samp;
463  PlayMode mode;
464  int active;
465 
466  tExpSmooth gain;
467 
468  float out;
469  float last, beforeLast;
470  float amp;
471  float last_amp;
472  float syncin;
473  float _p, _w, _z;
474  float _last_w;
475  float _f [FILLEN + STEP_DD_PULSE_LENGTH];
476  int _j;
477 
478  int start, end;
479  int currentLoopLength;
480  } _tMBSampler;
481 
482  typedef _tMBSampler* tMBSampler;
483 
484  void tMBSampler_init (tMBSampler* const, tBuffer* const, LEAF* const leaf);
485  void tMBSampler_initToPool (tMBSampler* const, tBuffer* const, tMempool* const);
486  void tMBSampler_free (tMBSampler* const);
487 
488  float tMBSampler_tick (tMBSampler* const);
489  void tMBSampler_setSample (tMBSampler* const, tBuffer* const);
490  void tMBSampler_setMode (tMBSampler* const, PlayMode mode);
491  void tMBSampler_play (tMBSampler* const);
492  void tMBSampler_stop (tMBSampler* const);
493  void tMBSampler_setStart (tMBSampler* const, int32_t start);
494  void tMBSampler_setEnd (tMBSampler* const, int32_t end);
495  void tMBSampler_setLength (tMBSampler* const, int32_t length);
496  void tMBSampler_setRate (tMBSampler* const, float rate);
497 
498 #ifdef __cplusplus
499 }
500 #endif
501 
502 #endif // LEAF_SAMPLING_H_INCLUDED
503 
504 //==============================================================================
505 
tBuffer_setRecordPosition
void tBuffer_setRecordPosition(tBuffer *const, int pos)
Set the recording position, from where the buffer will next add samples.
Definition: leaf-sampling.c:118
tMBSampler_setEnd
void tMBSampler_setEnd(tMBSampler *const, int32_t end)
Definition: leaf-sampling.c:1552
tAutoSampler_stop
void tAutoSampler_stop(tAutoSampler *const)
Definition: leaf-sampling.c:1184
tSampler_setRate
void tSampler_setRate(tSampler *const, float rate)
Definition: leaf-sampling.c:1073
tBuffer_clear
void tBuffer_clear(tBuffer *const)
Clear the buffer.
Definition: leaf-sampling.c:130
tSampler_play
void tSampler_play(tSampler *const)
Definition: leaf-sampling.c:843
tMBSampler_setStart
void tMBSampler_setStart(tMBSampler *const, int32_t start)
Definition: leaf-sampling.c:1543
tBuffer_read
void tBuffer_read(tBuffer *const, float *buff, uint32_t len)
Read an input buffer into the buffer.
Definition: leaf-sampling.c:81
tMBSampler_setRate
void tMBSampler_setRate(tMBSampler *const, float rate)
Definition: leaf-sampling.c:1568
tBuffer_init
void tBuffer_init(tBuffer *const, uint32_t length, LEAF *const leaf)
Initialize a tBuffer to the default mempool of a LEAF instance.
Definition: leaf-sampling.c:27
tBuffer_getRecordPosition
int tBuffer_getRecordPosition(tBuffer *const)
Get the recording position, from where the buffer will next add samples.
Definition: leaf-sampling.c:112
tAutoSampler_play
void tAutoSampler_play(tAutoSampler *const)
Definition: leaf-sampling.c:1179
tMBSampler_setMode
void tMBSampler_setMode(tMBSampler *const, PlayMode mode)
Definition: leaf-sampling.c:1514
tMBSampler_initToPool
void tMBSampler_initToPool(tMBSampler *const, tBuffer *const, tMempool *const)
Initialize a tMBSampler to a specified mempool.
Definition: leaf-sampling.c:1222
tAutoSampler_init
void tAutoSampler_init(tAutoSampler *const, tBuffer *const, LEAF *const leaf)
Initialize a tAutoSampler to the default mempool of a LEAF instance.
Definition: leaf-sampling.c:1094
tBuffer_setRecordedLength
void tBuffer_setRecordedLength(tBuffer *const sb, int length)
Set the length of what is considered recorded audio in the buffer.
Definition: leaf-sampling.c:163
tSampler_setLength
void tSampler_setLength(tSampler *const, int32_t length)
Definition: leaf-sampling.c:1066
tMBSampler_tick
float tMBSampler_tick(tMBSampler *const)
Definition: leaf-sampling.c:1273
tBuffer_setRecordMode
void tBuffer_setRecordMode(tBuffer *const, RecordMode mode)
Set the recording mode.
Definition: leaf-sampling.c:124
tBuffer_tick
void tBuffer_tick(tBuffer *const, float sample)
If recording, add a sample to the buffer. Otherwise do nothing.
Definition: leaf-sampling.c:56
tAutoSampler_setCrossfadeLength
void tAutoSampler_setCrossfadeLength(tAutoSampler *const, uint32_t length)
Definition: leaf-sampling.c:1204
tMBSampler_free
void tMBSampler_free(tMBSampler *const)
Free a tMBSampler from its mempool.
Definition: leaf-sampling.c:1250
tBuffer_isActive
int tBuffer_isActive(tBuffer *const sb)
Check if the buffer is recording.
Definition: leaf-sampling.c:169
tSampler_stop
void tSampler_stop(tSampler *const)
Definition: leaf-sampling.c:884
tSampler_setEnd
void tSampler_setEnd(tSampler *const, int32_t end)
Definition: leaf-sampling.c:996
tBuffer_record
void tBuffer_record(tBuffer *const)
Start recording samples into the buffer.
Definition: leaf-sampling.c:99
tAutoSampler_setThreshold
void tAutoSampler_setThreshold(tAutoSampler *const, float thresh)
Definition: leaf-sampling.c:1190
tBuffer_getBufferLength
uint32_t tBuffer_getBufferLength(tBuffer *const)
Get the length of the buffer.
Definition: leaf-sampling.c:151
tMBSampler_play
void tMBSampler_play(tMBSampler *const)
Definition: leaf-sampling.c:1520
tSampler_free
void tSampler_free(tSampler *const)
Free a tSampler from its mempool.
Definition: leaf-sampling.c:244
tBuffer_get
float tBuffer_get(tBuffer *const, int idx)
Get the sample recorded at a given position in the buffer.
Definition: leaf-sampling.c:92
tMBSampler_setLength
void tMBSampler_setLength(tMBSampler *const, int32_t length)
Definition: leaf-sampling.c:1562
tSampler_setCrossfadeLength
void tSampler_setCrossfadeLength(tSampler *const sp, uint32_t length)
Definition: leaf-sampling.c:834
tSampler_init
void tSampler_init(tSampler *const, tBuffer *const, LEAF *const leaf)
Initialize a tSampler to the default mempool of a LEAF instance.
Definition: leaf-sampling.c:181
tSampler_setMode
void tSampler_setMode(tSampler *const, PlayMode mode)
Definition: leaf-sampling.c:828
tSampler_setSample
void tSampler_setSample(tSampler *const, tBuffer *const)
Definition: leaf-sampling.c:252
tAutoSampler_setBuffer
void tAutoSampler_setBuffer(tAutoSampler *const, tBuffer *const)
Definition: leaf-sampling.c:1164
tMBSampler_init
void tMBSampler_init(tMBSampler *const, tBuffer *const, LEAF *const leaf)
Initialize a tMBSampler to the default mempool of a LEAF instance.
Definition: leaf-sampling.c:1217
tAutoSampler_setWindowSize
void tAutoSampler_setWindowSize(tAutoSampler *const, uint32_t size)
Definition: leaf-sampling.c:1196
tBuffer_getRecordedLength
uint32_t tBuffer_getRecordedLength(tBuffer *const sb)
Get the length of recorded audio in the buffer.
Definition: leaf-sampling.c:157
tBuffer_free
void tBuffer_free(tBuffer *const)
Free a tBuffer from its mempool.
Definition: leaf-sampling.c:48
tAutoSampler_tick
float tAutoSampler_tick(tAutoSampler *const, float input)
Definition: leaf-sampling.c:1121
tSampler_setStart
void tSampler_setStart(tSampler *const, int32_t start)
Definition: leaf-sampling.c:926
tBuffer_stop
void tBuffer_stop(tBuffer *const)
Stop recordings samples into the buffer.
Definition: leaf-sampling.c:106
tAutoSampler_free
void tAutoSampler_free(tAutoSampler *const)
Free a tAutoSampler from its mempool.
Definition: leaf-sampling.c:1111
tAutoSampler_setMode
void tAutoSampler_setMode(tAutoSampler *const, PlayMode mode)
Definition: leaf-sampling.c:1173
LEAF
Struct for an instance of LEAF.
Definition: leaf-global.h:31
tAutoSampler_setRate
void tAutoSampler_setRate(tAutoSampler *const, float rate)
Definition: leaf-sampling.c:1210
tBuffer_initToPool
void tBuffer_initToPool(tBuffer *const sb, uint32_t length, tMempool *const mp)
Initialize a tBuffer to a specified mempool.
Definition: leaf-sampling.c:32
tSampler_tick
float tSampler_tick(tSampler *const)
Definition: leaf-sampling.c:273
tMBSampler_stop
void tMBSampler_stop(tMBSampler *const)
Definition: leaf-sampling.c:1535
tMBSampler_setSample
void tMBSampler_setSample(tMBSampler *const, tBuffer *const)
Definition: leaf-sampling.c:1259