Merge 35daf2e832
into fda87985c7
This commit is contained in:
commit
17405aca5e
4 changed files with 89 additions and 1 deletions
|
@ -305,6 +305,9 @@ Interpreter.prototype.initPrims = function() {
|
|||
this.primitiveTable['showBubble'] = function(b) { console.log(interp.arg(b, 1)); };
|
||||
this.primitiveTable['timerReset'] = function(b) { interp.timerBase = Date.now(); };
|
||||
this.primitiveTable['timer'] = function(b) { return (Date.now() - interp.timerBase) / 1000; };
|
||||
|
||||
//Edge-triggered hats
|
||||
this.primitiveTable['whenSensorGreaterThan'] = this.primNoop;
|
||||
|
||||
new Primitives().addPrimsTo(this.primitiveTable);
|
||||
};
|
||||
|
|
|
@ -39,7 +39,7 @@ var Reporter = function(data) {
|
|||
|
||||
Reporter.prototype.determineReporterLabel = function() {
|
||||
if (this.target === 'Stage' && this.cmd === "getVar:") return this.param;
|
||||
if (this.target === 'Stage' && this.param === null) return this.cmd;
|
||||
if (this.target === 'Stage' && this.param === null) return (this.cmd === 'soundLevel' ? "loudness" : this.cmd);
|
||||
return this.target + ': ' + this.param;
|
||||
}
|
||||
|
||||
|
@ -112,6 +112,9 @@ Reporter.prototype.update = function() {
|
|||
case 'timer':
|
||||
newValue = '' + Math.round(interp.primitiveTable.timer() * 10) / 10;
|
||||
break;
|
||||
case 'soundLevel':
|
||||
newValue = '' + runtime.soundLevel();
|
||||
break;
|
||||
}
|
||||
if (typeof newValue === 'number' && Math.abs(newValue) > 0.001) {
|
||||
newValue = Math.round(newValue * 1000) / 1000;
|
||||
|
|
|
@ -35,6 +35,10 @@ var Runtime = function() {
|
|||
this.audioPlaying = [];
|
||||
this.notesPlaying = [];
|
||||
this.projectLoaded = false;
|
||||
this.audioLiveSource = null;
|
||||
this.audioRequestMic = true;
|
||||
this.audioMicrophoneAmplitude = -1;
|
||||
this.allowProcessEdgeTriggerHats = false;
|
||||
};
|
||||
|
||||
// Initializer for the drawing and audio contexts.
|
||||
|
@ -81,6 +85,7 @@ Runtime.prototype.greenFlag = function() {
|
|||
interp.threads = [];
|
||||
interp.primitiveTable.timerReset();
|
||||
this.startGreenFlags();
|
||||
runtime.allowProcessEdgeTriggerHats = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -104,6 +109,7 @@ Runtime.prototype.step = function() {
|
|||
for (var r = 0; r < runtime.reporters.length; r++) {
|
||||
runtime.reporters[r].update();
|
||||
}
|
||||
runtime.processEdgeTriggeredHats();
|
||||
};
|
||||
|
||||
// Stack functions -- push and remove stacks
|
||||
|
@ -167,6 +173,27 @@ Runtime.prototype.startClickedHats = function(sprite) {
|
|||
runtime.allStacksDo(startIfClicked);
|
||||
};
|
||||
|
||||
Runtime.prototype.startEdgeTriggeredHats = function() {
|
||||
function startIfEdgeTriggered(stack, target) {
|
||||
if (stack.op == "whenSensorGreaterThan" && !interp.isRunning(stack)) {
|
||||
var sensorName = interp.arg(stack, 0);
|
||||
var threshold = interp.numarg(stack, 1);
|
||||
|
||||
if ((sensorName == "loudness" && runtime.soundLevel() > threshold) ||
|
||||
(sensorName == "timer" && interp.primitiveTable.timer() > threshold) /* ||
|
||||
Video Motion goes here, but will leave blank until video is added */) {
|
||||
interp.toggleThread(stack, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
runtime.allStacksDo(startIfEdgeTriggered);
|
||||
};
|
||||
Runtime.prototype.processEdgeTriggeredHats = function() {
|
||||
if (runtime.allowProcessEdgeTriggerHats){
|
||||
runtime.startEdgeTriggeredHats();
|
||||
}
|
||||
};
|
||||
|
||||
// Returns true if a key is pressed.
|
||||
Runtime.prototype.keyIsDown = function(ch) {
|
||||
return this.keysDown[ch] || false;
|
||||
|
@ -200,6 +227,59 @@ Runtime.prototype.getTimeString = function(which) {
|
|||
return ''; // shouldn't happen
|
||||
};
|
||||
|
||||
//Sound level
|
||||
Runtime.prototype.soundLevel = function() {
|
||||
if (runtime.audioRequestMic) {
|
||||
//If live source isn't set, we need to set it
|
||||
//Cross-browser for getUserMedia
|
||||
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
|
||||
//Make sure that both the audio context and getUserMedia exists
|
||||
if (this.audioContext && navigator.getUserMedia) {
|
||||
//We will call to get user media for the microphone
|
||||
navigator.getUserMedia({ audio: true }, function(stream) {
|
||||
|
||||
runtime.audioLiveSource = runtime.audioContext.createMediaStreamSource(stream);
|
||||
|
||||
var levelChecker = runtime.audioContext.createScriptProcessor(4096, 1, 1);
|
||||
runtime.audioLiveSource.connect(levelChecker);
|
||||
|
||||
levelChecker.connect(runtime.audioContext.destination);
|
||||
|
||||
levelChecker.onaudioprocess = window.audioProcess = function(e){
|
||||
var buffer = e.inputBuffer.getChannelData(0);
|
||||
|
||||
var maxVal = 0;
|
||||
//Iterate through buffer to check if any of the |values| exceeds the set maxVal.
|
||||
for(var i = 0; i < buffer.length; i++)
|
||||
{
|
||||
if (maxVal < buffer[i]) {
|
||||
maxVal = buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
runtime.audioMicrophoneAmplitude = (maxVal * 100);
|
||||
};
|
||||
|
||||
}, function(err) {
|
||||
console.error("Error in getUserMedia: " + err);
|
||||
});
|
||||
|
||||
//We asked the browser...
|
||||
runtime.audioRequestMic = false;
|
||||
//Wait 60 seconds to ask again (assuming we need to)
|
||||
//setTimeout(function(){
|
||||
// runtime.audioRequestMic = true;
|
||||
//}, 60000);
|
||||
}
|
||||
}
|
||||
|
||||
return Math.floor(runtime.audioMicrophoneAmplitude);
|
||||
};
|
||||
Runtime.prototype.isLoud = function() {
|
||||
//Sound considered loud if above 10%
|
||||
return (runtime.soundLevel() > 10);
|
||||
};
|
||||
|
||||
// Reassigns z-indices for layer functions
|
||||
Runtime.prototype.reassignZ = function(target, move) {
|
||||
var sprites = this.sprites;
|
||||
|
|
|
@ -30,6 +30,8 @@ SensingPrims.prototype.addPrimsTo = function(primTable) {
|
|||
primTable['mouseX'] = function(b) { return runtime.mousePos[0]; };
|
||||
primTable['mouseY'] = function(b) { return runtime.mousePos[1]; };
|
||||
primTable['distanceTo:'] = this.primDistanceTo;
|
||||
primTable['soundLevel'] = function(b) { return runtime.soundLevel(); };
|
||||
primTable['isLoud'] = function(b) { return runtime.isLoud(); };
|
||||
|
||||
primTable['getAttribute:of:'] = this.primGetAttribute;
|
||||
|
||||
|
|
Reference in a new issue