Skip to content

EZ Web Audio / crossfade

Function: crossfade()

crossfade(fromTrack, toTrack, duration): Promise<void>

Defined in: utils/crossfade.ts:58

Smoothly crossfades from one Track to another using equal-power curves. This creates a DJ-style transition without volume dips at the midpoint.

Behavior:

  • Source track (fromTrack): fades out from current gain to 0
  • Destination track (toTrack): fades in from current gain (or 0) to 1
  • Source track automatically stops and resets gain to 1.0 after fade completes
  • If destination is already playing, continues from current position
  • If destination is not playing, starts it at gain 0 then fades in

Parameters

fromTrack

Track

Track to fade out (will be stopped after fade)

toTrack

Track

Track to fade in

duration

number

Crossfade duration in seconds

Returns

Promise<void>

Promise that resolves when crossfade completes

Example

typescript
// Crossfade from track1 to track2 over 2 seconds
await crossfade(track1, track2, 2)
// track1 is now stopped, track2 is playing