Permalink
Browse files

added Javascript externals and updated README

  • Loading branch information...
mykedean committed Sep 11, 2015
1 parent ea6ccd4 commit 59156a7856279b13b47211412496e1e8a16ef84a
@@ -0,0 +1,76 @@
//Beta distribution generator - adapted from "Computer Music" by Dodge and Jerse
//--by Mike Dean
//--GLOBAL-------
this.autowatch = 1; //automatically recompile everytime script is saved
//declare number of inlets and outlets
inlets = 1;
outlets = 1;
//--------------------------------------------
//------------MAIN----------------------------
//--------------------------------------------
//function runs whenever a bang is received
function bang() {
var x = 0;
var y = 0;
var ainv = 1/second_arg();
var binv = 1/third_arg();
//generate random numbers that are not 0 to avoid potential of underflow during exponentiation
while(x==0) {
x = Math.random();
}
while(y==0) {
y = Math.random();
}
//exponentiate
x = Math.pow(x,ainv);
y = Math.pow(y,binv);
//add them together
var sum = x + y;
//divide the first number by the sum
var result = x/sum;
outlet(0, result*first_arg());
}
function first_arg() {
var range = 0;
//define range
if (jsarguments[1] == undefined) {
range = 1; //set range automatically if no argument is given
} else {
range = jsarguments[1]; //otherwise, set resolution to first argument
}
return range;
}
function second_arg() {
//define a
if (jsarguments[2] == undefined) {
a = 0.5; //set a automatically if no argument is given
} else {
a = jsarguments[2]; //otherwise, set resolution to first argument
}
return a;
}
function third_arg() {
//define b
if (jsarguments[3] == undefined) {
b = 0.5; //set a automatically if no argument is given
} else {
b = jsarguments[3]; //otherwise, set resolution to first argument
}
return b;
}
@@ -0,0 +1,94 @@
//----------------------------------------------------------------------------------
//OBJECT ATTRIBUTES
autocompile = 1; //compile on save
//define number of inlets and outlets
inlets = 1;
outlets = 1;
//GLOBALS
//define global variables
var tsk = new Task(repeatingBangs,this); //associate the tsk object with the repeatingBangs() function
var count = 0;
var decay = 1.0;
var dcoeff = -0.0002; // decay coefficient
var polarity = 1; //direction of changing interval time
var count_dir = 1; //direction the counter is moving
//argument defaults
var delay_time = 1000;
//ARGUMENT HANDLING
// process arguments (decay coefficient, note to trigger)
if (jsarguments.length>1) {
delay_time = jsarguments[1];
}
if (jsarguments.length>2) {
dcoeff = jsarguments[2];
}
//------------
//-FUNCTIONS-
//------------
//reset the task everytime a bang is received
function bang() {
stop(); //cancel the task if it's already running
count = 0;
decay = 1.0;
tsk.interval = delay_time; //set the initial interval between bangs
tsk.repeat(); //repeat the task until canceled
}
//function associate with the task object
function repeatingBangs() {
outlet(0, 0); // send a 0 out the left outlet
if(arguments.callee.task.interval<5 || arguments.callee.task.interval>delay_time) {
polarity = polarity*-1;
}
if (count_dir>0) {
count++;
} else {
count--;
}
if (count<1 || count>60 ) {
count_dir = count_dir*-1; //reverse directions
decay = 1.0;
}
//post(count + "\n");
decay = decay*Math.exp(count*dcoeff); // increment the decay variable
// update the task interval
if(polarity>0) {
arguments.callee.task.interval=arguments.callee.task.interval*decay;
} else {
arguments.callee.task.interval=arguments.callee.task.interval/decay;
}
}
//prevent triggering the task from anywhere other than this js object. ie. make function local/private
repeatingBangs.local = 1;
//allow the user to cancel the process by sending the message 'stop' to the object
function stop() {
count = 0;
tsk.interval = delay_time;
tsk.cancel();
}
function msg_int(value) {
if (value==0) {
stop();
} else if (value==1) {
bang();
}
}
@@ -0,0 +1,83 @@
//Gaussian distribution generator - continuous domain
//--by Mike Dean
//--DESCRIPTION--
//Given two arguments, the object generates random numbers that conform to a Gaussian distribution probability density curve
//Argument 1 is the range that numbers are generated in. For example, if 100, numbers will be generated that occur between 0-99
//Argument 2 is the total number of random numbers used to calculate the Gaussian distributed result.
// - A higher number will result in a more narrow curve centered around the mean.
// - A smaller number will result in a wider curve.
// - Less than 3 numbers cannot be used since 2 will result in a triangle distribution
//--GLOBAL-------
this.autowatch = 1; //automatically recompile everytime script is saved
//declare number of inlets and outlets
inlets = 1;
outlets = 1;
//--------------------------------------------
//------------MAIN----------------------------
//--------------------------------------------
//function runs whenever a bang message is received
function bang() {
var range = first_arg(); //range is the range that random numbers are generated between
var size = second_arg(); //size is the total number of random numbers generated to create a distributed result
var sumNumbers = 0; //the sum of all randomly generated numbers
var result = 0; //the result of the calculations
//generate random numbers in the given range and sum them together
for (var i=0; i<size;i++) {
var randomnumber = rand_num(range);
sumNumbers = sumNumbers + randomnumber;
}
//find the average of the numbers generated to get a Gaussian distributed result
result = sumNumbers/size;
outlet(0,result); //print out the leftmost inlet
}
//---------------------------------------------
//Generate a random number - [0,range-1]
//---------------------------------------------
function rand_num(range) {
var rn = Math.random()*range;
return rn;
}
//----------------------------------------------------
//Determine the range that numbers will be generated between
//----------------------------------------------------
function first_arg() {
var range = 0;
//define range
if (jsarguments[1] == undefined) {
range = 1; //set range automatically if no argument is given
} else {
range = jsarguments[1]; //otherwise, set resolution to first argument
}
return range;
}
//--------------------------------------------------------
//Determine how many random numbers to generate for calculations
//--------------------------------------------------------
function second_arg() {
var size = 0;
//define size - total number of random numbers generated
if (jsarguments[2] == undefined) { //set size if no argument is given
size = 100;
} else { //set size from user's arguments
if (jsarguments[2]<3) { //avoid getting a triangle distribution when only 2 numbers are used
size = 3;
} else {
size = jsarguments[2];
}
}
return size;
}
@@ -0,0 +1,34 @@
//GLOBAL -------
//declare attributes
inlets = 1;
outlets = 2;
autowatch = 1; //autocompile
// set constants
var alpha = 1.4;
var beta = 0.3;
// initial conditions (0,0)
msg_int(0);
var xn;
var yn;
//FUNCTIONS-----
function bang() {
//output current
outlet(0,xn);
outlet(1,yn);
xn1 = 1 - alpha*Math.pow(xn,2) + yn;
yn1 = beta*xn;
yn = yn1;
xn = xn1;
}
function msg_int(value) {
if (value==0) {
xn = 0;
yn = 0;
}
}
@@ -0,0 +1,101 @@
//important key facts
//-----------------
//use post() to write to max window
//use outlet() to print out the outlets ---look are the msg_float function for an example
//inlet is a keyword. use to distinguish inputs
//use jsarguments[index] to access arguments inside object box (indexed from 0, where 0 is the filename)
//-----------------------
//---GLOBAL ATTRIBUTES---
//-----------------------
//This code will run as soon as you instantiate the object
//define object's number of inlets/outlets
inlets = 4;
outlets = 3;
//the autowatch message, followed by a 1 enables automatic file watching
//i.e. anytime the currently loaded javascript file has been modified by another editor,
//js will reload and recompile the file
autowatch = 1;
//define a global variable
//this variable will hold it's value even when messages are not sent to object
var global_var = 22;
var some_other_global; //initialize global, set value with runonload()
var another_global = setsomevalue(); //this shows how you can set a variable using a function
//run this function on load to set initial values
runonload();
//-----------------
//---FUNCTIONS----
//-----------------
//These functions are the type of data an object can accept
//------------------------------------------------
//when the object receives a bang, this function runs
function bang() {
//this variable only exists while this function is running. It does not hold its value afterward
var local_var = 100;
//control the behavior depending on what inlet the bang is received.
//if a bang is sent in the 2nd inlet, do this behavior
if(inlet==0) {
//quotes prints text, no quotes is for variables
//use \n to print new line in the max window
post("meow. 1st inlet\n");
}
//if a bang is sent in the 2nd inlet, do this behavior
if(inlet==1) {
//quotes prints text, no quotes is for variables
//use \n to print new line in the max window
post("horray!!! 2nd inlet\n");
}
} //end bang function
function msg_int() {
//this just proves that the first index is the name of the javascript file
//it will post to the max window when it receives a integer in any inlet, since we have not specified
post(jsarguments[0]);
}
//this just shows that to store the incoming float, you store it in the functions argument
//you can call this whatever the fuck you want
function msg_float(the_float) {
//the first arguement is the index of the outlet to be sent out
//the second argument is what is to be sent out
outlet(0,the_float);
}
//use this to process a list of items
function list() {
}
//use this function to process symbols etc, external/labeled variables
//if you want to access the first item in the array, use a[0], 2nd item a[1] etc...
function anything() {
//create an array, stored in variable 'a', with the arguments that are passed to the object
var a = arrayfromargs(messagename, a);
post("received message " + a + "\n");
myval = a;
bang();
}
//These functions are only defined within the code
//------------------------------------------
function runonload() {
//if no argument has been specified, set the value to some default
if (jsarguments[1] == undefined) {
some_other_global = 0;
} else {
some_other_global = jsarguments[1];
}
}
function setsomevalue() {
return 35;
}
Oops, something went wrong.

0 comments on commit 59156a7

Please sign in to comment.