Android solves recording and playback audio issues through tinyalsa debugging

When developing audio on Android devices, we may encounter various problems. One of them is that the recording volume is too low. In this article, I'll explore how to diagnose and fix this problem, and briefly introduce the use of the tinyalsa tool.

Problem Description

Recently, I am using the Allwinner Android platform. A customer has a requirement for a MIC, which requires the function of recording and playing audio. I found the Allwinner platform sound card driver, which provides two microphone inputs, namely MIC1 and MIC2. I want to use MIC1 as the recording input source instead of MIC2.

I've tried many things, including modifying the sound card configuration files, mixer controls, and recording commands, without success.

Solution

After some searching and trying, I found a solution, which is to use MIC1 as the recording input source by modifying the sound card configuration file, mixer control and recording commands, and increase the MIC recording and amplifier headphone sound volume.

Modify sound card configuration file

The sound card configuration file is a file used to define sound card parameters and devices. It is usually located in system\etc\codec_paths.xml. :

  <path name="media-main-mic">
   <ctl name="Left Input Mixer MIC2 Boost Switch" value="1" />
   <ctl name="Right Input Mixer MIC2 Boost Switch" value="1" />
  </path>

  <path name="media-main-mic-close">
   <ctl name="Left Input Mixer MIC2 Boost Switch" value="0" />
   <ctl name="Right Input Mixer MIC2 Boost Switch" value="0" />
  </path>

Source code location:hardware/aw/audio/codec_paths.xml

This content defines the audio path configuration in the Android audio system. These path configurations are typically used to determine how audio signals flow between hardware and software.

  • When media-main-micthe path is activated, the MIC2 gain switch is turned on on both the left and right input mixers, allowing the MIC2 audio signal to enter the audio processing chain.

  • When media-main-mic-closethe path is activated, MIC2's gain switch is turned off on both the left and right input mixers, which prevents MIC2's audio signal from entering the audio processing chain.

I compared the file on the normal machine and it was displayed MIC1, but on the abnormal machine it was displayed, MIC2so I think it is effective to change this.

Modify mixer controls

Then I remembered the debugging I had used a long time ago tinyalsa , if there was no need to execute it mmm external/tinyalsa/.

Mixer controls are parameters used to control sound card settings, such as input source, output device, gain, volume, etc. To modify the mixer controls, you need to use a command line tool to view and set these parameters. On the Allwinner platform, you can use the tool tinyalsa to implement this function. tinyalsa is actually a simplified ALSA library that provides some command line tools to test and control audio devices.

Use tinyalsa to modify the mixer controls, such as adjusting the MIC recording and amplifier headphone sound volume parameters. You can use the tinymix command to modify the values ​​of these controls, for example:

tinymix "digital volume" 63
tinymix "Headphone volume" 63
tinymix "MIC gain volume" 7 7
tinymix "MIC1 boost volume" 7
tinymix "MIC2 boost volume" 7
tinymix "ADC gain volume" 7

They can affect the MIC's recording and amplifier headphone sound levels:

  • digital volume: This control can adjust the digital volume. Its range is 0 to 63. The larger the value, the greater the volume.
  • Headphone volume: This control can adjust the headphone volume. Its range is 0 to 63. The larger the value, the greater the volume.
  • MIC gain volume: This control can adjust the gain of MIC. It has two channels, corresponding to MIC1 and MIC2 respectively. Its range is 0 to 7, the larger the value, the greater the gain.
  • MIC1 boost volume: This control can adjust the additional gain of MIC1. Its range is 0 to 7. The larger the value, the greater the gain.
  • MIC2 boost volume: This control can adjust the additional gain of MIC2. Its range is 0 to 7. The larger the value, the greater the gain.
  • ADC gain volume: This control can adjust the gain of the ADC. Its range is 0 to 7. The larger the value, the greater the gain.

These commands will set the values ​​of these controls to the maximum value, thereby temporarily and quickly increasing the MIC recording and amplifier headphone sound volume. Then adjust these values ​​of the code according to the actual situation.

Modify recording command

The recording command is a command line tool for recording audio files. It can specify parameters such as sampling rate, number of channels, format, duration, etc. On the Allwinner platform, you can use the tool tinyalsa to implement this function.

