Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created October 7, 2018 08:24
Show Gist options
  • Save zeffii/a6d535d6497015fbb64eb4a991b14ead to your computer and use it in GitHub Desktop.
Save zeffii/a6d535d6497015fbb64eb4a991b14ead to your computer and use it in GitHub Desktop.
max msp node.script
const maxApi = require("max-api");
let x = 0;
let y = 0;
maxApi.addHandler("input", (dir) => {
switch (dir) {
case "UP":
y += 1;
break;
case "DOWN":
y -= 1;
break;
case "LEFT":
x += 1;
break;
case "RIGHT":
x -= 1;
}
maxApi.post(`x: ${x}, y: ${y}`);
maxApi.outlet(x, y);
});
@zeffii
Copy link
Author

zeffii commented Oct 7, 2018

reading/loading a file from disk using node's fs

const maxApi = require("max-api");
const fs = require('fs');
var filePath = "C:\\Users\\zeffi\\Desktop\\maxmsp_patches_SC\\drum_patterns.ptx";

fs.readFile(filePath, {encoding: 'utf-8'}, function(err, data) {

    if (!err) {
        maxApi.post('received data: ' + data);
    } else {
        maxApi.post(err);
    }
});

filereader

@zeffii
Copy link
Author

zeffii commented Oct 8, 2018

image

further.. for later

const maxApi = require("max-api");
const fs = require('fs');
var filePath = "C:\\Users\\zeffi\\Desktop\\maxmsp_patches_SC\\drum_patterns.ptx";

// C:\\Users\\zeffi\\Desktop\\maxmsp_patches_SC\\noddemo_ptxreader.js

let mat = [];
let xdata = "";
let xmat = [];

function parse_ptx_data(xdata){
    var xmat = [];
    mat = xdata.split('\n');
    for (matrix in mat) {
        var items = mat[matrix].split(' ');
        var item_list = [];
        for (item in items) {
            item_list.push(parseInt(items[item]));
        }
        if (mat[matrix]) {
            xmat.push(item_list);
        }
    }
    return xmat;
}

maxApi.post("happened");

maxApi.addHandler("input", (msg_type) => {
    maxApi.post(msg_type);

    if (msg_type === "LOAD_MATRIX") {
        fs.readFile(filePath, {encoding: 'utf-8'}, (err, data) => {
            if (!err) {
                maxApi.post('received data: ' + data);
                xdata = data;
                maxApi.outlet(data);
            } else {
                maxApi.post(err);
            }
        });
    } else if (msg_type === "PARSE") {
        xmat = parse_ptx_data(xdata);
        maxApi.post(xmat);
        maxApi.outlet(xmat[0]);
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment