JavaScript/TypeScript Stems and Remix API
Decomposition and remix calls of the libsonare JavaScript/TypeScript package. They take the same request-object conventions as Feature Extraction.
decomposeStems(request) Heavy
Factorize a recording with NMF into nComponents unnamed components and return each one as audio, not as factors.
function decomposeStems(request: DecomposeStemsRequest): DecomposeStemsResult
function decomposeStems(
samples: Float32Array,
sampleRate: number,
options?: OptionsOf<DecomposeStemsRequest>,
): DecomposeStemsResult
interface DecomposeStemsRequest {
samples: Float32Array; // Mono audio samples.
sampleRate: number; // Sample rate in Hz, an integer in `[8000, 384000]`; there is no default.
nComponents?: number; // Number of NMF components (default 4).
nFft?: number; // STFT size (default 2048).
hopLength?: number; // STFT hop (default 512).
nIter?: number; // NMF multiplicative-update iterations (default 100).
beta?: number; // Beta divergence: 2 = Frobenius (default), 1 = Kullback-Leibler.
init?: NmfInit; // NMF initialisation (default `'random'`).
maskPower?: number; // Soft-mask exponent (default 1). 1 keeps the magnitude ratio; 2 is the Wiener-style power ratio, which separates harder at the cost of more artefacts on overlapping partials. Must be >= 1.
}
interface DecomposeStemsResult {
components: Float32Array[]; // Component signals, each the length of the input.
w: Float32Array; // Component matrix [nBins x nComponents], row-major.
h: Float32Array; // Activation matrix [nComponents x nFrames], row-major.
sampleRate: number; // Sample rate of the component signals in Hz.
}This is the entry point to reach for when you want stems, and decompose is not. decompose factorizes a magnitude spectrogram and hands back w and h; magnitudes carry no phase, so turning a factor back into audio needs a phase estimator, and what comes out is a reconstruction rather than a piece of the recording. decomposeStems runs the same factorization but uses it to build a per-component soft mask, which it applies to the original complex spectrogram. Every component therefore keeps the source's own phase, and because the masks sum to one wherever the model has energy and the inverse STFT is linear, the components sum back to the input.
maskPower sets how hard the masks separate: 1 is the magnitude ratio, 2 is the Wiener-style power ratio, which separates more aggressively at the cost of more artifacts where partials overlap. Values below 1 are refused.
nComponents is optional here (default 4), unlike in decompose, whose nComponents is required and whose nIter defaults to 50.
This is not HPSS. The components carry no label: components[1] is whatever repeating spectral pattern the factorization settled on, and which index a voice or a kick lands in changes with init, nIter and the material, so audition them to learn what each holds. hpss (with or without withResidual) on Audio Effects answer a different question: they split by median filtering into exactly two labeled parts, harmonic and percussive (plus the residual). Reach for HPSS when the question is "sustained versus struck"; reach for decomposeStems when it is "which recurring patterns make up this recording".
NNDSVD seeding is computed in double precision
With init: 'nndsvd', the SVD warm start is computed in double precision. This makes the seed reproducible: a magnitude spectrogram's trailing singular vectors sit at single precision's noise floor, so a float seed depends on the order the target happened to sum in, and two platforms would answer with different components for the same input.
It is a reproducibility property, not an accuracy one: shapes, non-negativity and reconstruction quality are unaffected. The factors are not the same numbers a float seed produces, so stored w/h matrices or a stem render made with a single-precision seed will differ; re-derive them rather than treating a mismatch as a bug. The same applies to decompose({ ..., init: 'nndsvd' }) and decomposeStemsLinked(...).
decomposeStemsLinked(request) Heavy
Use this form for a multichannel recording when every channel must use one shared separation while keeping its own stereo image.
function decomposeStemsLinked(request: DecomposeStemsLinkedRequest): DecomposeStemsLinkedResult
interface DecomposeStemsLinkedRequest {
channels: Float32Array[]; // At least one channel; all the same length.
sampleRate: number; // Sample rate in Hz, an integer in `[8000, 384000]`; there is no default.
nComponents?: number; // Number of NMF components (default 4).
nFft?: number; // STFT size (default 2048).
hopLength?: number; // STFT hop (default 512).
nIter?: number; // NMF multiplicative-update iterations (default 100).
beta?: number; // Beta divergence: 2 = Frobenius (default), 1 = Kullback-Leibler.
init?: NmfInit; // NMF initialisation (default `'random'`).
maskPower?: number; // Soft-mask exponent (default 1); see DecomposeStemsRequest.maskPower.
}
interface DecomposeStemsLinkedResult {
components: Float32Array[][]; // Component signals: `components[k][c]` is component `k`'s signal on channel `c`, each the length of the input.
w: Float32Array; // Component matrix [nBins x nComponents], row-major.
h: Float32Array; // Activation matrix [nComponents x nFrames], row-major.
sampleRate: number; // Sample rate of the component signals in Hz.
}The function averages channel magnitudes to fit one NMF model and one set of soft masks. It applies each mask unchanged to each channel's original complex spectrogram, so interchannel level and phase differences stay in place. Pass at least one same-length channel and no more than 64 channels. w and h describe the shared factorization. With one channel, this result is bit-identical to decomposeStems(...) with the same options. All NMF option defaults match decomposeStems(...).
remixAlignedIntervals(samples, sampleRate, options)
Resolve one set of cut points from one signal so the same cuts can be applied to every channel.
function remixAlignedIntervals(request: RemixRequest): Int32Array
function remixAlignedIntervals(
samples: Float32Array,
sampleRate: number,
options: OptionsOf<RemixRequest>,
): Int32Array
interface RemixRequest {
samples: Float32Array; // Mono audio samples.
sampleRate: number; // Sample rate in Hz, an integer in `[8000, 384000]`; there is no default.
intervals: Int32Array | ArrayLike<number>; // Flat (start, end) sample pairs (even length).
alignZeros?: boolean; // Snap the cut boundaries to zero crossings. Default `true`.
}Zero-crossing snapping is a per-signal decision: a cut point nudged to the nearest zero crossing lands on a different sample in the left channel than in the right, because the two channels cross zero at different times. Calling remix channel by channel with alignZeros therefore snaps each channel to its own frame and drifts a stereo take apart — a small, cumulative desync that is hard to spot until it is in the master. remixAlignedIntervals returns the snapped (start, end) pairs as a flat Int32Array instead of audio, so you resolve them once against one channel and then hand the identical list to remix for each channel with alignZeros: false.
alignZeros defaults to true on both remix and remixAlignedIntervals, so pass alignZeros: false to remix when you hand it intervals that were already snapped. Two guards keep the snap safe: a signal with no sign change at all is not snapped, and a slice that would collapse to empty keeps its unsnapped boundaries.
const cuts = Int32Array.from([0, sampleRate, 2 * sampleRate, 3 * sampleRate]);
const aligned = remixAlignedIntervals(left, sampleRate, { intervals: cuts });
const outLeft = remix(left, sampleRate, { intervals: aligned, alignZeros: false });
const outRight = remix(right, sampleRate, { intervals: aligned, alignZeros: false });