SOLVED - writing binary (sysex) file using Javascript


    Aug 15 2019 | 6:26 pm
    Relative Javascript newb here. I have a sequence of ~3000 integers that I will be generating in Max that I need to write to a binary file (a Sysex (.syx) file), whose full path I will also have available within the remainder of my Max patch. Best I can tell, Javascript seems the best tool for the job! (If not someone please point me somewhere else - and I'd prefer not to go the Java route if possible).
    Could someone help with some initial code that I might look at to help me on my way? I have written rudimentary Javascript tools before so you don't need to start at square one, but in looking online I'm getting confused with the various Javascript writing methods, typed arrays, should I use Node.js or not, etc...
    Thanks for any help in advance!

    • Aug 17 2019 | 11:31 pm
      Bump - anyone?
    • Aug 18 2019 | 4:58 pm
      OK, making attempts myself at using Node for Max, hoping someone can help. Here's a rudimentary attempt (javascript and Max patch below), which is giving me an error saying it can't create the file, but I thought the fs.createWriteStream() method would make the file if it didn't exist? I'm using createWriteStream by the way because I'll be sending each byte I need written one at a time (over 3000 of them in my actual use case, just 4 as a test in the example below).
      const fs = require("fs");
      const maxAPI = require("max-api");
      var wstream;
      const handlers = {
       
      	'file_to_write': (arg) => {
      		wstream = fs.createWriteStream(arg);
      	},
      	'writebyte': (arg) => {
      		wstream.write(new Buffer(arg), 'binary');
      	}
      };
      maxAPI.addHandlers(handlers);
    • Aug 20 2019 | 1:31 am
      Just to follow up for myself when I forget how I do this down the road - didn't need Node.js at all, was very simple using regular Max Javascript:
      
      inlets = 1;
      var f;
      function file_to_write (filename) {
      	f = new File(filename, "write", "Midi");
      }
      function writebyte (thebyte) {
      	f.writebytes(thebyte);
      }