Skip to content

Instantly share code, notes, and snippets.

@gotascii
Created September 14, 2011 13:35
Show Gist options
  • Save gotascii/1216556 to your computer and use it in GitHub Desktop.
Save gotascii/1216556 to your computer and use it in GitHub Desktop.
Node in Max/MSP
# Compile the coffee in bare joined mode.
{exec, spawn} = require 'child_process'
fs = require 'fs'
util = require 'util'
print = (data) ->
console.log data.trimRight()
task 'build', 'Compile Coffeescript to Javascript', ->
exec 'mkdir -p lib && coffee --bare --output lib --compile --join weird.js src/*.coffee'
task 'clean', 'Remove generated Javascripts', ->
exec 'rm -fr lib'
task 'test', 'Run the tests', ->
vows = exec "vows --spec test/*test*"
vows.stdout.on 'data', print
vows.stderr.on 'data', print
# test/helper.coffee
# Create globals for the classes under test.
global.util = require 'util'
global.assert = require 'assert'
require '../src/array'
global.World = require('../src/world').World
global.Inspector = require('../src/inspector').Inspector
global.Lseq = require('../src/lseq').Lseq
vows = require 'vows'
w = new World 16, 8
gw = new Global "world"
@bang = ->
post w.cells.length
# src/world.coffee
# Define classes as top level vars.
@World = class
constructor: (@lseq, @cells) ->
unless @cells?
@cells = @generateCells()
@reset()
generateCells: ->
len = @length() - 1
@lseq.toCoords(i) for i in [0..len]
# test/world_test.coffee
# Test the globals defined in helper.coffee.
require './helper'
test module, 'World',
'constructor with an lseq and no provided cells':
topic: ->
@lseq = new Lseq({x:0, y:0}, 2, 2)
@world = new World @lseq
'has an lseq': ->
@world.lseq.should eq @lseq
'has an array of cells with 0 state': ->
cells = [
{x: 0, y: 0, state: 0},
{x: 1, y: 0, state: 0},
{x: 0, y: 1, state: 0},
{x: 1, y: 1, state: 0}
]
@world.cells.should deq cells
@gotascii
Copy link
Author

I write the tests, then make the codes to make the tests pass. Then I compile it all down to one file and reference that as a module in Max/MSP. The max_obj.coffee is the key bit of interface between Max and the JS. Yes, this is kinda ghetto, but it is has been working. Honestly, I was just curious to see if I could develop some sort of workflow from node to max and this was my first crack. I think building stuff using a JVM language is probably a better approach. Maybe Rhino would run inside of Max? In any case, let me know if this makes any sense or if you have any questions.

@flowerornament
Copy link

Hey Justin – I don't know node.js, coffee script or js particularly well, but I'm trying to understand here. The first thing that's puzzling me is this: I'm assuming you're running the tests outside of Max (using v8 as the interpreter, not the built-in Max interpreter). In that case, what does v8 make of the function Lseq (which I'm assuming is Lseq from Lobjects)?

@gotascii
Copy link
Author

Actually, LSeq is just the name of a class I created. It's located in /src and pulled into the test environment in helper.coffee: global.Lseq = require('../src/lseq').Lseq Sorry if that was confusing. I'd post the whole project but it has some closed source bits in it. I'll try and whip up a full example project and post that up as a public repo.

@flowerornament
Copy link

Ok, so you're just writing tested javascript that doesn't make use of the Max API and using it in Max then?

@gotascii
Copy link
Author

I've been stubbing out the Max API using a little mock/stub library I put together, but you've got the basic gist. I've been testing my own class' functionality to make sure those work correctly and then using the resulting JS inside of Max. If you were interested in writing tests that exercised the Max API itself, I would give Rhino a shot. If you are able to get Rhino running inside of Max let me know because I think that could be awesome.

@hems
Copy link

hems commented May 16, 2013

I talked to some c74 guys when i speak at http://code-control.com and showed them coffee script and talked about Node.

Lets see what they will do about it ( :

Hope they enjoyed the ideas, and pay me a beer after they get millionare

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