Compare commits

...

2 Commits

Author SHA1 Message Date
tuna2134
e7a1575cbc Merge pull request #233 from kono-dada/feature/stereo-output
feat: add stereo synthesis option via SBV2_FORCE_STEREO env var
2025-08-11 17:13:19 +09:00
kono-dada
873bbb77b6 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.
2025-08-11 11:38:32 +08:00

View File

@@ -173,8 +173,15 @@ pub fn parse_text_blocking(
} }
pub fn array_to_vec(audio_array: Array3<f32>) -> Result<Vec<u8>> { pub fn array_to_vec(audio_array: Array3<f32>) -> Result<Vec<u8>> {
// 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 { let spec = WavSpec {
channels: 1, channels,
sample_rate: 44100, sample_rate: 44100,
bits_per_sample: 32, bits_per_sample: 32,
sample_format: SampleFormat::Float, sample_format: SampleFormat::Float,
@@ -183,10 +190,18 @@ pub fn array_to_vec(audio_array: Array3<f32>) -> Result<Vec<u8>> {
let mut writer = WavWriter::new(&mut cursor, spec)?; let mut writer = WavWriter::new(&mut cursor, spec)?;
for i in 0..audio_array.shape()[0] { for i in 0..audio_array.shape()[0] {
let output = audio_array.slice(s![i, 0, ..]).to_vec(); let output = audio_array.slice(s![i, 0, ..]).to_vec();
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 { for sample in output {
writer.write_sample(sample)?; writer.write_sample(sample)?;
} }
} }
}
writer.finalize()?; writer.finalize()?;
Ok(cursor.into_inner()) Ok(cursor.into_inner())
} }