How to adjust DMIC gain (or audio volume) ?

This is a follow up question regarding DMIC. https://devtalk.nvidia.com/default/topic/1030012/how-to-record-dmic-audio-in-gstreamer-device-name-/#5240034

After got the DMIC and audio working with video, we faced another challenge in the handling the volume. The default volume is very low.

In order to increase the volume, we tried to use amixer to boost the gain, for example, setting the gain to max:

amixer -c tegrasndt186ref sset "DMIC3 Boost Gain" 25600

An immediate consequence of using amixer to boost the gain is the lost of stereo. It turns the playback channel to Mono.

Any idea?

I tried to follow the example on ridgerun.com, where it shows how to insert a MVC volume control in the audio path.
http://developer.ridgerun.com/wiki/index.php?title=ASoC_Driver_in_Tegra_X1_and_X2#Master_volume_control_.28MVC.29
Applying the method to my case, I assume I should do as follows

amixer -c tegrasndt186ref sset 'ADMAIF2 Mux' 'MVC2'
amixer -c tegrasndt186ref sset 'MVC2 Mux' 'DMIC3'
amixer -c tegrasndt186ref cset name="MVC2 Vol" 50%

Afterward, I ran Jon’s arecord command “arecord -D hw:tegrasndt186ref,1 -r 48000 -c2 -f S16_LE cap.wav”. But no luck in hearing the sound.
It’s noticed that the audio file “cap.wav” is actually pretty big in size. Hopefully I’m moving in a right direction.

Please help and share your insight.

Many thanks in advance.

P.S. For removing the uncertainty, here are the things I checked and working:

  1. All Settings > Sound > Output is set to HDMI/DisplayPort 2 (tegra-hda). “Test Sound” works.
  2. Jon’s amixer/arecord commands work fine if I didn’t insert a MVC to the audio path.

Still trying to resolve DMIC issues (well, it works but need to figure out how to control it), I wrote a small python code to detect the audio input devices on TX2. Here is my code

import pyaudio
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print ("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))

And the results are:

Input Device id  2  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,0)
Input Device id  3  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,1)
Input Device id  4  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,2)
Input Device id  5  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,3)
Input Device id  6  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,4)
Input Device id  7  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,5)
Input Device id  8  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,6)
Input Device id  9  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,7)
Input Device id  10  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,8)
Input Device id  11  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,9)
Input Device id  12  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,10)
Input Device id  13  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,11)
Input Device id  14  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,12)
Input Device id  15  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,13)
Input Device id  16  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,14)
Input Device id  17  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,15)
Input Device id  18  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,16)
Input Device id  19  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,17)
Input Device id  20  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,18)
Input Device id  21  -  tegra-snd-t186ref-mobile-rt565x: - (hw:1,19)
Input Device id  22  -  pulse
Input Device id  26  -  default

Why don’t I also list the errors in the PyAudio output, could be useful for the debugging purpose. Here are the rest of the program output:

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4771:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4771:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:968:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_params.c:2162:(snd1_pcm_hw_refine_slave) Slave PCM not usable
ALSA lib pcm_params.c:2162:(snd1_pcm_hw_refine_slave) Slave PCM not usable
ALSA lib pcm_params.c:2162:(snd1_pcm_hw_refine_slave) Slave PCM not usable
ALSA lib pcm_params.c:2162:(snd1_pcm_hw_refine_slave) Slave PCM not usable
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave

So, the question is, which input device id is for DMIC?

For reference, the commands to record the audio via DMICs are:

amixer -c tegrasndt186ref sset "ADMAIF2 Mux" DMIC3
arecord -D hw:tegrasndt186ref,1 -r 48000 -c2 -f S16_LE cap.wav

Any help would be much appreciated.

The input device for DMIC3 is dependendent upon the ADMAIF it is routed too.

The mapping that you care about is …

ADMAIF1 Mux → hw:tegrasndt186ref,0 (typically hw:1,0)
ADMAIF2 Mux → hw:tegrasndt186ref,1 (typically hw:1,1)
ADMAIF3 Mux → hw:tegrasndt186ref,2 (typically hw:1,2)

You can see this mapping by looking at the output from ‘aplay -l’.

So if DMIC3 is routed to ADMAIF2 then you want hw:tegrasndt186ref,1 as you have above.

Please note that ensure DMIC3 is not routed to more than one ADMAIFx Mux or the audio maybe routed to the wrong interface. So always worth checking it is not mux’ed to more than one ADMAIF interface.

Is volume still a problem? I maybe able to look at this next week.

Regards,
Jon

Thanks, Jon. Let me see ‘aplay -l’, it produces a list of devices, such as

