Open the recording device only when the game starts microphone capture.

The game opens two input streams while it starts and leaves both stopped through ordinary play, and each open took the system's default recording device and held it, paused, until the game ended: Windows shows the microphone as in use for the whole session, and a Bluetooth headset can drop to its call profile. An open now checks that a default input exists and prepares the stream and its bounded buffer; the device is opened at StartInputStream and released at stop or close, after which the game can still read or purge what was buffered. A start that is refused returns an input error and can be tried again.

tests/audio_input_check.py did not compile on this line (it had no stand-in for the microphone setting); with the change it builds and passes: idle streams hold no device, a refused start can be retried, a stop releases the device and keeps the unread frames, two streams are independent. Not checked with a real microphone in the game.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 22:59:02 -05:00
parent 6750e0bee1
commit 093cf949df
4 changed files with 112 additions and 22 deletions

View File

@@ -6,6 +6,7 @@
struct MicCapture {
SDL_AudioStream *stream;
SDL_AudioDeviceID device;
unsigned char *ring;
int cap, head, used, bpf;
};
@@ -95,24 +96,45 @@ MicCapture *mic_open(int format, int channels, int rate, int frames) {
return NULL;
}
SDL_AudioSpec spec = {.format = sf, .channels = channels, .freq = rate};
m->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, received, m);
if (!m->stream) {
free(m->ring);
free(m);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_AudioSpec source;
if (SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &source, NULL))
m->stream = SDL_CreateAudioStream(&source, &spec);
if (!m->stream || !SDL_SetAudioStreamPutCallback(m->stream, received, m)) {
mic_close(m);
return NULL;
}
return m;
}
int mic_start(MicCapture *m) { return m && SDL_ResumeAudioStreamDevice(m->stream); }
int mic_start(MicCapture *m) {
if (!m)
return 0;
if (m->device)
return 1;
SDL_AudioSpec spec;
if (!SDL_GetAudioStreamFormat(m->stream, NULL, &spec))
return 0;
SDL_AudioDeviceID device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec);
if (!device)
return 0;
if (!SDL_BindAudioStream(device, m->stream)) {
SDL_CloseAudioDevice(device);
return 0;
}
m->device = device;
return 1;
}
void mic_stop(MicCapture *m) {
if (m)
SDL_PauseAudioStreamDevice(m->stream);
if (m && m->device) {
SDL_CloseAudioDevice(m->device); /* unbinds the stream; unread frames remain in the ring */
m->device = 0;
}
}
void mic_close(MicCapture *m) {
if (!m)
return;
SDL_DestroyAudioStream(m->stream); /* joins capture before freeing userdata */
mic_stop(m);
if (m->stream)
SDL_DestroyAudioStream(m->stream);
free(m->ring);
free(m);
SDL_QuitSubSystem(SDL_INIT_AUDIO);

View File

@@ -86,7 +86,7 @@ static void open_input(Cpu *c) {
}
MicCapture *m = mic_open(f[0], f[1], f[2], frames);
if (!m) {
rt_log("mic: cannot open the default recording device: %s", mic_error());
rt_log("mic: cannot prepare the input stream: %s", mic_error());
ret_x(c, BAD_INPUT);
return;
}
@@ -97,7 +97,7 @@ static void open_input(Cpu *c) {
s->next_log = (uint64_t)f[2] * 5;
uint64_t impl = key + 8;
memcpy(ipc_guest(key, 8), &impl, 8);
rt_log("mic: default recording device opened; bounded buffer %d frames", mic_capacity(m));
rt_log("mic: input stream ready; bounded buffer %d frames; device opens when capture starts", mic_capacity(m));
ret_x(c, 0);
}
static void start_input(Cpu *c) {
@@ -115,6 +115,8 @@ static void stop_input(Cpu *c) {
Input *s = find(c->x[0]);
if (s) {
mic_stop(s->mic);
if (s->running)
rt_log("mic: capture stopped; recording device released");
s->running = 0;
}
ret_x(c, 0);