Calibrate volume-independent 157 Hz haptic vibration on nokia-sparkler
- Compensate PipeWire softVolumes and RX3 Digital Volume via libpulse FLOAT_LE PCM - Tune L1/L2/L3 reference amplitudes to 1950/2550/3300 at HAPTIC_REF_RX3_VOL=84 - Fix root HOME lookup, muted-sink bypass, build-dwm-plugins sync, and refresh a1120 image checksums
This commit is contained in:
1 parent
d7992904bf
commit
0d8047ddfe
9 files changed
+548
-128
No files matched your search
+508
-107
@@ -6,7 +6,9 @@
|
||||
#include <grp.h>
|
||||
#include <linux/input.h>
|
||||
#include <math.h>
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -19,6 +21,11 @@
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
#define HAPTIC_REF_RX3_VOL 84L
|
||||
#define HAPTIC_REF_AMP_L1 1950.0
|
||||
#define HAPTIC_REF_AMP_L2 2550.0
|
||||
#define HAPTIC_REF_AMP_L3 3300.0
|
||||
|
||||
static volatile sig_atomic_t g_stop = 0;
|
||||
|
||||
static void handle_signal(int sig) {
|
||||
@@ -181,6 +188,14 @@ static int is_haptic_audio_device(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_non_speaker_name(const char* s) {
|
||||
return contains_icase(s, "Headphone") ||
|
||||
contains_icase(s, "Headset") ||
|
||||
contains_icase(s, "Earpiece") ||
|
||||
contains_icase(s, "Bluetooth") ||
|
||||
contains_icase(s, "bluez");
|
||||
}
|
||||
|
||||
static int is_non_speaker_audio_route(void) {
|
||||
char buf[128];
|
||||
const char* paths[2];
|
||||
@@ -197,13 +212,7 @@ static int is_non_speaker_audio_route(void) {
|
||||
if (!fp) continue;
|
||||
if (fgets(buf, sizeof(buf), fp)) {
|
||||
fclose(fp);
|
||||
if (contains_icase(buf, "Headphone") ||
|
||||
contains_icase(buf, "Headset") ||
|
||||
contains_icase(buf, "Bluetooth") ||
|
||||
contains_icase(buf, "bluez")) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
return is_non_speaker_name(buf);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
@@ -218,6 +227,269 @@ static int is_pw_sink_muted(void) {
|
||||
return ch == '1';
|
||||
}
|
||||
|
||||
static double read_haptic_scale_env(void) {
|
||||
const char* env_sc = getenv("SXMO_VIBRATE_HAPTIC_SCALE");
|
||||
if (!env_sc || !*env_sc) return 1.0;
|
||||
char* endp = NULL;
|
||||
errno = 0;
|
||||
double v = strtod(env_sc, &endp);
|
||||
if (errno != 0 || !endp || *endp != '\0' || !isfinite(v)) return 1.0;
|
||||
if (v < 0.05) v = 0.05;
|
||||
if (v > 4.0) v = 4.0;
|
||||
return v;
|
||||
}
|
||||
|
||||
static long monotonic_ms(void) {
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0;
|
||||
return (long)ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;
|
||||
}
|
||||
|
||||
#define HAPTIC_PW_MAX_INPUTS 32
|
||||
|
||||
struct haptic_pw_input {
|
||||
uint32_t index;
|
||||
int muted;
|
||||
};
|
||||
|
||||
struct haptic_pw_state {
|
||||
pa_mainloop* ml;
|
||||
pa_context* ctx;
|
||||
int connected;
|
||||
int sink_found;
|
||||
int non_speaker;
|
||||
uint32_t sink_index;
|
||||
int sink_muted;
|
||||
pa_cvolume sink_vol;
|
||||
double linear_gain;
|
||||
int n_inputs;
|
||||
struct haptic_pw_input inputs[HAPTIC_PW_MAX_INPUTS];
|
||||
int n_saved_muted_inputs;
|
||||
uint32_t saved_muted_inputs[HAPTIC_PW_MAX_INPUTS];
|
||||
int modified_sink_vol;
|
||||
int modified_sink_mute;
|
||||
pa_cvolume orig_sink_vol;
|
||||
int orig_sink_mute;
|
||||
};
|
||||
|
||||
struct pw_query_wait {
|
||||
struct haptic_pw_state* pw;
|
||||
int ctx_ready;
|
||||
int ctx_failed;
|
||||
int sink_done;
|
||||
int inputs_done;
|
||||
int op_done;
|
||||
};
|
||||
|
||||
static void pw_context_state_cb(pa_context* c, void* userdata) {
|
||||
struct pw_query_wait* q = (struct pw_query_wait*)userdata;
|
||||
pa_context_state_t st = pa_context_get_state(c);
|
||||
if (st == PA_CONTEXT_READY) {
|
||||
q->ctx_ready = 1;
|
||||
} else if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
|
||||
q->ctx_failed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void pw_sink_info_cb(pa_context* c, const pa_sink_info* i, int eol,
|
||||
void* userdata) {
|
||||
(void)c;
|
||||
struct pw_query_wait* q = (struct pw_query_wait*)userdata;
|
||||
struct haptic_pw_state* pw = q->pw;
|
||||
if (eol > 0) {
|
||||
q->sink_done = 1;
|
||||
return;
|
||||
}
|
||||
if (!i || pw->sink_found) return;
|
||||
|
||||
pw->sink_found = 1;
|
||||
pw->sink_index = i->index;
|
||||
pw->sink_muted = i->mute ? 1 : 0;
|
||||
pw->sink_vol = i->volume;
|
||||
pw->orig_sink_vol = i->volume;
|
||||
pw->orig_sink_mute = i->mute ? 1 : 0;
|
||||
pw->linear_gain = pa_sw_volume_to_linear(pa_cvolume_max(&i->volume));
|
||||
|
||||
if (is_non_speaker_name(i->name) || is_non_speaker_name(i->description)) {
|
||||
pw->non_speaker = 1;
|
||||
} else if (i->active_port &&
|
||||
(is_non_speaker_name(i->active_port->name) ||
|
||||
is_non_speaker_name(i->active_port->description))) {
|
||||
pw->non_speaker = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void pw_sink_input_info_cb(pa_context* c, const pa_sink_input_info* i,
|
||||
int eol, void* userdata) {
|
||||
(void)c;
|
||||
struct pw_query_wait* q = (struct pw_query_wait*)userdata;
|
||||
struct haptic_pw_state* pw = q->pw;
|
||||
if (eol > 0) {
|
||||
q->inputs_done = 1;
|
||||
return;
|
||||
}
|
||||
if (!i || !pw->sink_found || i->sink != pw->sink_index || i->corked) return;
|
||||
if (pw->n_inputs < HAPTIC_PW_MAX_INPUTS) {
|
||||
pw->inputs[pw->n_inputs].index = i->index;
|
||||
pw->inputs[pw->n_inputs].muted = i->mute ? 1 : 0;
|
||||
pw->n_inputs++;
|
||||
}
|
||||
}
|
||||
|
||||
static void pw_success_cb(pa_context* c, int success, void* userdata) {
|
||||
(void)c;
|
||||
(void)success;
|
||||
struct pw_query_wait* q = (struct pw_query_wait*)userdata;
|
||||
q->op_done = 1;
|
||||
}
|
||||
|
||||
static void pw_wait_until(pa_mainloop* ml, const int* done_flag,
|
||||
const int* fail_flag, long timeout_ms,
|
||||
int honor_stop) {
|
||||
long start = monotonic_ms();
|
||||
while (!*done_flag && (!fail_flag || !*fail_flag)) {
|
||||
if (honor_stop && g_stop) break;
|
||||
if (monotonic_ms() - start >= timeout_ms) break;
|
||||
if (pa_mainloop_prepare(ml, 10000) < 0) break;
|
||||
int prc = pa_mainloop_poll(ml);
|
||||
if (prc < 0 && errno != EINTR) break;
|
||||
if (pa_mainloop_dispatch(ml) < 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
static void haptic_pw_run_op(struct haptic_pw_state* pw, pa_operation* op,
|
||||
struct pw_query_wait* q, int* flag,
|
||||
long timeout_ms, int honor_stop) {
|
||||
if (!op) return;
|
||||
pw_wait_until(pw->ml, flag, &q->ctx_failed, timeout_ms, honor_stop);
|
||||
if (pa_operation_get_state(op) == PA_OPERATION_RUNNING) {
|
||||
pa_operation_cancel(op);
|
||||
}
|
||||
pa_operation_unref(op);
|
||||
}
|
||||
|
||||
static void haptic_pw_connect_and_query(struct haptic_pw_state* pw) {
|
||||
memset(pw, 0, sizeof(*pw));
|
||||
pw->ml = pa_mainloop_new();
|
||||
if (!pw->ml) return;
|
||||
|
||||
pa_mainloop_api* api = pa_mainloop_get_api(pw->ml);
|
||||
pw->ctx = pa_context_new(api, "sxmo_vibrate");
|
||||
if (!pw->ctx) {
|
||||
pa_mainloop_free(pw->ml);
|
||||
pw->ml = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
struct pw_query_wait q;
|
||||
memset(&q, 0, sizeof(q));
|
||||
q.pw = pw;
|
||||
pa_context_set_state_callback(pw->ctx, pw_context_state_cb, &q);
|
||||
if (pa_context_connect(pw->ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
|
||||
pa_context_unref(pw->ctx);
|
||||
pw->ctx = NULL;
|
||||
pa_mainloop_free(pw->ml);
|
||||
pw->ml = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
pw_wait_until(pw->ml, &q.ctx_ready, &q.ctx_failed, 150, 1);
|
||||
if (!q.ctx_ready || q.ctx_failed) {
|
||||
pa_context_disconnect(pw->ctx);
|
||||
pa_context_unref(pw->ctx);
|
||||
pw->ctx = NULL;
|
||||
pa_mainloop_free(pw->ml);
|
||||
pw->ml = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
pw->connected = 1;
|
||||
pa_operation* op = pa_context_get_sink_info_by_name(
|
||||
pw->ctx, "@DEFAULT_SINK@", pw_sink_info_cb, &q);
|
||||
haptic_pw_run_op(pw, op, &q, &q.sink_done, 150, 1);
|
||||
|
||||
if (pw->sink_found && !pw->non_speaker && !g_stop) {
|
||||
op = pa_context_get_sink_input_info_list(pw->ctx, pw_sink_input_info_cb, &q);
|
||||
haptic_pw_run_op(pw, op, &q, &q.inputs_done, 150, 1);
|
||||
}
|
||||
}
|
||||
|
||||
static int haptic_pw_prepare_muted_bypass(struct haptic_pw_state* pw) {
|
||||
if (!pw || !pw->connected || !pw->sink_found) return -1;
|
||||
if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pw->ctx))) return -1;
|
||||
|
||||
struct pw_query_wait q;
|
||||
memset(&q, 0, sizeof(q));
|
||||
q.pw = pw;
|
||||
|
||||
/* Silence active inputs before temporarily unmuting the muted sink. */
|
||||
for (int i = 0; i < pw->n_inputs; ++i) {
|
||||
if (!pw->inputs[i].muted &&
|
||||
pw->n_saved_muted_inputs < HAPTIC_PW_MAX_INPUTS) {
|
||||
q.op_done = 0;
|
||||
pa_operation* op = pa_context_set_sink_input_mute(
|
||||
pw->ctx, pw->inputs[i].index, 1, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, op, &q, &q.op_done, 100, 1);
|
||||
pw->saved_muted_inputs[pw->n_saved_muted_inputs++] = pw->inputs[i].index;
|
||||
}
|
||||
}
|
||||
|
||||
pa_cvolume unity_vol;
|
||||
uint8_t ch = pw->orig_sink_vol.channels;
|
||||
if (ch < 1 || ch > PA_CHANNELS_MAX) ch = 2;
|
||||
pa_cvolume_set(&unity_vol, ch, PA_VOLUME_NORM);
|
||||
q.op_done = 0;
|
||||
pa_operation* vop = pa_context_set_sink_volume_by_index(
|
||||
pw->ctx, pw->sink_index, &unity_vol, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, vop, &q, &q.op_done, 100, 1);
|
||||
pw->modified_sink_vol = 1;
|
||||
|
||||
q.op_done = 0;
|
||||
pa_operation* mop = pa_context_set_sink_mute_by_index(
|
||||
pw->ctx, pw->sink_index, 0, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, mop, &q, &q.op_done, 100, 1);
|
||||
pw->modified_sink_mute = 1;
|
||||
return g_stop ? -1 : 0;
|
||||
}
|
||||
|
||||
static void haptic_pw_restore_and_close(struct haptic_pw_state* pw) {
|
||||
if (!pw) return;
|
||||
if (pw->ctx) {
|
||||
if (pw->connected && pw->sink_found &&
|
||||
PA_CONTEXT_IS_GOOD(pa_context_get_state(pw->ctx))) {
|
||||
struct pw_query_wait q;
|
||||
memset(&q, 0, sizeof(q));
|
||||
q.pw = pw;
|
||||
|
||||
if (pw->modified_sink_mute) {
|
||||
q.op_done = 0;
|
||||
pa_operation* mop = pa_context_set_sink_mute_by_index(
|
||||
pw->ctx, pw->sink_index, pw->orig_sink_mute, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, mop, &q, &q.op_done, 100, 0);
|
||||
}
|
||||
if (pw->modified_sink_vol) {
|
||||
q.op_done = 0;
|
||||
pa_operation* vop = pa_context_set_sink_volume_by_index(
|
||||
pw->ctx, pw->sink_index, &pw->orig_sink_vol, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, vop, &q, &q.op_done, 100, 0);
|
||||
}
|
||||
for (int i = 0; i < pw->n_saved_muted_inputs; ++i) {
|
||||
q.op_done = 0;
|
||||
pa_operation* iop = pa_context_set_sink_input_mute(
|
||||
pw->ctx, pw->saved_muted_inputs[i], 0, pw_success_cb, &q);
|
||||
haptic_pw_run_op(pw, iop, &q, &q.op_done, 100, 0);
|
||||
}
|
||||
}
|
||||
pa_context_disconnect(pw->ctx);
|
||||
pa_context_unref(pw->ctx);
|
||||
pw->ctx = NULL;
|
||||
}
|
||||
if (pw->ml) {
|
||||
pa_mainloop_free(pw->ml);
|
||||
pw->ml = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct haptic_ctl_state {
|
||||
snd_ctl_t* ctl;
|
||||
int has_spk_sw;
|
||||
@@ -231,6 +503,9 @@ struct haptic_ctl_state {
|
||||
int changed_rx3_mux;
|
||||
int has_rx3_vol;
|
||||
long saved_rx3_vol;
|
||||
long active_rx3_vol;
|
||||
long min_rx3_vol;
|
||||
long max_rx3_vol;
|
||||
int changed_rx3_vol;
|
||||
};
|
||||
|
||||
@@ -249,7 +524,7 @@ static int ctl_read_elem(snd_ctl_t* ctl, const char* name,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st) {
|
||||
static void haptic_ctl_open(struct haptic_ctl_state* st) {
|
||||
memset(st, 0, sizeof(*st));
|
||||
if (snd_ctl_open(&st->ctl, "hw:0", 0) < 0) {
|
||||
st->ctl = NULL;
|
||||
@@ -265,46 +540,70 @@ static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st) {
|
||||
info, val) == 0) {
|
||||
st->has_pri_mm1 = 1;
|
||||
st->saved_pri_mm1 = snd_ctl_elem_value_get_boolean(val, 0);
|
||||
if (st->saved_pri_mm1 == 0) {
|
||||
snd_ctl_elem_value_set_boolean(val, 0, 1);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_pri_mm1 = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) {
|
||||
st->has_rx3_mux = 1;
|
||||
st->saved_rx3_mux = snd_ctl_elem_value_get_enumerated(val, 0);
|
||||
if (st->saved_rx3_mux == 0) {
|
||||
unsigned int items = snd_ctl_elem_info_get_items(info);
|
||||
for (unsigned int i = 0; i < items; ++i) {
|
||||
snd_ctl_elem_info_set_item(info, i);
|
||||
if (snd_ctl_elem_info(st->ctl, info) == 0 &&
|
||||
strcmp(snd_ctl_elem_info_get_item_name(info), "RX1") == 0) {
|
||||
snd_ctl_elem_value_set_enumerated(val, 0, i);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_mux = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) {
|
||||
st->has_spk_sw = 1;
|
||||
st->saved_spk_sw = snd_ctl_elem_value_get_boolean(val, 0);
|
||||
}
|
||||
if (ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) {
|
||||
st->has_rx3_vol = 1;
|
||||
st->saved_rx3_vol = snd_ctl_elem_value_get_integer(val, 0);
|
||||
st->active_rx3_vol = st->saved_rx3_vol;
|
||||
st->min_rx3_vol = snd_ctl_elem_info_get_min(info);
|
||||
st->max_rx3_vol = snd_ctl_elem_info_get_max(info);
|
||||
}
|
||||
}
|
||||
|
||||
static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st,
|
||||
long target_rx3_vol) {
|
||||
if (!st || !st->ctl) return;
|
||||
|
||||
snd_ctl_elem_info_t* info = NULL;
|
||||
snd_ctl_elem_value_t* val = NULL;
|
||||
snd_ctl_elem_info_alloca(&info);
|
||||
snd_ctl_elem_value_alloca(&val);
|
||||
|
||||
if (st->has_pri_mm1 && st->saved_pri_mm1 == 0 &&
|
||||
ctl_read_elem(st->ctl, "PRI_MI2S_RX Audio Mixer MultiMedia1",
|
||||
info, val) == 0) {
|
||||
snd_ctl_elem_value_set_boolean(val, 0, 1);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_pri_mm1 = 1;
|
||||
}
|
||||
|
||||
if (st->has_rx3_mux && st->saved_rx3_mux == 0 &&
|
||||
ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) {
|
||||
unsigned int items = snd_ctl_elem_info_get_items(info);
|
||||
for (unsigned int i = 0; i < items; ++i) {
|
||||
snd_ctl_elem_info_set_item(info, i);
|
||||
if (snd_ctl_elem_info(st->ctl, info) == 0 &&
|
||||
strcmp(snd_ctl_elem_info_get_item_name(info), "RX1") == 0) {
|
||||
snd_ctl_elem_value_set_enumerated(val, 0, i);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_mux = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) {
|
||||
st->has_spk_sw = 1;
|
||||
st->saved_spk_sw = snd_ctl_elem_value_get_boolean(val, 0);
|
||||
if (st->saved_spk_sw == 0) {
|
||||
snd_ctl_elem_value_set_boolean(val, 0, 1);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_spk_sw = 1;
|
||||
}
|
||||
if (st->has_spk_sw && st->saved_spk_sw == 0 &&
|
||||
ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) {
|
||||
snd_ctl_elem_value_set_boolean(val, 0, 1);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_spk_sw = 1;
|
||||
}
|
||||
|
||||
if (ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) {
|
||||
st->has_rx3_vol = 1;
|
||||
st->saved_rx3_vol = snd_ctl_elem_value_get_integer(val, 0);
|
||||
if (st->saved_rx3_vol < 80) {
|
||||
long maxv = snd_ctl_elem_info_get_max(info);
|
||||
long target = (maxv >= 88) ? 88 : maxv;
|
||||
snd_ctl_elem_value_set_integer(val, 0, target);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_vol = 1;
|
||||
if (st->has_rx3_vol && target_rx3_vol >= 0 &&
|
||||
st->saved_rx3_vol != target_rx3_vol &&
|
||||
ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) {
|
||||
long target = target_rx3_vol;
|
||||
if (target < st->min_rx3_vol) target = st->min_rx3_vol;
|
||||
if (target > st->max_rx3_vol) target = st->max_rx3_vol;
|
||||
snd_ctl_elem_value_set_integer(val, 0, target);
|
||||
if (snd_ctl_elem_write(st->ctl, val) >= 0) {
|
||||
st->changed_rx3_vol = 1;
|
||||
st->active_rx3_vol = target;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,11 +641,14 @@ static void haptic_restore_speaker_ctl(struct haptic_ctl_state* st) {
|
||||
st->ctl = NULL;
|
||||
}
|
||||
|
||||
static int write_pcm_frames_all(snd_pcm_t* pcm, const short* buf,
|
||||
snd_pcm_uframes_t frames) {
|
||||
static int write_pcm_frames_all(snd_pcm_t* pcm, const void* buf,
|
||||
snd_pcm_uframes_t frames,
|
||||
size_t frame_bytes) {
|
||||
const char* bytes = (const char*)buf;
|
||||
snd_pcm_uframes_t off = 0;
|
||||
while (off < frames && !g_stop) {
|
||||
snd_pcm_sframes_t rc = snd_pcm_writei(pcm, buf + off * 2, frames - off);
|
||||
snd_pcm_sframes_t rc =
|
||||
snd_pcm_writei(pcm, bytes + off * frame_bytes, frames - off);
|
||||
if (rc == -EAGAIN || rc == -EINTR) {
|
||||
continue;
|
||||
}
|
||||
@@ -360,36 +662,52 @@ static int write_pcm_frames_all(snd_pcm_t* pcm, const short* buf,
|
||||
return g_stop ? -1 : 0;
|
||||
}
|
||||
|
||||
static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp) {
|
||||
static double haptic_pulse_env(long idx, long total_frames, long fade_frames) {
|
||||
if (fade_frames <= 0) return 1.0;
|
||||
if (idx < fade_frames) {
|
||||
return 0.5 * (1.0 - cos(M_PI * (double)idx / (double)fade_frames));
|
||||
}
|
||||
if (idx >= total_frames - fade_frames) {
|
||||
long tail = total_frames - 1 - idx;
|
||||
return 0.5 * (1.0 - cos(M_PI * (double)tail / (double)fade_frames));
|
||||
}
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp,
|
||||
int is_float) {
|
||||
if (ms <= 0 || g_stop) return 0;
|
||||
const long rate = 48000L;
|
||||
const double step = 2.0 * M_PI * 157.0 / (double)rate;
|
||||
const long total_frames = (ms * rate) / 1000L;
|
||||
long fade_frames = (5L * rate) / 1000L;
|
||||
long fade_frames = (10L * rate) / 1000L;
|
||||
if (fade_frames > total_frames / 2) fade_frames = total_frames / 2;
|
||||
|
||||
short chunk[2400 * 2];
|
||||
short chunk_s16[2400 * 2];
|
||||
float chunk_f32[2400 * 2];
|
||||
long pos = 0;
|
||||
while (pos < total_frames && !g_stop) {
|
||||
long n = total_frames - pos;
|
||||
if (n > 2400L) n = 2400L;
|
||||
for (long i = 0; i < n; ++i) {
|
||||
long idx = pos + i;
|
||||
double env = 1.0;
|
||||
if (fade_frames > 0 && idx < fade_frames) {
|
||||
env = 0.5 * (1.0 - cos(M_PI * (double)idx / (double)fade_frames));
|
||||
} else if (fade_frames > 0 && idx >= total_frames - fade_frames) {
|
||||
long tail = total_frames - 1 - idx;
|
||||
env = 0.5 * (1.0 - cos(M_PI * (double)tail / (double)fade_frames));
|
||||
double v = haptic_pulse_env(idx, total_frames, fade_frames) * amp *
|
||||
sin(step * (double)idx);
|
||||
if (is_float) {
|
||||
float fv = (float)v;
|
||||
chunk_f32[i * 2] = fv;
|
||||
chunk_f32[i * 2 + 1] = fv;
|
||||
} else {
|
||||
if (v > 32767.0) v = 32767.0;
|
||||
if (v < -32767.0) v = -32767.0;
|
||||
short s = (short)lrint(v);
|
||||
chunk_s16[i * 2] = s;
|
||||
chunk_s16[i * 2 + 1] = s;
|
||||
}
|
||||
double v = env * amp * sin(step * (double)idx);
|
||||
if (v > 32767.0) v = 32767.0;
|
||||
if (v < -32767.0) v = -32767.0;
|
||||
short s = (short)lrint(v);
|
||||
chunk[i * 2] = s;
|
||||
chunk[i * 2 + 1] = s;
|
||||
}
|
||||
if (write_pcm_frames_all(pcm, chunk, (snd_pcm_uframes_t)n) < 0) {
|
||||
const void* ptr = is_float ? (const void*)chunk_f32 : (const void*)chunk_s16;
|
||||
size_t fbytes = is_float ? (sizeof(float) * 2U) : (sizeof(short) * 2U);
|
||||
if (write_pcm_frames_all(pcm, ptr, (snd_pcm_uframes_t)n, fbytes) < 0) {
|
||||
return -1;
|
||||
}
|
||||
pos += n;
|
||||
@@ -397,17 +715,24 @@ static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_haptic_silence(snd_pcm_t* pcm, long ms) {
|
||||
static int write_haptic_silence(snd_pcm_t* pcm, long ms, int is_float) {
|
||||
if (ms <= 0 || g_stop) return 0;
|
||||
const long total_frames = (ms * 48000L) / 1000L;
|
||||
short zeros[2400 * 2];
|
||||
memset(zeros, 0, sizeof(zeros));
|
||||
short zeros_s16[2400 * 2];
|
||||
float zeros_f32[2400 * 2];
|
||||
if (is_float) {
|
||||
memset(zeros_f32, 0, sizeof(zeros_f32));
|
||||
} else {
|
||||
memset(zeros_s16, 0, sizeof(zeros_s16));
|
||||
}
|
||||
|
||||
const void* ptr = is_float ? (const void*)zeros_f32 : (const void*)zeros_s16;
|
||||
size_t fbytes = is_float ? (sizeof(float) * 2U) : (sizeof(short) * 2U);
|
||||
long pos = 0;
|
||||
while (pos < total_frames && !g_stop) {
|
||||
long n = total_frames - pos;
|
||||
if (n > 2400L) n = 2400L;
|
||||
if (write_pcm_frames_all(pcm, zeros, (snd_pcm_uframes_t)n) < 0) {
|
||||
if (write_pcm_frames_all(pcm, ptr, (snd_pcm_uframes_t)n, fbytes) < 0) {
|
||||
return -1;
|
||||
}
|
||||
pos += n;
|
||||
@@ -415,6 +740,49 @@ static int write_haptic_silence(snd_pcm_t* pcm, long ms) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void play_haptic_pattern(snd_pcm_t* pcm, long duration_ms, int level,
|
||||
double amp, int is_float) {
|
||||
if (duration_ms <= 90) {
|
||||
long actual_ms = (level <= 1 && duration_ms > 60) ? 60 : duration_ms;
|
||||
(void)write_haptic_pulse(pcm, actual_ms, amp, is_float);
|
||||
return;
|
||||
}
|
||||
|
||||
long on_ms = 140;
|
||||
long off_ms = 90;
|
||||
if (level <= 1) {
|
||||
on_ms = 85;
|
||||
off_ms = 115;
|
||||
} else if (level >= 3) {
|
||||
on_ms = 240;
|
||||
off_ms = 60;
|
||||
}
|
||||
|
||||
long rem = duration_ms;
|
||||
while (rem > 0 && !g_stop) {
|
||||
long cur_on = (rem < on_ms) ? rem : on_ms;
|
||||
if (write_haptic_pulse(pcm, cur_on, amp, is_float) < 0) break;
|
||||
rem -= cur_on;
|
||||
if (rem <= 0 || g_stop) break;
|
||||
long cur_off = (rem < off_ms) ? rem : off_ms;
|
||||
if (write_haptic_silence(pcm, cur_off, is_float) < 0) break;
|
||||
rem -= cur_off;
|
||||
}
|
||||
}
|
||||
|
||||
static snd_pcm_t* open_haptic_pcm(const char* dev, snd_pcm_format_t fmt) {
|
||||
snd_pcm_t* pcm = NULL;
|
||||
if (snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (snd_pcm_set_params(pcm, fmt, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000,
|
||||
1, 500000) < 0) {
|
||||
snd_pcm_close(pcm);
|
||||
return NULL;
|
||||
}
|
||||
return pcm;
|
||||
}
|
||||
|
||||
static int vibrate_haptic_audio(long duration_ms, int level) {
|
||||
if (is_non_speaker_audio_route()) {
|
||||
return 0;
|
||||
@@ -424,6 +792,7 @@ static int vibrate_haptic_audio(long duration_ms, int level) {
|
||||
if (getuid() == 0 && access("/run/user/10000/pipewire-0", F_OK) == 0) {
|
||||
(void)initgroups("user", 10000);
|
||||
if (setgid(10000) == 0 && setuid(10000) == 0) {
|
||||
(void)setenv("HOME", "/home/user", 1);
|
||||
(void)setenv("XDG_RUNTIME_DIR", "/run/user/10000", 1);
|
||||
}
|
||||
} else {
|
||||
@@ -437,63 +806,94 @@ static int vibrate_haptic_audio(long duration_ms, int level) {
|
||||
}
|
||||
}
|
||||
|
||||
struct haptic_ctl_state ctl_st;
|
||||
memset(&ctl_st, 0, sizeof(ctl_st));
|
||||
|
||||
snd_pcm_t* pcm = NULL;
|
||||
const char* devs[2];
|
||||
if (is_pw_sink_muted()) {
|
||||
devs[0] = "plughw:0,0";
|
||||
devs[1] = "default";
|
||||
} else {
|
||||
devs[0] = "default";
|
||||
devs[1] = "plughw:0,0";
|
||||
struct haptic_pw_state pw;
|
||||
haptic_pw_connect_and_query(&pw);
|
||||
if (pw.non_speaker || g_stop) {
|
||||
haptic_pw_restore_and_close(&pw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* opened_dev = NULL;
|
||||
for (int i = 0; i < 2 && !pcm; ++i) {
|
||||
if (snd_pcm_open(&pcm, devs[i], SND_PCM_STREAM_PLAYBACK, 0) == 0) {
|
||||
if (snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
|
||||
SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000,
|
||||
1, 500000) < 0) {
|
||||
snd_pcm_close(pcm);
|
||||
pcm = NULL;
|
||||
} else {
|
||||
opened_dev = devs[i];
|
||||
double amp_ref = HAPTIC_REF_AMP_L2;
|
||||
if (level <= 1) amp_ref = HAPTIC_REF_AMP_L1;
|
||||
else if (level >= 3) amp_ref = HAPTIC_REF_AMP_L3;
|
||||
amp_ref *= read_haptic_scale_env();
|
||||
|
||||
double amp_norm = amp_ref / 32768.0;
|
||||
if (amp_norm > 0.92) amp_norm = 0.92;
|
||||
|
||||
struct haptic_ctl_state ctl_st;
|
||||
haptic_ctl_open(&ctl_st);
|
||||
|
||||
snd_pcm_t* pcm = NULL;
|
||||
int use_float = 0;
|
||||
double play_amp = amp_ref;
|
||||
|
||||
int pw_unmuted = pw.connected && pw.sink_found && !pw.sink_muted &&
|
||||
pw.linear_gain >= 1e-4 && !is_pw_sink_muted();
|
||||
if (pw_unmuted) {
|
||||
long cur_rx3 = ctl_st.has_rx3_vol ? ctl_st.saved_rx3_vol : HAPTIC_REF_RX3_VOL;
|
||||
double cur_gain_rx3 =
|
||||
(cur_rx3 <= 0) ? 0.0 : pow(10.0, ((double)cur_rx3 - 84.0) / 20.0);
|
||||
long target_rx3 = -1;
|
||||
if (cur_rx3 <= 0 ||
|
||||
(pw.n_inputs == 0 && (cur_gain_rx3 <= 0.0 ||
|
||||
amp_norm / cur_gain_rx3 > 0.92))) {
|
||||
target_rx3 = HAPTIC_REF_RX3_VOL;
|
||||
}
|
||||
|
||||
haptic_ensure_speaker_ctl(&ctl_st, target_rx3);
|
||||
long eff_rx3 =
|
||||
ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL;
|
||||
double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0);
|
||||
double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92;
|
||||
if (post_pw > 0.92) post_pw = 0.92;
|
||||
|
||||
pcm = open_haptic_pcm("default", SND_PCM_FORMAT_FLOAT_LE);
|
||||
if (pcm) {
|
||||
use_float = 1;
|
||||
play_amp = post_pw / pw.linear_gain;
|
||||
} else {
|
||||
haptic_restore_speaker_ctl(&ctl_st);
|
||||
haptic_ctl_open(&ctl_st);
|
||||
}
|
||||
}
|
||||
|
||||
if (!pcm) {
|
||||
pcm = open_haptic_pcm("plughw:0,0", SND_PCM_FORMAT_S16_LE);
|
||||
if (pcm) {
|
||||
haptic_ensure_speaker_ctl(&ctl_st, HAPTIC_REF_RX3_VOL);
|
||||
long eff_rx3 =
|
||||
ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL;
|
||||
double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0);
|
||||
double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92;
|
||||
if (post_pw > 0.92) post_pw = 0.92;
|
||||
use_float = 0;
|
||||
play_amp = post_pw * 32767.0;
|
||||
} else if (pw.connected && pw.sink_found) {
|
||||
if (haptic_pw_prepare_muted_bypass(&pw) == 0) {
|
||||
haptic_ensure_speaker_ctl(&ctl_st, HAPTIC_REF_RX3_VOL);
|
||||
pcm = open_haptic_pcm("default", SND_PCM_FORMAT_FLOAT_LE);
|
||||
if (pcm) {
|
||||
long eff_rx3 =
|
||||
ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL;
|
||||
double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0);
|
||||
double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92;
|
||||
if (post_pw > 0.92) post_pw = 0.92;
|
||||
use_float = 1;
|
||||
play_amp = post_pw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pcm) {
|
||||
haptic_restore_speaker_ctl(&ctl_st);
|
||||
haptic_pw_restore_and_close(&pw);
|
||||
fprintf(stderr, "sxmo_vibrate: cannot open haptic audio PCM\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (opened_dev && strcmp(opened_dev, "plughw:0,0") == 0) {
|
||||
haptic_ensure_speaker_ctl(&ctl_st);
|
||||
}
|
||||
|
||||
double amp = 21000.0;
|
||||
if (level <= 1) amp = 13000.0;
|
||||
else if (level >= 3) amp = 29000.0;
|
||||
|
||||
if (level >= 3 || duration_ms <= 90) {
|
||||
long actual_ms = (level == 1 && duration_ms > 55) ? 55 : duration_ms;
|
||||
(void)write_haptic_pulse(pcm, actual_ms, amp);
|
||||
} else {
|
||||
long on_ms = (level == 1) ? 75 : 160;
|
||||
long off_ms = (level == 1) ? 125 : 80;
|
||||
long rem = duration_ms;
|
||||
while (rem > 0 && !g_stop) {
|
||||
long cur_on = (rem < on_ms) ? rem : on_ms;
|
||||
if (write_haptic_pulse(pcm, cur_on, amp) < 0) break;
|
||||
rem -= cur_on;
|
||||
if (rem <= 0 || g_stop) break;
|
||||
long cur_off = (rem < off_ms) ? rem : off_ms;
|
||||
if (write_haptic_silence(pcm, cur_off) < 0) break;
|
||||
rem -= cur_off;
|
||||
}
|
||||
}
|
||||
play_haptic_pattern(pcm, duration_ms, level, play_amp, use_float);
|
||||
|
||||
if (g_stop) {
|
||||
(void)snd_pcm_drop(pcm);
|
||||
@@ -502,6 +902,7 @@ static int vibrate_haptic_audio(long duration_ms, int level) {
|
||||
}
|
||||
snd_pcm_close(pcm);
|
||||
haptic_restore_speaker_ctl(&ctl_st);
|
||||
haptic_pw_restore_and_close(&pw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user