namespace NT { /** Represents the configuration for a audio file. These are typically created and stored in a audio config file. */ class AudioConfig { /** The name of this audio. */ public name : string ; /** The audio file path of this audio config. */ public audioFile : string ; /** Indicates if this audio effect will loop. */ public loop : boolean = false ; param json public static fromJson ( json ) { let config = new AudioConfig ( ) ; if ( json . name !== undefined ) { config . name = String ( json . name ) ; } if ( json . loop !== undefined ) { config . loop = Boolean ( json . loop ) ; } if ( json . audioFile !== undefined ) { config . audioFile = String ( json . audioFile ) ; } else { throw new Error ( "Cannot create a audio config without a audioFile." ) ; } return config ; } } /**
     * Manages various aspects of audio output.
     */ export class AudioManager { private static _configLoaded : boolean = false ; private static _soundEffects : { [ name ] : SoundEffect ; } = { } ; param message public static onMessage ( message ) { if ( message . code === MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/audio/audio.json" ) { Message . unsubscribeCallback ( MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/audio/audio.json" , AudioManager . onMessage ) ; AudioManager . processAudioConfigAsset ( message . context as JsonAsset ) ; } } /**
         * Loads registered audio.
         */ public static load ( ) { let asset = AssetManager . getAsset ( "assets/audio/audio.json" ) ; if ( asset !== undefined ) { AudioManager . processAudioConfigAsset ( asset as JsonAsset ) ; } else { Message . subscribeCallback ( MESSAGE_ASSET_LOADER_ASSET_LOADED + "assets/audio/audio.json" , AudioManager . onMessage ) ; } } param name param assetPath param loop public static loadSoundFile ( name , assetPath , loop ) { AudioManager . _soundEffects [ name ] = new SoundEffect ( assetPath , loop ) ; } param name public static playSound ( name ) { if ( AudioManager . _soundEffects [ name ] !== undefined ) { AudioManager . _soundEffects [ name ] . play ( ) ; } } param name public static pauseSound ( name ) { if ( AudioManager . _soundEffects [ name ] !== undefined ) { AudioManager . _soundEffects [ name ] . pause ( ) ; } } /**
         * Pauses all sound effects.
         */ public static pauseAll ( ) { for ( let sfx in AudioManager . _soundEffects ) { AudioManager . _soundEffects [ sfx ] . pause ( ) ; } } param name public static stopSound ( name ) { if ( AudioManager . _soundEffects [ name ] !== undefined ) { AudioManager . _soundEffects [ name ] . stop ( ) ; } } /**
         * Stops all sound effects.
         */ public static stopAll ( ) { for ( let sfx in AudioManager . _soundEffects ) { AudioManager . _soundEffects [ sfx ] . stop ( ) ; } } private static processAudioConfigAsset ( asset ) { let configs = asset . Data . soundEffects ; if ( configs ) { for ( let config of configs ) { let a = AudioConfig . fromJson ( config ) ; AudioManager . loadSoundFile ( a . name , a . audioFile , a . loop ) ; } } AudioManager . _configLoaded = true ; } } }