Skip to content

Commit

Permalink
added lockout timer for mass sound generator, see #181
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Oct 29, 2019
1 parent fd2e2ed commit 4cce57a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions js/gravity-force-lab/view/GravityForceLabScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ define( require => {
model.object1.valueProperty,
GravityForceLabConstants.MASS_RANGE,
model.resetInProgressProperty,
{ initialOutputLevel: 0.7 }
{ initialOutputLevel: 0.7, lockoutTime: 0.2 }
) );
soundManager.addSoundGenerator( new MassSoundGenerator(
model.object2.valueProperty,
GravityForceLabConstants.MASS_RANGE,
model.resetInProgressProperty,
{ initialOutputLevel: 0.7 }
{ initialOutputLevel: 0.7, lockoutTime: 0.2 }
) );

const resetAllButton = new ResetAllButton( {
Expand Down
16 changes: 14 additions & 2 deletions js/gravity-force-lab/view/MassSoundGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ define( require => {
'use strict';

// modules
const SoundClip = require( 'TAMBO/sound-generators/SoundClip' );
const merge = require( 'PHET_CORE/merge' );
const gravityForceLab = require( 'GRAVITY_FORCE_LAB/gravityForceLab' );
const SoundClip = require( 'TAMBO/sound-generators/SoundClip' );

// sounds
const massSound = require( 'sound!GRAVITY_FORCE_LAB/rubber-band-v3.mp3' );
Expand All @@ -29,18 +30,28 @@ define( require => {
*/
constructor( massProperty, massRange, resetInProgressProperty, options ) {

options = merge( {

// {number} - the minimum amount of time, in seconds, between plays of the sound clip
lockoutTime: 0

}, options );

// Rate changes should never affect the mass sound that is already playing.
options.rateChangesAffectPlayingSounds = false;

super( massSound, options );

// variable to track when last play occurred
let timeOfLastPlay = Number.NEGATIVE_INFINITY;

// function for playing the mass sound
const massListener = mass => {

// range checking
assert && assert( massRange.contains( mass ), 'mass value out of range' );

if ( !resetInProgressProperty.value ) {
if ( !resetInProgressProperty.value && ( Date.now() - timeOfLastPlay ) / 1000 >= options.lockoutTime ) {

// convert the mass to a playback rate, see the design document for an explanation
const normalizedMass = ( mass - massRange.min ) / ( massRange.max - massRange.min );
Expand All @@ -49,6 +60,7 @@ define( require => {
const playbackSpeed = Math.pow( 2, midiNote / 12 );
this.setPlaybackRate( playbackSpeed );
this.play();
timeOfLastPlay = Date.now();
}
};
massProperty.lazyLink( massListener );
Expand Down

0 comments on commit 4cce57a

Please sign in to comment.