To modify the recording command using tinyalsa, you need to run the following command:

tinycap -D 0 -d mic -c 2 -r 44100 -b 16 -t 10 /sdcard/test.wav

This command means to use the mic device of sound card 0, dual channel, 44.1 kHz, 16 bits, to record a 10-second audio file to /sdcard/test.wav.

Result verification

After the above modifications, I can use MIC2 to record and play audio. My results can be verified using the following command:

tinyplay -D 0 /sdcard/test.wav

This command means to use sound card 0 to play the /sdcard/test.wav file. The audio content I recorded can be heard and the sound is clear and loud.

So far my problem is solved,


Other Audio debugging methods

Here are some common commands and methods for debugging audio-related issues:

1. Audio Profile Check

On certain platforms, such as Allwinner, there may be specific configuration files or tools for audio settings. /system/etcThese files can be found in or other relevant directories.

2. Use dumpsyscommands

dumpsysIt provides detailed information about system services. In order to get detailed information about the audio system, you can use:

XXX # dumpsys media.audio_flinger
Library loudness_enhancer
  Loudness Enhancer / The Android Open Source Project
    UUID: fa415329-2034-4bea-b5dc-5b381c8d1e2c
    TYPE: fe3199be-aed0-413f-87bb-11260eb63cf1
    apiVersion: 00020000
    flags: 00000008
Library downmix
  Multichannel Downmix To Stereo / The Android Open Source Project
    UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
    TYPE: 381e49cc-a858-4aa2-87f6-e8388e7601b2
    apiVersion: 00020000
    flags: 00000008
Library visualizer
  Visualizer / The Android Open Source Project
    UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
    TYPE: e46b26a0-dddd-11db-8afd-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000008
Library reverb
  Insert Preset Reverb / NXP Software Ltd.
    UUID: 172cdf00-a3bc-11df-a72f-0002a5d5c51b
    TYPE: 47382d60-ddd8-11db-bf3a-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000048
  Auxiliary Preset Reverb / NXP Software Ltd.
    UUID: f29a1400-a3bb-11df-8ddc-0002a5d5c51b
    TYPE: 47382d60-ddd8-11db-bf3a-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000001
  Insert Environmental Reverb / NXP Software Ltd.
    UUID: c7a511a0-a3bb-11df-860e-0002a5d5c51b
    TYPE: c2e5d5f0-94bd-4763-9cac-4e234d06839e
    apiVersion: 00020000
    flags: 00000048
  Auxiliary Environmental Reverb / NXP Software Ltd.
    UUID: 4a387fc0-8ab3-11df-8bad-0002a5d5c51b
    TYPE: c2e5d5f0-94bd-4763-9cac-4e234d06839e
    apiVersion: 00020000
    flags: 00000001
Library bundle
  Volume / NXP Software Ltd.
    UUID: 119341a0-8469-11df-81f9-0002a5d5c51b
    TYPE: 09e8ede0-ddde-11db-b4f6-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000050
  Equalizer / NXP Software Ltd.
    UUID: ce772f20-847d-11df-bb17-0002a5d5c51b
    TYPE: 0bed4300-ddd6-11db-8f34-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000048
  Virtualizer / NXP Software Ltd.
    UUID: 1d4033c0-8557-11df-9f2d-0002a5d5c51b
    TYPE: 37cc2c00-dddd-11db-8577-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000250
  Dynamic Bass Boost / NXP Software Ltd.
    UUID: 8631f300-72e2-11df-b57e-0002a5d5c51b
    TYPE: 0634f220-ddd4-11db-a0fc-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000248
Clients:
  pid: 2404
Notification Clients:
  pid: 1755
  pid: 1765
  pid: 2404
  pid: 4551
  pid: 5872
Global session refs:
  session   pid count
        9  2404     1
Hardware status: 0
Standby Time mSec: 3000

