From 6ae03be1cd917e45d7344bd85a1465e33997ec5f Mon Sep 17 00:00:00 2001 From: jbphet Date: Tue, 9 Feb 2021 12:37:23 -0700 Subject: [PATCH] added sound for drift velocity, see https://github.com/phetsims/balloons-and-static-electricity/issues/479 --- .../view/BalloonNode.js | 8 +-- .../view/BalloonVelocitySoundGenerator.js | 62 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 js/balloons-and-static-electricity/view/BalloonVelocitySoundGenerator.js diff --git a/js/balloons-and-static-electricity/view/BalloonNode.js b/js/balloons-and-static-electricity/view/BalloonNode.js index 8e7d075e..6e0048fe 100644 --- a/js/balloons-and-static-electricity/view/BalloonNode.js +++ b/js/balloons-and-static-electricity/view/BalloonNode.js @@ -36,6 +36,7 @@ import BASEQueryParameters from '../BASEQueryParameters.js'; import BalloonDirectionEnum from '../model/BalloonDirectionEnum.js'; import PlayAreaMap from '../model/PlayAreaMap.js'; import BalloonInteractionCueNode from './BalloonInteractionCueNode.js'; +import BalloonVelocitySoundGenerator from './BalloonVelocitySoundGenerator.js'; import BalloonDescriber from './describers/BalloonDescriber.js'; import MinusChargeNode from './MinusChargeNode.js'; import PlusChargeNode from './PlusChargeNode.js'; @@ -207,6 +208,9 @@ class BalloonNode extends Node { } } ); + // sound generation for drift velocity + soundManager.addSoundGenerator( new BalloonVelocitySoundGenerator( model.velocityProperty ) ); + // pdom balloonImageNode.focusHighlight = new FocusHighlightFromNode( balloonImageNode ); @@ -319,10 +323,6 @@ class BalloonNode extends Node { grabDragInteraction.reset(); } ); - model.velocityProperty.link( velocity => { - console.log( 'velocity.magnitude = ' + velocity.magnitude ); - } ); - // Handle a query parameter that adds a line and a marker at the "charge center". This can be useful for debugging. if ( BASEQueryParameters.showBalloonChargeCenter ) { const parentToLocalChargeCenter = this.parentToLocalPoint( model.getChargeCenter() ); diff --git a/js/balloons-and-static-electricity/view/BalloonVelocitySoundGenerator.js b/js/balloons-and-static-electricity/view/BalloonVelocitySoundGenerator.js new file mode 100644 index 00000000..e22c09ec --- /dev/null +++ b/js/balloons-and-static-electricity/view/BalloonVelocitySoundGenerator.js @@ -0,0 +1,62 @@ +// Copyright 2018-2020, University of Colorado Boulder + +/** + * BalloonVelocitySoundGenerator is used to produce a sound that corresponds to the drifting velocity of the balloon. + * It does NOT produce sound when the balloon is being dragged by the user. + * + * @author John Blanco + */ + +import LinearFunction from '../../../../dot/js/LinearFunction.js'; +import merge from '../../../../phet-core/js/merge.js'; +import NoiseGenerator from '../../../../tambo/js/sound-generators/NoiseGenerator.js'; +import balloonsAndStaticElectricity from '../../balloonsAndStaticElectricity.js'; + +// constants +const INITIAL_CENTER_FREQUENCY = 500; + +// function for mapping the speed of the balloon to the center frequency of the filter +const mapSpeedToFrequency = new LinearFunction( 0, 2, INITIAL_CENTER_FREQUENCY, INITIAL_CENTER_FREQUENCY * 1.5 ); + +class BalloonVelocitySoundGenerator extends NoiseGenerator { + + /** + * {Property.} balloonVelocityProperty - velocity of the balloon when drifting (i.e. when it is not being + * dragged by a user). + * {Object} [options] + */ + constructor( balloonVelocityProperty, options ) { + + options = merge( { + noiseType: 'pink', + centerFrequency: INITIAL_CENTER_FREQUENCY, + qFactor: 200, + + // The output level has to be far higher than normal, since so much of the noise energy ends up getting filtered + // out. + initialOutputLevel: 15.0 + }, + options + ); + + super( options ); + + // monitor the molecule oscillation amplitude and update local state + balloonVelocityProperty.lazyLink( velocity => { + const speed = velocity.magnitude; + if ( speed > 0 ) { + if ( !this.isPlaying ){ + this.start(); + } + this.setBandpassFilterCenterFrequency( mapSpeedToFrequency( speed ) ); + } + else if ( speed === 0 && this.isPlaying ) { + this.stop(); + } + } ); + } +} + +balloonsAndStaticElectricity.register( 'BalloonVelocitySoundGenerator', BalloonVelocitySoundGenerator ); + +export default BalloonVelocitySoundGenerator; \ No newline at end of file