import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2020-2022, University of Colorado Boulder
/**
 * SoundClipChord plays a chord built from the given sound.
 *
 * @author Michael Kauzmann (PhET Interactive Simulations)
 */
import DerivedProperty from '../../../axon/js/DerivedProperty.js';
import merge from '../../../phet-core/js/merge.js';
import optionize from '../../../phet-core/js/optionize.js';
import SoundClip from '../../../tambo/js/sound-generators/SoundClip.js';
import SoundGenerator from '../../../tambo/js/sound-generators/SoundGenerator.js';
import tambo from '../tambo.js';
class SoundClipChord extends SoundGenerator {
    constructor(sound, providedOptions) {
        const options = optionize()({
            initialOutputLevel: 0.7,
            soundClipOptions: null,
            arpeggiate: false,
            arpeggiateTime: 0.10, // in seconds, the total time that it will take the chord to arpeggiate
            chordPlaybackRates: [Math.pow(2, 1 / 12), Math.pow(2, 4 / 12), Math.pow(2, 7 / 12)] // default to major chord
        }, providedOptions);
        if (options.soundClipOptions) {
            assert && assert(options.soundClipOptions.initialPlaybackRate === undefined, 'SoundClipChord sets the initialPlaybackRate for its SoundClips');
        }
        super(options);
        this.arpeggiate = options.arpeggiate;
        this.arpeggiateTime = options.arpeggiateTime;
        this.playbackSoundClips = options.chordPlaybackRates.map(playbackRate => {
            const soundClip = new SoundClip(sound, merge({
                initialPlaybackRate: playbackRate
            }, options.soundClipOptions));
            soundClip.connect(this.soundSourceDestination);
            return soundClip;
        });
        this.isPlayingProperty = DerivedProperty.or(this.playbackSoundClips.map(soundClip => soundClip.isPlayingProperty));
    }
    /**
     * Play the chord.
     */
    play() {
        this.playbackSoundClips.forEach((soundClip, index) => {
            const delay = this.arpeggiate ? index * this.arpeggiateTime / this.playbackSoundClips.length : 0;
            soundClip.play(delay);
        });
    }
    /**
     * Stop the chord if it's playing.  This is mostly here to complete the TSoundPlayer interface.
     */
    stop() {
        this.playbackSoundClips.forEach(soundClip => {
            soundClip.stop();
        });
    }
    /**
     * Release any memory references in order to avoid memory leaks.
     */
    dispose() {
        this.playbackSoundClips.forEach(soundClip => soundClip.dispose());
        this.playbackSoundClips.length = 0;
        super.dispose();
    }
}
tambo.register('SoundClipChord', SoundClipChord);
export default SoundClipChord;
//# sourceMappingURL=SoundClipChord.js.map