Skip to content

Commit

Permalink
use carrier sound for balloon velocity, see #483
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Feb 16, 2021
1 parent 7c5f913 commit 2bb3dbd
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@

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 SoundClip from '../../../../tambo/js/sound-generators/SoundClip.js';
import driftVelocityLoop from '../../../sounds/carrier-000_wav.js';
// import driftVelocityLoop from '../../../sounds/carrier-001_wav.js';
// import driftVelocityLoop from '../../../sounds/carrier-002_wav.js';
import balloonsAndStaticElectricity from '../../balloonsAndStaticElectricity.js';

// constants
const INITIAL_CENTER_FREQUENCY = 500;
// function for mapping the speed of the balloon to the playback rate of the carrier sound, empirically determined
const mapSpeedToPlaybackRate = new LinearFunction( 0, 3, 0.5, 2, true );
const mapSpeedToOutputLevel = new LinearFunction( 0, 3, 0.2, 1, false );

// function for mapping the speed of the balloon to the center frequency of the filter
const mapSpeedToFrequency = new LinearFunction( 0, 3, INITIAL_CENTER_FREQUENCY, INITIAL_CENTER_FREQUENCY * 2, true );

class BalloonVelocitySoundGenerator extends NoiseGenerator {
class BalloonVelocitySoundGenerator extends SoundClip {

/**
* {Property.<number>} balloonVelocityProperty - velocity of the balloon when drifting (i.e. when it is not being
Expand All @@ -27,31 +28,54 @@ class BalloonVelocitySoundGenerator extends NoiseGenerator {
*/
constructor( balloonVelocityProperty, options ) {

options = merge( {
noiseType: 'pink',
centerFrequency: INITIAL_CENTER_FREQUENCY,
qFactor: 400,
options = merge(
{
loop: true,

// 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
// {number) The output level is set as a function of the speed at which the balloon is moving. This value
// specifies the maximum value. It will generally be between 0 and 1.
maxOutputLevel: 0.5
},
options
);

super( options );
// options checking
assert && assert(
!options.initialOutputLevel,
'initialOutputLevel should not be specified for this sound generator'
);

// Start the initial output level at zero so that the sound will fade in smoothly the first time it is played.
options.initialOutputLevel = 0;

// monitor the molecule oscillation amplitude and update local state
super( driftVelocityLoop, options );

// Monitor the balloon velocity and modify the output sound as changes occur.
balloonVelocityProperty.lazyLink( velocity => {
const speed = velocity.magnitude;
if ( speed > 0 ) {
if ( !this.isPlaying ){
this.start();
if ( !this.isPlaying ) {

// Before starting playback, set the playback rate immediately, otherwise there can be a bit of the "chirp"
// sound.
this.setPlaybackRate( mapSpeedToPlaybackRate( speed, 0 ) );

// Start the sound playing.
this.play();
}
else{

// Set the playback rate. This will use the nominal time constant, which should lead to a reasonably smooth
// transition of the rate as the velocity changes.
this.setPlaybackRate( mapSpeedToPlaybackRate( speed ) );
}
this.setBandpassFilterCenterFrequency( mapSpeedToFrequency( speed ) );

// Set the output level based on the velocity.
this.setOutputLevel( mapSpeedToOutputLevel( speed, 0.1 ) * options.maxOutputLevel );
}
else if ( speed === 0 && this.isPlaying ) {
this.stop();
this.setOutputLevel( 0 );
}
} );
}
Expand Down
Binary file added sounds/carrier-000.wav
Binary file not shown.
21 changes: 21 additions & 0 deletions sounds/carrier-000_wav.js

Large diffs are not rendered by default.

Binary file added sounds/carrier-001.wav
Binary file not shown.
21 changes: 21 additions & 0 deletions sounds/carrier-001_wav.js

Large diffs are not rendered by default.

Binary file added sounds/carrier-002.wav
Binary file not shown.
21 changes: 21 additions & 0 deletions sounds/carrier-002_wav.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions sounds/license.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
"license": "contact [email protected]",
"notes": "created by Ashton Morris (PhET Interactive Simulations)"
},
"carrier-000.wav": {
"text": [
"Copyright 2002-2020 University of Colorado Boulder"
],
"projectURL": "http://phet.colorado.edu",
"license": "contact [email protected]",
"notes": "created by Ashton Morris (PhET Interactive Simulations)"
},
"carrier-001.wav": {
"text": [
"Copyright 2002-2020 University of Colorado Boulder"
],
"projectURL": "http://phet.colorado.edu",
"license": "contact [email protected]",
"notes": "created by Ashton Morris (PhET Interactive Simulations)"
},
"carrier-002.wav": {
"text": [
"Copyright 2002-2020 University of Colorado Boulder"
],
"projectURL": "http://phet.colorado.edu",
"license": "contact [email protected]",
"notes": "created by Ashton Morris (PhET Interactive Simulations)"
},
"grab-magnet.mp3": {
"text": [
"Copyright 2002-2020 University of Colorado Boulder"
Expand Down

0 comments on commit 2bb3dbd

Please sign in to comment.