Fix WAV call recording/playback, ALSA call audio routing, and tracker ringtones

This commit is contained in:
Luxferre committed 2026-10-25 18:15:36 +02:00
1 parent b706865ee8
commit f4fe5ce8e9
21 files changed
+339 -33

No files matched your search

+97 -18
View File
@@ -207,6 +207,61 @@ std::string getRecordingsDir() {
return dir;
}
void finalizeWavHeader(const std::string& path) {
if (path.empty()) return;
int fd = ::open(path.c_str(), O_RDWR);
if (fd < 0) return;
struct stat st {};
if (::fstat(fd, &st) != 0 || st.st_size <= 44) {
::close(fd);
return;
}
uint8_t hdr[64] = {0};
ssize_t n = :: pread(fd, hdr, sizeof(hdr), 0);
if (n < 44 || std::memcmp(hdr, "RIFF", 4) != 0 ||
std::memcmp(hdr + 8, "WAVE", 4) != 0) {
::close(fd);
return;
}
uint32_t riffLen = static_cast<uint32_t>(st.st_size - 8);
uint32_t curRiff = static_cast<uint32_t>(hdr[4]) |
(static_cast<uint32_t>(hdr[5]) << 8) |
(static_cast<uint32_t>(hdr[6]) << 16) |
(static_cast<uint32_t>(hdr[7]) << 24);
if (curRiff == 0 || curRiff != riffLen) {
uint8_t b[4] = {
static_cast<uint8_t>(riffLen & 0xff),
static_cast<uint8_t>((riffLen >> 8) & 0xff),
static_cast<uint8_t>((riffLen >> 16) & 0xff),
static_cast<uint8_t>((riffLen >> 24) & 0xff),
};
(void)::pwrite(fd, b, 4, 4);
}
for (ssize_t off = 12; off + 8 <= n;) {
uint32_t chunkLen = static_cast<uint32_t>(hdr[off + 4]) |
(static_cast<uint32_t>(hdr[off + 5]) << 8) |
(static_cast<uint32_t>(hdr[off + 6]) << 16) |
(static_cast<uint32_t>(hdr[off + 7]) << 24);
if (std::memcmp(hdr + off, "data", 4) == 0) {
uint32_t expectedData = static_cast<uint32_t>(st.st_size - (off + 8));
if (chunkLen == 0 || chunkLen != expectedData) {
uint8_t b[4] = {
static_cast<uint8_t>(expectedData & 0xff),
static_cast<uint8_t>((expectedData >> 8) & 0xff),
static_cast<uint8_t>((expectedData >> 16) & 0xff),
static_cast<uint8_t>((expectedData >> 24) & 0xff),
};
(void)::pwrite(fd, b, 4, off + 4);
}
break;
}
if (chunkLen == 0) break;
off += 8 + static_cast<ssize_t>((chunkLen + 1) & ~1u);
}
::fsync(fd);
::close(fd);
}
void wakeDisplayHardware(Display* dpy) {
int saved = readIntFile("/tmp/sxmo_saved_brightness", -1);
if (saved <= 1) {
@@ -365,7 +420,7 @@ int callscreen_run_main(int argc, char** argv) {
reloadContactsIfNeeded();
AlsaCallControl alsa;
unsigned int savedMicMux = 2;
unsigned int savedMicMux = 1;
CallState callState = CallState::Idle;
std::string callId;
@@ -400,17 +455,17 @@ int callscreen_run_main(int argc, char** argv) {
int64_t softkeyDeadlineMs = 0;
auto refreshHardwareAudioState = [&]() {
unsigned int dec1 = alsa.getEnum("DEC1 MUX", 2);
unsigned int dec1 = alsa.getEnum("DEC1 MUX", 1);
if (dec1 > 0) {
savedMicMux = dec1;
writeIntFile("/tmp/xdrawer_mic_mux", static_cast<int>(savedMicMux));
micMuted = false;
} else {
savedMicMux = static_cast<unsigned int>(std::max(1, readIntFile("/tmp/xdrawer_mic_mux", 2)));
savedMicMux = static_cast<unsigned int>(std::max(1, readIntFile("/tmp/xdrawer_mic_mux", 1)));
micMuted = true;
}
bool spkSwitch = alsa.getBoolean("SPK DAC Switch", false);
bool earSwitch = alsa.getBoolean("EAR_S", true);
bool earSwitch = (alsa.getEnum("EAR_S", 1) != 0);
speakerOn = (spkSwitch && !earSwitch);
long rawVol = speakerOn ? alsa.getInteger("RX3 Digital Volume", 68)
@@ -427,8 +482,9 @@ int callscreen_run_main(int argc, char** argv) {
auto setMicMuted = [&](bool mute) {
micMuted = mute;
alsa.setEnum("DEC1 MUX", mute ? 0 : savedMicMux);
alsa.setInteger("ADC3 Volume", mute ? 0 : 8);
alsa.setEnum("DEC1 MUX", mute ? 0 : (savedMicMux > 0 ? savedMicMux : 1));
alsa.setInteger("ADC1 Volume", mute ? 0 : 6);
alsa.setInteger("ADC3 Volume", mute ? 0 : 6);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", !mute);
runAsyncCmd(mute ? "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 1 >/dev/null 2>&1"
: "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 0 >/dev/null 2>&1");
@@ -440,21 +496,19 @@ int callscreen_run_main(int argc, char** argv) {
speakerOn = toSpeaker;
long hwVol = std::lround((callVolumePct * 84.0) / 100.0);
if (toSpeaker) {
alsa.setBoolean("EAR_S", false);
alsa.setEnum("EAR_S", 0);
alsa.setEnum("RX1 MIX1 INP1", 0);
alsa.setBoolean("SPK DAC Switch", true);
alsa.setEnum("RX3 MIX1 INP1", 1);
alsa.setEnum("RX3 MIX1 INP1", 3);
alsa.setInteger("RX3 Digital Volume", hwVol);
runAsyncCmd("sxmo_audio.sh device set Speaker >/dev/null 2>&1");
statusNotice = "Audio routed to Speaker";
} else {
alsa.setBoolean("SPK DAC Switch", false);
alsa.setEnum("RX3 MIX1 INP1", 0);
alsa.setEnum("RX1 MIX1 INP1", 1);
alsa.setEnum("RDAC2 MUX", 1);
alsa.setBoolean("EAR_S", true);
alsa.setEnum("RX1 MIX1 INP1", 3);
alsa.setEnum("RDAC2 MUX", 0);
alsa.setEnum("EAR_S", 1);
alsa.setInteger("RX1 Digital Volume", hwVol);
runAsyncCmd("sxmo_audio.sh device set Earpiece >/dev/null 2>&1");
statusNotice = "Audio routed to Earpiece";
}
statusNoticeUntilMs = nowMs() + 2000;
@@ -482,6 +536,7 @@ int callscreen_run_main(int argc, char** argv) {
pid_t r = ::waitpid(recordPid, &status, WNOHANG);
if (r == recordPid || (r < 0 && ::kill(recordPid, 0) != 0)) {
recordPid = -1;
finalizeWavHeader(recordFilePath);
return false;
}
return true;
@@ -492,7 +547,7 @@ int callscreen_run_main(int argc, char** argv) {
return;
}
::kill(recordPid, SIGINT);
for (int i = 0; i < 15; ++i) {
for (int i = 0; i < 150; ++i) {
int status = 0;
if (::waitpid(recordPid, &status, WNOHANG) == recordPid) {
recordPid = -1;
@@ -502,9 +557,15 @@ int callscreen_run_main(int argc, char** argv) {
}
if (recordPid > 0) {
::kill(recordPid, SIGTERM);
(void)::waitpid(recordPid, nullptr, WNOHANG);
for (int i = 0; i < 20; ++i) {
if (::waitpid(recordPid, nullptr, WNOHANG) == recordPid) {
break;
}
usleep(10000);
}
recordPid = -1;
}
finalizeWavHeader(recordFilePath);
if (!recordFileName.empty()) {
statusNotice = "Saved: " + recordFileName;
statusNoticeUntilMs = nowMs() + 3500;
@@ -527,7 +588,12 @@ int callscreen_run_main(int argc, char** argv) {
recordFileName = "call_" + tag + "_" + tsBuf + ".wav";
recordFilePath = dir + "/" + recordFileName;
// Ensure MultiMedia2 capture mixer is enabled for PipeWire recording
// Ensure MultiMedia2 capture mixer and primary mic path are active for PipeWire recording
alsa.setEnum("CIC1 MUX", 0);
if (!micMuted) {
alsa.setEnum("DEC1 MUX", savedMicMux > 0 ? savedMicMux : 1);
alsa.setInteger("ADC1 Volume", 6);
}
alsa.setBoolean("MultiMedia2 Mixer TERT_MI2S_TX", true);
pid_t pid = ::fork();
@@ -536,6 +602,12 @@ int callscreen_run_main(int argc, char** argv) {
}
if (pid == 0) {
::setsid();
std::string runtimeDir = "/run/user/" + std::to_string(::getuid());
const char* xdg = std::getenv("XDG_RUNTIME_DIR");
struct stat stRuntime {};
if (!xdg || !*xdg || ::stat(xdg, &stRuntime) != 0) {
::setenv("XDG_RUNTIME_DIR", runtimeDir.c_str(), 1);
}
int nullfd = ::open("/dev/null", O_RDWR);
if (nullfd >= 0) {
::dup2(nullfd, STDIN_FILENO);
@@ -545,8 +617,9 @@ int callscreen_run_main(int argc, char** argv) {
::close(nullfd);
}
}
execlp("pw-record", "pw-record", "--rate", "16000", "--channels", "1",
recordFilePath.c_str(), static_cast<char*>(nullptr));
execlp("pw-record", "pw-record", "--format", "s16", "--rate", "16000",
"--channels", "1", recordFilePath.c_str(),
static_cast<char*>(nullptr));
execlp("parecord", "parecord", "--rate=16000", "--channels=1",
"--file-format=wav", recordFilePath.c_str(), static_cast<char*>(nullptr));
execlp("arecord", "arecord", "-f", "S16_LE", "-r", "16000", "-c", "1",
@@ -1055,6 +1128,12 @@ int callscreen_run_main(int argc, char** argv) {
if (micMuted) {
setMicMuted(false);
}
alsa.setBoolean("PRI_MI2S_RX Voice Mixer VoiceMMode1", false);
alsa.setBoolean("VoiceMMode1 Capture Mixer TERT_MI2S_TX", false);
alsa.setEnum("EAR_S", 0);
alsa.setEnum("RX3 MIX1 INP1", 3);
alsa.setBoolean("SPK DAC Switch", true);
runAsyncCmd("sxmo_modemaudio.sh disable_call_audio_mode >/dev/null 2>&1");
if (callStartMs > 0 &&
(callState == CallState::Active || callState == CallState::Outgoing)) {
lastFinalDurationSec = static_cast<int>((nowMs() - callStartMs) / 1000);