-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
119 lines (99 loc) · 3.26 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var argv = require('optimist').argv,
express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
path = require('path'),
stylus = require('stylus'),
nib = require('nib'),
five = require("johnny-five");
var ranges = {
distance: {
min: 60, max: 120
},
pitch: {
// range of 88 key piano => min: 27.5, max: 4186.01
// human hearing range => min: 20, max: 20000
min: 27.5, max: 4187.01
}
};
server.listen(process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/public/img/favicon.png'));
app.use(express.logger('dev'));
app.use(app.router);
app.locals.pretty = true;
app.locals({
ranges: ranges,
noboard: !!argv.noboard
});
app.use(stylus.middleware({
src: __dirname + '/public',
compile: function(str, path){
return stylus(str).set('filename', path).set('compress', true).use(nib());
}
}));
app.use(express.static(path.join(__dirname, 'public')));
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', function (req, res) {
res.render('index', { title: 'sine5' });
});
// TODO: convert to factory, or try to get johnny-five.Fn exposed.
function voltsToHz(value){
var vRange = ranges.distance;
hzRange = ranges.pitch,
//Figure out how 'wide' each range is
leftSpan = vRange.max - vRange.min,
rightSpan = hzRange.max - hzRange.min;
if(value < vRange.min) value = vRange.min;
if(value > vRange.max) value = vRange.max;
// Convert the left range into a 0-1 range (float)
var scaled = (value - vRange.min) / leftSpan;
// Convert the 0-1 range into a value in the right range.
var val = hzRange.min + (scaled * rightSpan);
var val = (value - vRange.min) * (hzRange.max - hzRange.min) / (vRange.max - vRange.min) + hzRange.min;
return val;
}
// extra normalization to smooth values
// TODO: find a clean way to implement this (minSamples?) in johnny-five.Sonor instead.
var samples = [];
var smooth = function smooth(v){
//console.log('raw', v);
//console.log('before', samples);
samples.push(v);
if(samples.length > 6){
samples.splice(0, samples.length - 6);
}
//console.log('after', samples);
var sum = samples.reduce(function(prev, current){
return prev + current;
});
return sum / samples.length;
}
// check for board skipping.
if(argv.noboard) {
console.log('Bypassing board. Boring, boring, boring.');
} else {
var board = new five.Board();
// "read" get the current reading from the proximity sensor
board.on("ready", function() {
// proximity sensor
var sonor = new five.Sonar({pin:"A0", freq: 30});
sonor.on("read", function( err, v ) {
var volts = smooth(this.voltage);
var freq = voltsToHz(volts);
io.sockets.emit('frequency:change', {frequency: freq});
//console.log('v', v, ' to ', freq, 'Hz');
});
// air pressure sensor
var sensor = new five.Sensor({pin:"A1", freq: 100});
sensor.scale(-1, 1);
sensor.on("change", function(err, val){
//console.log({argValue: val, normalized: this.normalized, scaled: this.scaled });
io.sockets.emit('volume:change', {volume: (this.scaled > 0) ? this.scaled.toFixed(3) : 0});
});
});
}