Skip to content

Commit

Permalink
added sound for drift velocity, see #479
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Feb 9, 2021
1 parent 306dc50 commit 6ae03be
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
8 changes: 4 additions & 4 deletions js/balloons-and-static-electricity/view/BalloonNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
@@ -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.<number>} 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;

0 comments on commit 6ae03be

Please sign in to comment.