-
Notifications
You must be signed in to change notification settings - Fork 21
/
mod_miditransfn.h
458 lines (377 loc) · 17.2 KB
/
mod_miditransfn.h
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
/*
__ __| | | /_) | ___| | |
| __ \ _ \ ' / | | / | _ \ __ \ | _` | __ \ __|
| | | | __/ . \ | < | | __/ | | | ( | | |\__ \
_| _| |_|\___| _|\_\_|_|\_\\____|\___|_| _| _____|\__,_|_.__/ ____/
-----------------------------------------------------------------------------
USBMIDIKLIK 4X4 - USB Midi advanced firmware for STM32F1 platform.
Copyright (C) 2019 by The KikGen labs.
LICENCE CREATIVE COMMONS - Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
This file is part of the USBMIDIKLIK-4x4 distribution
https://github.com/TheKikGen/USBMidiKliK4x4
Copyright (c) 2019 TheKikGen Labs team.
-----------------------------------------------------------------------------
Disclaimer.
This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/
or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
NON COMMERCIAL - PERSONAL USE ONLY : You may not use the material for pure
commercial closed code solution without the licensor permission.
You are free to copy and redistribute the material in any medium or format,
adapt, transform, and build upon the material.
You must give appropriate credit, a link to the github site
https://github.com/TheKikGen/USBMidiKliK4x4 , provide a link to the license,
and indicate if changes were made. You may do so in any reasonable manner,
but not in any way that suggests the licensor endorses you or your use.
You may not apply legal terms or technological measures that legally restrict
others from doing anything the license permits.
You do not have to comply with the license for elements of the material
in the public domain or where your use is permitted by an applicable exception
or limitation.
No warranties are given. The license may not give you all of the permissions
necessary for your intended use. This program is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
///////////////////////////////////////////////////////////////////////////////
// MIDI TRANSFORMATION FUNCTIONS
//-----------------------------------------------------------------------------
// Due to the unusual make process of Arduino platform, this file is directly
// included in the main ino one.
//
// The midi transformation engine relies on a pipeline process. A "pipe" is
// an elementary midi tranformation function accepting a midi packet and some
// parameters. So a pipeline, is a chain of transformation functions, delivering
// a packet (or many) from a source port to a destination port.
//
// SRC PORT (midi packet) -> | fn1 | fn3 | fn4 -> (modified packet) DEST PORT
//
// To reduce memory consumption, and facilitate resuse of pipelines, thery are
// stored in a "slot". When a port requires a transformation pipeline,
// you simply attach the slot to it.
//
// Number of pipes in a pipelie is defined by TRANS_PIPELINE_SIZE
// and number of pipelines slots is defined by TRANS_PIPELINE_SLOT_SIZE
///////////////////////////////////////////////////////////////////////////////
#pragma once
///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS PROTOTYPES
///////////////////////////////////////////////////////////////////////////////
void TransPacketPipeline_Clear(transPipeline_t *);
boolean TransPacketPipeline_ClearSlot(uint8_t);
//Shared. See usbmidiKlik4x4.h
//boolean TransPacketPipeline_CopySlot(uint8_t ,uint8_t ) ;
//boolean TransPacketPipeline_AttachPort(uint8_t ,uint8_t ,uint8_t );
void TransPacketPipe_Clear(transPipe_t *);
//Shared. See usbmidiKlik4x4.h
//boolean TransPacketPipe_AddToSlot(uint8_t , transPipe_t *);
//boolean TransPacketPipe_InsertToSlot(uint8_t , uint8_t , transPipe_t *,boolean);
//boolean TransPacketPipe_ClearSlotIndexPid(uint8_t , boolean ,uint8_t);
//boolean TransPacketPipe_ByPass(uint8_t , uint8_t ,uint8_t);
boolean TransPacketPipelineExec(uint8_t, uint8_t, midiPacket_t *);
//Shared. See usbmidiKlik4x4.h
//void ShowPipelineSlot(uint8_t );
// SLOT LOCK to avaid infinite loop and filter internal sysex
// Eight Slot are possible currently. Change to uint16_t if more.
#if TRANS_PIPELINE_SLOT_SIZE > 8
uint16_t slotLockMsk = 0;
#else
uint8_t slotLockMsk = 0;
#endif
///////////////////////////////////////////////////////////////////////////////
// PIPES.
///////////////////////////////////////////////////////////////////////////////
#include "mod_miditransfn_pipes.h"
///////////////////////////////////////////////////////////////////////////////
// Reset a tranformation pipeline
///////////////////////////////////////////////////////////////////////////////
void TransPacketPipeline_Clear(transPipeline_t *pl) {
transPipe_t *pipe = pl->pipeline;
for (uint8_t p=0 ; p != TRANS_PIPELINE_SIZE ; p++ )
TransPacketPipe_Clear( pipe++ );
}
///////////////////////////////////////////////////////////////////////////////
// Clear a pipeline slot. 0x7F = ALL
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipeline_ClearSlot(uint8_t pipelineSlot) {
// clear all
if ( pipelineSlot == 0x7F ) {
for (uint8_t s=0 ; s != TRANS_PIPELINE_SLOT_SIZE ; s++ )
TransPacketPipeline_Clear(&EE_Prm.pipelineSlot[s]);
return true;
} else
if ( pipelineSlot < 1 || pipelineSlot > TRANS_PIPELINE_SLOT_SIZE )
return false;
// Clear one
TransPacketPipeline_Clear(&EE_Prm.pipelineSlot[--pipelineSlot]);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Copy source slot to destination slot
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipeline_CopySlot(uint8_t sourceSlot,uint8_t destSlot) {
if ( sourceSlot < 1 || sourceSlot > TRANS_PIPELINE_SLOT_SIZE )
return false;
if ( destSlot < 1 || destSlot > TRANS_PIPELINE_SLOT_SIZE )
return false;
if ( sourceSlot == destSlot ) return false;
sourceSlot--; destSlot--;
memcpy(&EE_Prm.pipelineSlot[destSlot],
&EE_Prm.pipelineSlot[sourceSlot],sizeof(transPipeline_t));
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Detach/Attach a port from/to a pipeline slot
// Only one slot attached to a given port.
// To Detach pass a pipeline == 0.
// If portType = 0x7F , ALL ports are detached.
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipeline_AttachPort(uint8_t portType,uint8_t port,uint8_t pipelineSlot )
{
// Detach ALL
if (portType == 0x7F) {
for (uint8_t i = 0; i != UsbCableInterfaceMax ; i++)
EE_Prm.rtRulesCable[i].slot=0;
for (uint8_t i = 0; i != SERIAL_INTERFACE_COUNT ; i++) {
EE_Prm.rtRulesJack[i].slot = 0;
EE_Prm.rtRulesIthru[i].slot=0;
}
for (uint8_t i = 0; i != VIRTUAL_INTERFACE_MAX ; i++) {
EE_Prm.rtRulesVirtual[i].slot = 0;
}
return true;
}
if ( pipelineSlot > TRANS_PIPELINE_SLOT_SIZE ) return false;
if (portType == PORT_TYPE_CABLE ) {
if ( port < UsbCableInterfaceMax )
EE_Prm.rtRulesCable[port].slot = pipelineSlot ;
else return false;
}
else
if (portType == PORT_TYPE_JACK ) {
if ( port < SERIAL_INTERFACE_COUNT )
EE_Prm.rtRulesJack[port].slot = pipelineSlot ;
else return false;
}
else
if (portType == PORT_TYPE_VIRTUAL ) {
if ( port < VIRTUAL_INTERFACE_MAX )
EE_Prm.rtRulesVirtual[port].slot = pipelineSlot ;
else return false;
}
else
if (portType == PORT_TYPE_ITHRU ) {
if ( port < SERIAL_INTERFACE_COUNT )
EE_Prm.rtRulesIthru[port].slot = pipelineSlot ;
else return false;
}
else return false;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Reset a tranformation pipe
///////////////////////////////////////////////////////////////////////////////
void TransPacketPipe_Clear(transPipe_t *p) {
p->pId = FN_TRANSPIPE_NOPIPE;
p->byPass = p->par1 = p->par2 = p->par3 = p->par4 = 0;
}
///////////////////////////////////////////////////////////////////////////////
// Add a pipe to a transformation pipeline slot
// Return true if pipe added succefully
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipe_AddToSlot(uint8_t pipelineSlot, transPipe_t *pipe) {
// The slot must exists here
if (pipelineSlot < 1 || pipelineSlot > TRANS_PIPELINE_SLOT_SIZE )
return false;
pipelineSlot--; // Adjust for C array
// Check the pipe parameters
if ( pipe->pId >= FN_TRANSPIPE_VECTOR_SIZE ) return false;
if ( !MidiTransFnVector[pipe->pId].checkFn(pipe) ) return false;
// Get a pointer of the 1st pipe in the attached pipeline
transPipe_t *pipeline = EE_Prm.pipelineSlot[pipelineSlot].pipeline ;
// Find a location for the pipe in the pipeline.
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) {
// Copy the pipe at this free location
*pipeline = *pipe;
return true;
}
pipeline++;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Insert a pipe to a transformation pipeline slot at a specific index
// Return true if pipe added succefully
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipe_InsertToSlot(uint8_t pipelineSlot, uint8_t index, transPipe_t *pipe, boolean replace) {
// The slot must exists here
if (pipelineSlot < 1 || pipelineSlot > TRANS_PIPELINE_SLOT_SIZE ) return false;
if ( index >= TRANS_PIPELINE_SIZE) return false;
pipelineSlot--; // Adjust for C array
// Get a pointer of the 1st/last pipes in the attached pipeline
transPipe_t *pipeline = &EE_Prm.pipelineSlot[pipelineSlot].pipeline[0] ;
transPipe_t *pipeline2 = &EE_Prm.pipelineSlot[pipelineSlot].pipeline[TRANS_PIPELINE_SIZE-1] ;
// Slot full ? can't insert .
if ( !replace && pipeline2->pId != FN_TRANSPIPE_NOPIPE ) return false;
// Check the pipe parameters
if ( pipe->pId >= FN_TRANSPIPE_VECTOR_SIZE ) return false;
if ( !MidiTransFnVector[pipe->pId].checkFn(pipe) ) return false;
// Find the location where to insert the pipe in the pipeline.
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE && i < index ) return false;
if ( i == index) {
if (! replace) {
// Move pipes down from the next index.
while ( pipeline2-- > pipeline ) *(pipeline2+1) = *pipeline2;
// Copy the pipe at this now free location
}
*pipeline = *pipe;
return true;
}
pipeline++;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Clear a pipe in a transformation pipeline slot at a specific index
// and compress pipes to remove "holes".
// Return true if pipe cleared succefully.
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipe_ClearSlotIndexPid(uint8_t pipelineSlot, boolean isIndex,uint8_t value) {
// The slot must exists here
if (pipelineSlot < 1 || pipelineSlot > TRANS_PIPELINE_SLOT_SIZE ) return false;
if ( isIndex && value >= TRANS_PIPELINE_SIZE) return false;
else if ( !isIndex && value >= FN_TRANSPIPE_VECTOR_SIZE) return false;
pipelineSlot--; // Adjust for C array
// Get a pointer of the 1st/last pipes in the attached pipeline
transPipe_t *pipeline = EE_Prm.pipelineSlot[pipelineSlot].pipeline ;
transPipe_t *pipeline2 = &EE_Prm.pipelineSlot[pipelineSlot].pipeline[TRANS_PIPELINE_SIZE-1] ;
// Find the index/pId for the pipe in the pipeline.
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
// Nothing to do if pipe is empty
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) return false;
if ( (isIndex && i == value) || (!isIndex && pipeline->pId == value) ) {
// // move other pipes up at the current index
while ( pipeline < pipeline2) {
*pipeline = *(pipeline+1);
pipeline++;
}
// Clear the last pipe
TransPacketPipe_Clear(pipeline2);
return true;
}
pipeline++;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// ByPass/enable a pipe in a transformation pipeline slot with at a specific index
// Return true if pipe bypassed succefully.
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipe_ByPass(uint8_t pipelineSlot, uint8_t index,uint8_t byPass) {
// The slot must exists here
if (pipelineSlot < 1 || pipelineSlot > TRANS_PIPELINE_SLOT_SIZE ) return false;
if ( index >= TRANS_PIPELINE_SIZE) return false;
if (byPass >1) return false;
pipelineSlot--; // Adjust for C array
// Get a pointer of the 1st pipe in the attached pipeline
transPipe_t *pipeline = EE_Prm.pipelineSlot[pipelineSlot].pipeline ;
// Find the index for the pipe in the pipeline.
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
// Nothing to do if pipe is empty
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) return false;
if ( i == index ) {
pipeline->byPass = byPass;
return true;
}
pipeline++;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Packet transformer pipeline - Exec
//-----------------------------------------------------------------------------
// Modify the packet according pipeline slot affected to the source port
// return true if the result must be routed, false if the packet is finally dropped
///////////////////////////////////////////////////////////////////////////////
boolean TransPacketPipelineExec(uint8_t source, uint8_t slot , midiPacket_t *pk) {
if ( slot < 1 || slot > TRANS_PIPELINE_SLOT_SIZE) return false;
// Check if the pipeline slot is already running, and block it to avoid infinite loop
if ( slotLockMsk & ( 1 << --slot) ) return true; // True will allow port routing
slotLockMsk |= (1 << slot); // Lock slot
transPipe_t *pipeline = EE_Prm.pipelineSlot[slot].pipeline;
// Save the original midi packet
midiPacket_t pkSource = *pk;
boolean r = true ;
// Apply transformation function pipes
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
// Apply active pipes only
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) break;
if ( ! pipeline->byPass ) {
r = MidiTransFnVector[pipeline->pId].pipeFn(source, &pkSource, pk , pipeline);
if (! r ) break;
}
pipeline++;
}
slotLockMsk &= ~(1 << slot); // Unlock slot
return r;
}
//////////////////////////////////////////////////////////////////////////////
// Show the content of a pipeline
///////////////////////////////////////////////////////////////////////////////
void ShowPipelineSlot(uint8_t s) {
if (s < 1 || s > TRANS_PIPELINE_SLOT_SIZE ) return ;
// Get a pointer of the 1st pipe in the attached pipeline
transPipe_t *pipeline = EE_Prm.pipelineSlot[s-1].pipeline ;
Serial.println();
SerialPrintf("%M %M %d :",str_PIPELINE,str_SLOT,s);
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) {
Serial.println(" EMPTY");
} else {
Serial.println();Serial.println();
SerialPrintf("| Idx | Pipe id ( p1, p2, p3, p4 ) | %y |%n",str_BYPASS);
for (uint8_t i=0; i != TRANS_PIPELINE_SIZE ; i++) {
SerialPrintf("| %2d | %02x %s (",i,pipeline->pId,MidiTransFnVector[pipeline->pId].shortName);
SerialPrintf(" %02x, %02x, %02x, %02x ) |",pipeline->par1,pipeline->par2,pipeline->par3,pipeline->par4);
SerialPrintf(" %c |%n",pipeline->byPass ? 'X':' ');
pipeline++;
if ( pipeline->pId == FN_TRANSPIPE_NOPIPE ) break;
}
}
Serial.println();
SerialPrintf("| Attached %s | %s |%n",str_PORT,str_71DIGITS);
SerialPrintf("| | %s |%n",str_16DIGITS);
SerialPrintf("| %y %s | ",str_CABLE,str_OUT);
for (uint8_t j=0; j < 16 ; j++) {
if (j >= UsbCableInterfaceMax) Serial.print(" ");
else if ( EE_Prm.rtRulesCable[j].slot == s)
Serial.print ("X");
else Serial.print(".");
}
Serial.println(" |");
SerialPrintf("| %y %s | ",str_JACK,str_IN);
for (uint8_t j=0; j < 16 ; j++) {
if (j >= SERIAL_INTERFACE_COUNT) Serial.print(" ");
else if ( EE_Prm.rtRulesJack[j].slot == s)
Serial.print ("X");
else Serial.print(".");
}
Serial.println(" |");
SerialPrintf("| %y %s %y | ",str_JACK,str_IN,str_ITHRU);
for (uint8_t j=0; j < 16 ; j++) {
if (j >= SERIAL_INTERFACE_COUNT) Serial.print(" ");
else if ( EE_Prm.rtRulesIthru[j].slot == s)
Serial.print ("X");
else Serial.print(".");
}
Serial.println(" |");
SerialPrintf("| %y %s | ",str_VIRTUAL,str_IN);
for (uint8_t j=0; j < 16 ; j++) {
if (j >= VIRTUAL_INTERFACE_MAX) Serial.print(" ");
else if ( EE_Prm.rtRulesVirtual[j].slot == s)
Serial.print ("X");
else Serial.print(".");
}
Serial.println(" |");
}