Skip to content

Enabling stereo output of acc (Code Suggestion) #36

Description

@Tom-M-Git

The WebUI SVC pipeline currently collapses audio to mono before preprocessing and again during mixing, even though the separator supports stereo.

After modifying the code to preserve stereo:

>>> import soundfile as sf
>>> x, sr = sf.read("acc.wav")
>>> print(x.shape)
(663617, 2)

This shows the separator can output stereo when given stereo input.

In the current implementation:

  • _trim_and_save_audio() uses librosa.load(..., mono=True)
  • final mix also uses mono=True

This collapses stereo input before it reaches the separator.

Effect:

  • Accompaniment loses stereo width
  • Final output becomes fully mono

Preserving stereo significantly improves listening quality (wide instrumental + centered vocal).

Suggested changes:

  1. Preserve stereo in input:
    librosa.load(..., mono=False)

  2. Handle shape correctly:

    • (channels, samples) → transpose before saving
  3. Avoid forcing mono in final mix:
    librosa.load(..., mono=False)

  4. Duplicate mono vocal to stereo before mixing

My Code:

def _trim_and_save_audio(src_audio_path: str, dst_wav_path: Path, max_sec: int, sr: int = SAMPLE_RATE) -> None:
    audio_data, _ = librosa.load(src_audio_path, sr=sr, mono=False)

    max_samples = max_sec * sr

    if audio_data.ndim == 1:
        audio_data = audio_data[:max_samples]
    else:
        audio_data = audio_data[:, :max_samples]
        audio_data = audio_data.T

    dst_wav_path.parent.mkdir(parents=True, exist_ok=True)
    sf.write(dst_wav_path, audio_data, sr)
					mix_sr = self.svc_config.audio.sample_rate
					vocal, _ = librosa.load(str(generated), sr=mix_sr, mono=False)
					acc, _ = librosa.load(str(acc_path), sr=mix_sr, mono=False)
					
					if vocal.ndim == 1:
					    vocal = np.stack([vocal, vocal], axis=0)
					if acc.ndim == 1:
					    acc = np.stack([acc, acc], axis=0)

					if acc_shift != 0:
					    acc = np.stack([
					        librosa.effects.pitch_shift(acc[ch], sr=mix_sr, n_steps=acc_shift)
					        for ch in range(acc.shape[0])
					    ], axis=0)
					    print(f"Applied pitch shift of {acc_shift} semitones to accompaniment to match vocal shift of {vocal_shift} semitones.")

					mix_len = min(vocal.shape[-1], acc.shape[-1])
					if mix_len > 0:
					    mixed = vocal[:, :mix_len] + acc[:, :mix_len]
					    peak = float(np.max(np.abs(mixed))) if mixed.size > 0 else 1.0
					    if peak > 1.0:
					        mixed = mixed / peak
					    mixed_path = save_dir / "generated_mixed.wav"
					    sf.write(str(mixed_path), mixed.T, mix_sr)
					    generated = mixed_path

Caveat

It slightly changes the separation result.
For example, if there is an echo of vocal panning left-to-right, in my case, it wasn't treated as vocal and remained in acc.

Maybe there is a way to keep the prompt mono anyway then sparate vocal more reliably, while turning target acc stereo?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions