Skip to main content

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​

ParameterTypeDefaultDescription
idstringrequiredID 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 getSource again when you need it.
  • Pausing or stopping a sound stops and drops its source, so the method returns undefined until it plays again.
  • Streams use a MediaElementAudioSourceNode that this method does not return. A stream id logs an error and gives undefined.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​