Skip to content

Commit b86ead7

Browse files
author
Michael Ganss
committed
Add clip creation feature
1 parent 87f28fa commit b86ead7

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Aicd.amxd

-2.08 KB
Binary file not shown.

acid.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,93 @@ function setVelocity(v) {
105105
function setPatternLength(p) {
106106
patternLength = p;
107107
}
108+
109+
var fixGate = false;
110+
111+
function setFixGate(v) {
112+
fixGate = v === 1;
113+
}
114+
115+
function Note(pitch, start, duration, velocity, muted) {
116+
this.Pitch = pitch;
117+
this.Start = start;
118+
this.Duration = duration;
119+
this.Velocity = velocity;
120+
}
121+
122+
var liveSteps = [];
123+
var liveStepsLen = 0;
124+
125+
function dumpStep(s, i, pitch, velocity, l, slide, gate) {
126+
if (gate === undefined) return;
127+
liveSteps[i - 1] = {
128+
pitch: pitch,
129+
velocity: velocity,
130+
slide: slide,
131+
gate: gate
132+
};
133+
liveStepsLen = i;
134+
}
135+
136+
function generateMidi() {
137+
var notes = [];
138+
var i;
139+
var previousNote = null;
140+
141+
for (i = 0; i < liveStepsLen; i++) {
142+
var step = liveSteps[i];
143+
var previousStep = i > 0 ? liveSteps[i - 1] : null;
144+
if (!fixGate && step.gate === 0) continue;
145+
if (previousStep !== null && previousStep.pitch === step.pitch && previousStep.slide === 1 && (fixGate || previousStep.gate === 1)) {
146+
// slide and same pitch as previous step -> extend duration of previous note
147+
if (step.slide === 1)
148+
previousNote.Duration += 0.25;
149+
} else {
150+
var note = new Note(step.pitch, i / 4.0, step.slide === 1 ? 0.375 : 0.125, step.velocity);
151+
notes.push(note);
152+
previousNote = note;
153+
}
154+
}
155+
156+
return notes;
157+
}
158+
159+
function clip() {
160+
var track = new LiveAPI("this_device canonical_parent");
161+
var clipSlots = track.getcount("clip_slots");
162+
var clipSlot;
163+
164+
var firstClip = null;
165+
166+
for (var clipSlotNum = 0; clipSlotNum < clipSlots; clipSlotNum++) {
167+
clipSlot = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum);
168+
var hasClip = clipSlot.get("has_clip").toString() !== "0";
169+
if (!hasClip) break;
170+
}
171+
172+
if (clipSlotNum === clipSlots) {
173+
// have to create new clip slot (scene)
174+
var set = new LiveAPI("live_set");
175+
set.call("create_scene", -1);
176+
clipSlot = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum);
177+
}
178+
179+
var beats = Math.ceil(patternLength / 4);
180+
clipSlot.call("create_clip", beats);
181+
var clip = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum + " clip");
182+
var notes = generateMidi();
183+
184+
setNotes(clip, notes);
185+
}
186+
187+
function setNotes(clip, notes) {
188+
clip.call("set_notes");
189+
clip.call("notes", notes.length);
190+
191+
for (var i = 0; i < notes.length; i++) {
192+
var note = notes[i];
193+
clip.call("note", note.Pitch, note.Start.toFixed(4), note.Duration.toFixed(4), note.Velocity, note.Muted);
194+
}
195+
196+
clip.call("done");
197+
}

0 commit comments

Comments
 (0)