leaf-midi.h
1 /*==============================================================================
2 
3  leaf-midi.h
4  Created: 30 Nov 2018 11:29:26am
5  Author: airship
6 
7  ==============================================================================*/
8 
9 #ifndef LEAF_MIDI_H_INCLUDED
10 #define LEAF_MIDI_H_INCLUDED
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16  //==============================================================================
17 
18 #include "leaf-global.h"
19 #include "leaf-mempool.h"
20 #include "leaf-math.h"
21 #include "leaf-envelopes.h"
22 
31  //==============================================================================
32 
33  // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
34 
108 #define STACK_SIZE 128
109  typedef struct _tStack
110  {
111 
112  tMempool mempool;
113  int data[STACK_SIZE];
114  uint16_t pos;
115  uint16_t size;
116  uint16_t capacity;
117  int ordered;
118  } _tStack;
119 
120  typedef _tStack* tStack;
121 
122  void tStack_init (tStack* const stack, LEAF* const leaf);
123  void tStack_initToPool (tStack* const stack, tMempool* const pool);
124  void tStack_free (tStack* const stack);
125 
126  void tStack_setCapacity (tStack* const stack, uint16_t cap);
127  int tStack_addIfNotAlreadyThere (tStack* const stack, uint16_t item);
128  void tStack_add (tStack* const stack, uint16_t item);
129  int tStack_remove (tStack* const stack, uint16_t item);
130  void tStack_clear (tStack* const stack);
131  int tStack_first (tStack* const stack);
132  int tStack_getSize (tStack* const stack);
133  int tStack_contains (tStack* const stack, uint16_t item);
134  int tStack_next (tStack* const stack);
135  int tStack_get (tStack* const stack, int index);
136 
251  typedef struct _tPoly
252  {
253 
254  tMempool mempool;
255 
256  tStack stack;
257  tStack orderStack;
258 
259  tRamp* ramps;
260  float* rampVals;
261  int* firstReceived;
262  float glideTime;
263  int pitchGlideIsActive;
264 
265  int numVoices;
266  int maxNumVoices;
267 
268  //int voices[POLY_NUM_MAX_VOICES][2];
269  int** voices;
270 
271  int notes[128][2];
272 
273  int CCs[128];
274 
275  uint8_t CCsRaw[128];
276 
277  int lastVoiceToChange;
278 
279  float pitchBend;
280  tRamp pitchBendRamp;
281 
282  int currentNote;
283  int currentVoice;
284  int currentVelocity;
285  int maxLength;
286  } _tPoly;
287 
288  typedef _tPoly* tPoly;
289 
290  void tPoly_init (tPoly* const poly, int maxNumVoices, LEAF* const leaf);
291  void tPoly_initToPool (tPoly* const poly, int maxNumVoices, tMempool* const pool);
292  void tPoly_free (tPoly* const poly);
293 
294  int tPoly_noteOn (tPoly* const poly, int note, uint8_t vel);
295  int tPoly_noteOff (tPoly* const poly, uint8_t note);
296  void tPoly_orderedAddToStack (tPoly* const poly, uint8_t note);
297  void tPoly_setNumVoices (tPoly* const poly, uint8_t numVoices);
298  void tPoly_setPitchGlideActive (tPoly* const poly, int isActive);
299  void tPoly_setPitchGlideTime (tPoly* const poly, float t);
300  void tPoly_setPitchBend (tPoly* const poly, float pitchBend);
301  void tPoly_setBendGlideTime (tPoly* const poly, float t);
302  void tPoly_setBendSamplesPerTick (tPoly* const poly, float t);
303  void tPoly_tickPitch (tPoly* const poly);
304  void tPoly_tickPitchGlide (tPoly* const poly);
305  void tPoly_tickPitchBend (tPoly* const poly);
306  int tPoly_getNumVoices (tPoly* const poly);
307  int tPoly_getNumActiveVoices (tPoly* const poly);
308  float tPoly_getPitch (tPoly* const poly, uint8_t voice);
309  int tPoly_getKey (tPoly* const poly, uint8_t voice);
310  int tPoly_getVelocity (tPoly* const poly, uint8_t voice);
311  int tPoly_isOn (tPoly* const poly, uint8_t voice);
312 
313  //==============================================================================
314 
398  typedef struct _tSimplePoly
399  {
400  tMempool mempool;
401 
402  tStack stack;
403 
404  int numVoices;
405  int maxNumVoices;
406  int** voices;
407  int stealing_on;
408  int recover_stolen;
409 
410  int notes[128][2];
411  } _tSimplePoly;
412 
413  typedef _tSimplePoly* tSimplePoly;
414 
415  void tSimplePoly_init (tSimplePoly* const poly, int maxNumVoices, LEAF* const leaf);
416  void tSimplePoly_initToPool (tSimplePoly* const poly, int maxNumVoices, tMempool* const pool);
417  void tSimplePoly_free (tSimplePoly* const poly);
418 
419  int tSimplePoly_noteOn (tSimplePoly* const poly, int note, uint8_t vel);
420  int tSimplePoly_noteOff (tSimplePoly* const poly, uint8_t note);
421  void tSimplePoly_deactivateVoice (tSimplePoly* const polyh, uint8_t voice);
422  int tSimplePoly_markPendingNoteOff (tSimplePoly* const polyh, uint8_t note);
423  int tSimplePoly_findVoiceAssignedToNote (tSimplePoly* const polyh, uint8_t note);
424  void tSimplePoly_setNumVoices (tSimplePoly* const poly, uint8_t numVoices);
425  int tSimplePoly_getNumVoices (tSimplePoly* const poly);
426  int tSimplePoly_getNumActiveVoices (tSimplePoly* const poly);
427  int tSimplePoly_getPitch (tSimplePoly* const poly, uint8_t voice);
428  int tSimplePoly_getPitchAndCheckActive(tSimplePoly* const polyh, uint8_t voice);
429  int tSimplePoly_getVelocity (tSimplePoly* const poly, uint8_t voice);
430  int tSimplePoly_isOn (tSimplePoly* const poly, uint8_t voice);
431 
432  //==============================================================================
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
438 #endif // LEAF_MIDI_H_INCLUDED
439 
440 //==============================================================================
441 
442 
tStack_add
void tStack_add(tStack *const stack, uint16_t item)
Add a value to the stack.
Definition: leaf-midi.c:60
tPoly_free
void tPoly_free(tPoly *const poly)
Free a tPoly from its mempool.
Definition: leaf-midi.c:311
tPoly_getPitch
float tPoly_getPitch(tPoly *const poly, uint8_t voice)
Get the current pitch of a given voice.
Definition: leaf-midi.c:584
tPoly_isOn
int tPoly_isOn(tPoly *const poly, uint8_t voice)
Get the current play state of a given voice.
Definition: leaf-midi.c:602
tStack_init
void tStack_init(tStack *const stack, LEAF *const leaf)
Initialize a tStack to the default mempool of a LEAF instance.
Definition: leaf-midi.c:23
tSimplePoly_getNumVoices
int tSimplePoly_getNumVoices(tSimplePoly *const poly)
Get the current number of voices available to play notes.
Definition: leaf-midi.c:864
tStack_setCapacity
void tStack_setCapacity(tStack *const stack, uint16_t cap)
Set the capacity of the stack.
Definition: leaf-midi.c:174
tSimplePoly_noteOn
int tSimplePoly_noteOn(tSimplePoly *const poly, int note, uint8_t vel)
Add a note with a given velocity to the poly handler.
Definition: leaf-midi.c:660
tPoly_setPitchGlideActive
void tPoly_setPitchGlideActive(tPoly *const poly, int isActive)
Set whether pitch glide over note changes in voices is active.
Definition: leaf-midi.c:544
tPoly_tickPitch
void tPoly_tickPitch(tPoly *const poly)
Execute all tick-rate changes in the poly handler's pitch, including glide and bend.
Definition: leaf-midi.c:332
tPoly_noteOff
int tPoly_noteOff(tPoly *const poly, uint8_t note)
Remove a note from the poly handler.
Definition: leaf-midi.c:438
tPoly_setBendGlideTime
void tPoly_setBendGlideTime(tPoly *const poly, float t)
Definition: leaf-midi.c:560
tStack_getSize
int tStack_getSize(tStack *const stack)
Get the current size of the stack.
Definition: leaf-midi.c:200
tPoly_initToPool
void tPoly_initToPool(tPoly *const poly, int maxNumVoices, tMempool *const pool)
Initialize a tPoly to a specified mempool.
Definition: leaf-midi.c:264
tSimplePoly_isOn
int tSimplePoly_isOn(tSimplePoly *const poly, uint8_t voice)
Get the current play state of a given voice.
Definition: leaf-midi.c:896
tSimplePoly_init
void tSimplePoly_init(tSimplePoly *const poly, int maxNumVoices, LEAF *const leaf)
Initialize a tSimplePoly to the default mempool of a LEAF instance.
Definition: leaf-midi.c:615
tPoly_setBendSamplesPerTick
void tPoly_setBendSamplesPerTick(tPoly *const poly, float t)
Definition: leaf-midi.c:566
tPoly_noteOn
int tPoly_noteOn(tPoly *const poly, int note, uint8_t vel)
Add a note with a given velocity to the poly handler.
Definition: leaf-midi.c:360
tSimplePoly_getPitchAndCheckActive
int tSimplePoly_getPitchAndCheckActive(tSimplePoly *const polyh, uint8_t voice)
Returns negative one if the voice is inactive.
Definition: leaf-midi.c:884
tSimplePoly_findVoiceAssignedToNote
int tSimplePoly_findVoiceAssignedToNote(tSimplePoly *const polyh, uint8_t note)
Find if there is a voice with that note – useful for note offs where you want to wait to remove it fr...
Definition: leaf-midi.c:810
tPoly_getVelocity
int tPoly_getVelocity(tPoly *const poly, uint8_t voice)
Get the current MIDI velocity of a given voice.
Definition: leaf-midi.c:596
tStack_initToPool
void tStack_initToPool(tStack *const stack, tMempool *const pool)
Initialize a tStack to a specified mempool.
Definition: leaf-midi.c:28
tSimplePoly_deactivateVoice
void tSimplePoly_deactivateVoice(tSimplePoly *const polyh, uint8_t voice)
Definition: leaf-midi.c:783
tSimplePoly_noteOff
int tSimplePoly_noteOff(tSimplePoly *const poly, uint8_t note)
Remove a note from the poly handler.
Definition: leaf-midi.c:739
tSimplePoly_getVelocity
int tSimplePoly_getVelocity(tSimplePoly *const poly, uint8_t voice)
Get the current MIDI velocity of a given voice.
Definition: leaf-midi.c:890
tStack_remove
int tStack_remove(tStack *const stack, uint16_t item)
Remove a single instance of a value from the stack.
Definition: leaf-midi.c:138
tSimplePoly_getNumActiveVoices
int tSimplePoly_getNumActiveVoices(tSimplePoly *const poly)
Get the number of voices currently playing notes.
Definition: leaf-midi.c:870
tPoly_getNumActiveVoices
int tPoly_getNumActiveVoices(tPoly *const poly)
Get the number of voices currently playing notes.
Definition: leaf-midi.c:578
tPoly_setPitchGlideTime
void tPoly_setPitchGlideTime(tPoly *const poly, float t)
Set how long pitch glide over note changes in voices takes.
Definition: leaf-midi.c:550
tPoly_setNumVoices
void tPoly_setNumVoices(tPoly *const poly, uint8_t numVoices)
Set the number of voices available to play notes.
Definition: leaf-midi.c:538
tPoly_tickPitchBend
void tPoly_tickPitchBend(tPoly *const poly)
Execute the tick-rate change of the poly handler's pitch bend.
Definition: leaf-midi.c:347
tPoly_tickPitchGlide
void tPoly_tickPitchGlide(tPoly *const poly)
Execute the tick-rate change of the poly handler's pitch glide.
Definition: leaf-midi.c:338
tPoly_getNumVoices
int tPoly_getNumVoices(tPoly *const poly)
Get the current number of voices available to play notes.
Definition: leaf-midi.c:572
tStack_first
int tStack_first(tStack *const stack)
Get the first value in the stack.
Definition: leaf-midi.c:251
tPoly_setPitchBend
void tPoly_setPitchBend(tPoly *const poly, float pitchBend)
Set the amount of pitch bend.
Definition: leaf-midi.c:353
tSimplePoly_getPitch
int tSimplePoly_getPitch(tSimplePoly *const poly, uint8_t voice)
Get the current MIDI note number of a given voice.
Definition: leaf-midi.c:877
tStack_clear
void tStack_clear(tStack *const stack)
Clear the stack.
Definition: leaf-midi.c:207
tStack_get
int tStack_get(tStack *const stack, int index)
Get the value at a given index of the stack.
Definition: leaf-midi.c:245
tStack_addIfNotAlreadyThere
int tStack_addIfNotAlreadyThere(tStack *const stack, uint16_t item)
Add a value to the stack only if that value is not already in the stack.
Definition: leaf-midi.c:94
tSimplePoly_initToPool
void tSimplePoly_initToPool(tSimplePoly *const poly, int maxNumVoices, tMempool *const pool)
Initialize a tSimplePoly to a specified mempool.
Definition: leaf-midi.c:620
tPoly_init
void tPoly_init(tPoly *const poly, int maxNumVoices, LEAF *const leaf)
Initialize a tPoly to the default mempool of a LEAF instance.
Definition: leaf-midi.c:259
tPoly_getKey
int tPoly_getKey(tPoly *const poly, uint8_t voice)
Get the current MIDI note number of a given voice.
Definition: leaf-midi.c:590
tStack_free
void tStack_free(tStack *const stack)
Free a tStack from its mempool.
Definition: leaf-midi.c:42
LEAF
Struct for an instance of LEAF.
Definition: leaf-global.h:31
tSimplePoly_free
void tSimplePoly_free(tSimplePoly *const poly)
Free a tSimplePoly from its mempool.
Definition: leaf-midi.c:647
tSimplePoly_setNumVoices
void tSimplePoly_setNumVoices(tSimplePoly *const poly, uint8_t numVoices)
Set the number of voices available to play notes.
Definition: leaf-midi.c:857
tSimplePoly_markPendingNoteOff
int tSimplePoly_markPendingNoteOff(tSimplePoly *const polyh, uint8_t note)
Definition: leaf-midi.c:828
tStack_next
int tStack_next(tStack *const stack)
Get the next value in the stack, starting from the earliest added values.
Definition: leaf-midi.c:220
tPoly_orderedAddToStack
void tPoly_orderedAddToStack(tPoly *const poly, uint8_t note)
Definition: leaf-midi.c:498
tStack_contains
int tStack_contains(tStack *const stack, uint16_t item)
Check if the stack contains a value, and if it does, get the index of that value.
Definition: leaf-midi.c:50