import { Filter } from './filters/Filter' ; import { IMediaContext } from './interfaces/IMediaContext' ; import { IMediaInstance } from './interfaces/IMediaInstance' ; import { SoundLoader } from './SoundLoader' ; import { CompleteCallback , Options , PlayOptions , Sound } from './Sound' ; import { HTMLAudioContext } from './htmlaudio/HTMLAudioContext' ; import { WebAudioContext } from './webaudio/WebAudioContext' ; type SoundSourceMap = Record < string , Options | string | ArrayBuffer | AudioBuffer | HTMLAudioElement > ; type SoundMap = Record < string , Sound > ; example class SoundLibrary { /**
     * For legacy approach for Audio. Instead of using WebAudio API
     * for playback of sounds, it will use HTML5 `<audio>` element.
     */ private _useLegacy : boolean ; /** The global context to use. */ private _context : IMediaContext ; /** The WebAudio specific context */ private _webAudioContext : WebAudioContext ; /** The HTML Audio (legacy) context. */ private _htmlAudioContext : HTMLAudioContext ; /** The map of all sounds by alias. */ private _sounds : SoundMap ; constructor ( ) { this . init ( ) ; } return public init ( ) { if ( this . supported ) { this . _webAudioContext = new WebAudioContext ( ) ; } this . _htmlAudioContext = new HTMLAudioContext ( ) ; this . _sounds = { } ; this . useLegacy = ! this . supported ; return this ; } readonly public get context ( ) : IMediaContext { return this . _context ; } example public get filtersAll ( ) : Filter [ ] { if ( ! this . useLegacy ) { return this . _context . filters ; } return [ ] ; } public set filtersAll ( filtersAll ) { if ( ! this . useLegacy ) { this . _context . filters = filtersAll ; } } /**
     * `true` if WebAudio is supported on the current browser.
     */ public get supported ( ) : boolean { return WebAudioContext . AudioContext !== null ; } method instance param { string } alias param { Sound } sound return { Sound } param alias param { ArrayBuffer | AudioBuffer | String | Options | HTMLAudioElement } options return public add ( alias , options ) ; param map - Map of sounds to add, the key is the alias, the value is the
     *        `string`, `ArrayBuffer`, `AudioBuffer`, `HTMLAudioElement` or the list of options
     *        (see  Options for full options).
     *  param globalOptions return public add ( map , globalOptions ? ) ; ignore public add ( source , sourceOptions ? ) { if ( typeof source === 'object' ) { const results = { } ; for ( const alias in source ) { const options = this . _getOptions ( source [ alias ] , sourceOptions as Options ) ; results [ alias ] = this . add ( alias , options ) ; } return results ; } console . assert ( ! this . _sounds [ source ] , `Sound with alias ${ source } already exists.` ) ; if ( sourceOptions instanceof Sound ) { this . _sounds [ source ] = sourceOptions ; return sourceOptions ; } const options = this . _getOptions ( sourceOptions ) ; const sound = Sound . from ( options ) ; this . _sounds [ source ] = sound ; return sound ; } private param source param overrides return private _getOptions ( source , overrides ? ) { let options ; if ( typeof source === 'string' ) { options = { url : source } ; } else if ( source instanceof ArrayBuffer || source instanceof AudioBuffer || source instanceof HTMLAudioElement ) { options = { source } ; } else { options = source as Options ; } options = { ... options , ... ( overrides || { } ) } ; return options ; } /**
     * Do not use WebAudio, force the use of legacy. This **must** be called before loading any files.
     */ public get useLegacy ( ) : boolean { return this . _useLegacy ; } public set useLegacy ( legacy ) { SoundLoader . setLegacy ( legacy ) ; this . _useLegacy = legacy ; this . _context = ( ! legacy && this . supported ) ? this . _webAudioContext : this . _htmlAudioContext ; } param alias return public remove ( alias ) { this . exists ( alias , true ) ; this . _sounds [ alias ] . destroy ( ) ; delete this . _sounds [ alias ] ; return this ; } /**
     * Set the global volume for all sounds. To set per-sound volume see  SoundLibrary .
      public get volumeAll ( ) : number { return this . _context . volume ; } public set volumeAll ( volume ) { this . _context . volume = volume ; this . _context . refresh ( ) ; } /**
     * Set the global speed for all sounds. To set per-sound speed see  SoundLibrary .
      public get speedAll ( ) : number { return this . _context . speed ; } public set speedAll ( speed ) { this . _context . speed = speed ; this . _context . refresh ( ) ; } return public togglePauseAll ( ) { return this . _context . togglePause ( ) ; } return public pauseAll ( ) { this . _context . paused = true ; this . _context . refreshPaused ( ) ; return this ; } return public resumeAll ( ) { this . _context . paused = false ; this . _context . refreshPaused ( ) ; return this ; } return public toggleMuteAll ( ) { return this . _context . toggleMute ( ) ; } return public muteAll ( ) { this . _context . muted = true ; this . _context . refresh ( ) ; return this ; } return public unmuteAll ( ) { this . _context . muted = false ; this . _context . refresh ( ) ; return this ; } return public removeAll ( ) { for ( const alias in this . _sounds ) { this . _sounds [ alias ] . destroy ( ) ; delete this . _sounds [ alias ] ; } return this ; } return public stopAll ( ) { for ( const alias in this . _sounds ) { this . _sounds [ alias ] . stop ( ) ; } return this ; } param alias param assert return public exists ( alias , assert = false ) { const exists = ! ! this . _sounds [ alias ] ; if ( assert ) { console . assert ( exists , `No sound matching alias '${ alias }'.` ) ; } return exists ; } param alias return public find ( alias ) { this . exists ( alias , true ) ; return this . _sounds [ alias ] ; } method instance param { string } alias param { string } sprite return { IMediaInstance | null } param alias param { PlayOptions | Function } options return public play ( alias , options ? ) { return this . find ( alias ) . play ( options ) ; } param alias return public stop ( alias ) { return this . find ( alias ) . stop ( ) ; } param alias return public pause ( alias ) { return this . find ( alias ) . pause ( ) ; } param alias return public resume ( alias ) { return this . find ( alias ) . resume ( ) ; } param alias param volume return public volume ( alias , volume ? ) { const sound = this . find ( alias ) ; if ( volume !== undefined ) { sound . volume = volume ; } return sound . volume ; } param alias param speed return public speed ( alias , speed ? ) { const sound = this . find ( alias ) ; if ( speed !== undefined ) { sound . speed = speed ; } return sound . speed ; } param alias return public duration ( alias ) { return this . find ( alias ) . duration ; } /**
     * Closes the sound library. This will release/destroy
     * the AudioContext(s). Can be used safely if you want to
     * initialize the sound library later. Use `init` method.
     */ public close ( ) { this . removeAll ( ) ; this . _sounds = null ; if ( this . _webAudioContext ) { this . _webAudioContext . destroy ( ) ; this . _webAudioContext = null ; } if ( this . _htmlAudioContext ) { this . _htmlAudioContext . destroy ( ) ; this . _htmlAudioContext = null ; } this . _context = null ; return this ; } } export { SoundLibrary } ; export type { SoundSourceMap , SoundMap } ;