Output thread 0xae003d40 type 0 (MIXER):
  Thread name: AudioOut_D
  I/O handle: 13
  TID: 1823
  Standby: yes
  Sample rate: 44100 Hz
  HAL frame count: 2720
  HAL format: 0x1 (pcm16)
  HAL buffer size: 10880 bytes
  Channel count: 2
  Channel mask: 0x00000003 (front-left, front-right)
  Processing format: 0x1 (pcm16)
  Processing frame size: 4 bytes
  Pending config events: none
  Output device: 0x2 (SPEAKER)
  Input device: 0 (NONE)
  Audio source: 0 (default)
  Normal frame count: 2720
  Last write occurred (msecs): 2158695
  Total writes: 4459
  Delayed writes: 0
  Blocked in write: no
  Suspend count: 0
  Sink buffer : 0xae054000
  Mixer buffer: 0xae072000
  Effect buffer: 0xae057000
  Fast track availMask=0xfe
  Standby delay ns=3000000000
  AudioStreamOut: 0xae548150 flags 0x2 (PRIMARY)
  Frames written: 12128480
  Suspended frames: 0
  Hal stream dump:
  Thread throttle time (msecs): 1037
  AudioMixer tracks: 0x00000001
  Master mono: off
  FastMixer not initialized
  Stream volumes in dB: 0:-5.9, 1:-6, 2:0, 3:0, 4:0, 5:0, 6:0, 7:-6, 8:-6, 9:0, 10:0, 11:0, 12:0
  Normal mixer raw underrun counters: partial=0 empty=0
  1 Tracks of which 0 are active
    Name Active Client Type      Fmt Chn mask Session fCount S F SRate  L dB  R dB    Server Main buf  Aux Buf Flags UndFrmCnt
       0     no   2404    1 00000001 00000003       9   4512 S 1 48000  -inf  -inf  0005B6E0 0xae054000 0x0 0x600         0
  0 Effect Chains

USB audio module:
  No output streams.

  No input streams.

Reroute submix audio module:
 route[0] rate in=0 out=0, addr=[]
 route[1] rate in=0 out=0, addr=[]
 route[2] rate in=0 out=0, addr=[]
 route[3] rate in=0 out=0, addr=[]
 route[4] rate in=0 out=0, addr=[]
 route[5] rate in=0 out=0, addr=[]
 route[6] rate in=0 out=0, addr=[]
 route[7] rate in=0 out=0, addr=[]
 route[8] rate in=0 out=0, addr=[]
 route[9] rate in=48000 out=48000, addr=[]
a40-p1:/data/data # cat /proc/asound/cards
 0 [audiocodec     ]: audiocodec - audiocodec
                      audiocodec
 1 [fmidi          ]: MIDI Gadget - f_midi
                      MIDI Gadget
a40-p1:/data/data # dumpsys media.audio_flinger
Library loudness_enhancer
  Loudness Enhancer / The Android Open Source Project
    UUID: fa415329-2034-4bea-b5dc-5b381c8d1e2c
    TYPE: fe3199be-aed0-413f-87bb-11260eb63cf1
    apiVersion: 00020000
    flags: 00000008
Library downmix
  Multichannel Downmix To Stereo / The Android Open Source Project
    UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
    TYPE: 381e49cc-a858-4aa2-87f6-e8388e7601b2
    apiVersion: 00020000
    flags: 00000008
Library visualizer
  Visualizer / The Android Open Source Project
    UUID: d069d9e0-8329-11df-9168-0002a5d5c51b
    TYPE: e46b26a0-dddd-11db-8afd-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000008
Library reverb
  Insert Preset Reverb / NXP Software Ltd.
    UUID: 172cdf00-a3bc-11df-a72f-0002a5d5c51b
    TYPE: 47382d60-ddd8-11db-bf3a-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000048
  Auxiliary Preset Reverb / NXP Software Ltd.
    UUID: f29a1400-a3bb-11df-8ddc-0002a5d5c51b
    TYPE: 47382d60-ddd8-11db-bf3a-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000001
  Insert Environmental Reverb / NXP Software Ltd.
    UUID: c7a511a0-a3bb-11df-860e-0002a5d5c51b
    TYPE: c2e5d5f0-94bd-4763-9cac-4e234d06839e
    apiVersion: 00020000
    flags: 00000048
  Auxiliary Environmental Reverb / NXP Software Ltd.
    UUID: 4a387fc0-8ab3-11df-8bad-0002a5d5c51b
    TYPE: c2e5d5f0-94bd-4763-9cac-4e234d06839e
    apiVersion: 00020000
    flags: 00000001
