Sound <TName>
Implements
- Loadable<AudioBuffer>
Index
Constructors
Properties
Accessors
Methods
Constructors
publicconstructor
Construct a Sound from an options object with an explicit
name. The class genericTNameis inferred from thenameliteral when one is provided.Type parameters
- TName: string = string
Parameters
options: SoundOptions<TName>
Returns Sound<TName>
Properties
publicdata
Data associated with a loadable
publicevents
publiclogger
publicmaxConcurrentTracks
publicreadonlyname
The registered name of this Sound. Either the explicit name option or
the basename-without-extension of the selected path. Used as the default
key when registered with a SoundManager.
publicoptionalonPlay
Optional hook to wire custom Web Audio nodes between this sound's source and its output, runs each time a track (re)starts. See AudioGraphBuilder.
publicposition
Position in seconds new tracks start from, see PlayOptions.position for a one-off
publicvoiceStealing
Accessors
publicbustCache
Should excalibur add a cache busting querystring? By default false. Must be set before loading
Returns boolean
Parameters
val: boolean
Returns void
publicduration
Get the duration that this audio should play. If unset the total natural playback duration will be used.
Returns number | undefined
Set the duration that this audio should play. If unset the total natural playback duration will be used.
Note: if you seek to a specific point the duration will start from that point, for example
If you have a 10 second clip, seek to 5 seconds, then set the duration to 2, it will play the clip from 5-7 seconds.
Parameters
duration: number | undefined
Returns void
publicinstances
Return array of Current SoundTrack instances playing or being paused
Returns SoundTrack[]
publicloop
Returns boolean
Indicates whether the clip should loop when complete
Parameters
value: boolean
Set the looping flag
Returns void
publicoutput
The GainNode every track of this sound is mixed into, carrying Sound.volume. Connected straight to the audio context destination until a SoundManager re-routes it through its mixer graph.
Returns GainNode
publicpath
Returns string
Parameters
val: string
Returns void
publicpitch
Pitch shift in cents (semitones are 100 cents). Default 0 (no change). Backed by AudioBufferSourceNode.detune; composes with Sound.playbackRate. Note: changing pitch also affects playback speed (this is a Web Audio limitation).
Returns number
Parameters
pitch: number
Returns void
publicplaybackRate
Returns number
Parameters
playbackRate: number
Returns void
publicvolume
Returns number
Volume [0-1] applied to every track of this sound, ramped smoothly while playing
Parameters
value: number
Returns void
Methods
publicdecodeAudio
Parameters
data: ArrayBuffer
Returns Promise<AudioBuffer>
publicemit
Type parameters
- TEventName: EventKey<SoundEvents>
Parameters
eventName: TEventName
event: SoundEvents[TEventName]
Returns void
publicgetPlaybackPosition
Return the current playback time of the playing track in seconds from the start.
Optionally specify the track to query if multiple are playing at once.
Parameters
trackId: number = 0
Returns number
publicgetTotalPlaybackDuration
Returns number
publicgetTrackId
Get Id of provided SoundTrack in current trackList
Parameters
track: SoundTrack
SoundTrack which Id is to be given
Returns number
publicinstanceCount
Returns how many tracks of the sound currently exist (playing or paused)
Returns number
publicisLoaded
Returns true if the loadable is loaded
Returns boolean
publicisPaused
Returns boolean
publicisPlaying
Whether or not the sound is playing right now
Returns boolean
publicisStopped
Returns boolean
publicload
Begins loading the resource and returns a promise to be resolved on completion
Returns Promise<AudioBuffer>
publicoff
Type parameters
- TEventName: EventKey<SoundEvents>
Parameters
eventName: TEventName
handler: Handler<SoundEvents[TEventName]>
Returns void
publicon
Type parameters
- TEventName: EventKey<SoundEvents>
Parameters
eventName: TEventName
handler: Handler<SoundEvents[TEventName]>
Returns Subscription
publiconce
Type parameters
- TEventName: EventKey<SoundEvents>
Parameters
eventName: TEventName
handler: Handler<SoundEvents[TEventName]>
Returns Subscription
publicpause
Stop the sound, and do not rewind
Returns void
publicplay
Play a new track of this sound, returns a promise that resolves when that track is done playing (false if the play was dropped, e.g. by Sound.maxConcurrentTracks).
Options apply to this track only and never change the Sound's own configuration:
sound.play(); // the sound as configured sound.play({ volume: 0.3, pitch: -200 }); // quieter and lower, this time only sound.play({ position: 2, duration: 1 }); // one second starting two seconds inTo continue paused tracks use Sound.resume, to get a handle on the track use Sound.start.
Parameters
optionaloptions: PlayOptions
Returns Promise<boolean>
publicplayingCount
Returns how many tracks of the sound are currently playing
Returns number
publicresume
Resume every paused (or seeked) track, resolves when they are done playing. Resolves
falseimmediately if nothing was paused.Returns Promise<boolean>
publicseek
Seek a track to a position in seconds, the track is paused until the next Sound.resume.
If no track exists one is created so that
seek(); resume()starts from the position.Parameters
position: number
trackId: number = 0
Returns void
publicstart
Like Sound.play but returns the SoundTrack synchronously so it can be stopped, seeked or adjusted while it plays. Returns
nullif the play was dropped.const footstep = sound.start({ volume: 0.5 }); footstep?.stop(); await footstep?.done;Parameters
optionaloptions: PlayOptions
Returns SoundTrack | null
publicstop
Stop the sound if it is currently playing and rewind the track. If the sound is not playing, rewinds the track.
Returns void
publicwireEngine
Parameters
engine: Engine
Returns void
staticcanPlayFile
Whether or not the browser can play this file as HTML5 Audio
Parameters
file: string
Returns boolean
staticcanPlayMime
Parameters
mime: string
Returns boolean
publicstaticfromBlob
Parameters
blob: Blob
Returns Promise<Sound<string>>
The Sound object allows games built in Excalibur to load audio components, from soundtracks to sound effects. Sound is an Loadable which means it can be passed to a Loader to pre-load before a game or level.