Help with M4L Javascript and get_selected_notes

autodidactic's icon

I'm trying to retrieve all the midi information from a specific clip on a specific track. I've managed to get data returned but what I am assuming is the pitch info doesn't match what's being played in the clip. The code is below:

function myfunction(trk, slot){
var api = new LiveAPI(function(result){
        this.call('select_all_notes');
        var notes = this.call('get_selected_notes','pitch'); //adding the pitch argument seems to make no difference
        /*
        SHOULD RETURN:
        count [int] is the number of note lines to follow.
        pitch [int] is the MIDI note number, 0...127, 60 is C3.
        time [double] is the note start time in beats of absolute clip time.
        duration [double] is the note length in beats.
        velocity [int] is the note velocity, 1 ... 127.
        muted [bool] is 1 if the note is deactivated.
        */
        post(notes);

    }, "live_set tracks "+trk+" clip_slots "+slot+" clip");
}

The output I get looks like this: "notes, 312, note, 46,13.12,0.12,76,0,note,48,13,0.12,73,0,note,48...etc"

Based on the docs the first two values tell me the "count" or total number of notes in the clip. and then there is a "note" followed by 5 values. I am assuming that as per the docs, the values should be pitch, time, duration,velocity,muted in that order. However as I stated before, the pitches listed do not match what is being played and in this set I put together there is only one midi clip. Am I reading this wrong? or is there something wrong with how I'm calling the function?

Furthermore, according to the docs, arguments can be added to "get_selected_notes" so if I wanted only the pitch values I could add the "pitch" argument to "get_selected_notes". When I tried to add the pitch argument in javascript it still returns everything though. What am I doing wrong?

autodidactic's icon

I also posted this question in the Ableton forums and figured it out. I filtered the data in js and took a second look. It turns out that "get_selected_notes" returns all the notes in ascending pitch order. So it would list all the E3s and then the E4s and so on. So that makes sense now. If I want to list the notes by order of appearance then I have to sort it myself.

Tom Swirly's icon

Oh, sorry I missed this at the time!

I have a tool to help you here.

Here's something that gets all the notes from a track, puts them into a neat JS dictionary, and then sorts them according to time.

Jay Walker's icon

Hey Tom, thanks for sharing! That looks interesting and I'd like to give it a try. My goal is to store midi notes in a file and then swap them out as midi presets. For example, one clip could have several different midi alternatives I could call. Unfortunately, I only know how to delete all notes from a clip or import notes one by one using JS. The Max get notes function returns stuff like:

"[notes,58,note,36,0,0.5625,127,1,note,36,1,0.515625,127,1,note,36,2,0.5390625,127,1,note,36,3,0.4453125,127,1"...

But I can't figure out a way to bulk import this information onto a clip. Is your example pointing towards a solution to bulk import the get notes data? If so, do you have hints on implementing your code and getting started with it? I'm not sure how to get things going! Thank you!