Library bundle
  Volume / NXP Software Ltd.
    UUID: 119341a0-8469-11df-81f9-0002a5d5c51b
    TYPE: 09e8ede0-ddde-11db-b4f6-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000050
  Equalizer / NXP Software Ltd.
    UUID: ce772f20-847d-11df-bb17-0002a5d5c51b
    TYPE: 0bed4300-ddd6-11db-8f34-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000048
  Virtualizer / NXP Software Ltd.
    UUID: 1d4033c0-8557-11df-9f2d-0002a5d5c51b
    TYPE: 37cc2c00-dddd-11db-8577-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000250
  Dynamic Bass Boost / NXP Software Ltd.
    UUID: 8631f300-72e2-11df-b57e-0002a5d5c51b
    TYPE: 0634f220-ddd4-11db-a0fc-0002a5d5c51b
    apiVersion: 00020000
    flags: 00000248
Clients:
  pid: 2404
Notification Clients:
  pid: 1755
  pid: 1765
  pid: 2404
  pid: 4551
  pid: 5872
Global session refs:
  session   pid count
        9  2404     1
Hardware status: 0
Standby Time mSec: 3000

Output thread 0xae003d40 type 0 (MIXER):
  Thread name: AudioOut_D
  I/O handle: 13
  TID: 1823
  Standby: yes
  Sample rate: 44100 Hz
  HAL frame count: 2720
  HAL format: 0x1 (pcm16)
  HAL buffer size: 10880 bytes
  Channel count: 2
  Channel mask: 0x00000003 (front-left, front-right)
  Processing format: 0x1 (pcm16)
  Processing frame size: 4 bytes
  Pending config events: none
  Output device: 0x2 (SPEAKER)
  Input device: 0 (NONE)
  Audio source: 0 (default)
  Normal frame count: 2720
  Last write occurred (msecs): 2540304
  Total writes: 4459
  Delayed writes: 0
  Blocked in write: no
  Suspend count: 0
  Sink buffer : 0xae054000
  Mixer buffer: 0xae072000
  Effect buffer: 0xae057000
  Fast track availMask=0xfe
  Standby delay ns=3000000000
  AudioStreamOut: 0xae548150 flags 0x2 (PRIMARY)
  Frames written: 12128480
  Suspended frames: 0
  Hal stream dump:
  Thread throttle time (msecs): 1037
  AudioMixer tracks: 0x00000001
  Master mono: off
  FastMixer not initialized
  Stream volumes in dB: 0:-5.9, 1:-6, 2:0, 3:0, 4:0, 5:0, 6:0, 7:-6, 8:-6, 9:0, 10:0, 11:0, 12:0
  Normal mixer raw underrun counters: partial=0 empty=0
  1 Tracks of which 0 are active
    Name Active Client Type      Fmt Chn mask Session fCount S F SRate  L dB  R dB    Server Main buf  Aux Buf Flags UndFrmCnt
       0     no   2404    1 00000001 00000003       9   4512 S 1 48000  -inf  -inf  0005B6E0 0xae054000 0x0 0x600         0
  0 Effect Chains

USB audio module:
  No output streams.

  No input streams.

Reroute submix audio module:
 route[0] rate in=0 out=0, addr=[]
 route[1] rate in=0 out=0, addr=[]
 route[2] rate in=0 out=0, addr=[]
 route[3] rate in=0 out=0, addr=[]
 route[4] rate in=0 out=0, addr=[]
 route[5] rate in=0 out=0, addr=[]
 route[6] rate in=0 out=0, addr=[]
 route[7] rate in=0 out=0, addr=[]
 route[8] rate in=0 out=0, addr=[]
 route[9] rate in=48000 out=48000, addr=[]

3. View ALSA PCM equipment

To view the ALSA PCM devices currently on the system, use the following command:

XXX:/data/data # cat /proc/asound/pcm
00-00: SUNXI-CODEC sun8iw11codec-0 :  : playback 1 : capture 1

4. List all sound cards

To view all sound cards in the system, use the following command:

XXX:/data/data # cat /proc/asound/cards
 0 [audiocodec     ]: audiocodec - audiocodec
                      audiocodec
 1 [fmidi          ]: MIDI Gadget - f_midi
                      MIDI Gadget

