Skip to content

Commit

Permalink
- Fix grouped device bug
Browse files Browse the repository at this point in the history
- Add MIDI mappable preset recall
- Update for Live 11
  • Loading branch information
Michael Ganss committed Nov 22, 2022
1 parent c405bb5 commit 95a7cb4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
Binary file modified DrumBrain.amxd
Binary file not shown.
89 changes: 66 additions & 23 deletions drumbrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var maxContext = 32;
var numInstruments = 12;
var style = "house";
var clipLength = 32;
var majorVersion;

function bang() {
generateAll();
Expand All @@ -35,6 +36,18 @@ function generateAll() {
clipOut();
}

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 generate(i, dumpStorage) {
if (dumpStorage === undefined || dumpStorage)
callPresetStorageDump();
Expand Down Expand Up @@ -81,14 +94,15 @@ var storage = {};

function getTextToContinue(i) {
if (i > 0 && storage["cont"] === 1) {
var track = new LiveAPI("this_device canonical_parent");
var trackPath = getTrackPath();
var track = new LiveAPI(trackPath);
var clipSlots = track.getcount("clip_slots");

for (var clipSlotNum = 0; clipSlotNum < clipSlots; clipSlotNum++) {
var clipSlot = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum);
var clipSlot = new LiveAPI(trackPath + " clip_slots " + clipSlotNum);
var hasClip = clipSlot.get("has_clip").toString() !== "0";
if (hasClip) {
var firstClip = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum + " clip");
var firstClip = new LiveAPI(trackPath + " clip_slots " + clipSlotNum + " clip");
var notes = getClipNotes(firstClip);
var clipLength = firstClip.get("length");
var texts = getTextFromMidi(notes, clipLength);
Expand All @@ -101,14 +115,15 @@ function getTextToContinue(i) {
}

function createClip() {
var track = new LiveAPI("this_device canonical_parent");
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("this_device canonical_parent clip_slots " + clipSlotNum);
clipSlot = new LiveAPI(trackPath + " clip_slots " + clipSlotNum);
var hasClip = clipSlot.get("has_clip").toString() !== "0";
if (!hasClip) break;
}
Expand All @@ -117,12 +132,12 @@ function createClip() {
// have to create new clip slot (scene)
var set = new LiveAPI("live_set");
set.call("create_scene", -1);
clipSlot = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum);
clipSlot = new LiveAPI(trackPath + " clip_slots " + clipSlotNum);
}

var beats = Math.ceil(clipLength / 4);
clipSlot.call("create_clip", beats);
var clip = new LiveAPI("this_device canonical_parent clip_slots " + clipSlotNum + " clip");
var clip = new LiveAPI(trackPath + " clip_slots " + clipSlotNum + " clip");
var notes = generateMidi();

setNotes(clip, notes);
Expand All @@ -137,15 +152,37 @@ function Note(pitch, start, duration, velocity, muted) {
}

function setNotes(clip, notes) {
clip.call("set_notes");
clip.call("notes", notes.length);
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);
}

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);
}
var notesToAdd = {
notes: noteSpecifications
};

clip.call("done");
clip.call("add_new_notes", notesToAdd);
}
}

var callingPresetStorageDump = false;
Expand Down Expand Up @@ -280,6 +317,7 @@ var ids = {
var init = false;

function liveInit() {
majorVersion = new LiveAPI("live_app").call("get_major_version");
init = true;
if (ids.out !== 0) {
setOut(ids.out);
Expand Down Expand Up @@ -314,16 +352,21 @@ function clipOut() {
}

function replaceAllNotes(clip, notes) {
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);
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);
}

clip.call("done");
}

function callNote(clip, note) {
Expand Down

0 comments on commit 95a7cb4

Please sign in to comment.