Skip to content

Commit

Permalink
First commit of wind_mapper.js, lots of small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Feb 17, 2015
1 parent b1811d9 commit 248d521
Show file tree
Hide file tree
Showing 7 changed files with 573 additions and 93 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ PREPROCESS=gcc\

MAX4LIVE=~/Music/Ableton/User\ Library/Presets/MIDI\ Effects/Max\ MIDI\ Effect

all: conductor speedlimit
all: conductor speedlimit wind_mapper

speedlimit: max/speedlimit/speedlimit.jso
wind_mapper: max/wind_mapper/wind_mapper.jso
conductor: ${MAX4LIVE}/conductor/conductor.jso

gather:
Expand Down
122 changes: 122 additions & 0 deletions js/max/wind_mapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "swirly/max/inlets.js"
#include "swirly/util/logging.js"

autowatch = 1;
outlets = 1;

function convertColor(color) {
return Math.floor(Math.min(color * 128, 127));
};

// From http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c

/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function hsvToRgb(h, s, v) {
var r, g, b;

var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);

switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}

return [convertColor(r), convertColor(g), convertColor(b)];
}



var breath_level = 0, // 0..1
mic_level = 0, // 0..1
note_level = 0, // 0..1
note_min = 10,
note_range = 7 * 12,
note_max = note_min + note_range - 1,

tilt_min = 40,
tilt_max = 60,
tilt_range = tilt_max - tilt_min + 1,

dimmer_min = 8,
dimmer_max = 134,
dimmer_range = dimmer_max - dimmer_min + 1,

channel_r = 6,
channel_g = channel_r + 1,
channel_b = channel_g + 1,
channel_tilt = 2,
channel_dimmer = 5,

gain = 1.0,
threshold = 0.0;

function limit(x) {
return Math.max(0.0, Math.min(1.0, x));
};

function levelOut() {
var level = Math.max(mic_level, breath_level);
var value = Math.min(dimmer_max, Math.floor((dimmer_min + level * dimmer_range) / 2));
outlet(0, channel_dimmer, value);

var value2 = Math.min(tilt_max, Math.floor(tilt_min + level * tilt_range) / 2);
outlet(0, channel_tilt, Math.floor(value2));
};

Max.SetInlets(
['note',
function(note) {
var hue = limit((note - note_min) / note_range);
var rgb = hsvToRgb(hue, 1.0, 1.0);
outlet(0, channel_r, rgb[0]);
outlet(0, channel_g, rgb[1]);
outlet(0, channel_b, rgb[2]);
},
'Note number.'],

['breath',
function(breath) {
breath_level = breath / 127.0;
levelOut();
},
'Breath control.'],

['mic',
function(mic) {
mic_level = Math.max(0.0, mic - threshold) * gain;
levelOut();
},
'Mic level (float).'],

['gain',
function(g) {
gain = g;
},
'Mic gain.'],

['threshold',
function(t) {
threshold = t;
},
'Mic trigger threshold.']
);

LOADED();
2 changes: 2 additions & 0 deletions js/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This is the new one.

// This is /development/swirly/js/test.js
// in /development/swirly/test.maxpat

Expand Down
2 changes: 1 addition & 1 deletion max/speedlimit/speedlimit.jso
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,4 @@ Max.SetInlets(['queue', _speedlim.Output,
'Any input here clears the speedlimit queue.']);

post('Speedlimit set to ' + _speedlim.limit + 'ms\n');
post('Original source:', "js/max/speedlimit.js", ' Compile date:', 'Sun Oct 5 16:34:39 EDT 2014');
post('Original source:', "js/max/speedlimit.js", ' Compile date:', 'Tue Feb 17 00:03:49 EST 2015');
177 changes: 177 additions & 0 deletions max/wind_mapper/wind_mapper.jso
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@






var Max = new Object();

Max.inlets = {};
Max.messageNames = {msg_int: 1, msg_float: 1, list: 1};
Max.applyEntry = false;

// Name each inlet and set a callback function
// Usage:
// Max.SetInlets(['inletName', callbackFn, 'help'],
// ['nextInletName', callbackFn2, 'more help']);
// If there is no help entry, it defaults to the name of the inlet.
Max.SetInlets = function(_) {
inlets = arguments.length;
for (var i = 0; i < arguments.length; ++i) {
var entry = arguments[i];
Max.inlets[i] = {name: entry[0], func: entry[1]};
var help = entry[2] ? (entry[0] + ": " + entry[2]) : entry[0];
setinletassist(i, help);
}
};

// Return the name of the current inlet, or the numeric name if you haven't set
// the names.
Max.Inlet = function() {
return (inlet in Max.inlets) ? Max.inlets[inlet] : inlet;
};

function anything(_) {
var entry = Max.inlets[inlet];
if (entry && entry.func) {
var args = arrayfromargs(arguments);
if (!Max.messageNames[messagename])
args = [messagename].concat(args);
if (Max.applyEntry)
entry.func.apply(this, args);
else
entry.func(args);
} else {
post("Didn't understand input for", Max.Inlet(), '\n');
}
};



var Logging = {};

Logging.setLogging = function(on) {
Logging.Log = on ? Postln : function() {};
};

Logging.setLogging(false);

autowatch = 1;
outlets = 1;

function convertColor(color) {
return Math.floor(Math.min(color * 128, 127));
};

// From http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c

/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function hsvToRgb(h, s, v) {
var r, g, b;

var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);

switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}

return [convertColor(r), convertColor(g), convertColor(b)];
}



var breath_level = 0, // 0..1
mic_level = 0, // 0..1
note_level = 0, // 0..1
note_min = 10,
note_range = 7 * 12,
note_max = note_min + note_range - 1,

tilt_min = 40,
tilt_max = 60,
tilt_range = tilt_max - tilt_min + 1,

dimmer_min = 8,
dimmer_max = 134,
dimmer_range = dimmer_max - dimmer_min + 1,

channel_r = 6,
channel_g = channel_r + 1,
channel_b = channel_g + 1,
channel_tilt = 2,
channel_dimmer = 5,

gain = 1.0,
threshold = 0.0;

function limit(x) {
return Math.max(0.0, Math.min(1.0, x));
};

function levelOut() {
var level = Math.max(mic_level, breath_level);
var value = Math.min(dimmer_max, Math.floor((dimmer_min + level * dimmer_range) / 2));
outlet(0, channel_dimmer, value);

var value2 = Math.min(tilt_max, Math.floor(tilt_min + level * tilt_range) / 2);
outlet(0, channel_tilt, Math.floor(value2));
};

Max.SetInlets(
['note',
function(note) {
var hue = limit((note - note_min) / note_range);
var rgb = hsvToRgb(hue, 1.0, 1.0);
outlet(0, channel_r, rgb[0]);
outlet(0, channel_g, rgb[1]);
outlet(0, channel_b, rgb[2]);
},
'Note number.'],

['breath',
function(breath) {
breath_level = breath / 127.0;
levelOut();
},
'Breath control.'],

['mic',
function(mic) {
mic_level = Math.max(0.0, mic - threshold) * gain;
levelOut();
},
'Mic level (float).'],

['gain',
function(g) {
gain = g;
},
'Mic gain.'],

['threshold',
function(t) {
threshold = t;
},
'Mic trigger threshold.']
);

post('Original source:', "js/max/wind_mapper.js", ' Compile date:', 'Tue Feb 17 00:03:49 EST 2015');
Loading

0 comments on commit 248d521

Please sign in to comment.