5. Use tinyalsa tool

tinyalsaIs a tool for interacting with the ALSA interface. To list all mixer controls, use:

XXX:/ # tinymix
Mixer name: 'audiocodec'
Number of controls: 48
ctl     type    num     name                                     value
0       ENUM    1       codec hub mode                           hub_disable
1       INT     1       digital volume                           63
2       INT     1       Headphone volume                         59
3       INT     2       LINEIN Mixer volume                      3 3
4       INT     1       FM gain volume                           3
5       INT     1       LINEIN gain volume                       3
6       INT     2       MIC gain volume                          3 3
7       INT     1       phoneout volume                          3
8       INT     1       MIC1 boost volume                        4
9       INT     1       MIC2 boost volume                        4
10      INT     1       ADC gain volume                          3
11      ENUM    1       MIC2 Mux                                 MIC2IN
12      ENUM    1       HPL Mux                                  DAC
13      ENUM    1       HPR Mux                                  DAC
14      BOOL    1       Phone Out Mixer LOMIX Switch             Off
15      BOOL    1       Phone Out Mixer ROMIX Switch             Off
16      BOOL    1       Phone Out Mixer MIC2 Boost Switch        Off
17      BOOL    1       Phone Out Mixer MIC1 Boost Switch        Off
18      BOOL    1       Right Input Mixer LOMIX Switch           Off
19      BOOL    1       Right Input Mixer ROMIX Switch           Off
20      BOOL    1       Right Input Mixer FMR Switch             Off
21      BOOL    1       Right Input Mixer LINEINR Switch         Off
22      BOOL    1       Right Input Mixer LINEINLR Switch        Off
23      BOOL    1       Right Input Mixer MIC2 Boost Switch      On
24      BOOL    1       Right Input Mixer MIC1 Boost Switch      Off
25      BOOL    1       Left Input Mixer ROMIX Switch            Off
26      BOOL    1       Left Input Mixer LOMIX Switch            Off
27      BOOL    1       Left Input Mixer FML Switch              Off
28      BOOL    1       Left Input Mixer LINEINL Switch          Off
29      BOOL    1       Left Input Mixer LINEINLR Switch         Off
30      BOOL    1       Left Input Mixer MIC2 Boost Switch       On
31      BOOL    1       Left Input Mixer MIC1 Boost Switch       On
32      BOOL    1       Right Output Mixer DACL Switch           Off
33      BOOL    1       Right Output Mixer DACR Switch           On
34      BOOL    1       Right Output Mixer FMR Switch            Off
35      BOOL    1       Right Output Mixer LINEINR Switch        Off
36      BOOL    1       Right Output Mixer LINEINLR Switch       Off
37      BOOL    1       Right Output Mixer MIC2 Boost Switch     Off
38      BOOL    1       Right Output Mixer MIC1 Boost Switch     Off
39      BOOL    1       Left Output Mixer DACR Switch            Off
40      BOOL    1       Left Output Mixer DACL Switch            On
41      BOOL    1       Left Output Mixer FML Switch             Off
42      BOOL    1       Left Output Mixer LINEINL Switch         Off
43      BOOL    1       Left Output Mixer LINEINLR Switch        Off
44      BOOL    1       Left Output Mixer MIC2 Boost Switch      Off
45      BOOL    1       Left Output Mixer MIC1 Boost Switch      Off
46      BOOL    1       Headphone Switch                         On
47      BOOL    1       Phoneout Speaker Switch                  Off

6. View audio related logs

Using logcattools, you can check the logs related to system audio to see if there are any problems or errors:

logcat | grep -i audio

7. View kernel logs

dmesgYou can use the command to look at the kernel log and check if there are any problems with the audio driver:

dmesg | grep -i audio

Summarize

Through this process, I learned how to use the sound card driver of the Allwinner Android platform to record and play audio, especially how to use MIC1 as the recording input source. I also learned how to use tinyalsa as a tool to view and set sound card parameters and devices.

I hope this blog was helpful to you, if you have any questions or suggestions, please leave them in the comments section. Thanks!

Guess you like

Origin blog.csdn.net/SHH_1064994894/article/details/133272500