From 682b9429888d1fd5b14be50e7bd4bd23d6315a1b Mon Sep 17 00:00:00 2001
From: Florian Meissner <florian.meissner@mailbox.org>
Date: Sun, 24 Sep 2023 13:01:30 +0200
Subject: [PATCH] wait: increase dB linearly

---
 helpers/wake/dB_to_volume.c | 29 +++++++++++++++++++++++++++++
 wake.sh                     | 14 ++++++++++++--
 2 files changed, 41 insertions(+), 2 deletions(-)
 create mode 100644 helpers/wake/dB_to_volume.c

diff --git a/helpers/wake/dB_to_volume.c b/helpers/wake/dB_to_volume.c
new file mode 100644
index 0000000..9a39124
--- /dev/null
+++ b/helpers/wake/dB_to_volume.c
@@ -0,0 +1,29 @@
+#include <assert.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <pulse/volume.h>
+
+
+int main(int argc, char **argv) {
+  assert(argc == 2);
+
+  errno = 0;
+  char *endptr;
+  double dB = strtod(argv[1], &endptr);
+
+  if (errno != 0 || *endptr != '\0') {
+    fprintf(stderr, "Failed to parse dB value\n");
+    return 1;
+  }
+
+  // somehow pa_sw_volume_from_dB returns approx. power of db/3
+  uint32_t volume = pa_sw_volume_from_dB(dB * 3);
+//  double vol_out = pa_sw_volume_to_linear(volume);
+
+//  printf("%f\n", vol_out);
+  printf("%f\n", ((double)volume) / 65536.0);
+
+  return 0;
+}
diff --git a/wake.sh b/wake.sh
index c7eb35f..a95fa58 100755
--- a/wake.sh
+++ b/wake.sh
@@ -1,5 +1,8 @@
 #!/usr/bin/env fish
 
+set dB_to_vol (mktemp)
+gcc -lpulse -o $dB_to_vol ~/scripts/helpers/wake/dB_to_volume.c || begin; echo 'gcc failed on dB_to_volume.c; status = '$status; exit 1; end;
+
 # delta - time between 0.01 increase
 # when - date formatted string for target datetime
 argparse --name wake --min-args 1 'd/delta=' 'w/when=' -- $argv
@@ -7,12 +10,19 @@ set -q _flag_when _flag_delta || exit 1
 
 sudo rtcwake -m mem -u -t $(date +%s -d $_flag_when)
 
+# set to analog profile
+# also sink name changes with profile. TODO match only part of profile name
+pactl set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
+set sink (pactl list sinks | string match --regex --groups-only 'Name:\s+(alsa_output.pci-0000_00_1b.0\S+)')
+pactl set-default-sink $sink
+
 mpv --loop-playlist $argv &
 
 # TODO catch Ctrl+C or do sth that volume raising is interruptible but we still get mpv console
 
-for i in (LC_NUMERIC= seq 0.01 0.01 1)
-  pactl set-sink-volume alsa_output.pci-0000_00_1b.0.analog-stereo.3 $i
+
+for i in (LC_NUMERIC= seq -40 0.4 0) #dB
+  pactl set-sink-volume alsa_output.pci-0000_00_1b.0.analog-stereo.3 ($dB_to_vol $i)
   sleep $_flag_delta
 end
 
-- 
GitLab