import '../../../globals.js';
// Copyright 2020-2023, University of Colorado Boulder
/**
 * OscillatorSoundGenerator is a Web Audio oscillator node wrapped in a sound generator so that it can be easily be used
 * in PhET sims.
 *
 * @author John Blanco (PhET Interactive Simulations)
 */
import optionize from '../../../phet-core/js/optionize.js';
import phetAudioContext from '../phetAudioContext.js';
import tambo from '../tambo.js';
import SoundGenerator from './SoundGenerator.js';
class OscillatorSoundGenerator extends SoundGenerator {
    constructor(providedOptions) {
        const options = optionize()({
            initialFrequency: 440,
            initialWaveformType: 'sine'
        }, providedOptions);
        super(options);
        // state initialization
        this.oscillatorNode = null;
        this.frequency = options.initialFrequency;
        this.waveformType = options.initialWaveformType;
    }
    /**
     * Starts the oscillator. The name 'play' is used because this is commonly used in the tambo library for sound
     * generators. If the oscillator is already playing, this has no effect.
     */
    play() {
        if (!this.oscillatorNode) {
            this.oscillatorNode = phetAudioContext.createOscillator();
            this.oscillatorNode.type = this.waveformType;
            this.oscillatorNode.frequency.setValueAtTime(this.frequency, phetAudioContext.currentTime);
            this.oscillatorNode.connect(this.mainGainNode);
            this.oscillatorNode.start();
        }
    }
    /**
     * Stops the oscillator. If the oscillator isn't playing, this has no effect.
     */
    stop() {
        if (this.oscillatorNode) {
            this.oscillatorNode.stop();
            this.oscillatorNode = null;
        }
    }
    /**
     * Sets the waveform type.
     */
    setWaveformType(waveformType) {
        this.waveformType = waveformType;
        if (this.oscillatorNode) {
            this.oscillatorNode.type = waveformType;
        }
    }
}
tambo.register('OscillatorSoundGenerator', OscillatorSoundGenerator);
export default OscillatorSoundGenerator;
//# sourceMappingURL=OscillatorSoundGenerator.js.map