From e80b02f6aa02a235eddd14f8bc4b5fae5b3fb7ce Mon Sep 17 00:00:00 2001 From: Rahul-cs50 Date: Tue, 3 Nov 2026 14:41:37 +0530 Subject: [PATCH] fix: undo the gain normalize() applied to the MDX input, not the input peak separate() multiplied the demixed stem by the input's peak, but demix() returns audio at the scale of the mix it was given, and normalize() only rescales a mix whose peak is above normalization_threshold or below amplification_threshold. For any peak in between, the primary stem came out scaled down by that peak, and the secondary stem (mix - primary) kept the missing part of it. normalize() also scaled the mix in place, so the secondary stem was subtracted from the normalized mix instead of the original. Normalize a copy, divide the model output by the gain normalize() actually applied, and build the secondary stem from the original mix. Fixes #318 --- .../separator/architectures/mdx_separator.py | 9 ++- tests/unit/test_mdx_input_gain.py | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_mdx_input_gain.py diff --git a/audio_separator/separator/architectures/mdx_separator.py b/audio_separator/separator/architectures/mdx_separator.py index af5e311e..08e51af8 100644 --- a/audio_separator/separator/architectures/mdx_separator.py +++ b/audio_separator/separator/architectures/mdx_separator.py @@ -165,11 +165,14 @@ def separate(self, audio_file_path, custom_output_names=None): mix = self.prepare_mix(self.audio_file_path) self.logger.debug("Normalizing mix before demixing...") + # normalize() scales in place, and only when the peak is outside [min_peak, max_peak], so work on a copy and + # undo exactly the gain it applied. The model's output is at the scale of the mix it was given. peak = np.abs(mix).max() - mix = spec_utils.normalize(wave=mix, max_peak=self.normalization_threshold, min_peak=self.amplification_threshold) + normalized_mix = spec_utils.normalize(wave=mix.copy(), max_peak=self.normalization_threshold, min_peak=self.amplification_threshold) + gain = np.abs(normalized_mix).max() / peak if peak > 0 else 1.0 # Start the demixing process - source = self.demix(mix) * peak + source = self.demix(normalized_mix) / gain self.logger.debug("Demixing completed.") @@ -185,7 +188,7 @@ def separate(self, audio_file_path, custom_output_names=None): # Process the secondary source if not already an array if not isinstance(self.secondary_source, np.ndarray): self.logger.debug("Producing secondary source: demixing in match_mix mode") - raw_mix = self.demix(mix, is_match_mix=True) + raw_mix = self.demix(normalized_mix, is_match_mix=True) / gain if self.invert_using_spec: self.logger.debug("Inverting secondary stem using spectogram as invert_using_spec is set to True") diff --git a/tests/unit/test_mdx_input_gain.py b/tests/unit/test_mdx_input_gain.py new file mode 100644 index 00000000..48eecdb1 --- /dev/null +++ b/tests/unit/test_mdx_input_gain.py @@ -0,0 +1,56 @@ +"""MDX stems keep the input's level whatever gain normalization applied before demixing (#318).""" + +import logging +from unittest.mock import Mock + +import numpy as np +import pytest + +from audio_separator.separator.architectures.mdx_separator import MDXSeparator + +VOCAL_SHARE = 0.25 + + +def mdx_double(mix, amplification_threshold): + separator = Mock(spec=MDXSeparator) + separator.logger = logging.getLogger(__name__) + separator.normalization_threshold = 0.9 + separator.amplification_threshold = amplification_threshold + separator.output_single_stem = None + separator.prepare_mix.return_value = mix + # A linear stand-in for the model: the "vocals" are a fixed share of whatever mix it is given. + separator.demix.side_effect = lambda wave, is_match_mix=False: VOCAL_SHARE * wave + separator.primary_source = None + separator.secondary_source = None + separator.primary_stem_name = "Vocals" + separator.secondary_stem_name = "Instrumental" + separator.compensate = 1.0 + separator.get_stem_output_path.side_effect = ["input_(Instrumental).wav", "input_(Vocals).wav"] + return separator + + +@pytest.mark.parametrize( + "peak, amplification_threshold", + [ + (0.4, 0.0), # between the thresholds: normalize() leaves the mix alone + (1.5, 0.0), # above normalization_threshold: scaled down before demixing + (0.05, 0.1), # below amplification_threshold: scaled up before demixing + ], +) +@pytest.mark.parametrize("invert_using_spec", [False, True]) +def test_mdx_stems_keep_input_level(peak, amplification_threshold, invert_using_spec, monkeypatch): + rng = np.random.default_rng(318) + mix = rng.uniform(-1, 1, (2, 64)).astype(np.float32) + mix *= peak / np.abs(mix).max() + original = mix.copy() + + separator = mdx_double(mix, amplification_threshold) + separator.invert_using_spec = invert_using_spec + # Spectrogram inversion is linear for these inputs; subtracting is enough to check the scale it is given. + monkeypatch.setattr("audio_separator.separator.architectures.mdx_separator.spec_utils.invert_stem", lambda raw_mix, stem: raw_mix.T * (1 / VOCAL_SHARE) - stem) + + MDXSeparator.separate(separator, "input.wav") + + np.testing.assert_allclose(separator.primary_source, VOCAL_SHARE * original.T, rtol=1e-5) + np.testing.assert_allclose(separator.secondary_source, (1 - VOCAL_SHARE) * original.T, rtol=1e-5) + np.testing.assert_allclose(separator.primary_source + separator.secondary_source, original.T, rtol=1e-5)