card 1: tegrasndt186ref [tegra-snd-t186ref-mobile-rt565x], device 0: ADMAIF1 CIF ADMAIF1-0
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: tegrasndt186ref [tegra-snd-t186ref-mobile-rt565x], device 1: ADMAIF2 CIF ADMAIF2-1
Subdevices: 1/1
Subdevice #0: subdevice #0
. . .

From your description, seems that I’m allowed to mux the DMIC to any audio DMA interfaces as long as they were not taken. By the way, is DMIC3 the set of digital MICs assign to the pins on the camera extension board? (just wonder if the number ‘3’ for DMIC3 means anything special).

It’d be great if you can help further. My volume problem is still there.

Let me be a little bit more specific,

What’s my DMIC hardware:
Two Knowles DMICs with L/R tied up and down, share the CLK and DATA pins connecting to camera connector AO_DMIC_IN_CLK and AO_DMIC_in_DAT (71 and 73) respectively.

What have been working:
(1) With the help of your ‘amixer’ command, I’m able to insert the audio to gst and generate ‘mpg’ video+audio using terminal command such as
gst-launch-1.0 nvcamerasrc sensor-id=0 ! ‘video/x-raw(memory:NVMM), width=2592, height=1944, framerate=15/1, format=I420’ ! nvvidconv flip-method=0 ! ‘video/x-raw, width=960, height=672, framerate=15/1, format=I420’ ! omxh264enc ! mpegtsmux name=mux alsasrc ‘device=hw:tegrasndt186ref,1’ ! audioresample ! audio/x-raw,rate=48000,channels=2 ! queue ! voaacenc bitrate=32000 ! queue ! mux. mux. ! filesink location=1cam_mix_audio.mpg sync=true async=false qos=true

(2) Tapping the left/right DMIC device on left or right while recording would produce the clear indication to distinguish the two audio channels.

(3) No obvious delay was observed between video and audio. That’s really good, since A/V sync was a major potential concern.

(4) Able to increase the level of output volume (default was at 50%) in “all settings, sound, output”

What’re the goal trying to achieve:
(1) Seek a better way to control the gain and volume for audio, either command line, python code, or others.

What’re the problems:
(1) The sound output volume is decent but not enough when the setting in system, sound, output is max out. Still searching solutions in how to further increase the volume (or DMIC gain) and how to use software to adjust the settings.

(2) The command ‘amixer -c tegrasndt186ref sset “DMIC3 Boost Gain” 25600’ would boost the gain and significantly increased audio record volume. But the also causes the stereo sound automatically converted to mono. Is there a command to boost the gain for stereo DMICs?

(3) “cset” the master volume control (MVC) with the commands as show in post#2 would instead fail the audio. No sound was produced by the commands. What’s the proper way to set the MVC?

Correct you can mux the DMIC via any of the ADMAIFs. There are 4 DMIC interfaces on the Tegra186 chip and DMIC3 is available via the camera connnector and the gpio expansion header on the Jetson TX2.

Thanks for the explanation. I will give this a test my side sometime next week and let you know what I find.

Regards,
Jon

I tried this today on my Jetson TX2 with the stock r28.1 release and this is working for me. I was able to hear the recording on both channels and verify that increasing the vol to 100% increased the recorded volume.

If it is not working, then check all the “ADMAIFx Mux” settings because if the DMIC3 is routed to more than one, then you might not be routing the audio correctly. Same applies to the other Muxes too such as MVC Mux, etc. Basically, you want to make sure DMIC3 is not routed to anywhere other than the ADMAIF2 Mux.

Regards,
Jon

(2) I think that there is a bug in the driver because ‘25600’ is too big and I seeing the following error in dmesg when this is set …

[ 314.961362] tegra210-dmic tegra210-dmic.2: Boost Gain overflow

The max value should be ‘25599’. This is not your problem, but FYI, this value is too big. We need to fix the driver so that it shows the correct limits and not …

Simple mixer control 'DMIC3 Boost Gain',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 25600
  Mono: 0 [0%]

So I tried testing this and setting the gain to ‘400’ and I was able to verify that this worked and both channels were amplified. Setting the gain to 25599 also worked, but it was inaudible, basically noise, but again I did not have any issues with recording stereo. So at the moment, I am unable to reproduce this problem on my side. Can you try lower gain settings and see if you have the same problems?

(3) Your MVC settings look good to me and work for me. So per my other response check the Mux settings to make sure DMIC is not being routed to more than one device.

Regards,
Jon

Hi Jon, thanks for checking out the audio related issue.

I was half way to verify the commands follow your instruction, the SD card seemed crashed suddenly. Let me report the test I ran about the boost command by setting it to 400:

amixer -c tegrasndt186ref sset "DMIC3 Boost Gain" 400

