User Tools

Site Tools


alsa

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
alsa [2026-11-21 22:41 UTC (6 days ago)] – more info on how to enable audio out. jazzyalsa [2026-11-22 22:40 UTC (5 days ago)] (current) – remove some useless AI text / verbosity jazzy
Line 1: Line 1:
-====== Alsa ======+====== ALSA and the speakers ======
  
-Alsa is used for audio playback.+The two grille speakers are on the Line-Out of a TI TLV320AIC32x4 codec. The 
 +codec is on I2C bus 0 at address ''%%0x18%%'', fed over SSI, and shows up as ALSA card 
 +0, ''%%PM3 Audio%%''. There is a capture path too (Mic PGA / ADC).
  
-NX does some initialization of audio: +The Line-Out does not drive the speakers directly. It feeds an external power 
-  * ''amixer sset PCM 144'' +amplifier, ''%%PAmobil2%%'', which is off at power-on. The amp is switched on by the 
-  * ''NxExe prox2 amp''+PIC32 over ''%%/dev/ttymxc2%%'':
  
-The first command turns up the volume (be aware 144 can be quite loud) and the second asks the PIC32 to enable the power amplifier for the speakers. Keep in mind that the NxExe command //toggles// the amplifier, and this persists between reboots as long as the pic32 still has power. I assume the proper way is to read out the amp state on boot and toggle if needed.+  * PIC32 key ''%%0x07E0%%'' (''%%ValueWrite%%'', type ''%%0x04%%''): ''%%1%%'' = amp on, ''%%0%%'' = amp off. 
 +  * It reads back with ''%%ValueRead%%'', so the state can be queried. 
 +  * It remains in this state until the PIC32 resets at the next power-off.
  
-People have had mixed results with Alsa initialization. One person only had success by running NX: 
-> Saving and restoring alsa device state isn't enough to initialize with from boot. The NX binary is built with [[https://github.com/mackron/miniaudio|miniaudio]] inside of it so perhaps it does some external hardware initialization of the speaker? Couldn't get miniaudio in a custom binary to work without initializing by running NX. Maybe there's a way to coax out the miniaudio debug logs from a running NX instance. 
  
