Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASAPI multi-channel microphone input #96947

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion drivers/wasapi/audio_driver_wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {

// fixme: Only works for floating point atm
for (UINT32 j = 0; j < num_frames_available; j++) {
int32_t l, r;
int32_t l = 0, r = 0;

if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
l = r = 0;
Expand All @@ -859,6 +859,29 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
} else if (ad->audio_input.channels == 1) {
l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
} else if (ad->audio_input.channels > 2) {
int64_t ltemp = 0, rtemp = 0;
int channels = ad->audio_input.channels;
for (int ch = 0; ch < channels - 1; ch++) {
int32_t sample = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * channels + ch);
if (ch % 2 == 0) {
rtemp += sample;
} else {
ltemp += sample;
}
}
int32_t last_sample = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * channels + (channels - 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can input have the odd number of channels at all (the assumption here seems to be L,R,Center channel config)?
Also, do this odd/even channel distribution to left/right always holds (is defined somewhere in the WASAPI documented, or based on specific mic sample)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the assumption that the microphone might have a potential configuration with odd channels (e.g., Left, Right, Center), although such configurations are uncommon (in some professional audio equipment). The goal was to ensure that the microphone is at least usable within Godot, regardless of the channel count. The WASAPI documentation itself doesn't explicitly specify how multi-channel microphones should be handled, so I aimed for a more general solution to accommodate various configurations.

r += last_sample;
if (channels % 2 != 0) {
ltemp += last_sample;
ltemp /= ((channels + 1) / 2);
rtemp /= ((channels + 1) / 2);
} else {
ltemp /= (channels / 2);
rtemp /= (channels / 2);
}
l = static_cast<int32_t>(ltemp);
r = static_cast<int32_t>(rtemp);
} else {
l = r = 0;
ERR_PRINT("WASAPI: unsupported channel count in microphone!");
Expand Down