From 2acccf59c5b29295f8b1884276e20f4c736b3611 Mon Sep 17 00:00:00 2001 From: jbphet Date: Tue, 23 Feb 2021 12:12:10 -0700 Subject: [PATCH] added prototype sound generator for wall charges, see #486 --- .../model/WallModel.js | 4 +- .../view/WallNode.js | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/js/balloons-and-static-electricity/model/WallModel.js b/js/balloons-and-static-electricity/model/WallModel.js index 09e7e764..68ea68cd 100644 --- a/js/balloons-and-static-electricity/model/WallModel.js +++ b/js/balloons-and-static-electricity/model/WallModel.js @@ -65,12 +65,12 @@ class WallModel { for ( let i = 0; i < this.numX; i++ ) { for ( let k = 0; k < this.numY; k++ ) { - //plus + // plus const position = this.calculatePosition( i, k ); const plusCharge = new PointChargeModel( x + position[ 0 ], position[ 1 ], Tandem.OPT_OUT, false ); this.plusCharges.push( plusCharge ); - //minus + // minus const minusCharge = new MovablePointChargeModel( x + position[ 0 ] - PointChargeModel.RADIUS, position[ 1 ] - PointChargeModel.RADIUS, diff --git a/js/balloons-and-static-electricity/view/WallNode.js b/js/balloons-and-static-electricity/view/WallNode.js index a93a86ac..1e555f7d 100644 --- a/js/balloons-and-static-electricity/view/WallNode.js +++ b/js/balloons-and-static-electricity/view/WallNode.js @@ -6,15 +6,24 @@ @author Vasily Shakhov (Mlearner) */ +import DerivedProperty from '../../../../axon/js/DerivedProperty.js'; import Bounds2 from '../../../../dot/js/Bounds2.js'; +import Range from '../../../../dot/js/Range.js'; import Image from '../../../../scenery/js/nodes/Image.js'; import Node from '../../../../scenery/js/nodes/Node.js'; +import DiscreteSoundGenerator from '../../../../tambo/js/sound-generators/DiscreteSoundGenerator.js'; +import soundManager from '../../../../tambo/js/soundManager.js'; import wallImage from '../../../images/wall_png.js'; import balloonsAndStaticElectricity from '../../balloonsAndStaticElectricity.js'; import BASEA11yStrings from '../BASEA11yStrings.js'; import MinusChargesCanvasNode from './MinusChargesCanvasNode.js'; import PlusChargeNode from './PlusChargeNode.js'; import WallDescriber from './describers/WallDescriber.js'; +import chargeDeflectionSound from '../../../../tambo/sounds/release_mp3.js'; +// import chargeDeflectionSound from '../../../../tambo/sounds/grab_mp3.js'; +// import chargeDeflectionSound from '../../../../tambo/sounds/bright-marimba-short_mp3.js'; +// import chargeDeflectionSound from '../../../../tambo/sounds/slider-click-01_mp3.js'; +// import chargeDeflectionSound from '../../../../tambo/sounds/slider-click-01_mp3.js'; const wallLabelString = BASEA11yStrings.wallLabel.value; @@ -69,12 +78,39 @@ class WallNode extends Node { this.visible = isVisible; } ); - //s how charges based on draw property + // show charges based on draw property model.showChargesProperty.link( value => { plusChargesNode.visible = ( value === 'all' ); minusChargesNode.visible = ( value === 'all' ); } ); + // TODO: The following is a temporary prototype of sound generation for changes in the wall. It's mostly a proof + // of concept at this point, and will be replaced with something more thought out shortly. See + // https://github.com/phetsims/balloons-and-static-electricity/issues/486. + const nonDeflectedXPosition = model.wall.minusCharges[ 0 ].positionProperty.value.x; + const maxDeflectedXPosition = 739; // empirically determined + + _.times( model.wall.numY, index => { + if ( index % 3 === 0 ){ + const xPositionProperty = new DerivedProperty( + [ model.wall.minusCharges[ index ].positionProperty ], + positionVector => positionVector.x + ); + const deflectionSoundGenerator = new DiscreteSoundGenerator( + xPositionProperty, + new Range( nonDeflectedXPosition, maxDeflectedXPosition ), + { + sound: chargeDeflectionSound, + playbackRateRange: new Range( 1, 2 ), + numBins: 15, + initialOutputLevel: 0.2, + outOfRangeValuesOK: true + } + ); + soundManager.addSoundGenerator( deflectionSoundGenerator ); + } + } ); + // pdom - when the balloons change position, update the description of the induced charge in the wall const updateWallDescription = () => { this.setDescriptionContent( this.wallDescriber.getWallDescription( model.yellowBalloon, model.greenBalloon, model.getBalloonsAdjacent() ) );