-Another person found that it worked out-of-the-box: +===== Turning the amp on ===== 
-> Copying an audio file (in my case, a 44.1kHz mono WAV file) onto the SD card and then playing it with ''%%aplay%%'' worked even with NX disabled. ''%%alsamixer%%'' is preinstalled and works for setting the volume.+ 
 +You can toggle the amp using NxExe: 
 + 
 +<code> 
 +NxExe prox2 amp 
 +</code> 
 + 
 +That command toggles the amplifier. The state persists until the PIC32 loses power, so 
 +blindly toggling after a reboot can turn it off if it was already on. 
 + 
 +To set the amp explicitly, write the PIC32 frame directly. These are IPP frames 
 +with sequence 1 and no payload CRC, because ''%%0x04%%'' and ''%%0x02%%'' are below ''%%0x80%%'': 
 + 
 +<code> 
 +stty -F /dev/ttymxc2 115200 cs8 -cstopb -parenb raw -echo 
 + 
 +# amp on: ValueWrite 0x07E0 = 1 
 +printf '\274\001\000\004\000\003\313\007\340\001' > /dev/ttymxc2 
 + 
 +# amp off: ValueWrite 0x07E0 = 0 
 +printf '\274\001\000\004\000\003\313\007\340\000' > /dev/ttymxc2 
 + 
 +# read state: ValueRead 0x07E0 
 +printf '\274\001\000\002\000\002\104\007\340' > /dev/ttymxc2 
 +timeout -t 2 dd if=/dev/ttymxc2 bs=1 count=10 2>/dev/null | hexdump -C 
 +</code> 
 + 
 +The reply to the read is a ''%%0x03%%'' frame with the 2-byte key and the current 
 +value. The examples use sequence 1. If you write a script, increment the sequence 
 +byte and recompute the CRC-8 for each frame. 
 + 
 +===== ALSA setup ===== 
 + 
 +''%%alsa-restore.service%%'' restores ''%%/etc/asound.state%%'' at boot, so the routing is 
 +usually already set. The two details that still cause silence are: 
 + 
 +Line-Out driver gain must be at the 0 dB baseline. ''%%6%%'' is the baseline, not a volume control. 
 + 
 +<code> 
 +amixer -c 0 sset 'LO Driver Gain' 6 
 +</code> 
 + 
 +Playing a file: 
 + 
 +<code> 
 +aplay /tmp/tone.wav 
 +</code> 
 + 
 +Volume is the ''%%PCM%%'' control. Use it as a percentage: 
 + 
 +<code> 
 +amixer -c 0 sset PCM 65% 
 +</code> 
 + 
 +A complete manual sequence: 
 + 
 +<code> 
 +printf '\274\001\000\004\000\003\313\007\340\001' > /dev/ttymxc2 
 +alsactl -f /etc/asound.state restore 
 +amixer -c 0 sset 'LO Driver Gain' 6 
 +amixer -c 0 sset PCM 65% 
 +scp -O tone.wav root@<unit-ip>:/tmp/ 
 +ssh root@<unit-ip> aplay /tmp/tone.wav 
 +</code> 
 + 
 + 
 +===== Why nx seemed to be required ===== 
 + 
 +nx turned the speaker amp on once at startup and never turns it off. The PIC32 
 +keeps that state until it loses power. 
 + 
 +The codec can be configured fine without the amp but the speakers will remain silent. Its registers, DAPM, GPIO, clocks, AUDMUX, and SSI1 
 +can all match while the speaker is silent. The amp is a separate PIC32 value on 
 +''%%ttymxc2%%'', after the codec. nx does not touch any extra ALSA mixer or codec 
 +control. It writes ''%%0x07E0 = 1%%'' once near boot. 
 + 
 + 
 +===== Notes (maybe outdated) ===== 
 + 
 +People have had mixed results with ALSA initialization. One person only had 
 +success by running nx: 
 + 
 +> Saving and restoring ALSA device state isn't enough to initialize from boot. 
 +> The nx binary is built with [[https://github.com/mackron/miniaudio|miniaudio]] 
 +> inside it, so perhaps it does some external hardware initialization of the 
 +> speaker. I couldn't get miniaudio in a custom binary to work without 
 +> initializing by running nx. Maybe there is a way to get the miniaudio debug 
 +> logs from a running nx instance. 
 + 
 +Another person found that it worked out of the box: 
 + 
 +> Copying an audio file (a 44.1 kHz mono WAV in my case) onto the SD card and 
 +> playing it with ''%%aplay%%'' worked even with nx disabled. ''%%alsamixer%%'' 
 +> is preinstalled and works for setting the volume. 
 + 
 +===== Addendum ===== 
 + 
 +The older reports about mixed ALSA results are consistent with the amp latch. 
 + 
 +If nx has run since power-on, the amp is on and ''%%aplay%%'' works even after nx is 
 +stopped. That is the "worked out of the box" case. 
 + 
 +If the unit is cold-booted with nx never started, ''%%aplay%%'' is silent until 
 +''%%0x07E0%%'' is set to 1. Saving and restoring ''%%/etc/asound.state%%'' does not touch 
 +the amp, so it cannot fix that case. 
 + 
 +The miniaudio theory was speculation. nx does not need to stay running for 
 +audio. It only needs to leave the amp switched on. 
 + 
 +===== Stock ALSA state ===== 
 + 
 +The stock ''%%/etc/asound.state%%'' is below. If you build a custom rootfs, keep this 
 +file and restore it at boot: 
 + 
 +<code> 
 +alsactl -f /etc/asound.state restore 
 +</code> 
 + 
 +The important parts are the Line-Out DAC switches, ''%%LO Driver Gain%%'' at 6 
 +(0 dB), and ''%%PCM Playback Volume%%'' at 85 (about -21 dB). The rest is the input 
 +and AGC configuration. 
 + 
 +<code> 
 +state.Audio { 
 + control.1 { 
 + iface MIXER 
 + name 'PCM Playback Volume' 
 + value.0 85 
 + value.1 85 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 175' 
 + dbmin -6350 
 + dbmax 2400 
 + dbvalue.0 -2100 
 + dbvalue.1 -2100 
 + } 
 + } 
 + control.2 { 
 + iface MIXER 
 + name 'HP Driver Gain Volume' 
 + value.0 6 
 + value.1 6 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 35' 
 + dbmin -600 
 + dbmax 2900 
 + dbvalue.0 0 
 + dbvalue.1 0 
 + } 
 + } 
 + control.3 { 
 + iface MIXER 
 + name 'LO Driver Gain Volume' 
 + value.0 6 
 + value.1 6 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 35' 
 + dbmin -600 
 + dbmax 2900 
 + dbvalue.0 0 
 + dbvalue.1 0 
 + } 
 + } 
 + control.4 { 
 + iface MIXER 
 + name 'HP DAC Playback Switch' 
 + value.0 false 
 + value.1 false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 2 
 + } 
 + } 
 + control.5 { 
 + iface MIXER 
 + name 'LO DAC Playback Switch' 
 + value.0 true 
 + value.1 true 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 2 
 + } 
 + } 
 + control.6 { 
 + iface MIXER 
 + name 'Mic PGA Switch' 
 + value.0 false 
 + value.1 false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 2 
 + } 
 + } 
 + control.7 { 
 + iface MIXER 
 + name 'ADCFGA Left Mute Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.8 { 
 + iface MIXER 
 + name 'ADCFGA Right Mute Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.9 { 
 + iface MIXER 
 + name 'ADC Level Volume' 
 + value.0 24 
 + value.1 24 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 64' 
 + dbmin -1200 
 + dbmax 2000 
 + dbvalue.0 0 
 + dbvalue.1 0 
 + } 
 + } 
 + control.10 { 
 + iface MIXER 
 + name 'PGA Level Volume' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 95' 
 + dbmin 0 
 + dbmax 4750 
 + dbvalue.0 0 
 + dbvalue.1 0 
 + } 
 + } 
 + control.11 { 
 + iface MIXER 
 + name 'Auto-mute Switch' 
 + value 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 1 
 + range '0 - 7' 
 + } 
 + } 
 + control.12 { 
 + iface MIXER 
 + name 'AGC Left Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.13 { 
 + iface MIXER 
 + name 'AGC Right Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.14 { 
 + iface MIXER 
 + name 'AGC Target Level' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 7' 
 + } 
 + } 
 + control.15 { 
 + iface MIXER 
 + name 'AGC Gain Hysteresis' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 3' 
 + } 
 + } 
 + control.16 { 
 + iface MIXER 
 + name 'AGC Hysteresis' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 3' 
 + } 
 + } 
 + control.17 { 
 + iface MIXER 
 + name 'AGC Noise Threshold' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 31' 
 + } 
 + } 
 + control.18 { 
 + iface MIXER 
 + name 'AGC Max PGA' 
 + value.0 127 
 + value.1 127 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 127' 
 + } 
 + } 
 + control.19 { 
 + iface MIXER 
 + name 'AGC Attack Time' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 31' 
 + } 
 + } 
 + control.20 { 
 + iface MIXER 
 + name 'AGC Decay Time' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 31' 
 + } 
 + } 
 + control.21 { 
 + iface MIXER 
 + name 'AGC Noise Debounce' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 31' 
 + } 
 + } 
 + control.22 { 
 + iface MIXER 
 + name 'AGC Signal Debounce' 
 + value.0 0 
 + value.1 0 
 + comment { 
 + access 'read write' 
 + type INTEGER 
 + count 2 
 + range '0 - 15' 
 + } 
 + } 
 + control.23 { 
 + iface MIXER 
 + name 'HPL Output Mixer L_DAC Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.24 { 
 + iface MIXER 
 + name 'HPL Output Mixer IN1_L Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.25 { 
 + iface MIXER 
 + name 'LOL Output Mixer L_DAC Switch' 
 + value true 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.26 { 
 + iface MIXER 
 + name 'HPR Output Mixer R_DAC Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.27 { 
 + iface MIXER 
 + name 'HPR Output Mixer IN1_R Switch' 
 + value false 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.28 { 
 + iface MIXER 
 + name 'LOR Output Mixer R_DAC Switch' 
 + value true 
 + comment { 
 + access 'read write' 
 + type BOOLEAN 
 + count 1 
 + } 
 + } 
 + control.29 { 
 + iface MIXER 
 + name 'IN1_R to Right Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.30 { 
 + iface MIXER 
 + name 'IN2_R to Right Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.31 { 
 + iface MIXER 
 + name 'IN3_R to Right Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.32 { 
 + iface MIXER 
 + name 'IN2_L to Right Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.33 { 
 + iface MIXER 
 + name 'IN1_L to Right Mixer Negative Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.34 { 
 + iface MIXER 
 + name 'IN3_L to Right Mixer Negative Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.35 { 
 + iface MIXER 
 + name 'IN1_L to Left Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.36 { 
 + iface MIXER 
 + name 'IN2_L to Left Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.37 { 
 + iface MIXER 
 + name 'IN3_L to Left Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.38 { 
 + iface MIXER 
 + name 'IN1_R to Left Mixer Positive Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.39 { 
 + iface MIXER 
 + name 'IN2_R to Left Mixer Negative Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 + control.40 { 
 + iface MIXER 
 + name 'IN3_R to Left Mixer Negative Resistor' 
 + value Off 
 + comment { 
 + access 'read write' 
 + type ENUMERATED 
 + count 1 
 + item.0 Off 
 + item.1 '10 kOhm' 
 + item.2 '20 kOhm' 
 + item.3 '40 kOhm' 
 + } 
 + } 
 +} 
 +</code> 
alsa.1795300868.txt.gz · Last modified: by jazzy