art2script.sh 13.2 KB
Newer Older
Steve Schow's avatar
Steve Schow committed
1
2
3
4
5
#!/bin/bash

#==========================================================================
# art2script
#
6
# v 1.5
Steve Schow's avatar
Steve Schow committed
7
8
9
10
11
12
13
14
#
# This script will generate a Scripter script when provided
# with an ArticulationSet Plist file, it will use all keyswitches
# found in the OUTPUT section of the ArticulationSet.
# After generating this script, the OUTPUT section of the articulation set
# should be cleared to blank by the user
#==========================================================================

15
16
17
18
19
20
21
22
23
24
# Usage message function
usage() {
   echo 1>&2
   echo "Usage: $0 [options] <artset.plist>" 1>&2
   echo 1>&2
   echo "Options:" 1>&2
   echo 1>&2
   echo "  --id|-i   (default) Produces full script based on articulationID" 1>&2
   echo "  --lane|-l           Produces full script based on automation lane" 1>&2
   echo "  --array|-a          Only output the ART array" 1>&2
25
26
   echo "  --port|-p           Specify midi port, default=1" 1>&2
   echo "  --channel|-c        Specify midi channel, default=1" 1>&2
27
28
29
30
31
32
33
34
35
36
37
   echo "  --help|-h           Usage Info" 1>&2
   echo 1>&2
}

# First translate long option names 
for arg in "$@"; do
  shift
  case "$arg" in
    "--array") set -- "$@" "-a" ;;
    "--id") set -- "$@" "-i" ;;
    "--lane") set -- "$@" "-l" ;;
38
39
    "--port") set -- "$@" "-p" ;;
    "--channel") set -- "$@" "-c" ;;
40
41
42
43
44
45
46
47
48
49
    "--help") set -- "$@" "-h" ;;
    *)        set -- "$@" "$arg"
  esac
done


# Now parse the actual command line for options
OPTIND=1
ARRAYONLY=false
LANE=false
50
51
CHANNUM=1
PORTNUM=1
52

53
while getopts ":alip:c:h" opt; do
54
55
56
57
58
59
60
61
62
63
64
    case $opt in
    a)
        # Array only
        ARRAYONLY=true
        ;;

    i)
        # articulationID mode (default)
        LANE=false
        ;;

65
66
67
68
69
70
71
72
73
74
    p)
        # specify port
        PORTNUM=${OPTARG}
        ;;

    c)
        # specify channel
        CHANNUM=${OPTARG}
        ;;

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    l)
        # Automation Lane mode
        LANE=true
        ;;

    h)
        usage
        exit 0
        ;;

    \?)
        echo "ERROR: Invalid Option" 1>&2
        usage
        exit 1
        ;;
    esac
done


# remove the options so we can process file name
shift $(expr $OPTIND - 1)


