diff --git a/DrumBrain.amxd b/DrumBrain.amxd index 8f24f43..8044b65 100644 Binary files a/DrumBrain.amxd and b/DrumBrain.amxd differ diff --git a/drumbrain.js b/drumbrain.js index d1a7399..32eb6b2 100644 --- a/drumbrain.js +++ b/drumbrain.js @@ -10,6 +10,7 @@ var maxContext = 32; var numInstruments = 12; var style = "house"; var clipLength = 32; +var majorVersion; function bang() { generateAll(); @@ -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(); @@ -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); @@ -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; } @@ -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); @@ -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; @@ -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); @@ -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) {