feat(audio): stereo mic capture on Windows & macOS desktop clients
Both desktop mics were hard-mono: the core defaults capture_channels=1 and neither client ever called vc_set_capture_channels (only iOS did). Add a persisted "Stereo microphone" toggle to each client's Audio settings, applied when the mic stream starts and live via vc_set_capture_channels + vc_audio_restart. Expose both ABI calls in the Windows interop; the macOS wrapper already had them. Core fix: encode_and_send_frame now folds a stereo mic frame to mono on a mono channel - previously the channels==2 branch encoded interleaved L/R directly even on a mono channel, feeding a mono opus_encode 2x its samples (wrong pitch/garbage). Real stereo still only reaches the wire on a stereo channel; on a mono channel the mic is cleanly downmixed. Test: test_stereo_mic_mono_channel. ctest --preset dev green (28/28). Docs: voice.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -662,6 +662,59 @@ static void test_stereo_mic_capture() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// ── 6. Stereo mic capture on a MONO channel (downmix safety) ──────────────────
|
||||
// A stereo mic (vc_set_capture_channels=2) can be enabled while on a mono channel. The mic
|
||||
// then delivers interleaved L/R, but the channel's Opus encoder is mono. encode_and_send_frame
|
||||
// must fold L/R to mono before encoding — handing interleaved pairs straight to a mono
|
||||
// opus_encode makes it read 2× the samples it should (wrong pitch / garbage). This mirrors that
|
||||
// fold and proves the result is a valid mono bitstream that decodes to the expected averaged
|
||||
// signal, rather than half-length junk.
|
||||
#if defined(VOICECAT_HAS_AUDIO) && defined(VOICECAT_HAS_OPUS)
|
||||
static void test_stereo_mic_mono_channel() {
|
||||
voicecat::codec::OpusParams mono_params;
|
||||
mono_params.stereo = false; // mono channel — encoder is mono
|
||||
mono_params.application = voicecat::codec::OpusApplication::Voip;
|
||||
mono_params.bitrate_bps = 64000;
|
||||
const int frame_samples = voicecat::codec::opus_frame_samples(mono_params);
|
||||
|
||||
voicecat::codec::OpusEncoder enc;
|
||||
CHECK(enc.init(mono_params));
|
||||
|
||||
// Loud left, silent right — folding (L+R)/2 yields a half-amplitude tone on every sample.
|
||||
std::vector<int16_t> interleaved(static_cast<size_t>(frame_samples) * 2);
|
||||
for (int i = 0; i < frame_samples; ++i) {
|
||||
float t = static_cast<float>(i) / 48000.0f;
|
||||
interleaved[i * 2] = static_cast<int16_t>(std::sin(2.0f * 3.14159265f * 440.0f * t) * 20000.0f);
|
||||
interleaved[i * 2 + 1] = 0;
|
||||
}
|
||||
|
||||
// Fold exactly as encode_and_send_frame does for a stereo frame on a mono channel.
|
||||
std::vector<int16_t> folded(frame_samples);
|
||||
for (int i = 0; i < frame_samples; ++i)
|
||||
folded[i] = static_cast<int16_t>(
|
||||
(static_cast<int32_t>(interleaved[i * 2]) + static_cast<int32_t>(interleaved[i * 2 + 1])) / 2);
|
||||
|
||||
uint8_t opus_buf[1500];
|
||||
int opus_len = enc.encode(folded.data(), frame_samples, opus_buf, sizeof(opus_buf));
|
||||
CHECK(opus_len > 0);
|
||||
|
||||
// Decode mono and verify a full-length frame with real energy survived (a garbage half-read
|
||||
// would either fail to decode the full frame_samples or come back near-silent / wrong length).
|
||||
voicecat::codec::OpusDecoder dec;
|
||||
CHECK(dec.init(mono_params));
|
||||
std::vector<int16_t> decoded(frame_samples, 0);
|
||||
int dec_samples = dec.decode(opus_buf, opus_len, decoded.data(), frame_samples);
|
||||
CHECK(dec_samples == frame_samples);
|
||||
|
||||
int64_t energy = 0;
|
||||
for (int i = 0; i < frame_samples; ++i) energy += std::abs(static_cast<int>(decoded[i]));
|
||||
CHECK(energy > static_cast<int64_t>(frame_samples) * 500); // clearly audible, not silence
|
||||
|
||||
std::printf("test_stereo_mic_mono_channel: ok (opus_len=%d, energy=%lld)\n",
|
||||
opus_len, static_cast<long long>(energy));
|
||||
}
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
test_device_enumeration();
|
||||
#if defined(VOICECAT_HAS_AUDIO) && defined(VOICECAT_HAS_OPUS)
|
||||
@@ -670,6 +723,7 @@ int main() {
|
||||
test_loopback_stereo_capture();
|
||||
#endif
|
||||
test_stereo_mic_capture();
|
||||
test_stereo_mic_mono_channel();
|
||||
test_playout_resync();
|
||||
#endif
|
||||
#ifdef VOICECAT_HAS_AUDIO
|
||||
|
||||
Reference in New Issue
Block a user