-
Notifications
You must be signed in to change notification settings - Fork 1
/
MidiBot.js
400 lines (292 loc) · 9.72 KB
/
MidiBot.js
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
//-----------------------------------------------------------------------------
// MidiBot =================== 24-FEB-2018
// Build: 1.06b
//
// A Logic/MainStage MIDI FX 'Scripter' hack
// robbykilgore.com
// midirotator@gmail.com
//
// NUMBER_OF_VOICES: Number of concurrent MidiBot voices (Default=1)
//
// THE GLOBAL PARAMETERS
// ---------------------------------------------------------------
// Sequence Length: The number of steps in the sequence
// Clock Divisions: The number of subdivisions
// Swing: The amount of swing applied
// Midi Input: Enables/Disables midi note input to the Root parameter
//
// VOICE PARAMETERS
// ---------------------------------------------------------------
// Root: The Root
// Modality: Scale or Mode
// Note Range: The number of notes to choose from the Modality
// Play Every: Plays a note every X number of Clock Divisions
// Offset: The number of Clock Divisions to offset the "Play Every" notes
// Root Probability: Applies to "Play Every" notes
// Root Velocity: Applies to "Play Every" notes
// Fill Probability: The chance that a note will be inserted on non "Play Every" steps
// Fill Velocity: The velocity of the fill notes
//
// PLAY: Activate MidiBot.
// STOP: All notes off. Resets counters.
//
//-----------------------------------------------------------------------------
// USER CAN ADJUST THESE VALUES
var NUMBER_OF_VOICES = 1; // 1, 2, or 3 voices work ok
// RYTHMIC INITIALIZATION
var divisionLabels = [
"1/16t", "1/16", "1/16d",
"1/8t", "1/8", "1/8d",
"1/2t", "1/2", "1/2d"
];
var divisionBeats = [
.166, .25, .375, // 1/16t, 1/16, 1/16d
.333, .5, .75, // 1/8t, 1/8, 1/8d
.666, 1, 1.5, // 1/4t, 1/4, 1/4d
1.333, 2, 3 // 1/2t, 1/2, 1/2d
];
var SUBDIVISIONS = divisionBeats[1]; // Change the basic subdivision. Indexed from devisionBeats array (above)
var SWING_PERCENT = 0;
var SWING = 0;
// INITS
// ResetParameterDefaults = true;
var PluginParameters = [];
var NeedsTimingInfo = true;
var MIDI_NOTE = [];
var MODALITY = [];
var NOTE_RANGE = [];
var PLAY_EVERY = [];
var ROOT_PROBABILITY = [];
var VELOCITY = [];
var OFFSET = [];
var FILL_PROBABILITY = [];
var FILL_VELOCITY = [];
var PATTERN_LENGTH = 16;
// HARMONIC INITIALIZATION
var ROOT_NOTES = ["C-2", "C#-2", "D-2", "D#-2", "E-2", "F-2", "F#-2", "G-2", "G#-2", "A-2", "A#-2", "B-2",
"C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", "G#-1", "A-1", "A#-1", "B-1",
"C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0",
"C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1",
"C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2",
"C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3",
"C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4",
"C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
"C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6",
"C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7",
"C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8" ];
var modalSteps = [2,2,1,2,2,2,1];
var pentaSteps = [3,2,3,2,2];
var chromatic = [1,1,1,1,1,1,1,1,1,1,1,1];
var minorMajor = [2,2,1,2,1,2,2];
var fifths = [7,5];
var MODE_LIST = ["Ionian","Dorian","Phrygian","Lydian","Mixolydian","Aolian","Locrian",
"Pentatonic Minor","Pentatonic Major",
"Chromatic","Harmonic Minor","Dorian b9","Lydian #5", "Lydian Dominant", "Mixolydian b13", "Aolian b5","Augmented", "Fifths"]
// MISC INITS
var stepPlayed = false;
var nextBeat = 1;
var count = 1000;
// HANDLE MIDI INPUT
function HandleMIDI(event){
if (GetParameter("Midi Input") == 1) {
SetParameter(5, event.pitch); // Midi Note slider
}
}
// PROCESS MIDI - MAIN LOOP
function ProcessMIDI() {
var musicInfo = GetTimingInfo();
var blockStart = musicInfo.blockStartBeat;
var blockEnd = musicInfo.blockEndBeat
var rightCycle = musicInfo.rightCycleBeat;
var leftCycle = musicInfo.leftCycleBeat;
var cycleBeats = 0;
var cycleBeats = rightCycle - leftCycle;
var lookAhead = 0;
if (musicInfo.playing) {
// LOOP LENGTH
if (count > PATTERN_LENGTH - 1) {
if (count==1000){
1 < blockStart ? nextBeat = blockStart : nextBeat = 1
Trace(nextBeat);
}
count = 0;
}
// For each step in the sequence
if (!stepPlayed){
// THINGS HAPPEN HERE
for (var i = 0; i < NUMBER_OF_VOICES; i++) {
if ((count + OFFSET[i]) % PLAY_EVERY[i] == 0){
playNote(MIDI_NOTE[i], VELOCITY[i], ROOT_PROBABILITY[i], 100, NOTE_RANGE[i], MODALITY[i]);
}else{
playNote(MIDI_NOTE[i], FILL_VELOCITY[i], ROOT_PROBABILITY[i], FILL_PROBABILITY[i], NOTE_RANGE[i], MODALITY[i]);
}
}
// Advance to the next step
if(count%2){
// Unswung Beats
nextBeat += SUBDIVISIONS - SWING;
}else{
// Swung beats
nextBeat += SUBDIVISIONS + SWING;
}
//Trace(SWING)
count += 1;
stepPlayed = true;
}
// Holding for next sequence event
lookAhead = nextBeat + SUBDIVISIONS - SWING;
if (nextBeat < blockEnd){
if (musicInfo.cycling && lookAhead > rightCycle) {
nextBeat -= cycleBeats;
}
Trace(nextBeat)
stepPlayed = false;
}
}else{
// STOPPED - reset midi and counters
MIDI.allNotesOff();
nextBeat = 1;
count = 1000;
}
}
// CALCULATE AND PLAY THE RIGHT NOTES
function playNote(note,vel,Rprob,Fprob,range,modality){
var notes = buildScale(note,range,modality);
var rnd = Math.floor(Math.random() * 100);
var rndNoteIndex = Math.floor(Math.random() * notes.length);
var rndRoot = Math.floor(Math.random() * 100);
var rndFill = Math.floor(Math.random() * 100);
if (Rprob > rndRoot){
_note = note;
}else{
_note = notes[rndNoteIndex];
}
if (Fprob > rnd){
var noteOn = new NoteOn();
noteOn.pitch = _note;
noteOn.velocity = vel;
noteOn.send();
var noteOff = new NoteOff(noteOn);
noteOff.sendAtBeat(nextBeat + SUBDIVISIONS/2);
}
}
//
function ParameterChanged(param, value) {
if (param>13 && param <=24){
param = param + 4;
}else if(param > 24){
param = param + 8;
}
// which voice is it?
var voiceIndex = parseInt((param % 14 == 0) ? param / 14 : (param - 1) / 14);
var swingDistance=0;
switch (param % 14) {
case 0:
PATTERN_LENGTH = value;
break;
case 1:
SUBDIVISIONS = divisionBeats[value];
swingDistance = divisionBeats[value+1] - divisionBeats[value];
break;
case 2:
SWING_PERCENT = value;
SWING = -(swingDistance - (swingDistance * ((SWING_PERCENT/100)+1)));
break;
case 5:
MIDI_NOTE[voiceIndex] = value;
break;
case 6:
MODALITY[voiceIndex] = value;
break;
case 7:
NOTE_RANGE[voiceIndex] = value;
break;
case 8:
PLAY_EVERY[voiceIndex] = value;
break;
case 9:
OFFSET[voiceIndex] = value;
break;
case 10:
ROOT_PROBABILITY[voiceIndex] = value;
break;
case 11:
VELOCITY[voiceIndex] = value;
break;
case 12:
FILL_PROBABILITY[voiceIndex] = value;
break;
case 13:
FILL_VELOCITY[voiceIndex] = value;
break;
default:
break;
}
}
// BUILD GUI CONTROLS
// Global Controls
PluginParameters.push({name:"Sequence Length", type:"linear",
minValue:1, maxValue:32,
numberOfSteps:31, defaultValue:16});
PluginParameters.push({name:"Clock Divisions", type:"menu",
valueStrings:divisionLabels, defaultValue:1});
PluginParameters.push({name:"Swing", type:"%",
minValue:0, maxValue:100,
numberOfSteps:100, defaultValue:0});
PluginParameters.push({name:"Midi Input", type:"checkbox",
valueStrings: ["Disable","Enable"], defaultValue:1});
// Voice Controls
for (var i = 0; i < NUMBER_OF_VOICES; i++) {
var voiceNumber = i + 1;
PluginParameters.push({name:" MIDIBOT 1.06b robby kilgore", type:"text"});
PluginParameters.push({name:"Root", type:"menu",
valueStrings:ROOT_NOTES, defaultValue:60});
PluginParameters.push({name:"Modality", type:"menu",
valueStrings:MODE_LIST, defaultValue:0});
PluginParameters.push({name:"Note Range", type:"linear",
minValue:1, maxValue:24,
numberOfSteps:23, defaultValue:1});
PluginParameters.push({name:"Play Every", type:"linear",
minValue:1, maxValue:32,
numberOfSteps:31, defaultValue:4});
PluginParameters.push({name:"Offset", type:"linear",
minValue:0, maxValue:16,
numberOfSteps:16, defaultValue:0});
PluginParameters.push({name:"Root Probability", type:"%",
minValue:0, maxValue:100,
numberOfSteps:100, defaultValue:0});
PluginParameters.push({name:"Root Velocity", type:"linear",
minValue:0, maxValue:127,
numberOfSteps:127, defaultValue:111});
PluginParameters.push({name:"Fill Probability", type:"%",
minValue:0, maxValue:100,
numberOfSteps:100, defaultValue:0});
PluginParameters.push({name:"Fill Velocity", type:"linear",
minValue:0, maxValue:127,
numberOfSteps:127, defaultValue:75});
}
// CREATE HARMONIC CONTENT
function buildScale(root,range,modality){
var maxCount = range + modality;
var first = true;
var availableNotes = [];
for (i=modality;i<maxCount;i++){
if (first == true){
availableNotes.push(root);
first = false;
}else{
if (modality<7){
availableNotes.push(availableNotes[(i-modality)-1] + modalSteps[(i-1)%7]);
}else if(modality >= 7 && modality < 9) {
availableNotes.push(availableNotes[(i-modality)-1] + pentaSteps[(i-1)%5]);
}else if(modality == 9) {
availableNotes.push(availableNotes[(i-modality)-1] + chromatic[(i-1)%12]);
}else if(modality >= 10 && modality < 17) {
availableNotes.push(availableNotes[(i-modality)-1] + minorMajor[(i-1)%7]);
}else if(modality == 17) {
availableNotes.push(availableNotes[(i-modality)-1] + fifths[(i-1)%2]);
}
}
}
return(availableNotes);
}