简体   繁体   中英

ALSA - unmuting devices?

I have been trying to capture audio, within a native linux program running on an Android device via adb shell.

Since I seemed to be getting only (very quiet) noise, ie no actual signal (interestingly, an Android/Java program doing similar did show there was a signal on that input), I executed alsa_amixer, which had one entry that looked like the right one:

Simple mixer control 'Capture',0
  Capabilities: cvolume cswitch penum
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 63
  Front Left: Capture 31 [49%] [0.00dB] [off]
  Front Right: Capture 31 [49%] [0.00dB] [off]

"off". That would explain the noise. So I looked for examples of how to use alsa_amixer to unmute the channels, I found different suggestions for parameters like "49% on" or "49% unmute", or just "unmute" none of which works. (if the volume% is left out, it says "Invalid command!", otherwise, the volume is set, but the on/unmute is ignored)

I also searched how to do this programatically (which I'll ultimately need to do, although the manual approach would be helpful for now), but wasn't too lucky there. The only ALSA lib command I found which sounds like it could do something like that was "snd_mixer_selem_set_capture_switch_all", but the docs don't day what the parameter does (1/0 is not on/off, I tried that ;) )

The manual approach to set these things via alsa_amixer does work - but only if android is built with the 'BoardConfigCommon.mk' modified, at the entry: BOARD_USES_ALSA_AUDIO := false, instead of true.

Yeah, this will probably disable ALSA for android, which is why it wouldn't meddle with the mixer settings anymore.

To you android programmers out there, note that this is a very niche use case of course, as was to be expected by my original post to begin with. This is not what most people would want to do. I just happen to tinker with an android device here in unusual ways ;-)

Just posting the code as question giver suggested, also don't like external links.

#include <alsa/asoundlib.h>


int main()
{
    snd_mixer_t *handle;
    snd_mixer_selem_id_t *sid;

    snd_mixer_open(&handle, 0);
    snd_mixer_attach(handle, "default");
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);

    snd_mixer_selem_id_alloca(&sid);
    snd_mixer_selem_id_set_index(sid, 0);
    snd_mixer_selem_id_set_name(sid, "Capture");
    snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);

    snd_mixer_selem_set_capture_switch_all(elem, 0);
    snd_mixer_selem_set_capture_dB_all(elem, 0, 0);

    snd_mixer_close(handle);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM