getSource
Get the AudioBufferSourceNode of a buffered sound. It is the node that plays the buffer, useful for reading its playbackRate or loop settings while the sound runs.
getSource(id: string): AudioBufferSourceNode | undefined;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of a loaded sound or an overlapping instance. |
Returns
AudioBufferSourceNode | undefined: the current source node, or undefined when the sound has none (after a pause or a stop) or the id is not loaded.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true, seamlessLoop: true });
const source = soundHub.getSource('music');
console.log(source?.loop); // true
console.log(source?.playbackRate.value); // 1
Good to know
- A source node can only start once, so the hub makes a new one on every play, resume, seek and loop restart. Do not keep a reference; call
getSourceagain when you need it. - Pausing or stopping a sound stops and drops its source, so the method returns
undefineduntil it plays again. - Streams use a
MediaElementAudioSourceNodethat this method does not return. A stream id logs an error and givesundefined. - For an id that is not loaded, the error is logged, stored for
getLastErrorand dispatched as anerrorevent.
See also
getBuffer: the audio the source plays.getGainNode: the node after the source, carrying the volume.getSound: the whole sound object.