mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
Only measure loudness once per step
This commit is contained in:
parent
32e240f18a
commit
c661ac7cce
1 changed files with 16 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
||||||
const Cast = require('../util/cast');
|
const Cast = require('../util/cast');
|
||||||
|
const Timer = require('../util/timer');
|
||||||
|
|
||||||
class Scratch3SensingBlocks {
|
class Scratch3SensingBlocks {
|
||||||
constructor (runtime) {
|
constructor (runtime) {
|
||||||
|
@ -23,6 +24,8 @@ class Scratch3SensingBlocks {
|
||||||
this.runtime.on('ANSWER', this._onAnswer.bind(this));
|
this.runtime.on('ANSWER', this._onAnswer.bind(this));
|
||||||
this.runtime.on('PROJECT_START', this._resetAnswer.bind(this));
|
this.runtime.on('PROJECT_START', this._resetAnswer.bind(this));
|
||||||
this.runtime.on('PROJECT_STOP_ALL', this._clearAllQuestions.bind(this));
|
this.runtime.on('PROJECT_STOP_ALL', this._clearAllQuestions.bind(this));
|
||||||
|
|
||||||
|
this.timer = new Timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -217,7 +220,19 @@ class Scratch3SensingBlocks {
|
||||||
|
|
||||||
getLoudness () {
|
getLoudness () {
|
||||||
if (typeof this.runtime.audioEngine === 'undefined') return -1;
|
if (typeof this.runtime.audioEngine === 'undefined') return -1;
|
||||||
return this.runtime.audioEngine.getLoudness();
|
if (typeof this.runtime.currentStepTime === 'undefined') return -1;
|
||||||
|
|
||||||
|
// Only measure loudness once per step
|
||||||
|
if (this.loudnessTime) {
|
||||||
|
const timeSinceLoudness = this.timer.time() - this.loudnessTime;
|
||||||
|
if (timeSinceLoudness < this.runtime.currentStepTime) {
|
||||||
|
return this.loudness;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.loudnessTime = this.timer.time();
|
||||||
|
this.loudness = this.runtime.audioEngine.getLoudness();
|
||||||
|
return this.loudness;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAttributeOf (args) {
|
getAttributeOf (args) {
|
||||||
|
|
Loading…
Reference in a new issue