Implement headphone autodetection, earpiece routing, and linear volume controls

- Fix msm8916-wcd ALSA UCM2 configs (Earpiece.conf, Speaker.conf,
  Headphones.conf, PrimaryMic.conf, HeadsetMic.conf) so Speaker,
  Earpiece, and Headphones outputs and Mic1/Headset inputs are always
  available and mutually exclusive without invalid mixer controls
- Add event-driven wired headphone/headset jack detection and automatic
  audio routing in sxmo_soundmonitor.sh via evtest on the msm8916
  Headset Jack input device
- Make Earpiece, Speaker, and Headphones first-class routable output
  devices across sxmo_audio.sh, sxmo_modemaudio.sh, xdrawer, dmenu, and
  callscreen
- Make volume controls strictly linear across all channels by keeping
  PipeWire software gain at unity (100%) when unmuted and linearly
  mapping 0-100% to hardware digital volume registers (RX3 for Speaker,
  RX1 for Earpiece, RX1+RX2 for Headphones, TX1+ADC for Microphone)
This commit is contained in:
Luxferre committed 2026-10-29 20:42:26 +02:00
1 parent e42eb573ed
commit 370f8b085c
33 files changed
+1351 -488

No files matched your search

+5 -11
View File
@@ -824,7 +824,7 @@ class PcmAudioOut {
if (sysMuted_) {
volScale_ = 0;
} else {
volScale_ = std::max(1, (pct * pct * 20 + 5000) / 10000);
volScale_ = std::max(1, (pct * 20 + 50) / 100);
}
}
@@ -840,16 +840,10 @@ class PcmAudioOut {
std::ofstream ofs(cfgDir + "/speaker_vol", std::ios::trunc);
if (ofs) ofs << nextPct << "\n";
}
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
long targetRx3 =
(nextPct <= 0) ? 0 : (56 + ((nextPct * 28 + 50) / 100));
if (nextPct > 0) {
writeCtlBool(ctl, "SPK DAC Switch", true);
}
writeCtlInt(ctl, "RX3 Digital Volume", targetRx3);
snd_ctl_close(ctl);
}
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"sxmo_audio.sh vol set %d >/dev/null 2>&1 &", nextPct);
(void)std::system(cmd);
refreshVolume(false);
}
+15 -29
View File
@@ -57,6 +57,15 @@ class AlsaSoundEngine {
return sysMuted_.load(std::memory_order_relaxed);
}
static int readSimpleInt(const char* path, int fallback) {
FILE* f = std::fopen(path, "r");
if (!f) return fallback;
int v = fallback;
(void)std::fscanf(f, "%d", &v);
std::fclose(f);
return v;
}
static std::string sxmoConfigDir() {
const char* xdg = std::getenv("XDG_CONFIG_HOME");
if (xdg && *xdg) return std::string(xdg) + "/sxmo";
@@ -121,20 +130,13 @@ class AlsaSoundEngine {
}
}
volumePct.store(pct, std::memory_order_relaxed);
bool muted = !spkOn || pct <= 0;
bool muted = (readSimpleInt("/tmp/sxmo_out_muted", 0) == 1) || pct <= 0;
sysMuted_.store(muted, std::memory_order_relaxed);
// PipeWire clients get the sink volume applied as a cubic curve on top of
// the RX3 hardware attenuation; ReMEdy writes to hw:0,0 directly, so it
// applies the same cubic software gain to sound exactly as loud as every
// other application at the same system volume.
float lin = pct / 100.0f;
sysGain_.store(muted ? 0.0f : lin * lin * lin, std::memory_order_relaxed);
sysGain_.store(muted ? 0.0f : lin, std::memory_order_relaxed);
}
// Change the system-wide speaker volume exactly like the xdrawer "Speaker"
// slider does: persist speaker_vol, program the WCD9302 RX3 digital gain
// (60..92), unmute the speaker DAC, and mirror the level into the PipeWire
// default sink so every application follows the same setting.
// Change the system-wide output volume linearly via sxmo_audio.sh.
void setSystemVolume(int pct) {
pct = std::clamp(pct, 0, 100);
std::string dir = sxmoConfigDir();
@@ -146,22 +148,9 @@ class AlsaSoundEngine {
std::fclose(f);
}
}
snd_ctl_t* ctl = nullptr;
if (snd_ctl_open(&ctl, "hw:0", 0) == 0 && ctl) {
long hw = (pct <= 0) ? 0 : (60 + std::lround((pct * 32.0) / 100.0));
if (pct > 0) {
writeCtlBool(ctl, "SPK DAC Switch", true);
writeCtlEnum(ctl, "RX3 MIX1 INP1", 3);
}
writeCtlInt(ctl, "RX3 Digital Volume", hw);
snd_ctl_close(ctl);
}
char cmd[200];
char cmd[128];
std::snprintf(cmd, sizeof(cmd),
"(wpctl set-mute @DEFAULT_AUDIO_SINK@ %d; "
"wpctl set-volume @DEFAULT_AUDIO_SINK@ %d%%; "
"sxmo_hook_statusbar.sh volume) >/dev/null 2>&1 &",
(pct <= 0) ? 1 : 0, pct);
"sxmo_audio.sh vol set %d >/dev/null 2>&1 &", pct);
std::thread([c = std::string(cmd)]() { (void)std::system(c.c_str()); })
.detach();
refreshSystemVolume(true);
@@ -173,10 +162,7 @@ class AlsaSoundEngine {
static float perceptualGain(int levelPct) {
if (levelPct <= 0) return 0.0f;
if (levelPct >= 100) return 1.0f;
// MIDP VolumeControl levels are meant to be linear in perceived loudness.
// Loudness grows roughly with amplitude^0.6, so map level -> amplitude
// with an exponent of ~1.5 (level 50 -> ~35%, 25 -> ~13%, 80 -> ~72%).
return std::pow(std::clamp(levelPct, 0, 100) / 100.0f, 1.5f);
return std::clamp(levelPct, 0, 100) / 100.0f;
}
// 4096-entry sine table shared by the synth voices (avoids libm sin()