check if mic is muted and unmute it before capturing audio.

I lost 7 days worth of captures due to misclicking the microphone mute
button. Ended up with a week's worth of silent audio
This commit is contained in:
2025-07-07 23:37:33 +02:00
parent 0c918cb45c
commit ecaaf866ba
+53
View File
@@ -23,7 +23,60 @@ WAV_HEADER_SIZE :: 44
interrupted := false interrupted := false
unmute_if_we_are_mute :: proc() {
handle: ^alsa.mixer_t
sid: ^alsa.mixer_selem_id_t
selem_name :: "Capture"
alsa.mixer_selem_id_malloc(&sid)
defer alsa.mixer_selem_id_free(sid)
alsa.mixer_selem_id_set_index(sid, 0)
alsa.mixer_selem_id_set_name(sid, selem_name)
if alsa.mixer_open(&handle, 0) < 0 {
panic("Failed to open mixer")
}
defer alsa.mixer_close(handle)
if alsa.mixer_attach(handle, DEVICE) < 0 {
panic("Failed to attach mixer to device")
}
if alsa.mixer_selem_register(handle, nil, nil) < 0 {
panic("Failed to register mixer element")
}
if alsa.mixer_load(handle) < 0 {
panic("Failed to load mixer")
}
// We can loop over the available elements and print their names. "Capture" seems to work tho
// fmt.println(alsa.mixer_first_elem(handle))
// for elem := alsa.mixer_first_elem(handle); elem != nil; elem = alsa.mixer_elem_next(elem) {
// name := alsa.mixer_selem_get_name(elem)
// fmt.println(name)
// }
elem := alsa.mixer_find_selem(handle, sid)
assert(elem != nil, "Mixer element is nil")
mic_enabled: i32
if alsa.mixer_selem_has_capture_switch(elem) {
assert(alsa.mixer_selem_get_capture_switch(elem, .MONO, &mic_enabled) >= 0, "get_capture_switch for mono channel failed")
} else {
panic("No capture switch found")
}
if mic_enabled == 0 {
fmt.println("I Have no Ears, and I Must Listen (Attempting to unmute microphone)")
assert(alsa.mixer_selem_set_capture_switch(elem, .MONO, 1) >= 0, "Failed to unmute capture device")
}
}
main :: proc() { main :: proc() {
unmute_if_we_are_mute()
handle: ^alsa.pcm_t handle: ^alsa.pcm_t
alsa_err := alsa.pcm_open(&handle, DEVICE, .CAPTURE, .BLOCK) alsa_err := alsa.pcm_open(&handle, DEVICE, .CAPTURE, .BLOCK)