Skip to content

Commit

Permalink
Huge improvement in dynamics logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffJassky committed Nov 12, 2016
1 parent 0d1665a commit 7ad61de
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 54 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
node_modules
public/bower_components
public/bower_components
.AppleDouble
.DS_Store
30 changes: 24 additions & 6 deletions Brobot/brobot.js
Expand Up @@ -13,13 +13,13 @@ module.exports = {
initialize: function(){
// Calculate maximum latency
var slowestInstrument = _.max(config.instruments, function(instrument){
return instrument.softLatencyAbsolute;
return instrument.latency;
});
var maximumLatency = slowestInstrument.softLatencyAbsolute;
var maximumLatency = slowestInstrument.latency;

// instantiate instrument controllers
_.each(config.instruments, function(instrument){
instrument.softLatencyRelative = maximumLatency - instrument.softLatencyAbsolute;
instrument.relativeOffset = maximumLatency - instrument.latency;
instruments[instrument.note] = new instrumentController(instrument);
});
this.initializeArduino();
Expand Down Expand Up @@ -65,11 +65,29 @@ module.exports = {
if ([144,155,153].indexOf(instruction) !== -1){
this.onMidiNote(channel, velocity)
}
if([128,129].indexOf(instruction) !== -1){
this.onInstruction({
instruction: instruction,
channel: channel,
velocity: velocity
});
}
},
onInstruction: function(e){
if(e.instruction === 128){
if(instruments[e.channel]){
instruments[e.channel].determineMinimumDurationForContact();
}
}
},
onMidiNote: function (channel, velocity){
console.log("Midi note incoming", channel, velocity);
if(instruments[channel]){
instruments[channel].queue(velocity);
if(new Date().getHours() >= 10 || true){
if(instruments[channel]){
instruments[channel].queue(velocity);
}
}else{
console.log("Not within operating hours");
}
}
};
};
26 changes: 13 additions & 13 deletions Brobot/config.js
Expand Up @@ -4,44 +4,44 @@ var config = {
name: "(electric) Snare",
note: 40,
pinNumber: 9,
softVelocity: 20,
softLatencyAbsolute: 50
latency: 8 // tested
},
{
name: "(electric) Kick",
note: 36,
pinNumber: 5,
softVelocity: 40,
softLatencyAbsolute: 50
latency: 16 // tested
},
{
name: "(closed) Hat",
note: 42,
pinNumber: 10,
softVelocity: 20,
softLatencyAbsolute: 50
latency: 15 // tested
},
{
name: "Crash (cymbal 1)",
note: 49,
pinNumber: 11,
softVelocity: 20,
softLatencyAbsolute: 50
latency: 15 // tested
},
{
name: "Floor Tom (low)",
note: 41,
pinNumber: 6,
softVelocity: 50,
softLatencyAbsolute: 50
latency: 9 // tested
},
{
name: "Low Tom",
note: 45,
pinNumber: 7,
softVelocity: 50,
softLatencyAbsolute: 50
latency: 9 // tested
}
// {
// name: "Ride",
// note: 51,
// pinNumber: 10,
// softLatencyAbsolute: 8
// }
]
}
module.exports = config;
module.exports = config;
59 changes: 27 additions & 32 deletions Brobot/instrument-controller.js
Expand Up @@ -8,12 +8,25 @@ var InstrumentController = function(instrument, config) {
// properties and methods
InstrumentController.prototype = {

// delayForVelocity(velocity:Int) return Int
// calculates delay in MS from strike to impact for given :velocity
delayForVelocity: function(velocity){
// TODO : Make exponential, not linear
var velocityFactor = 1 / 128 * velocity;
return Math.ceil(this.softLatencyAbsolute * velocityFactor);
// DEVELOPMENT METHODS
determineMinimumDurationForContact: function(){
var self = this;
var min = 5; // minimum duration in milliseconds
var max = 30; // maximum duration in milliseconds

for(var x=min; x<max; x++){
setTimeout(function(duration){
console.log('starting duration test for '+self.pinNumber+' for '+duration+'ms');
self.pin.brightness(255);
setTimeout(
function(){
console.log('Pin ' + self.pinNumber + ' going to 0');
self.pin.brightness(0);
},
duration
)
}.bind(null, x), 1000 * (x - min));
}
},

// durationForVelocity(velocity:Int) return Int
Expand All @@ -22,27 +35,11 @@ InstrumentController.prototype = {
durationForVelocity: function(velocity){
in_min = 127;
in_max = 0;
out_min = 30;
out_max = 30 + this.softLatencyAbsolute;
out_max = this.latency;
out_min = this.latency + 20;
return Math.ceil((velocity - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
},

// trueVelocity(velocity:Int) return Int
// Returns proper velocity considering actual velocity range
trueVelocity: function(velocity){
in_min = 0;
in_max = 127;
out_min = this.softVelocity;
out_max = 127;
return Math.ceil((velocity - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
},

// pinValueForVelocity(velocity:Int) return Int
// Returns a PWM pin value for given :velocity
pinValueForVelocity: function(velocity){
return this.trueVelocity(velocity) * 2;
},

// queue(velocity:Int) return null
// Queue a strike for given :velocity
queue: function(velocity){
Expand All @@ -51,7 +48,7 @@ InstrumentController.prototype = {
function(){
self.strike(velocity)
},
this.delayForVelocity(velocity)
this.relativeOffset
);
},

Expand All @@ -61,16 +58,14 @@ InstrumentController.prototype = {
var self = this;
if(this.hasOwnProperty('pin')){
console.log({
name: this.name,
pin: this.pinNumber,
velocity: velocity,
trueVelocity: this.trueVelocity(velocity),
pwm: this.pinValueForVelocity(velocity),
delay: this.delayForVelocity(velocity),
pwm: 255,
relativeOffset: this.relativeOffset,
duration: this.durationForVelocity(velocity)
});
this.pin.brightness(
this.pinValueForVelocity(velocity)
);
this.pin.brightness(255);
setTimeout(
function(){
console.log('Pin ' + self.pinNumber + ' going to 0');
Expand All @@ -85,4 +80,4 @@ InstrumentController.prototype = {
};

// node.js module export
module.exports = InstrumentController;
module.exports = InstrumentController;
Empty file modified app.js 100644 → 100755
Empty file.
11 changes: 9 additions & 2 deletions package.json
Expand Up @@ -5,9 +5,16 @@
"dependencies": {
"dataio": "^0.1.1",
"express": "^4.13.0",
"johnny-five": "^0.8.81",
"johnny-five": "^0.10.0",
"lodash": "^4.15.0",
"midi": ">= 0.5.0",
"serialport": "^2.1.0",
"moment": "^2.15.0",
"node-pre-gyp": "^0.6.30",
"serialport": "^2.1.2",
"underscore": "^1.8.3"
},
"devDependencies": {
"debug": "^2.2.0",
"node-pre-gyp": "^0.6.30"
}
}

0 comments on commit 7ad61de

Please sign in to comment.