<commit_before>import { Audio } from '../../Interfaces/Audio';
import { Promise } from '../../Promises';
import * as Util from '../../Util/Util';
import { AudioContextFactory } from './AudioContext';

/**
 * Internal class for producing of AudioInstances
 */
/* istanbul ignore next */
export class AudioInstanceFactory {
  public static create(src: string | AudioBuffer): AudioInstance {
    if (typeof src === 'string') {
      return new AudioTagInstance(src);
    }

    if (src instanceof AudioBuffer) {
      return new WebAudioInstance(src);
    }

    return new AudioInstance(src);
  }
}

/**
 * Internal class representing base AudioInstance implementation
 */
/* istanbul ignore next */
export class AudioInstance implements Audio {
  public set loop(value: boolean) {
    this._loop = value;

    if (this._instance) {
      this._instance.loop = value;
      this._wireUpOnEnded();
    }
  }
  public get loop(): boolean {
    return this._loop;
  }
  public set volume(value: number) {
    this._volume = Util.clamp(value, 0, 1.0);
  }
  public get volume(): number {
    return this._volume;
  }
  public set duration(value: number | undefined) {
    this._duration = value;
  }

  /**
   * Duration of the sound, in seconds.
   */
  public get duration() {
    return this._duration;
  }

  protected _volume = 1;
  protected _duration: number | undefined = undefined;
  protected _loop = false;
  protected _playingPromise: Promise<boolean>;
  protected _isPlaying = false;
  protected _isPaused = false;
  protected _instance: HTMLAudioElement | AudioBufferSourceNode;

  constructor(protected _src: string | AudioBuffer) {}

  public isPlaying() {
    return this._isPlaying;
  }

  public pause() {
    if (!this._isPlaying) {
      return;
    }

    this._isPaused = true;
    this._isPlaying = false;
  }

  public stop() {
    if (!this._isPlaying) {
      return;
    }

    this._isPlaying = false;
    this._isPaused = false;
  }

  public play() {
    if (this._isPaused) {
      this._resumePlayBack();
    }

    if (!this._isPlaying) {
      this._startPlayBack();
    }

    return this._playingPromise;
  }

  protected _startPlayBack() {
    this._isPlaying = true;
    this._isPaused = false;
    this._playingPromise = new Promise<boolean>();
  }

  protected _resumePlayBack() {
    if (!this._isPaused) {
      return;
    }

    this._isPaused = false;
    this._isPlaying = true;
  }

  protected _wireUpOnEnded() {
    if (!this.loop) {
      this._instance.onended = () => this._handleOnEnded();
    }
  }

  protected _handleOnEnded() {
    /**
     * Override me
     */
  }
}

/**
 * Internal class representing a HTML5 audio instance
 */
/* istanbul ignore next */
export class AudioTagInstance extends AudioInstance {
  public set volume(value: number) {
    value = Util.clamp(value, 0, 1.0);

    this._volume = value;
    this._instance.volume = value;
  }
  public get volume(): number {
    return this._volume;
  }

  protected _src: string;
  protected _instance: HTMLAudioElement;

  constructor(src: string) {
    super(src);

    this._instance = new Audio(src);
  }

  public pause() {
    if (!this._isPlaying) {
      return;
    }

    this._instance.pause();
    this._isPaused = true;
    this._isPlaying = false;
  }

  public stop() {
    super.stop();

    this._instance.pause();
    this._instance.currentTime = 0;

    this._handleOnEnded();
  }

  protected _startPlayBack() {
    super._startPlayBack();

    this._instance.load();
    this._instance.loop = this.loop;
    this._instance.play();

    this._wireUpOnEnded();
  }

  protected _resumePlayBack() {
    super._resumePlayBack();

    this._instance.play();
    this._wireUpOnEnded();
  }

  protected _handleOnEnded() {
    this._isPlaying = false;
    this._isPaused = false;
    this._playingPromise.resolve(true);
  }<commit_msg>chore: [#1487] Remove audio tag implementation (#1552)<commit_after>import { Audio } from '../../Interfaces/Audio';
import { Promise } from '../../Promises';
import * as Util from '../../Util/Util';
import { AudioContextFactory } from './AudioContext';

/**
 * Internal class for producing of AudioInstances
 */
/* istanbul ignore next */

export class AudioInstanceFactory {
  public static create(src: string | AudioBuffer): AudioInstance {
    if (src instanceof AudioBuffer) {
      return new WebAudioInstance(src);
    }

    return new AudioInstance(src);
  }
}

/**
 * Internal class representing base AudioInstance implementation
 */
/* istanbul ignore next */
export class AudioInstance implements Audio {
  public set loop(value: boolean) {
    this._loop = value;

    if (this._instance) {
      this._instance.loop = value;
      this._wireUpOnEnded();
    }
  }
  public get loop(): boolean {
    return this._loop;
  }
  public set volume(value: number) {
    this._volume = Util.clamp(value, 0, 1.0);
  }
  public get volume(): number {
    return this._volume;
  }
  public set duration(value: number | undefined) {
    this._duration = value;
  }

  /**
   * Duration of the sound, in seconds.
   */
  public get duration() {
    return this._duration;
  }

  protected _volume = 1;
  protected _duration: number | undefined = undefined;
  protected _loop = false;
  protected _playingPromise: Promise<boolean>;
  protected _isPlaying = false;
  protected _isPaused = false;
  protected _instance: HTMLAudioElement | AudioBufferSourceNode;

  constructor(protected _src: string | AudioBuffer) {}

  public isPlaying() {
    return this._isPlaying;
  }

  public pause() {
    if (!this._isPlaying) {
      return;
    }

    this._isPaused = true;
    this._isPlaying = false;
  }

  public stop() {
    if (!this._isPlaying) {
      return;
    }

    this._isPlaying = false;
    this._isPaused = false;
  }

  public play() {
    if (this._isPaused) {
      this._resumePlayBack();
    }

    if (!this._isPlaying) {
      this._startPlayBack();
    }

    return this._playingPromise;
  }

  protected _startPlayBack() {
    this._isPlaying = true;
    this._isPaused = false;
    this._playingPromise = new Promise<boolean>();
  }

  protected _resumePlayBack() {
    if (!this._isPaused) {
      return;
    }

    this._isPaused = false;
    this._isPlaying = true;
  }

  protected _wireUpOnEnded() {
    if (!this.loop) {
      this._instance.onended = () => this._handleOnEnded();
    }
  }

  protected _handleOnEnded() {
    /**
     * Override me
     */
  }
{'fraction_non_alphanumeric': 0.1237991942981097, 'fraction_numerical': 0.0032537960954446853, 'mean_word_length': 2.7903699354081035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4322276', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1957, 'n_words': 487}