From 873bbb77b68bbec662725e33986405ca24428f97 Mon Sep 17 00:00:00 2001 From: kono-dada <121090029@link.cuhk.edu.cn> Date: Mon, 11 Aug 2025 11:38:32 +0800 Subject: [PATCH] feat: add stereo synthesis option via SBV2_FORCE_STEREO env var Previously, synthesis output was fixed to mono (channels=1). Now, setting the environment variable SBV2_FORCE_STEREO=1 forces stereo (2-channel) output. This allows generating stereo audio without changing the code, useful for users needing dual-channel output. --- crates/sbv2_core/src/tts_util.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/sbv2_core/src/tts_util.rs b/crates/sbv2_core/src/tts_util.rs index 37f89ae..334d877 100644 --- a/crates/sbv2_core/src/tts_util.rs +++ b/crates/sbv2_core/src/tts_util.rs @@ -173,8 +173,15 @@ pub fn parse_text_blocking( } pub fn array_to_vec(audio_array: Array3) -> Result> { + // If SBV2_FORCE_STEREO is set ("1"/"true"), duplicate mono to stereo + let force_stereo = std::env::var("SBV2_FORCE_STEREO") + .ok() + .map(|v| matches!(v.as_str(), "1" | "true" | "TRUE" | "True")) + .unwrap_or(false); + + let channels: u16 = if force_stereo { 2 } else { 1 }; let spec = WavSpec { - channels: 1, + channels, sample_rate: 44100, bits_per_sample: 32, sample_format: SampleFormat::Float, @@ -183,8 +190,16 @@ pub fn array_to_vec(audio_array: Array3) -> Result> { let mut writer = WavWriter::new(&mut cursor, spec)?; for i in 0..audio_array.shape()[0] { let output = audio_array.slice(s![i, 0, ..]).to_vec(); - for sample in output { - writer.write_sample(sample)?; + if force_stereo { + for sample in output { + // Write to Left and Right channels + writer.write_sample(sample)?; + writer.write_sample(sample)?; + } + } else { + for sample in output { + writer.write_sample(sample)?; + } } } writer.finalize()?;