Skip to content

Commit

Permalink
converted JS to TS, see #160
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Mar 24, 2022
1 parent 7a22ef9 commit a763ace
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 116 deletions.
17 changes: 9 additions & 8 deletions js/demo/testing/model/AmplitudeModulatorDemo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Copyright 2020-2022, University of Colorado Boulder

/**
* Demo and test harness for the AmplitudeModulator class. This creates several sound loops and routes them through
* an amplitude modulator instance. It also provides the properties that can be manipulated in the view to change the
* attributes of the modulation.
*
* @author John Blanco (PhET Interactive Simulations)
*/

import saturatedSineLoop220Hz_mp3 from '../../../../sounds/saturatedSineLoop220Hz_mp3.js';
import windsLoopC3Oscilloscope_mp3 from '../../../../sounds/demo-and-test/windsLoopC3Oscilloscope_mp3.js';
import windsLoopMiddleCOscilloscope_mp3 from '../../../../sounds/demo-and-test/windsLoopMiddleCOscilloscope_mp3.js';
Expand All @@ -9,18 +17,11 @@ import SoundGenerator, { SoundGeneratorOptions } from '../../../sound-generators
import tambo from '../../../tambo.js';
import NumberProperty from '../../../../../axon/js/NumberProperty.js';

/**
* Demo and test harness for the AmplitudeModulator class. This creates several sound loops and routes them through
* an amplitude modulator instance. It also provides the properties that can be manipulated in the view to change the
* attributes of the modulation.
*/


class AmplitudeModulatorDemo extends SoundGenerator {

public readonly amplitudeModulator: AmplitudeModulator;

constructor( sourceSoundIndexProperty: NumberProperty, options: SoundGeneratorOptions ) {
constructor( sourceSoundIndexProperty: NumberProperty, options?: SoundGeneratorOptions ) {

super( options );

Expand Down
28 changes: 15 additions & 13 deletions js/demo/testing/view/AmplitudeModulatorDemoNode.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
// Copyright 2020-2021, University of Colorado Boulder

// @ts-nocheck
/**
* View portion of the demo and test harness for the AmplitudeModulator class.
*
* @author John Blanco (PhET Interactive Simulations)
*/

import BooleanProperty from '../../../../../axon/js/BooleanProperty.js';
import NumberProperty from '../../../../../axon/js/NumberProperty.js';
import Range from '../../../../../dot/js/Range.js';
import merge from '../../../../../phet-core/js/merge.js';
import optionize from '../../../../../phet-core/js/optionize.js';
import PhetFont from '../../../../../scenery-phet/js/PhetFont.js';
import { HBox } from '../../../../../scenery/js/imports.js';
import { Text } from '../../../../../scenery/js/imports.js';
import { VBox } from '../../../../../scenery/js/imports.js';
import { HBox, Text, VBox, VBoxOptions } from '../../../../../scenery/js/imports.js';
import AquaRadioButtonGroup from '../../../../../sun/js/AquaRadioButtonGroup.js';
import Checkbox from '../../../../../sun/js/Checkbox.js';
import HSlider from '../../../../../sun/js/HSlider.js';
import soundManager from '../../../soundManager.js';
import tambo from '../../../tambo.js';
import AmplitudeModulatorDemo from '../model/AmplitudeModulatorDemo.js';

type SelfOptions = {};
export type AmplitudeModulatorDemoNodeOptions = SelfOptions & VBoxOptions;

// constants
const LABEL_FONT = new PhetFont( 16 );

/**
* View portion of the demo and test harness for the AmplitudeModulator class.
*/
class AmplitudeModulatorDemoNode extends VBox {

constructor( options ) {
constructor( providedOptions: AmplitudeModulatorDemoNodeOptions ) {

const soundSourceRadioButtonItems = [
{
Expand Down Expand Up @@ -62,7 +65,7 @@ class AmplitudeModulatorDemoNode extends VBox {
// LFO enabled control
const lfoEnabled = new Checkbox(
new Text( 'LFO Enabled', { font: LABEL_FONT } ),
amplitudeModulatorDemo.amplitudeModulator.enabledProperty,
amplitudeModulatorDemo.amplitudeModulator.enabledProperty as BooleanProperty,
{ boxWidth: 16 }
);

Expand Down Expand Up @@ -120,12 +123,11 @@ class AmplitudeModulatorDemoNode extends VBox {
spacing: 5
} );


super( merge( {
super( optionize<AmplitudeModulatorDemoNodeOptions, SelfOptions, VBoxOptions>( {
children: [ soundIndexSelectorVBox, lfoEnabled, frequencyControlHBox, depthControlHBox, waveformSelectorVBox ],
spacing: 15,
align: 'left'
}, options ) );
}, providedOptions ) );
}
}

Expand Down
12 changes: 5 additions & 7 deletions js/demo/testing/view/CompositeSoundClipTestNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2020-2022, University of Colorado Boulder

// @ts-nocheck

/**
* Test and demo of the CompositeSoundClip.
*
Expand All @@ -10,7 +8,7 @@

import merge from '../../../../../phet-core/js/merge.js';
import PhetFont from '../../../../../scenery-phet/js/PhetFont.js';
import { VBox } from '../../../../../scenery/js/imports.js';
import { VBox, VBoxOptions } from '../../../../../scenery/js/imports.js';
import TextPushButton from '../../../../../sun/js/buttons/TextPushButton.js';
import brightMarimba_mp3 from '../../../../sounds/brightMarimba_mp3.js';
import loonCall_mp3 from '../../../../sounds/demo-and-test/loonCall_mp3.js';
Expand All @@ -21,10 +19,10 @@ import tambo from '../../../tambo.js';

class CompositeSoundClipTestNode extends VBox {

/**
* @param {Object} [options]
*/
constructor( options ) {
// dispose function
private readonly disposeCompositeSoundClipTestNode: () => void;

constructor( options: VBoxOptions ) {

// sound clips to be played
const compositeSoundClip = new CompositeSoundClip( [
Expand Down
30 changes: 16 additions & 14 deletions js/demo/testing/view/ContinuousPropertySoundGeneratorTestNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2020-2022, University of Colorado Boulder

// @ts-nocheck

/**
* Test and demo of the ContinuousPropertySoundGenerator.
*
Expand All @@ -14,8 +12,7 @@ import NumberProperty from '../../../../../axon/js/NumberProperty.js';
import Range from '../../../../../dot/js/Range.js';
import merge from '../../../../../phet-core/js/merge.js';
import NumberControl from '../../../../../scenery-phet/js/NumberControl.js';
import { Text } from '../../../../../scenery/js/imports.js';
import { VBox } from '../../../../../scenery/js/imports.js';
import { Text, VBox, VBoxOptions } from '../../../../../scenery/js/imports.js';
import Checkbox from '../../../../../sun/js/Checkbox.js';
import Panel from '../../../../../sun/js/Panel.js';
import saturatedSineLoop220Hz_mp3 from '../../../../sounds/saturatedSineLoop220Hz_mp3.js';
Expand All @@ -25,31 +22,36 @@ import windsLoopMiddleCOscilloscope_mp3 from '../../../../sounds/demo-and-test/w
import ContinuousPropertySoundGenerator from '../../../sound-generators/ContinuousPropertySoundGenerator.js';
import soundManager from '../../../soundManager.js';
import tambo from '../../../tambo.js';
import Emitter from '../../../../../axon/js/Emitter.js';
import WrappedAudioBuffer from '../../../WrappedAudioBuffer.js';

type SelfOptions = {};
export type ContinuousPropertySoundGeneratorTestNodeOptions = SelfOptions & VBoxOptions;

class ContinuousPropertySoundGeneratorTestNode extends VBox {

/**
* @param {Emitter} stepEmitter
* @param {Object} [options]
*/
constructor( stepEmitter, options ) {
// dispose function
private readonly disposeContinuousPropertySoundGeneratorTestNode: () => void;

constructor( stepEmitter: Emitter<[ number ]>, providedOptions: ContinuousPropertySoundGeneratorTestNodeOptions ) {

// keep track of listeners added to the step emitter so that they can be disposed
const stepListeners = [];
const stepListeners: ( ( dt: number ) => void )[] = [];

// creates a panel that demonstrates a ContinuousPropertySoundGenerator
const createTester = ( sound, max ) => {
const createTester = ( sound: WrappedAudioBuffer, max: number ) => {
const numberProperty = new NumberProperty( 5 );
const range = new Range( 1, 10 );
const continuousPropertySoundGenerator = new ContinuousPropertySoundGenerator(
numberProperty,
sound,
// @ts-ignore
range
);
soundManager.addSoundGenerator( continuousPropertySoundGenerator );
const isOscillatingProperty = new BooleanProperty( false );
let phase = 0;
const stepListener = dt => {
const stepListener = ( dt: number ) => {
if ( isOscillatingProperty.value ) {
numberProperty.value = ( max * Math.sin( Date.now() / 1000 - phase ) + 1 ) * ( range.max - range.min ) / 2 + range.min;
}
Expand Down Expand Up @@ -81,9 +83,9 @@ class ContinuousPropertySoundGeneratorTestNode extends VBox {
],
spacing: 15,
align: 'left'
}, options ) );
}, providedOptions ) );

// @private - for memory cleanup
// define dispose function for memory cleanup
this.disposeContinuousPropertySoundGeneratorTestNode = () => {
stepListeners.forEach( listener => {
stepEmitter.removeListener( listener );
Expand Down
82 changes: 45 additions & 37 deletions js/demo/testing/view/RemoveAndDisposeSoundGeneratorsTestPanel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2018-2022, University of Colorado Boulder

// @ts-nocheck

/**
* a panel that contains controls used to exercise the addition, removal, and disposal of sound generators
*
Expand All @@ -12,23 +10,28 @@ import createObservableArray from '../../../../../axon/js/createObservableArray.
import Property from '../../../../../axon/js/Property.js';
import stepTimer from '../../../../../axon/js/stepTimer.js';
import dotRandom from '../../../../../dot/js/dotRandom.js';
import merge from '../../../../../phet-core/js/merge.js';
import optionize from '../../../../../phet-core/js/optionize.js';
import StringUtils from '../../../../../phetcommon/js/util/StringUtils.js';
import PhetFont from '../../../../../scenery-phet/js/PhetFont.js';
import { HBox } from '../../../../../scenery/js/imports.js';
import { Node } from '../../../../../scenery/js/imports.js';
import { Text } from '../../../../../scenery/js/imports.js';
import { VBox } from '../../../../../scenery/js/imports.js';
import { HBox, Node, Text, VBox } from '../../../../../scenery/js/imports.js';
import TextPushButton from '../../../../../sun/js/buttons/TextPushButton.js';
import ComboBox from '../../../../../sun/js/ComboBox.js';
import ComboBoxItem from '../../../../../sun/js/ComboBoxItem.js';
import Panel from '../../../../../sun/js/Panel.js';
import Panel, { PanelOptions } from '../../../../../sun/js/Panel.js';
import birdCall_mp3 from '../../../../sounds/demo-and-test/birdCall_mp3.js';
import cricketsLoop_mp3 from '../../../../sounds/demo-and-test/cricketsLoop_mp3.js';
import PitchedPopGenerator from '../../../sound-generators/PitchedPopGenerator.js';
import SoundClip from '../../../sound-generators/SoundClip.js';
import soundManager from '../../../soundManager.js';
import tambo from '../../../tambo.js';
import SoundGenerator from '../../../sound-generators/SoundGenerator.js';

type SelfOptions = {};
export type RemoveAndDisposeSoundGeneratorsTestPanelOptions = SelfOptions & PanelOptions;
type SoundGeneratorComboBoxItemInfo = {
comboBoxItemName: string;
createSoundGenerator: () => SoundGenerator;
}

// constants
const BUTTON_FONT = new PhetFont( 18 );
Expand All @@ -37,37 +40,42 @@ const TOTAL_ADDED_TEMPLATE = 'Total Added: {{numSoundGenerators}}';
const ADD_BUTTON_COLOR = '#C0D890';

// info needed for selecting and using different sound generators from the combo box
const SOUND_GENERATOR_INFO = {
recordedOneShot: {
comboBoxName: 'Recorded one shot',
createSoundGenerator: () => new SoundClip( birdCall_mp3 )
},
recordedLoop: {
comboBoxName: 'Recorded loop',
createSoundGenerator: () => new SoundClip( cricketsLoop_mp3, { loop: true } )
},
synthesizedSound: {
comboBoxName: 'Synthesized sound',
createSoundGenerator: () => new PitchedPopGenerator( { numPopGenerators: 2 } )
}
};
const SOUND_GENERATOR_INFO = new Map<string, SoundGeneratorComboBoxItemInfo>( [
[
'recordedOneShot',
{
comboBoxItemName: 'Recorded one shot',
createSoundGenerator: () => new SoundClip( birdCall_mp3 )
}
],
[
'recordedLoop',
{
comboBoxItemName: 'Recorded loop',
createSoundGenerator: () => new SoundClip( cricketsLoop_mp3, { loop: true } )
}
],
[
'synthesizedSound',
{
comboBoxItemName: 'Synthesized sound',
createSoundGenerator: () => new PitchedPopGenerator( { numPopGenerators: 2 } )
}
]
] );

class RemoveAndDisposeSoundGeneratorsTestPanel extends Panel {

/**
* @param {Object} [options]
* @constructor
*/
constructor( options ) {
constructor( providedOptions: RemoveAndDisposeSoundGeneratorsTestPanelOptions ) {

options = merge( {
const options = optionize<RemoveAndDisposeSoundGeneratorsTestPanelOptions, SelfOptions, PanelOptions>( {
fill: '#f5d3b3',
xMargin: 14,
yMargin: 14
}, options );
}, providedOptions );

// array of sound generators that have been added and not yet removed and disposed
const soundGenerators = createObservableArray();
const soundGenerators = createObservableArray<SoundGenerator>();

// node where the content goes, needed so that ComboBox will have a good place to put its list
const panelContentNode = new Node();
Expand All @@ -77,11 +85,11 @@ class RemoveAndDisposeSoundGeneratorsTestPanel extends Panel {
font: new PhetFont( { size: 18, weight: 'bold' } )
} );

// create the combo box for selecting the type of sound generator to add
const comboBoxItems = [];
_.keys( SOUND_GENERATOR_INFO ).forEach( soundGeneratorKey => {
// Create the combo box for selecting the type of sound generator to add.
const comboBoxItems: ComboBoxItem<string>[] = [];
SOUND_GENERATOR_INFO.forEach( ( soundGenerator, soundGeneratorKey ) => {
comboBoxItems.push( new ComboBoxItem(
new Text( SOUND_GENERATOR_INFO[ soundGeneratorKey ].comboBoxName, { font: COMBO_BOX_FONT } ),
new Text( soundGenerator.comboBoxItemName, { font: COMBO_BOX_FONT } ),
soundGeneratorKey
) );
} );
Expand All @@ -97,9 +105,9 @@ class RemoveAndDisposeSoundGeneratorsTestPanel extends Panel {
spacing: 7
} );

function addSoundGenerators( numToAdd ) {
_.times( numToAdd, () => {
const soundGenerator = SOUND_GENERATOR_INFO[ selectedSoundGeneratorTypeProperty.value ].createSoundGenerator();
function addSoundGenerators( numberToAdd: number ) {
_.times( numberToAdd, () => {
const soundGenerator = SOUND_GENERATOR_INFO.get( selectedSoundGeneratorTypeProperty.value )!.createSoundGenerator();
soundManager.addSoundGenerator( soundGenerator );
soundGenerators.push( soundGenerator );
} );
Expand Down
Loading

0 comments on commit a763ace

Please sign in to comment.