The sound quality turns out very good, but I can tell the stereo effect is gone. The left and right channels become the same.

Once I fixed the TX2 SD Card problem (at the moment SD crashed and some “disk management” problem prevents TX2 from shutdown), I may create a plot to show the difference of before and after. Please let me know if any command that can trace the operation.

@Jon, I plotted out the waveform of audio records to compare the ‘Boost Gain’ settings. I did more tests and the result shows that you are right about the command. It works well in its intended range.
I believe that the lost of stereo was instead caused by the position of our DMIC. As the gain increases, the sound recording gets crossed between the digital microphones as their sensitivity goes higher. The Boost Gain of 400 is already too much for the DMIC in my case given the position of DMIC placement.
The result is better by setting the gain to 200 in my case.

amixer -c tegrasndt186ref sset "DMIC3 Boost Gain" 200

The corresponding plots with Audacity for a test of tapping the left and right DMIC can be found in

gain=400 https://www.dropbox.com/s/o5h0ou5w193wfys/arec_gain400.png?dl=0
gain=200 https://www.dropbox.com/s/aurd86y4zno9i3a/arec_gain200.png?dl=0
gain=0 https://www.dropbox.com/s/l91in7qu1k78kq7/arec_gain0.png?dl=0

Now, for MVC, I haven’t got it to work. The audio would be mute if I ran the MVC master volume control (post #2, #7) after testing the Boost Gain. I will do more tests later.

Again, thanks a lot for your direction and confirmation.

Thanks for the feedback. It is difficult for me to test the gain well because my mic are only a few mm apart. However, glad to see reducing the gain setting helped.

With regard to the MVC, can you dump all the mixer setting using the following command and attach the output?

$ alsactl store -f alsa-settings.txt

Regards,
Jon

Thanks Jon. See attached for the alsa settings. Here is the sequence:

  1. Re-boot TX2
  2. Issue an audio mixer command
    $ amixer -c tegrasndt186ref sset “ADMAIF2 Mux” DMIC3
  3. Dump the ALSA setting (as attached)
    $ alsactl store -f alsa-settings_ori.txt
  4. Test the audio
    arecord -D hw:tegrasndt186ref,1 -r 48000 -c2 -f S16_LE cap.wav
    The sound appears normal
  5. Issue following commands:
    $ amixer -c tegrasndt186ref sset ‘ADMAIF2 Mux’ ‘MVC1’
    $ amixer -c tegrasndt186ref sset ‘MVC1 Mux’ ‘DMIC3’
    $ amixer -c tegrasndt186ref cset name=“MVC1 Vol” 500
  6. Dump the ALSA settings again
    $ alsactl store -f alsa-settings.txt
  7. Test the audio
    $ arecord -D hw:tegrasndt186ref,1 -r 48000 -c2 -f S16_LE cap.wav
    The sound is mute. Can’t hear anything.
  8. Compare the settings
    $ diff alsa-settings.txt alsa-settings_ori.txt > diff

4796c4796
< value 500

          value 12000

8129c8129
< value MVC1

          value DMIC3

11663c11663
< value DMIC3

          value None

Could my commands in step 5 be wrong?

P.S.
I’m not sure if I did it correctly by clicking the paperclip icon at the upper-right corner, trying to attach a text file after submit the post…
But stuck at [scanning , please wait] for a long time…
Why don’t I zip the setting file and put it up on Dropbox, here is the link
https://www.dropbox.com/s/5l25n4gw6ryyfbu/alsa-settings.txt.zip?dl=0
The link is to the setting file after MVC (no sound). It differs from the original in only three places as shown above.

P.P.S
Now I see the attachment at the end. It’s the settings before setting up MVC.
Good in a sense that now you have both files.
alsa-settings_ori.txt (399 KB)

Sorry for the delay. I have taken a look at your alsa settings and they look fine and yes your commands in step 5 look correct too.

The only thing I would suggest is setting the volume to the maximum of 16000 and seeing if you can hear anything then?

Regards,
Jon

Jon, thank you so much for your guidance. It works!
Apparently, the MVC value was set so low that the setting practically muted the sound.

In summary, here is the setting commands for audio in my TX2. The microphones are on the camera board.

$ amixer -c tegrasndt186ref sset ‘ADMAIF2 Mux’ ‘MVC1’
$ amixer -c tegrasndt186ref sset ‘MVC1 Mux’ ‘DMIC3’
$ amixer -c tegrasndt186ref cset name=“MVC1 Vol” 12000
$ amixer -c tegrasndt186ref sset “DMIC3 Boost Gain” 200

The sound volume can then be further adjusted to a high value, such as “amixer -c tegrasndt186ref cset name=“MVC1 Vol” 16000”