Get note and velocity values from [midiparse] as js function arguments


    Nov 18 2013 | 4:08 pm
    The first outlet of [midiparse] send note and velocity. I want to transfer these to variables in javascript. How can I do this? Does the midiparse send out two ints?? The code below works but it only prints the note value.. (v[0] and v[1] is undefined..)
    function msg_int(v){
    post("enter msg_int\\n");
    post(v + "\\n");
    post("exit msg_int\\n");
    }
    Or maybe I should use the midiin object directly??

    • Nov 18 2013 | 4:51 pm
      list Out leftmost outlet: A note-on message. The first number is a pitch value and the second number is a velocity value.
      The js function to handle a list is list().
      The JavaScript documentation on Basic Techniques says:
      To handle Max lists, i.e., a message that begins with a number, call your function list. In implementing a list function, you’ll probably want to use the Javascript arguments property, since otherwise you couldn’t handle input of varying length.
      Example:
      function list(a)
      {
      post("the list contains",arguments.length, "elements");
      }
      You could also unjoin or unpack the list and send only pitch or velocity to your js object as an int message. Or you could use notein.
    • Nov 18 2013 | 5:06 pm
      note that , note off ( key released ) will report the same note number but with velocity 0 . if you dont want note off's then use [stripnote] connected to [midiparse] to avoid note off as function call
    • Nov 18 2013 | 5:23 pm
      Thanks for the advise! However I must be missing something.. I tried the code below but get the message:"the list contains elements"
      function msg_int(v){
      post("enter msg_int ok\n");
      post("the list contains",v.length,"elements\n");
      }
      I was thinking about using unpack but this would give me two integers and I am not sure how to pass these two values simulaniously to the one and same js function - I assume I then would have to re-pack again or similar..
    • Nov 18 2013 | 5:38 pm
      function msg_int(v){
      
         post("the list contains",arguments.length,"elements\n");
      
      }
    • Nov 18 2013 | 6:31 pm
      Ah ok - so "arguments" is a predefined variable! Ok I am getting closer:)
      (Btw is arguments the same as jsarguments??)
      When I now print the argument.length I get:
      "the list contains 1 elements"
      So I assume this 1 element is the list however I do not find an example in the documentation for how I can extract the value itself..
      (If I try to print arguments[0] I get the name of the .js file itself)
    • Nov 18 2013 | 6:58 pm
      :) function arguments are similar to Array but they are not real array , you cant operate on it like on arrays where you would want to shift ,splice etc .
      In order to make it possible you can make arrays from arguments with function "arrayfromargs" that is implemented into MaxJs only . Anyway its just for the information ! .
      In order to send midi note and velocity together to your msg_int function you need to pack it . [pack i i] or [pack 0 0] . then you will receive both "guys" :)
      jsarguments and arguments that are passed ONTO the MAX object , in this case your JS object . It have nothing to do with your msg_int function .
      jsarguments[0] is js file sure . but jsarguments[1] would stand for the first argument that you specify after your "name.js" argument !
    • Nov 18 2013 | 8:09 pm
      Brilliant - I almost got it now:) Many thanks for the advise!
      This is what works:
      And the code:
      function msg_int(v){
      var myAwesomeArray = arrayfromargs(arguments);
      post("---------------------------------\n");
      post(myAwesomeArray[0],"\n");
      post(myAwesomeArray[1],"\n");
      post("---------------------------------\n");
      }
      The only challenge left is that I need the midi note as a frequency. I use the [mtof] object and try to re-pack and send to the script but the pack converts the float to an int. Any suggestions on how to work around this?
    • Nov 18 2013 | 8:19 pm
      you should check [pack] help first , make it as a routine to check what objects do and expect . the [pack] guy need to know what type of data it is assembling , in your particular case its a float from mtof and int from midiparse / unpack , so your [pack] should stand for [pack f i] or [pack 0. 0] . take a look at help file , you will have wider range ;)
    • Nov 18 2013 | 9:46 pm
      Hi - I do always check the help file first and I tried [pack 0. 0] before posting however then the javascript is failing with the error "js:no function list" (same error with [pack f i])
    • Nov 18 2013 | 10:08 pm
      all good KMLL .
      first of all msg_int will expect only integers , you have got float as a first argument . so here you will be forced to use list() function . OR you can create your own function which name you will [prepend] while sending your packed data !
      another thing , check your [pack] formating . be sure youve typed small "I" instead of small "L" if you use this symbolic type formating . this forum typeface isnt suited for small "I" letters ,where sometimes it can cause wonderings :)
      so again , msg_int will not understand floats at all .
      edit : otherwise post a patch , so it can be seen
    • Nov 18 2013 | 10:27 pm
      Ah - of course :o) my bad. Changing it to msg_float or list made it work fine:)
      Thanks for all help with this - I now have the values I need and I am ready to progress my code!
    • Nov 18 2013 | 10:33 pm
      great KMLL ! have a lot of fun !!!