# check for file name provided
Steve Schow's avatar
Steve Schow committed
99
100
if [ $# -lt 1 ]
then
101
102
    echo "ERROR: Missing articulation set plist filename" 1>&2
    usage
Steve Schow's avatar
Steve Schow committed
103
104
105
106
107
108
109
110
    exit 1
fi

FILE=$1

if [ ! -f "$FILE" ]
then
    echo "ERROR: File Not Found: $FILE" >&2
111
    usage
Steve Schow's avatar
Steve Schow committed
112
113
114
    exit 1
fi

115
116
117
#======================================
# verified all cmd line args, proceed
#======================================
Steve Schow's avatar
Steve Schow committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

PB="/usr/libexec/PlistBuddy -c"

#============================
# function handleSwitch()
#============================
handleSwitch () {

    local lKidx=$1
    local lArtID=$2
    local lKs=$3
    local lVel=$4
    local lStatus="$5"

    if [ ${lKidx} -lt 1 ]
    then 
134
        echo -n "    ART[${PORTNUM}][${CHANNUM}][${lArtID}] = {name:\"${artName}\", ks:["
Steve Schow's avatar
Steve Schow committed
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
    else
        echo -n ","
    fi

    #============================
    # Handle NoteOn switches
    #============================
    if [ "$lStatus" = "Note On" ]
    then
        echo $lKs | awk '
              BEGIN {
                split("C|C#|D|D#|E|F|F#|G|G#|A|A#|B", rootNames, "|")
                for(i=0; i<128; i++) {
                    octave= int(i/12)-2"";
                    name[i] = rootNames[i%12 + 1]""octave
                  }
              }
              { printf("\"%s\"", name[$0+0]); }'

    #============================
    # Handle CC switches
    #============================
    elif [ $lStatus = "Controller" ]
    then
        echo -n "[${lKs},${lVel}]"
    fi
}

163
164
165
166
if [ $ARRAYONLY = false ]
then
    if [ $LANE = true ] 
    then
167
        SVER="lanekeyswitcher v1.2"
168
    else
169
        SVER="keyswitcher v1.2"
170
171
    fi

Steve Schow's avatar
Steve Schow committed
172
cat <<EOB
Steve Schow's avatar
Steve Schow committed
173
174
175
176
/****************************************************************
 *
 * This script generated by art2script bash script
 *
177
 * based on: ${SVER}
Steve Schow's avatar
Steve Schow committed
178
179
180
181
182
183
184
 *
 * More info: https://gitlab.com/dewdman42/art2script/-/tree/main
 *
 * Make sure OUTPUT section of articulation set is cleared when
 * using this script to send keyswitches
 *
 ****************************************************************/
Steve Schow's avatar
Steve Schow committed
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
var MAXPORTS=48; 


/********************************************************
 * Initialize ART array
 *
 * ART[port][channel][artid]
 *
 *******************************************************/
  
function Initialize() {

    // Example

Steve Schow's avatar
Steve Schow committed
200
201
EOB

202
203
fi  # end of not ARRAYONLY

204
205
206
207
208
209
210
artsetName=`${PB} "Print Name" "${FILE}" 2>/dev/null`

echo
echo "    /*****************************************************"
echo "     *  port:${PORTNUM}  channel:${CHANNUM}  instrument: ${artsetName%.*}"
echo "     *****************************************************/"
echo
211

Steve Schow's avatar
Steve Schow committed
212
213
214
215
216
217
218
219
if [ $LANE = true ]
then
    echo "    /* initLane */"
    echo "    initLane( ${PORTNUM}, ${CHANNUM}, \"${artsetName%.*}\" );"
    echo
fi


Steve Schow's avatar
Steve Schow committed
220
221
222
223
224
#==============================================
# Loop through OUTPUT section of Articulations
#==============================================
idx=0
kidx=0
Steve Schow's avatar
Steve Schow committed
225
nextID=0
Steve Schow's avatar
Steve Schow committed
226
227
228
229
230
231
232
233
234
while [ true ]
do
    artID=`${PB} "Print Articulations:${idx}:ArticulationID" "${FILE}" 2>/dev/null`
    if [ $? -ne 0 ]
    then
        break  #no more articulations
    fi

    artID=`echo ${artID} | xargs`
Steve Schow's avatar
Steve Schow committed
235
236
237
238
239
240
241
242

    # If in LANE mode, just use consecutive ID values
    if [ $LANE = true ]
    then
        nextID=$((nextID+1))
        artID=$nextID
    fi

Steve Schow's avatar
Steve Schow committed
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
    artName=`${PB} "Print Articulations:${idx}:Name" "${FILE}" 2>/dev/null`
    found=false

    # Try non-array version of Output first
    status=`${PB} "Print Articulations:${idx}:Output:Status" "${FILE}" 2>/dev/null`
    if [ $? -eq 0 ]
    #================================
    # Handle single switch non-array
    #================================
    then
        found=true
        ks=`${PB} "Print Articulations:${idx}:Output:MB1" "${FILE}" 2>/dev/null`
        ks=`echo $ks | xargs`

        vel=`${PB} "Print Articulations:${idx}:Output:ValueLow" "${FILE}" 2>/dev/null`
        if [ $? -ne 0 ]
        then
            vel=1
        else
            vel=`echo $vel | xargs`
        fi

        handleSwitch 0 ${artID} ${ks} ${vel} "${status}"

    #=======================================
    # Handle multi-switch array 
    #=======================================
    else 
        kidx=0;
        while [ true ]
        do
            status=`${PB} "Print Articulations:${idx}:Output:${kidx}:Status" "${FILE}" 2>/dev/null`
            if [ $? -ne 0 ]
            then
                break
            fi

            found=true
            ks=`${PB} "Print Articulations:${idx}:Output:${kidx}:MB1" "${FILE}" 2>/dev/null`
            ks=`echo $ks | xargs`

            vel=`${PB} "Print Articulations:${idx}:Output:${kidx}:ValueLow" "${FILE}" 2>/dev/null`
            if [ $? -ne 0 ]
            then
                vel=1
            else
                vel=`echo $vel | xargs`
            fi

            handleSwitch ${kidx} ${artID}  ${ks} ${vel} "${status}"
            
            kidx=$((kidx+1))
        done
    fi


    #=========================================
    # If there were keyswitches, end the line
    #=========================================

    if [ $found = true ]
    then
305
306
307
        echo "]}; "
        #echo -n "/"
        #echo "/ ${artName}"
Steve Schow's avatar
Steve Schow committed
308
309
310
311
312
313
314
    fi

    # Increment idx
    idx=$((idx+1))

done

315

Steve Schow's avatar
Steve Schow committed
316

317
318


319
320
321
322
323
324
# If array only then get out
if [ $ARRAYONLY = true ]
then
    exit 0
fi

325
326
327
328
329
330

# Finish Initialize function
echo "    // etc"
echo "}"


331
332
333
334
335
# Now output the actual guts of the script

if [ $LANE = true ]
# Automation Lane handling
then
336

337
cat <<EOB
338

339
340
341
/***************************************************
 ************** DO NOT EDIT BELOW HERE *************
 ***************************************************/
342

343
344
345
346
347
348
349
350
function initLane(port, channel, name ) {

    // build names array for automation
    let names = ["Reset"];
    let a = ART[port][channel];
    if(a != undefined) {
        for(let i=1;i<a.length;i++) {
            if(a[i] != undefined) {
Steve Schow's avatar
Steve Schow committed
351
               names.push(a[i].name);
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
            }
        }
    }

    PluginParameters.push({
        type: "menu",
        name: name,
        defaultValue: 0,
        hidden: true,
        disableAutomation: false,
        valueStrings: names
    });

    LANEIDS[port][channel] = PluginParameters.length-1;
}
367

Steve Schow's avatar
Steve Schow committed
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

var ART = new Array(MAXPORTS+1);
for(let port=0; port<ART.length; port++) {
    ART[port] = new Array(17);
    for(let chan=0; chan<ART[port].length; chan++) {
        ART[port][chan] = [];
    }
}

var lastArtID = new Array(MAXPORTS+1);
for(let port=0; port<lastArtID.length; port++) {
    lastArtID[port] = new Array(17);
    for(let chan=0; chan<lastArtID[port].length; chan++) {
        lastArtID[port][chan] = 0;
    }
}

var LANEIDS = new Array(MAXPORTS+1);
for(let port=0; port<LANEIDS.length; port++) {
    LANEIDS[port] = new Array(17);
    for(let chan=0; chan<LANEIDS[port].length; chan++) {
        LANEIDS[port][chan] = 0
    }
}

var PluginParameters = [];
396
397
398
399
400
401
402
403
404

function HandleMIDI(event) {

    // Everything other then NoteOn
    if( !(event instanceof NoteOn)) {
        event.send();
        return;
    }
      
405
406
407
408
409
410
411
412
413
    // default the port
    if(event.port == undefined) event.port = 1;

    // lookup values per port/channel
    let laneID = LANEIDS[event.port][event.channel];
    let currentArtID = GuiParameters.get(laneID);
    let artObj = ART[event.port][event.channel][currentArtID];
    let last = lastArtID[event.port][event.channel];

414
    // If same artID as last note, skip keyswitching
415
416
417
418
419
420
421
    if(currentArtID == last) {
        event.send();
        return;
    }

    // check for missing entry
    if (artObj == undefined || artObj.ks == undefined) {
422
423
            
        event.send();
Steve Schow's avatar
Steve Schow committed
424
425
        Trace("WARNING: articulation entry not found: ART[" + event.port
                + "][" + event.channel + "][" + event.articulationID + "]");
426
        return;
Steve Schow's avatar
Steve Schow committed
427
428
    } 

429
430
    // remember the current ID 
    lastArtID[event.port][event.channel] = currentArtID;
431
432
    
    // send keyswitches
433
    let ksList = artObj.ks;
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
    
    for(let k=0; k<ksList.length; k++) {
    
        if(Array.isArray(ksList[k])) {
            sendCC(ksList[k], event);
        }
        else {
            sendSwitch(ksList[k], event);
        }
    }
     
    // send actual note    
    event.send();
}

// Function to send a single keyswitch

var ks = new NoteOn;
function sendSwitch(pitchName, evt) {
        ks.pitch = MIDI.noteNumber(pitchName);
        ks.channel = evt.channel;
        ks.velocity = 100;
        ks.send();
        ks.velocity = 0;
        ks.send();   
}

// Function to send a single CC switch

var cc = new ControlChange;
function sendCC(ccArray, evt) {
    cc.number = ccArray[0];
    cc.value = ccArray[1];
    cc.channel = evt.channel;
    cc.send();
}

471
472
473
474
475
476
477
478
479
480
481
482

// cached parameters
var GuiParameters = {
    data: [],
    set: function(id, val) {
        this.data[id] = val;
    },
    get: function(id) {
        if(this.data[id] == undefined) {
            this.data[id] = GetParameter(id);
        }
        return this.data[id];
483
    }
484
485
486
};
function ParameterChanged(id, val) {
    GuiParameters.set(id, val);
487
488
489
490
491
492
493
494
495
}

EOB

# articulationID handling
else

cat <<EOB

Steve Schow's avatar
Steve Schow committed
496
497
498
499
/***************************************************
 ************** DO NOT EDIT BELOW HERE *************
 ***************************************************/

500
501
502
503
504
505
506
507
508
509
510
511
512
513
var ART = new Array(MAXPORTS+1);
for(let port=0; port<ART.length; port++) {
    ART[port] = new Array(17);
    for(let chan=0; chan<ART[port].length; chan++) {
        ART[port][chan] = [];
    }
}

var lastArtID = new Array(MAXPORTS+1);
for(let port=0; port<ART.length; port++) {
    lastArtID[port] = new Array(17);
    for(let chan=0; chan<lastArtID[port].length; chan++) {
        lastArtID[port][chan] = 0;
    }
Steve Schow's avatar
Steve Schow committed
514
515
}

516
    
Steve Schow's avatar
Steve Schow committed
517
518
519
520
521
522
523
524
525
526
527
528
529
function HandleMIDI(event) {

    // Everything other then NoteOn
    if( !(event instanceof NoteOn)) {
        event.send();
        return;
    }
    
    // default articulationID 
    if(event.articulationID == undefined) {
        event.articulationID = 0;
    }
    
530
531
532
    // default midi port
    if(event.port == undefined) event.port = 1;

Steve Schow's avatar
Steve Schow committed
533
    // Track last articulationID
534
535
536
537
538
    let last = lastArtID[event.port][event.channel];
    lastArtID[event.port][event.channel] = event.articulationID;


    if( ART[event.port][event.channel][event.articulationID] == undefined ) {
Steve Schow's avatar
Steve Schow committed
539
540
        event.send();
        Trace("WARNING, missing entry: ART[" + event.port + "][" 
541
542
543
544
                + event.channel+ "][" + event.articulationID + "]");
        return;
    }
       
Steve Schow's avatar
Steve Schow committed
545
    // If same artID as last note, skip keyswitching
546
    if(event.articulationID == last) {
Steve Schow's avatar
Steve Schow committed
547
548
549
550
551
        event.send();
        return;
    }
    
    // send keyswitches
552
553
    
    let ksList = ART[event.port][event.channel][event.articulationID].ks;
Steve Schow's avatar
Steve Schow committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
    for(let k=0; k<ksList.length; k++) {
    
        if(Array.isArray(ksList[k])) {
            sendCC(ksList[k], event);
        }
        else {
            sendSwitch(ksList[k], event);
        }
    }
     
    // send actual note    
    event.send();
}

// Function to send a single keyswitch

var ks = new NoteOn;
function sendSwitch(pitchName, evt) {
        ks.pitch = MIDI.noteNumber(pitchName);
        ks.channel = evt.channel;
        ks.velocity = 100;
        ks.send();
        ks.velocity = 0;
        ks.send();   
}

// Function to send a single CC switch

var cc = new ControlChange;
function sendCC(ccArray, evt) {
    cc.number = ccArray[0];
    cc.value = ccArray[1];
    cc.channel = evt.channel;
    cc.send();
}
589

Steve Schow's avatar
Steve Schow committed
590
591
EOB

592
593
fi  # end of default articulationID handling