diff --git a/js/ohms-law/view/CurrentSoundGenerator.js b/js/ohms-law/view/CurrentSoundGenerator.js index 91a5fb9..2426b62 100644 --- a/js/ohms-law/view/CurrentSoundGenerator.js +++ b/js/ohms-law/view/CurrentSoundGenerator.js @@ -7,82 +7,75 @@ * * @author John Blanco */ -define( function( require ) { +define( require => { 'use strict'; // modules - var inherit = require( 'PHET_CORE/inherit' ); - var ohmsLaw = require( 'OHMS_LAW/ohmsLaw' ); - var OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' ); - var SoundClip = require( 'TAMBO/sound-generators/SoundClip' ); + const ohmsLaw = require( 'OHMS_LAW/ohmsLaw' ); + const OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' ); + const SoundClip = require( 'TAMBO/sound-generators/SoundClip' ); // constants - var PRE_FADE_TIME = 0.1; // in seconds - var FADE_OUT_TIME_CONSTANT = 3; // in seconds, larger values indicate faster fade out, see usage for details - var FADE_COMPLETE_OUTPUT_LEVEL = 0.001; // level at which fade out is considered complete and level is set to zero + const PRE_FADE_TIME = 0.1; // in seconds + const FADE_OUT_TIME_CONSTANT = 3; // in seconds, larger values indicate faster fade out, see usage for details + const FADE_COMPLETE_OUTPUT_LEVEL = 0.001; // level at which fade out is considered complete and level is set to zero // sounds - var currentLoopSound = require( 'sound!OHMS_LAW/current-v3-loop.mp3' ); + const currentLoopSound = require( 'sound!OHMS_LAW/current-v3-loop.mp3' ); - /** - * {NumberProperty} currentProperty - * @constructor - */ - function CurrentSoundGenerator( currentProperty, options ) { + class CurrentSoundGenerator extends SoundClip { - options = _.extend( { - initialOutputLevel: 1 - }, options ); - options.loop = true; // must be a loop to work properly - - SoundClip.call( this, currentLoopSound, options ); - var self = this; - - // @private {number} - max output level, used for fading - this.maxOutputLevel = options.initialOutputLevel; + /** + * {NumberProperty} currentProperty + * @constructor + */ + constructor( currentProperty, options ) { - // countdown timer used to play the current sound for a while, then stop - this.fadeCountdownTimer = Number.NEGATIVE_INFINITY; + options = _.extend( { + initialOutputLevel: 1 + }, options ); + options.loop = true; // must be a loop to work properly - // function to turn loop on/off and update playback rate - function updateSoundGeneration( current ) { + super( currentLoopSound, options ); - // any change turns on the playback - if ( !self.isPlaying ) { - self.play(); - } - self.fadeCountdownTimer = PRE_FADE_TIME; + // @private {number} - max output level, used for fading + this.maxOutputLevel = options.initialOutputLevel; - // calculate the normalized current value using a logarithmic formula to better handle the large range - var normalizedCurrent = Math.log( ( current / 1000 ) / OhmsLawConstants.CURRENT_RANGE.min ) / - Math.log( OhmsLawConstants.CURRENT_RANGE.max / OhmsLawConstants.CURRENT_RANGE.min ); + // countdown timer used to play the current sound for a while, then stop + this.fadeCountdownTimer = Number.NEGATIVE_INFINITY; - // Calculate the playback rate based on the normalized current. The formula came from the design document, and - // ranges from 0.5 to 2.0 times the default playback rate. - var playbackRate = 0.5 + 1.5 * Math.pow( normalizedCurrent, 2 ); + // function to turn loop on/off and update playback rate + const updateSoundGeneration = current => { - // set the playback rate for all loops - self.setPlaybackRate( playbackRate ); - } + // any change turns on the playback + if ( !this.isPlaying ) { + this.play(); + } + this.fadeCountdownTimer = PRE_FADE_TIME; - // start the loop playing when the current changes - currentProperty.lazyLink( updateSoundGeneration ); + // calculate the normalized current value using a logarithmic formula to better handle the large range + const normalizedCurrent = Math.log( ( current / 1000 ) / OhmsLawConstants.CURRENT_RANGE.min ) / + Math.log( OhmsLawConstants.CURRENT_RANGE.max / OhmsLawConstants.CURRENT_RANGE.min ); - this.disposeCurrentSoundGenerator = function() { - currentProperty.unlink( updateSoundGeneration ); - }; - } + // Calculate the playback rate based on the normalized current. The formula came from the design document, and + // ranges from 0.5 to 2.0 times the default playback rate. + const playbackRate = 0.5 + 1.5 * Math.pow( normalizedCurrent, 2 ); + // set the playback rate for all loops + this.setPlaybackRate( playbackRate ); + }; - ohmsLaw.register( 'CurrentSoundGenerator', CurrentSoundGenerator ); + // start the loop playing when the current changes + currentProperty.lazyLink( updateSoundGeneration ); - return inherit( SoundClip, CurrentSoundGenerator, { + this.disposeCurrentSoundGenerator = () => { currentProperty.unlink( updateSoundGeneration ); }; + } /** * @param {number} dt * @public */ - step: function( dt ) { + step( dt ) { if ( this.fadeCountdownTimer > Number.NEGATIVE_INFINITY ) { // decrement the fade countdown timer @@ -97,7 +90,7 @@ define( function( require ) { else if ( this.fadeCountdownTimer > Number.NEGATIVE_INFINITY ) { // fading out, calculate the level using the exponential decay formula - var level = this.maxOutputLevel * Math.pow( Math.E, FADE_OUT_TIME_CONSTANT * this.fadeCountdownTimer ); + const level = this.maxOutputLevel * Math.pow( Math.E, FADE_OUT_TIME_CONSTANT * this.fadeCountdownTimer ); if ( level > FADE_COMPLETE_OUTPUT_LEVEL ) { this.setOutputLevel( level ); } @@ -109,20 +102,25 @@ define( function( require ) { } } } - }, + } - reset: function() { + reset() { if ( this.isPlaying ) { this.stop(); } this.fadeCountdownTimer = Number.NEGATIVE_INFINITY; - }, + } /** * @public */ - dispose: function() { + dispose() { this.disposeCurrentSoundGenerator(); } - } ); + + } + + ohmsLaw.register( 'CurrentSoundGenerator', CurrentSoundGenerator ); + + return CurrentSoundGenerator; } );