repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

audio: pure node-tested audio.js mixer logic

664bae3613603a5648eff5d7f7d942d39dcf47b6

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Changed files (2)

quickshell/nuguland/audio.js

Added
new file mode 100644
1 +// audio.js — pure audio mixer logic: clamp/format volume, role coloring, row shaping.
2 +// NO I/O, NO native PipeWire access, NO Quickshell imports. Receives plain field dicts
3 +// (lifted off native nodes by Audio.qml), returns dicts/numbers/strings. Dual-mode:
4 +// node `require` (tests) + QML import. Same pattern as clip.js/launch.js/sysinfo.js.
6 +// --- Nerd Font glyphs (FA4 PUA, verified present in SymbolsNerdFont-Regular). \uXXXX escapes.
7 +var GLYPH_OUT = ""; // speaker / volume-up (output)
8 +var GLYPH_OUT_MUTE = ""; // volume-off / mute (output muted)
9 +var GLYPH_IN = ""; // microphone (input)
10 +var GLYPH_IN_MUTE = ""; // microphone-slash (input muted)
12 +// --- clamp a Number into 0..1.5; null/NaN/non-finite -> 0 ---
13 +function clampVolume(v) {
14 + var n = Number(v);
15 + return isFinite(n) ? Math.max(0, Math.min(1.5, n)) : 0;
18 +// --- integer percent of a clamped volume: 1.0->100, 1.5->150, 0->0 ---
19 +function volumePercent(v) {
20 + return Math.round(clampVolume(v) * 100);
23 +// --- display name precedence; null-safe -> "Audio". Native props arrive as flat keys. ---
24 +function nodeName(n) {
25 + n = n || {};
26 + return String(n.description
27 + || n.nickname
28 + || n.nodeDescription
29 + || n.applicationName
30 + || n.mediaName
31 + || n.name
32 + || "Audio");
35 +// --- normalize a list of channel labels to lowercase strings (enums resolved natively) ---
36 +function channelNames(channels) {
37 + var out = [];
38 + var list = channels || [];
39 + for (var i = 0; i < list.length; i++) {
40 + out.push(String(list[i]).toLowerCase());
41 + }
42 + return out;
45 +// --- muted flag lifted off node.audio.muted by the bridge ---
46 +function nodeMuted(n) {
47 + return !!(n && n.muted);
50 +// --- role color key by mute/volume (spec §3.2 contract):
51 +// muted -> "warning", volume 0 -> "ui.minor", else "ui.accent" ---
52 +function roleForSink(n) {
53 + if (nodeMuted(n)) return "warning";
54 + if (clampVolume(n && n.volume) <= 0) return "ui.minor";
55 + return "ui.accent";
58 +// --- privacy chip label from {mic,cam}; no flags -> "Audio" (Bar chip needs non-empty) ---
59 +function privacyLabel(flags) {
60 + var f = flags || {};
61 + if (f.mic && f.cam) return "Mic Cam";
62 + if (f.mic) return "Mic";
63 + if (f.cam) return "Cam";
64 + return "Audio";
67 +// --- glyph for a row by kind + muted state ---
68 +function glyphForRow(kind, muted) {
69 + if (kind === "source") return muted ? GLYPH_IN_MUTE : GLYPH_IN;
70 + return muted ? GLYPH_OUT_MUTE : GLYPH_OUT;
73 +// --- shape a raw bridge dict into the view/IPC row. ALL resolution happens here. ---
74 +function shapeRow(n, kind) {
75 + n = n || {};
76 + var vol = clampVolume(n.volume);
77 + return {
78 + id: String(n.id == null ? "" : n.id),
79 + name: nodeName(n),
80 + kind: String(kind || ""),
81 + volume: Math.round(vol * 100) / 100,
82 + percent: volumePercent(vol),
83 + muted: nodeMuted(n),
84 + role: roleForSink(n),
85 + isDefault: !!n.isDefault,
86 + active: !!n.active,
87 + channels: channelNames(n.channels),
88 + glyph: glyphForRow(kind, nodeMuted(n))
89 + };
92 +// --- index rows by id ---
93 +function rowsById(rows) {
94 + var out = {};
95 + var list = rows || [];
96 + for (var i = 0; i < list.length; i++) {
97 + out[list[i].id] = list[i];
98 + }
99 + return out;
102 +if (typeof module !== 'undefined' && module.exports) {
103 + module.exports = {
104 + clampVolume: clampVolume,
105 + volumePercent: volumePercent,
106 + nodeName: nodeName,
107 + channelNames: channelNames,
108 + nodeMuted: nodeMuted,
109 + roleForSink: roleForSink,
110 + privacyLabel: privacyLabel,
111 + glyphForRow: glyphForRow,
112 + shapeRow: shapeRow,
113 + rowsById: rowsById
114 + };

quickshell/nuguland/audio.test.js

Added
new file mode 100644
1 +const assert = require('assert');
2 +const A = require('./audio.js');
4 +// Glyph constants (must match audio.js exactly; these are the source-of-truth escapes).
5 +const GLYPH_OUT = "";
6 +const GLYPH_OUT_MUTE = "";
7 +const GLYPH_IN = "";
8 +const GLYPH_IN_MUTE = "";
10 +// --- clampVolume: 0..1.5, null/NaN -> 0 ---
11 +assert.strictEqual(A.clampVolume(-1), 0);
12 +assert.strictEqual(A.clampVolume(2), 1.5);
13 +assert.strictEqual(A.clampVolume(0.5), 0.5);
14 +assert.strictEqual(A.clampVolume(null), 0);
15 +assert.strictEqual(A.clampVolume(undefined), 0);
16 +assert.strictEqual(A.clampVolume(NaN), 0);
17 +assert.strictEqual(A.clampVolume("0.3"), 0.3);
19 +// --- volumePercent: integer percent of clamped volume ---
20 +assert.strictEqual(A.volumePercent(1), 100);
21 +assert.strictEqual(A.volumePercent(1.5), 150);
22 +assert.strictEqual(A.volumePercent(0), 0);
23 +assert.strictEqual(A.volumePercent(2), 150); // clamped first
24 +assert.strictEqual(A.volumePercent(null), 0);
26 +// --- nodeName: precedence description > nickname > name > "Audio"; null-safe ---
27 +assert.strictEqual(
28 + A.nodeName({ description: 'Desc', nickname: 'Nick', name: 'Name' }), 'Desc');
29 +// nickname wins only when description AND the superset fields are absent
30 +assert.strictEqual(
31 + A.nodeName({ nickname: 'Nick', name: 'Name' }), 'Nick');
32 +assert.strictEqual(
33 + A.nodeName({ name: 'Name' }), 'Name');
34 +// superset fields sit between nickname and name
35 +assert.strictEqual(
36 + A.nodeName({ nodeDescription: 'ND', name: 'Name' }), 'ND');
37 +assert.strictEqual(
38 + A.nodeName({ applicationName: 'App', name: 'Name' }), 'App');
39 +assert.strictEqual(
40 + A.nodeName({ mediaName: 'Media', name: 'Name' }), 'Media');
41 +assert.strictEqual(A.nodeName({}), 'Audio');
42 +assert.strictEqual(A.nodeName(null), 'Audio');
44 +// --- channelNames: lowercased string list ---
45 +assert.deepStrictEqual(A.channelNames(['FL', 'FR']), ['fl', 'fr']);
46 +assert.deepStrictEqual(A.channelNames([]), []);
47 +assert.deepStrictEqual(A.channelNames(null), []);
49 +// --- nodeMuted ---
50 +assert.strictEqual(A.nodeMuted({ muted: true }), true);
51 +assert.strictEqual(A.nodeMuted({ muted: false }), false);
52 +assert.strictEqual(A.nodeMuted({}), false);
53 +assert.strictEqual(A.nodeMuted(null), false);
55 +// --- roleForSink: muted->warning, vol0->ui.minor, vol0.5->ui.accent ---
56 +assert.strictEqual(A.roleForSink({ muted: true, volume: 0.5 }), 'warning');
57 +assert.strictEqual(A.roleForSink({ muted: false, volume: 0 }), 'ui.minor');
58 +assert.strictEqual(A.roleForSink({ muted: false, volume: 0.5 }), 'ui.accent');
59 +assert.strictEqual(A.roleForSink({ volume: 1.0 }), 'ui.accent');
60 +assert.strictEqual(A.roleForSink({}), 'ui.minor'); // no volume -> 0 -> ui.minor
62 +// --- privacyLabel: all four {mic,cam} combos ---
63 +assert.strictEqual(A.privacyLabel({ mic: true, cam: true }), 'Mic Cam');
64 +assert.strictEqual(A.privacyLabel({ mic: true, cam: false }), 'Mic');
65 +assert.strictEqual(A.privacyLabel({ mic: false, cam: true }), 'Cam');
66 +assert.strictEqual(A.privacyLabel({ mic: false, cam: false }), 'Audio');
67 +assert.strictEqual(A.privacyLabel({}), 'Audio');
68 +assert.strictEqual(A.privacyLabel(null), 'Audio');
70 +// --- glyphForRow: kind + muted matrix ---
71 +assert.strictEqual(A.glyphForRow('sink', false), GLYPH_OUT);
72 +assert.strictEqual(A.glyphForRow('sink', true), GLYPH_OUT_MUTE);
73 +assert.strictEqual(A.glyphForRow('source', false), GLYPH_IN);
74 +assert.strictEqual(A.glyphForRow('source', true), GLYPH_IN_MUTE);
76 +// --- shapeRow: full dict with correct percent/muted/glyph/role ---
77 +const sink = A.shapeRow({
78 + id: 42, description: 'Speakers', volume: 0.5, muted: false,
79 + isDefault: true, active: true, channels: ['FL', 'FR']
80 +}, 'sink');
81 +assert.strictEqual(sink.id, '42');
82 +assert.strictEqual(sink.name, 'Speakers');
83 +assert.strictEqual(sink.kind, 'sink');
84 +assert.strictEqual(sink.volume, 0.5);
85 +assert.strictEqual(sink.percent, 50);
86 +assert.strictEqual(sink.muted, false);
87 +assert.strictEqual(sink.role, 'ui.accent');
88 +assert.strictEqual(sink.isDefault, true);
89 +assert.strictEqual(sink.active, true);
90 +assert.deepStrictEqual(sink.channels, ['fl', 'fr']);
91 +assert.strictEqual(sink.glyph, GLYPH_OUT);
93 +// muted source row: warning role, muted glyph, desaturatable
94 +const src = A.shapeRow({
95 + id: 7, nodeDescription: 'Built-in Mic', volume: 0.8, muted: true
96 +}, 'source');
97 +assert.strictEqual(src.kind, 'source');
98 +assert.strictEqual(src.name, 'Built-in Mic');
99 +assert.strictEqual(src.muted, true);
100 +assert.strictEqual(src.role, 'warning');
101 +assert.strictEqual(src.glyph, GLYPH_IN_MUTE);
102 +assert.strictEqual(src.isDefault, false);
103 +assert.strictEqual(src.active, false);
105 +// null node -> safe defaults
106 +const empty = A.shapeRow(null, 'sink');
107 +assert.strictEqual(empty.id, '');
108 +assert.strictEqual(empty.name, 'Audio');
109 +assert.strictEqual(empty.percent, 0);
110 +assert.strictEqual(empty.role, 'ui.minor');
111 +assert.strictEqual(empty.glyph, GLYPH_OUT);
113 +// --- rowsById: keys by id ---
114 +const map = A.rowsById([{ id: 'a', x: 1 }, { id: 'b', x: 2 }]);
115 +assert.strictEqual(map.a.x, 1);
116 +assert.strictEqual(map.b.x, 2);
117 +assert.deepStrictEqual(Object.keys(A.rowsById([])), []);
118 +assert.deepStrictEqual(Object.keys(A.rowsById(null)), []);
120 +console.log('audio.test.js: ALL PASS');