-
Notifications
You must be signed in to change notification settings - Fork 11
/
acid.js
315 lines (265 loc) · 7.61 KB
/
acid.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
inlets = 1;
outlets = 3;
autowatch = 1;
include("lm.js");
var temperature = 1.0;
var velocity = 100;
var patternLength = 16;
var currentSteps = [];
var majorVersion;
function setInitialized(v) {
if (v === 0) {
outlet(2, 1);
bang();
outlet(0, "zoom_fit");
}
}
function generateLetter(lm, history, order, temperature) {
var h = history.slice(-order);
var dist = {};
var l = lm[h];
for (var key in l) {
if (l.hasOwnProperty(key)) {
var v = l[key];
dist[key] = (v / temperature) + 1.0 - (1.0 / temperature);
}
}
var x = Math.random();
for (var k in dist) {
if (dist.hasOwnProperty(k)) {
var p = dist[k];
x = x - p;
if (x <= 0.0) return k;
}
}
}
function generateText(lm, order, nletters, temperature) {
var starts = Object.keys(lm).filter(function (k) { return k.slice(-2) === "~\n"; });
var history = starts[Math.floor(Math.random() * Math.floor(starts.length))];
var s = "";
for (var i = 0; i < nletters; i++) {
var c = generateLetter(lm, history, order, temperature);
history = history.slice(1) + c;
s += c;
}
return s;
}
function generatePattern(lm, order, patternLength, temperature) {
var stepLength = 9;
var text = generateText(lm, order, (stepLength + 2) * patternLength, temperature);
var lines = text.split("\n");
var re = /^(.)!([01])\$([01])#([01])=$/;
var s = 0;
var steps = [];
var c1 = 36;
for (var l = 0; l < lines.length; l++) {
var line = lines[l];
if (line === "~") continue;
var m = line.match(re);
if (m === null) continue;
var step = {
note: m[1].charCodeAt(0) - 82 + c1,
accent: m[2] === "1",
slide: m[3] === "1" ? 1 : 0,
gate: m[4] === "1" ? 1 : 0
};
steps.push(step);
s = s + 1;
if (s === patternLength) break;
}
return steps;
}
function sendCurrentSteps() {
for (var s = 0; s < currentSteps.length; s++) {
var step = currentSteps[s];
outlet(0, "step", s + 1, step.note, step.accent ? 127 : velocity, 120, step.slide, step.gate);
}
clipOut();
}
function bang() {
currentSteps = generatePattern(lm, 18, patternLength, temperature);
sendCurrentSteps();
outlet(1, "bang");
}
function setTemperature(t) {
temperature = t;
}
function setVelocity(v) {
velocity = v;
sendCurrentSteps();
}
function setPatternLength(p) {
patternLength = p;
clipOut();
}
var fixGate = false;
function setFixGate(v) {
fixGate = v === 1;
clipOut();
}
function Note(pitch, start, duration, velocity, muted) {
this.Pitch = pitch;
this.Start = start;
this.Duration = duration;
this.Velocity = velocity;
}
var liveSteps = [];
var liveStepsLen = 0;
function dumpStep(s, i, pitch, velocity, l, slide, gate) {
if (s === "changed") {
clipOut();
} else {
if (gate === undefined) return;
liveSteps[i - 1] = {
pitch: pitch,
velocity: velocity,
slide: slide,
gate: gate
};
liveStepsLen = i;
}
}
function generateMidi() {
var notes = [];
var i;
var previousNote = null;
for (i = 0; i < liveStepsLen; i++) {
var step = liveSteps[i];
var previousStep = i > 0 ? liveSteps[i - 1] : null;
if (!fixGate && step.gate === 0) continue;
if (previousStep !== null && previousStep.pitch === step.pitch && previousStep.slide === 1 && (fixGate || previousStep.gate === 1)) {
// slide and same pitch as previous step -> extend duration of previous note
if (step.slide === 1)
previousNote.Duration += 0.25;
} else {
var note = new Note(step.pitch, i / 4.0, step.slide === 1 ? 0.375 : 0.125, step.velocity);
notes.push(note);
previousNote = note;
}
}
return notes;
}
function getTrackPath() {
var path = "this_device canonical_parent";
var parent = new LiveAPI(path);
while (parent.type !== "Track") {
path = path + " canonical_parent";
parent = new LiveAPI(path);
}
return path;
}
function clip() {
var trackPath = getTrackPath();
var track = new LiveAPI(trackPath);
var clipSlots = track.getcount("clip_slots");
var clipSlot;
var firstClip = null;
for (var clipSlotNum = 0; clipSlotNum < clipSlots; clipSlotNum++) {
clipSlot = new LiveAPI(trackPath + " clip_slots " + clipSlotNum);
var hasClip = clipSlot.get("has_clip").toString() !== "0";
if (!hasClip) break;
}
if (clipSlotNum === clipSlots) {
// have to create new clip slot (scene)
var set = new LiveAPI("live_set");
set.call("create_scene", -1);
clipSlot = new LiveAPI(trackPath + " clip_slots " + clipSlotNum);
}
var beats = Math.ceil(patternLength / 4);
clipSlot.call("create_clip", beats);
var clip = new LiveAPI(trackPath + " clip_slots " + clipSlotNum + " clip");
var notes = generateMidi();
setNotes(clip, notes);
}
function setNotes(clip, notes) {
if (majorVersion <= 10) {
clip.call("set_notes");
clip.call("notes", notes.length);
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
clip.call("note", note.Pitch, note.Start.toFixed(4), note.Duration.toFixed(4), note.Velocity, note.Muted);
}
clip.call("done");
} else {
var noteSpecifications = [];
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
var noteSpecification = {
pitch: note.Pitch,
start_time: note.Start,
duration: note.Duration,
velocity: note.Velocity,
mute: note.Muted
};
noteSpecifications.push(noteSpecification);
}
var notesToAdd = {
notes: noteSpecifications
};
clip.call("add_new_notes", notesToAdd);
}
}
var clips = {
out: null
};
var ids = {
out: 0
};
var init = false;
function liveInit() {
majorVersion = new LiveAPI("live_app").call("get_major_version");
init = true;
if (ids.out !== 0) {
setOut(ids.out);
}
}
function setClip(name, id) {
if (!init) {
ids[name] = id;
return;
}
if (id === 0) {
clips[name] = null;
return;
}
var clipId = "id " + id;
clips[name] = new LiveAPI(clipId);
}
function setOut(id) {
setClip("out", id);
clipOut();
}
function clipOut() {
if (clips.out !== null) {
var outClip = clips.out;
callPatternStepDump();
var stepNotes = generateMidi();
if (stepNotes === undefined) stepNotes = [];
replaceAllNotes(outClip, stepNotes);
}
}
function replaceAllNotes(clip, notes) {
if (majorVersion <= 10) {
clip.call("select_all_notes");
clip.call("replace_selected_notes");
clip.call("notes", notes.length);
for (var i = 0; i < notes.length; i++) {
var note = notes[i];
callNote(clip, note);
}
clip.call("done");
} else {
clip.call("remove_notes_extended", 0, 127, 0, 4294967295);
setNotes(clip, notes);
}
}
function callNote(clip, note) {
clip.call("note", note.Pitch, note.Start.toFixed(4), note.Duration.toFixed(4), note.Velocity, note.Muted);
}
function callPatternStepDump() {
var patternStep = this.patcher.getnamed("patternStep");
patternStep.message("dump");
}
function recallPreset() {
clipOut();
}