repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

audio: thin service delegates to audio.js, drop narration

b9321702d3efd09d1652cb7e264fe540bb808d5f

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Audio.qml imports audio.js and delegates all pure shaping/format/role
logic (clampVolume, volumePercent, nodeName, channelNames, nodeMuted,
roleForSink, privacyLabel, shapeRow, rowsById). Native PipeWire I/O and
every preserved IPC method stay in Audio.qml. nodeRow now emits a raw
field dict that shapeRow resolves. roleForSink adopts the flipped spec
mapping (muted->warning, vol0->ui.minor, else ui.accent).

Deletes narration: audioPanelContract, privacyShaderValues, the two
shader descriptors, and stateObject metadata (backend/usesNativePipewire/
helperProcesses/sampler). Drops orphaned helpers (microphoneActiveValue,
output/inputMutedValue) and unused readonly props (audioSamplerBackend,
output/inputActive). Adds selfTest() returning {pass,fail,cases} and the
audioSelfTest() IPC handler in shell.qml for the live gate test.

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

Changed files (2)

quickshell/nuguland/Audio.qml

Modified
1 1 import QtQuick
2 2 import Quickshell
3 3 import Quickshell.Services.Pipewire
4 +import "audio.js" as Audio
5 6 Item {
6 7 id: root
8 9 property QtObject osdService
9 - readonly property string audioSamplerBackend: "Pipewire.links"
10 - // Audio activity is derived from PipeWire link presence (event-driven), not from
11 - // capturing audio. nodeActive() scans Pipewire.links for an active link touching
12 - // the node, so these re-evaluate when links change — no capture stream, no timer.
13 - readonly property bool outputActive: nodeActive(defaultSinkNode()) && !nodeMuted(defaultSinkNode())
14 - readonly property bool inputActive: nodeActive(defaultSourceNode()) && !nodeMuted(defaultSourceNode())
15 10 property var lastAction: ({
17 12 property var lastOsd: ({
39 34 function nodeName(node) {
40 35 if (!node)
41 - return "";
36 + return Audio.nodeName(null);
43 38 const props = nodeProps(node);
44 - return String(node.description || node.nickname || props["node.description"] || props["application.name"] || props["media.name"] || node.name || node.id);
39 + return Audio.nodeName({
40 + "description": node.description,
41 + "nickname": node.nickname,
42 + "nodeDescription": props["node.description"],
43 + "applicationName": props["application.name"],
44 + "mediaName": props["media.name"],
45 + "name": node.name
46 + });
47 49 function channelNames(node) {
49 51 return [];
51 53 const channels = node.audio.channels || [];
52 - const names = [];
53 - for (let i = 0; i < channels.length; i++) names.push(String(PwAudioChannel.toString(channels[i])).toLowerCase())
54 - return names;
54 + const strings = [];
55 + for (let i = 0; i < channels.length; i++) strings.push(String(PwAudioChannel.toString(channels[i])))
56 + return Audio.channelNames(strings);
57 59 function nodeVolume(node) {
58 - return Math.max(0, Math.min(1.5, Number(node && node.audio ? node.audio.volume : 0)));
60 + return Audio.clampVolume(node && node.audio ? node.audio.volume : 0);
61 63 function nodeMuted(node) {
62 - return !!(node && node.audio && node.audio.muted);
64 + return Audio.nodeMuted({
65 + "muted": node && node.audio ? node.audio.muted : false
66 + });
65 69 function isAudioSink(node) {
140 144 return linkedNodeId(node).length > 0;
147 + // RAW native bridge: lift plain fields off the native node so audio.js can shape them.
148 + // No name/role/percent resolution here — Audio.shapeRow() owns that.
143 149 function nodeRow(node, kind) {
144 150 const props = nodeProps(node);
145 151 return {
146 152 "id": nodeId(node),
147 - "name": nodeName(node),
148 - "description": String(node ? node.description || node.name || "" : ""),
149 - "volume": Math.round(nodeVolume(node) * 100) / 100,
150 - "muted": nodeMuted(node),
151 - "isDefault": kind === "sink" ? node === Pipewire.defaultAudioSink : (kind === "source" ? node === Pipewire.defaultAudioSource : false),
153 + "description": String(node ? node.description || "" : ""),
154 + "nickname": String(node ? node.nickname || "" : ""),
155 + "nodeDescription": String(props["node.description"] || ""),
156 + "applicationName": String(props["application.name"] || ""),
157 + "mediaName": String(props["media.name"] || ""),
158 + "name": String(node ? node.name || "" : ""),
159 + "volume": Number(node && node.audio ? node.audio.volume : 0),
160 + "muted": !!(node && node.audio && node.audio.muted),
152 161 "channels": channelNames(node),
162 + "isDefault": kind === "sink" ? node === Pipewire.defaultAudioSink : (kind === "source" ? node === Pipewire.defaultAudioSource : false),
153 163 "active": nodeActive(node),
154 - "nodeName": String(node ? node.name || "" : ""),
155 - "mediaClass": String(props["media.class"] || ""),
156 - "application": String(props["application.name"] || ""),
157 - "title": String(props["media.name"] || nodeName(node)),
158 - "target": kind === "stream" ? linkedNodeId(node) : ""
164 + "target": kind === "stream" ? linkedNodeId(node) : "",
165 + "app": String(props["application.name"] || ""),
166 + "title": String(props["media.name"] || "")
162 170 function rowsById(rows) {
163 - const indexed = {
164 - };
165 - for (let i = 0; i < rows.length; i++) indexed[rows[i].id] = rows[i]
166 - return indexed;
171 + return Audio.rowsById(rows);
169 174 function roleForSink(sink) {
170 - if (!sink || nodeMuted(sink))
171 - return "ui.minor";
173 - const volume = nodeVolume(sink);
174 - if (volume >= 0.95)
175 - return "warning";
177 - if (volume <= 0.01)
178 - return "ui.minor";
180 - return "ui.accent";
175 + return Audio.roleForSink({
176 + "muted": nodeMuted(sink),
177 + "volume": nodeVolume(sink)
178 + });
183 181 function microphoneActive() {
188 186 return false;
191 - function microphoneActiveValue() {
192 - return microphoneActive() ? 1 : 0;
193 - }
195 189 function microphoneMuted() {
196 190 const source = defaultSourceNode();
197 191 return source ? nodeMuted(source) : true;
226 220 function privacyLabel() {
227 - const mic = microphoneActive();
228 - const cam = cameraActive();
229 - if (mic && cam)
230 - return "Mic Cam";
232 - if (mic)
233 - return "Mic";
235 - if (cam)
236 - return "Cam";
238 - return "Audio";
221 + return Audio.privacyLabel({
222 + "mic": microphoneActive(),
223 + "cam": cameraActive()
224 + });
241 227 function privacyActive() {
242 228 return microphoneActive() || cameraActive();
245 - function privacyShaderValues() {
246 - return [microphoneActiveValue(), cameraActive() ? 1 : 0, privacyActive() ? 1 : 0, outputMutedValue()];
247 - }
249 231 function privacyWidgetState() {
250 232 const mic = microphoneActive();
251 233 const cam = cameraActive();
259 241 "privacy": {
260 242 "active": mic || cam,
261 243 "microphoneActive": mic,
262 - "cameraActive": cam,
263 - "shader": {
264 - "kind": "privacy-veil",
265 - "clipped": true,
266 - "source": "shaders/privacy-veil.frag.qsb",
267 - "priorArt": "Scanline overlay and warning veil GLSL ideas plus Inigo Quilez-style rounded-box SDF alpha mask",
268 - "values": privacyShaderValues(),
269 - "colors": ["warning", "error", "ui.accent", "ui.backdrop"]
270 - }
244 + "cameraActive": cam
276 250 return JSON.stringify(privacyWidgetState());
279 - function outputMutedValue() {
280 - return nodeMuted(defaultSinkNode()) ? 1 : 0;
281 - }
283 - function inputMutedValue() {
284 - return nodeMuted(defaultSourceNode()) ? 1 : 0;
285 - }
287 253 function osd(event, value) {
288 254 if (!osdService)
289 255 return {
304 - function audioPanelContract() {
305 - return {
306 - "source": "PipeWire service nodes (event-driven link state)",
307 - "ownedBy": "PopoutHost.qml inline AudioPanel",
308 - "pipewireMixerPanel": true,
309 - "outputSelector": true,
310 - "inputSelector": true,
311 - "deviceVolumeSliders": true,
312 - "perStreamRows": true,
313 - "streamTargetReassignment": false,
314 - "nativeStreamMove": false,
315 - "noExternalMixer": true,
316 - "serviceActionsOnly": true,
317 - "rowKinds": ["sink", "source", "stream"],
318 - "wheelParity": ["volume-up", "volume-down"],
319 - "privacyRole": "microphoneRole"
320 - };
321 - }
323 270 function stateObject(osdState) {
324 271 const sink = defaultSinkNode();
325 272 const source = defaultSourceNode();
326 273 const sinkRows = audioSinks().map((node) => {
327 - return nodeRow(node, "sink");
274 + return Audio.shapeRow(nodeRow(node, "sink"), "sink");
329 276 const sourceRows = audioSources().map((node) => {
330 - return nodeRow(node, "source");
277 + return Audio.shapeRow(nodeRow(node, "source"), "source");
332 279 const streamRows = audioStreams().map((node) => {
333 - const row = nodeRow(node, "stream");
334 - row.app = row.application || row.name;
280 + const raw = nodeRow(node, "stream");
281 + const row = Audio.shapeRow(raw, "stream");
282 + row.app = String(raw.app || row.name);
283 + row.title = String(raw.title || row.name);
284 + row.target = String(raw.target || "");
335 285 return row;
337 287 return {
338 - "backend": "Audio.qml",
339 - "usesNativePipewire": true,
340 288 "pipewireReady": pipewireReady,
341 - "helperProcesses": [],
342 - "sampler": {
343 - "backend": "Pipewire.links",
344 - "outputActive": outputActive,
345 - "inputActive": inputActive,
346 - "targets": inputActive ? ["defaultAudioSink", "defaultAudioSource"] : ["defaultAudioSink"],
347 - "efficient": true,
348 - "native": true,
349 - "capture": false
350 - },
351 289 "serviceOwnedActions": true,
352 290 "boundedActions": boundedActions,
353 - "audioPanelContract": audioPanelContract(),
354 291 "defaults": {
355 292 "sink": nodeId(sink),
356 293 "source": nodeId(source)
364 301 "compact": {
365 302 "label": privacyLabel(),
366 303 "sink": nodeName(sink),
367 - "volumeLabel": `${Math.round(nodeVolume(sink) * 100)}%`,
304 + "volumeLabel": Audio.volumePercent(nodeVolume(sink)) + "%",
368 305 "muted": nodeMuted(sink),
369 306 "volumeRole": roleForSink(sink)
371 - "shader": {
372 - "kind": "audio-waveform",
373 - "clipped": true,
374 - "source": "shaders/audio-waveform.frag.qsb",
375 - "priorArt": "ISF raw audio waveform convention plus WebAudio oscilloscope time-domain visualizer; Quickshell PipeWire link-state activity",
376 - "values": [outputActive ? 1 : 0, outputMutedValue(), inputActive ? 1 : 0, inputMutedValue()],
377 - "colors": ["ui.accent", "ui.focus", "ui.minor", "ui.backdrop", "warning", "error"]
378 - },
379 308 "privacy": Object.assign({
380 309 "microphoneActive": microphoneActive(),
381 310 "microphoneMuted": microphoneMuted(),
321 + function selfTest() {
322 + var cases = [];
323 + var pass = 0;
324 + var fail = 0;
325 + var check = function (label, got, want) {
326 + const ok = JSON.stringify(got) === JSON.stringify(want);
327 + if (ok)
328 + pass += 1;
329 + else
330 + fail += 1;
331 + cases.push({
332 + "case": label,
333 + "ok": ok,
334 + "got": got,
335 + "want": want
336 + });
337 + };
338 + check("clampVolume(-1)", Audio.clampVolume(-1), 0);
339 + check("clampVolume(2)", Audio.clampVolume(2), 1.5);
340 + check("clampVolume(0.5)", Audio.clampVolume(0.5), 0.5);
341 + check("clampVolume(null)", Audio.clampVolume(null), 0);
342 + check("volumePercent(1)", Audio.volumePercent(1), 100);
343 + check("volumePercent(1.5)", Audio.volumePercent(1.5), 150);
344 + check("volumePercent(0)", Audio.volumePercent(0), 0);
345 + check("roleForSink(muted)", Audio.roleForSink({ "muted": true, "volume": 0.5 }), "warning");
346 + check("roleForSink(vol0)", Audio.roleForSink({ "muted": false, "volume": 0 }), "ui.minor");
347 + check("roleForSink(vol0.5)", Audio.roleForSink({ "muted": false, "volume": 0.5 }), "ui.accent");
348 + check("privacyLabel(mic+cam)", Audio.privacyLabel({ "mic": true, "cam": true }), "Mic Cam");
349 + check("privacyLabel(mic)", Audio.privacyLabel({ "mic": true, "cam": false }), "Mic");
350 + check("privacyLabel(cam)", Audio.privacyLabel({ "mic": false, "cam": true }), "Cam");
351 + check("privacyLabel(none)", Audio.privacyLabel({ "mic": false, "cam": false }), "Audio");
352 + const row = Audio.shapeRow({ "id": 42, "description": "Speakers", "volume": 0.5, "muted": false, "isDefault": true }, "sink");
353 + check("shapeRow.percent", row.percent, 50);
354 + check("shapeRow.role", row.role, "ui.accent");
355 + check("shapeRow.muted", row.muted, false);
356 + return JSON.stringify({
357 + "pass": pass,
358 + "fail": fail,
359 + "cases": cases
360 + });
392 363 function stateJson() {
393 364 return JSON.stringify(stateObject({
397 368 function setVolume(id, volume) {
398 369 const node = nodeById(id);
399 - const value = Math.max(0, Math.min(1.5, Number(volume)));
370 + const value = Audio.clampVolume(volume);
400 371 if (node && node.audio)
401 372 node.audio.volume = value;
405 376 "id": String(id),
406 377 "volume": Math.round(value * 100) / 100
408 - lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", String(Math.round(value * 100)));
379 + lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", String(Audio.volumePercent(value)));
409 380 return JSON.stringify(stateObject(lastOsd));

quickshell/nuguland/shell.qml

Modified
890 890 return audio.stateJson();
893 + function audioSelfTest() : string {
894 + return audio.selfTest();
893 897 function openAudio() : string {
894 898 return surfaceManager.openRegisteredPopout("audio");