AudioClipA single decoded audio clip that can send its signal to a Channel or a Master, be played, looped, analyzed, and controlled over time.
import { AudioClip } from "@fluex/fluexgl-dsp";
const clip = new AudioClip(data);
clip.Send(myChannel);
// Basic settings
clip.SetVolume(0.8);
clip.SetPanLevel(-0.2);
clip.Loop(true);
// Start playback
clip.Play();
// Manually seek to 30 seconds
clip.Seek(30);
Constructs a new AudioClip from pre-decoded source data.
new AudioClip(data: AudioSourceData): AudioClip;
data: AudioSourceData - A typed object created when calling LoadAudioSource() or LoadAudioSourceFromBlob().id: stringA unique id, automatically generated when constructing a new audio clip. Should NOT be changed.
label: string | nullA custom label. Can be changed.
loop: booleanWhether the clip is set to loop when played.
isPlaying: booleanIndicates whether this clip is currently playing.
startTime: numberThe AudioContext.currentTime at which the current playback started.
offsetAtStart: numberThe offset (in seconds) inside the buffer from which playback started.
progressUpdateSpeed: numberThe interval in milliseconds used to track the audio clip's time progress. Default value is 20.
gainNode: GainNode | nullPer-clip gain node used to control volume.
stereoPannerNode: StereoPannerNode | nullPer-clip stereo panner node used to control pan level.
context: AudioContext | nullAudio context, usually inherited from the DSP's pipeline context.
audioClipPlayer: AudioClipPlayer | nullAudioClipPlayer where this audio clip is attached to.
Initialize(audioClipPlayer: AudioClipPlayer): void;Initializes the audio clip using the AudioClipPlayer's audio context. Usually not needed because Send(channel: Channel | Master) initializes it automatically.
audioClipPlayer: AudioClipPlayer AudioClipPlayer, which is usually automatically generated when creating a new Channel or a new Master;voidPlay(timestamp?: number, offset?: number): AudioClip | nullStarts playback of the clip. The audio clip starts from the beginning if no arguments are provided.
timestamp?: number - Absolute AudioContext.currentTime at which to start. If omitted, playback starts immediately. This argument is optional.offset?: number - Offset in seconds inside the buffer to start from.
If omitted, uses the current offsetAtStart.AudioClip - The same AudioClip. Can be used to stack methods.null - Returns null if this method failed.Stop(): AudioClip | nullStops playback of this clip and disconnects all active buffer sources.
No arguments
AudioClip - The same AudioClip. Can be used to stack methods.null - Returns null if this method failed.SetVolume(volume: number): AudioClipSets the clip volume using its GainNode.
volume: number - The desired gain value (linear)AudioClip - The same AudioClip. Can be used to stack methods.SetPanLevel(panLevel: number): AudioClipSets the stereo pan level of the clip.
panLevel: number - Pan value between -1 (full left) and 1 (full right).AudioClip - The same AudioClip. Can be used to stack methods.Loop(loop?: boolean): AudioClipEnables or disables looping of this clip.
loop?: boolean - When omitted, defaults to true.AudioClip - The same AudioClip. Can be used to stack methods.SetMaxAudioBufferSourceNodes(value: number): AudioClipSets the maximum number of buffer source nodes (default 1). This can only be changed if the overrideMaxAudioBufferNodes property on DSP is set to true.
value: number - The desired maximum amount of buffer source nodes.AudioClip - The same AudioClip. Can be used to stack methods.AddEventListener<K extends keyof AudioClipEventMap>(event: K, cb: AudioClipEventMap[K]): () => voidRegisters a listener for clip events.
event: K (keyof AudioClipEventMap) - Event name (e.g. "progress").cb: (event: (event from keyof AudioClipEventMap)) => void - Callback function.() => void - Unsubscribe function to remove the listener.Once<K extends keyof AudioClipEventMap>(event: K, cb: AudioClipEventMap[K]): () => voidRegisters a one-time event listener that automatically removes itself after the first call.
event: K (keyof AudioClipEventMap) - Event name (e.g. "progress").cb: (event: (event from keyof AudioClipEventMap)) => void - Callback function.() => void - Unsubscribe function to remove the listener.RemoveEventListener<K extends keyof AudioClipEventMap>(event: K, cb: AudioClipEventMap[K]): AudioClipRemoves a specific listener from an event.
event: K (keyof AudioClipEventMap) - Event name (e.g. "progress").cb: (event: (event from keyof AudioClipEventMap)) => void - Callback function.AudioClip - The same instance, for chaining.ClearEventListeners(event?: keyof AudioClipEventMap): AudioClipClears event listeners.
event?: keyof AudioClipEventMap - If provided, clears listeners only for that event.AudioClip - The same instance, for chainingSend(channel: Channel | Master): voidAttaches this audio clip to a Channel or a Master and sends its signal to the channel.
voidUnsend(channel: Channel | Master): voidDetaches this audio clip from a Channel or a Master and stops sending this clip's signal to the channel.
void"progress"Emitted periodically while the clip is playing. Payload type (simplified):
type ProgressPayload = {
current: number; // Current playback position in seconds
startTime: number; // AudioContext timestamp when playback started
offset: number; // Offset in seconds at which playback started
contextTimestamp: number; // Current AudioContext.currentTime
formatted: string; // Human readable time, e.g. "01:23"
};
Registered via
clip.AddEventListener("progress", (event) => {
console.log(event.current, event.formatted);
});
get currentPlaybackTime(): numberReturns the current playback time in seconds relative to the start of the buffer. Returns 0 when the clip is not playing or no audio context is available. Otherwise: offsetAtStart + (audioContext.currentTime - startTime).
get duration(): numberTotal duration of the underlying audio buffer in seconds.
get formattedDuration(): stringDuration formatted as "mm:ss".
get sampleRate(): numberSample rate of the underlying AudioBuffer.
get numberOfChannels(): numberNumber of channels in the underlying AudioBuffer.
get byteLength(): numberByte length of the original ArrayBuffer used to construct this clip.
import { DspPipeline, LoadAudioSource, AudioClip } from "@fluex/fluexgl-dsp";
(async function() {
const pipeline = new DspPipeline({
pathToWasm: "/data/fluexgl-dsp-wasm_bg.wasm",
pathToWorklet: "/data/fluexgl-dsp-processor.worklet"
});
await pipeline.InitializeDspPipeline();
const audioDevice = await pipeline.ResolveDefaultAudioOutputDevice();
if(!audioDevice) return;
const audioSource = await LoadAudioSource("/music.mp3");
if(!audioSource) return;
const audioClip = new AudioClip(audioSource);
const myChannel = audioDevice.CreateChannel();
audioClip.Send(myChannel);
myChannel.Send(audioDevice.GetMasterChannel());
window.addEventListener("mousedown", function() {
audioClip.Play();
});
})();
import { DspPipeline, LoadAudioSource, AudioClip } from "@fluex/fluexgl-dsp";
(async function() {
const pipeline = new DspPipeline({
pathToWasm: "/data/fluexgl-dsp-wasm_bg.wasm",
pathToWorklet: "/data/fluexgl-dsp-processor.worklet",
options: {
overrideMaxAudioBufferNodes: true
}
});
await pipeline.InitializeDspPipeline();
const audioDevice = await pipeline.ResolveDefaultAudioOutputDevice();
if(!audioDevice) return;
const audioSource = await LoadAudioSource("/single-gun-shot.mp3");
if(!audioSource) return;
const audioClip = new AudioClip(audioSource);
const myChannel = audioDevice.CreateChannel();
audioClip.SetMaxAudioBufferSourceNodes(200);
audioClip.Send(myChannel);
myChannel.Send(audioDevice.GetMasterChannel());
let lastTimestamp = Date.now();
let isShooting = false;
let shootDelayInMs = 100;
function loop() {
const now = Date.now();
if((now - lastTimestamp >= shootDelayInMs) && isShooting) {
audioClip.Play();
lastTimestamp = now;
}
return window.requestAnimationFrame(loop);
}
window.addEventListener("mousedown", function() { isShooting = true; });
window.addEventListener("mouseup", function() { isShooting = false; });
loop();
})();