Skip to content

Commit

Permalink
sound player instead of sound generator for slider sounds, see phetsi…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed May 27, 2022
1 parent 7b656bb commit 657ee5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions js/demo/ui-components/view/SliderPitchChangeSoundGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright 2022, University of Colorado Boulder

import ValueChangeSoundGenerator from '../../../sound-generators/ValueChangeSoundGenerator.js';
import ValueChangeSoundPlayer from '../../../sound-generators/ValueChangeSoundPlayer.js';
import tambo from '../../../tambo.js';
import SoundClip from '../../../sound-generators/SoundClip.js';
import brightMarimbaShort_mp3 from '../../../../sounds/brightMarimbaShort_mp3.js';
import soundManager from '../../../soundManager.js';
import Range from '../../../../../dot/js/Range.js';

/**
* SliderPitchChangeSoundGenerator is intended as a demonstration of a ValueChangeSoundGenerator that changes the pitch
* SliderPitchChangeSoundGenerator is intended as a demonstration of a ValueChangeSoundPlayer that changes the pitch
* (aka the playback rate) of its produced sounds as the slider values change.
*
* @author John Blanco (PhET Interactive Simulations)
*/


class SliderPitchChangeSoundGenerator extends ValueChangeSoundGenerator {
class SliderPitchChangeSoundGenerator extends ValueChangeSoundPlayer {

/**
* @param valueRange - the range of values expected and over which sounds will be played
Expand Down
4 changes: 2 additions & 2 deletions js/demo/ui-components/view/SliderSoundTestNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author John Blanco (PhET Interactive Simulations)
*/

import ValueChangeSoundGenerator from '../../../sound-generators/ValueChangeSoundGenerator.js';
import ValueChangeSoundPlayer from '../../../sound-generators/ValueChangeSoundPlayer.js';
import tambo from '../../../tambo.js';
import brightMarimbaShort_mp3 from '../../../../sounds/brightMarimbaShort_mp3.js';
import Range from '../../../../../dot/js/Range.js';
Expand Down Expand Up @@ -55,7 +55,7 @@ class SliderSoundTestNode extends HBox {
// slider with custom sound generation, intended to be a little "out there"
new Text( 'Crazy Custom Sounds', { font: labelFont } ),
new HSlider( new NumberProperty( 0 ), new Range( 0, 100 ), {
soundGenerator: new ValueChangeSoundGenerator( new Range( 0, 100 ), {
soundGenerator: new ValueChangeSoundPlayer( new Range( 0, 100 ), {
middleMovingUpSoundPlayer: new SoundClipPlayer( brightMarimbaShort_mp3, {
soundClipOptions: { initialOutputLevel: 0.2 },
soundManagerOptions: { categoryName: 'user-interface' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright 2022, University of Colorado Boulder

/**
* ValueChangeSoundGenerator is used to produce sounds based on changes to a numerical value. It was initially created
* for supporting PhET's Slider class and variations thereof, but it may have other applications.
* ValueChangeSoundPlayer plays sounds based on changes to a numerical value. It was initially created for supporting
* sound generation in PhET's Slider class and variations thereof, but it may have other applications.
*
* Because the sounds should only be produced when direct interactions by the user change a value, and not in other
* situations (e.g. a reset), this class does not monitor a Property. Instead, it provides methods that can be used to
* evaluate changes in a value and potentially play sounds, and it is the client's responsibility to know the situations
* in which these methods should be called. Often these methods will be called in drag handlers and other code that
* handles user input.
* This class does not extend SoundGenerator and is not itself added to the sound manager. It is instead a player of
* a set of sounds, each of which should be registered with the sound manager elsewhere.
*
* Because the sounds should only be produced when users directly change a value, and not in side-effect-ish situations
* (such as a reset), this class does not monitor a Property. Instead, it provides methods that can be used to evaluate
* changes in a value and potentially play sounds, and it is the client's responsibility to know the situations in which
* these methods should be called. Often these methods will be called in drag handlers and other code that handles user
* input.
*
* @author John Blanco (PhET Interactive Simulations)
*/
Expand All @@ -19,7 +22,6 @@ import generalBoundaryBoopSoundPlayer from '../shared-sound-players/generalBound
import generalSoftClickSoundPlayer from '../shared-sound-players/generalSoftClickSoundPlayer.js';
import tambo from '../tambo.js';
import ISoundPlayer from '../ISoundPlayer.js';
import SoundGenerator, { SoundGeneratorOptions } from './SoundGenerator.js';
import phetAudioContext from '../phetAudioContext.js';
import SoundClipPlayer from './SoundClipPlayer.js';
import generalBoundaryBoop_mp3 from '../../sounds/generalBoundaryBoop_mp3.js';
Expand Down Expand Up @@ -53,7 +55,7 @@ const DEFAULT_VALUE_CONSTRAINT = ( value: number ) => Utils.roundToInterval( val
// A "no-op" function for mapping pitch values. Always returns one, which signifies no change to the playback rate.
const NO_PLAYBACK_RATE_CHANGE = ( value: number ) => 1;

type SelfOptions = {
export type ValueChangeSoundPlayerOptions = {

// The sound player for movement in the middle of the range in the up direction.
middleMovingUpSoundPlayer?: ISoundPlayer | SoundClip;
Expand Down Expand Up @@ -95,9 +97,7 @@ type SelfOptions = {
minimumInterMiddleSoundTime?: number;
};

export type ValueChangeSoundGeneratorOptions = SelfOptions & SoundGeneratorOptions;

class ValueChangeSoundGenerator extends SoundGenerator {
class ValueChangeSoundPlayer {

// thresholds that will be used to decide when to play sounds in some cases
private readonly thresholds: number[]
Expand Down Expand Up @@ -136,9 +136,9 @@ class ValueChangeSoundGenerator extends SoundGenerator {
* @param valueRange - the range of values expected and over which sounds will be played
* @param [providedOptions]
*/
constructor( valueRange: Range, providedOptions?: ValueChangeSoundGeneratorOptions ) {
constructor( valueRange: Range, providedOptions?: ValueChangeSoundPlayerOptions ) {

const options = optionize<ValueChangeSoundGeneratorOptions, SelfOptions, SoundGeneratorOptions>()( {
const options = optionize<ValueChangeSoundPlayerOptions>()( {
middleMovingUpSoundPlayer: generalSoftClickSoundPlayer,
middleMovingDownSoundPlayer: DEFAULT_MIDDLE_MOVING_DOWN_SOUND_PLAYER,
middleMovingUpPlaybackRateMapper: NO_PLAYBACK_RATE_CHANGE,
Expand Down Expand Up @@ -187,8 +187,6 @@ class ValueChangeSoundGenerator extends SoundGenerator {
options.numberOfMiddleThresholds = DEFAULT_NUMBER_OF_MIDDLE_THRESHOLDS;
}

super( options );

this.thresholds = [];
if ( options.numberOfMiddleThresholds !== null ) {
const interThresholdDistance = valueRange.getLength() / ( options.numberOfMiddleThresholds + 1 );
Expand Down Expand Up @@ -290,13 +288,13 @@ class ValueChangeSoundGenerator extends SoundGenerator {
/**
* Static instance that makes no sound. This is generally used as an option value to turn off sound generation.
*/
static NO_SOUND = new ValueChangeSoundGenerator( new Range( 0, 1 ), {
static NO_SOUND = new ValueChangeSoundPlayer( new Range( 0, 1 ), {
middleMovingUpSoundPlayer: nullSoundPlayer,
minSoundPlayer: nullSoundPlayer,
maxSoundPlayer: nullSoundPlayer
} )
}

tambo.register( 'ValueChangeSoundGenerator', ValueChangeSoundGenerator );
tambo.register( 'ValueChangeSoundPlayer', ValueChangeSoundPlayer );

export default ValueChangeSoundGenerator;
export default ValueChangeSoundPlayer;

0 comments on commit 657ee5d

Please sign in to comment.