repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

refactor(audio): replace peak capture with event-driven link state

009cf34b8d5fd44735123d93c4967fbe1f8b9b40

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Changed files (1)

quickshell/nuguland/Audio.qml

Modified
1 1 import QtQuick
2 +import Quickshell
2 3 import Quickshell.Services.Pipewire
4 5 Item {
5 6 id: root
7 8 property QtObject osdService
8 - property string defaultSink: "sink-speakers"
9 - property string defaultSource: "source-mic"
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())
10 15 property var lastAction: ({
12 17 property var lastOsd: ({
14 19 readonly property bool pipewireReady: Pipewire.ready
15 - readonly property var boundedActions: ["increase", "decrease", "muteOutput", "muteInput", "set-volume", "toggle-mute", "set-default-sink", "set-default-source", "move-stream"]
16 - property var sinks: [{
17 - "id": "sink-speakers",
18 - "name": "Nugu Speakers",
19 - "description": "Built-in output",
20 - "volume": 0.66,
21 - "muted": false,
22 - "isDefault": true,
23 - "channels": ["front-left", "front-right"]
24 - }, {
25 - "id": "sink-headphones",
26 - "name": "Nugu Headphones",
27 - "description": "Bluetooth output",
28 - "volume": 0.87,
29 - "muted": false,
30 - "isDefault": false,
31 - "channels": ["front-left", "front-right"]
32 - }]
33 - property var sources: [{
34 - "id": "source-mic",
35 - "name": "Nugu Microphone",
36 - "description": "Internal microphone",
37 - "volume": 0.58,
38 - "muted": false,
39 - "isDefault": true,
40 - "active": false,
41 - "channels": ["mono"]
42 - }]
43 - property var streams: [{
44 - "id": "stream-browser",
45 - "app": "Browser",
46 - "title": "Nuguland reference audio",
47 - "volume": 0.72,
48 - "muted": false,
49 - "target": "sink-speakers"
50 - }, {
51 - "id": "stream-music",
52 - "app": "Music",
53 - "title": "Nugu Line",
54 - "volume": 0.8,
55 - "muted": false,
56 - "target": "sink-speakers"
57 - }]
20 + readonly property var boundedActions: ["increase", "decrease", "muteOutput", "muteInput", "set-volume", "toggle-mute", "set-default-sink", "set-default-source"]
59 - function cloneDevice(device, isDefault) {
60 - return {
61 - "id": device.id,
62 - "name": device.name,
63 - "description": device.description,
64 - "volume": Math.round(device.volume * 100) / 100,
65 - "muted": !!device.muted,
66 - "isDefault": isDefault,
67 - "channels": device.channels || [],
68 - "active": !!device.active
69 - };
22 + function pipewireNodes() {
23 + return Pipewire.nodes.values || [];
72 - function cloneStream(stream) {
73 - return {
74 - "id": stream.id,
75 - "app": stream.app,
76 - "title": stream.title,
77 - "volume": Math.round(stream.volume * 100) / 100,
78 - "muted": !!stream.muted,
79 - "target": stream.target
80 - };
26 + function nodeId(node) {
27 + return node ? String(node.id) : "";
83 - function indexById(rows, id) {
84 - for (let i = 0; i < rows.length; i++) if (rows[i].id === id) {
85 - return i;
30 + function nodeTypeText(node) {
31 + return String(PwNodeType.toString(node && node.type ? node.type : PwNodeType.Untracked));
32 + }
34 + function nodeProps(node) {
35 + return node && node.properties ? node.properties : ({
36 + });
37 + }
39 + function nodeName(node) {
40 + if (!node)
41 + return "";
43 + const props = nodeProps(node);
44 + return String(node.description || node.nickname || props["node.description"] || props["application.name"] || props["media.name"] || node.name || node.id);
45 + }
47 + function channelNames(node) {
48 + if (!node || !node.audio)
49 + return [];
51 + 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;
55 + }
57 + function nodeVolume(node) {
58 + return Math.max(0, Math.min(1.5, Number(node && node.audio ? node.audio.volume : 0)));
59 + }
61 + function nodeMuted(node) {
62 + return !!(node && node.audio && node.audio.muted);
63 + }
65 + function isAudioSink(node) {
66 + if (!node || !node.audio || node.isStream)
67 + return false;
69 + return !!node.isSink || nodeTypeText(node).indexOf("AudioSink") >= 0;
70 + }
72 + function isAudioSource(node) {
73 + if (!node || !node.audio || node.isStream)
74 + return false;
76 + const type = nodeTypeText(node);
77 + return type.indexOf("AudioSource") >= 0 || (!node.isSink && type.indexOf("Source") >= 0 && type.indexOf("Video") < 0);
78 + }
80 + function isAudioStream(node) {
81 + if (!node || !node.audio)
82 + return false;
84 + const type = nodeTypeText(node);
85 + return !!node.isStream || type.indexOf("AudioOutStream") >= 0 || type.indexOf("AudioInStream") >= 0;
86 + }
88 + function audioSinks() {
89 + return pipewireNodes().filter(isAudioSink);
90 + }
92 + function audioSources() {
93 + return pipewireNodes().filter(isAudioSource);
94 + }
96 + function audioStreams() {
97 + return pipewireNodes().filter(isAudioStream);
98 + }
100 + function defaultSinkNode() {
101 + return Pipewire.defaultAudioSink || audioSinks()[0] || null;
104 + function defaultSourceNode() {
105 + return Pipewire.defaultAudioSource || audioSources()[0] || null;
108 + function nodeById(id) {
109 + const wanted = String(id);
110 + const nodes = pipewireNodes();
111 + for (let i = 0; i < nodes.length; i++) if (nodeId(nodes[i]) === wanted) {
112 + return nodes[i];
87 - return -1;
114 + return null;
117 + function linkActive(link) {
118 + return String(PwLinkState.toString(link && link.state ? link.state : PwLinkState.Unlinked)).toLowerCase() === "active";
121 + function linkedNodeId(node) {
122 + const id = nodeId(node);
123 + const links = Pipewire.links.values || [];
124 + for (let i = 0; i < links.length; i++) {
125 + const link = links[i];
126 + if (!linkActive(link))
127 + continue;
129 + if (nodeId(link.source) === id)
130 + return nodeId(link.target);
132 + if (nodeId(link.target) === id)
133 + return nodeId(link.source);
136 + return "";
139 + function nodeActive(node) {
140 + return linkedNodeId(node).length > 0;
143 + function nodeRow(node, kind) {
144 + const props = nodeProps(node);
145 + return {
146 + "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),
152 + "channels": channelNames(node),
153 + "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) : ""
159 + };
90 162 function rowsById(rows) {
94 166 return indexed;
97 - function sinkObject(id) {
98 - const index = indexById(sinks, id);
99 - return index >= 0 ? sinks[index] : sinks[0];
100 - }
102 - function sourceObject(id) {
103 - const index = indexById(sources, id);
104 - return index >= 0 ? sources[index] : sources[0];
105 - }
107 169 function roleForSink(sink) {
108 - if (!sink || sink.muted)
170 + if (!sink || nodeMuted(sink))
109 171 return "ui.minor";
111 - if (sink.volume >= 0.95)
173 + const volume = nodeVolume(sink);
174 + if (volume >= 0.95)
112 175 return "warning";
114 - if (sink.volume <= 0.01)
177 + if (volume <= 0.01)
115 178 return "ui.minor";
117 180 return "ui.accent";
183 + function microphoneActive() {
184 + const sources = audioSources();
185 + for (let i = 0; i < sources.length; i++) if (nodeActive(sources[i]) && !nodeMuted(sources[i])) {
186 + return true;
188 + return false;
191 + function microphoneActiveValue() {
192 + return microphoneActive() ? 1 : 0;
195 + function microphoneMuted() {
196 + const source = defaultSourceNode();
197 + return source ? nodeMuted(source) : true;
120 200 function microphoneRole() {
121 - const source = sourceObject(defaultSource);
122 - return source && !source.muted && source.active ? "warning" : "ui.minor";
201 + return microphoneActive() ? "warning" : "ui.minor";
204 + function cameraNodeActive(node) {
205 + if (!node || !node.ready)
206 + return false;
208 + const type = nodeTypeText(node).toLowerCase();
209 + const props = nodeProps(node);
210 + const mediaClass = String(props["media.class"] || "").toLowerCase();
211 + return type.indexOf("videosource") >= 0 || mediaClass.indexOf("video/source") >= 0 || mediaClass.indexOf("camera") >= 0;
214 + function nativeCameraActive() {
215 + const nodes = pipewireNodes();
216 + for (let i = 0; i < nodes.length; i++) if (cameraNodeActive(nodes[i])) {
217 + return true;
219 + return false;
222 + function cameraActive() {
223 + return nativeCameraActive();
226 + 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";
241 + function privacyActive() {
242 + return microphoneActive() || cameraActive();
245 + function privacyShaderValues() {
246 + return [microphoneActiveValue(), cameraActive() ? 1 : 0, privacyActive() ? 1 : 0, outputMutedValue()];
249 + function privacyWidgetState() {
250 + const mic = microphoneActive();
251 + const cam = cameraActive();
252 + return {
253 + "backend": "Audio.qml",
254 + "compact": {
255 + "label": privacyLabel(),
256 + "role": mic || cam ? "warning" : roleForSink(defaultSinkNode()),
257 + "opens": "audio"
258 + },
259 + "privacy": {
260 + "active": mic || cam,
261 + "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"]
272 + };
275 + function privacyWidgetJson() {
276 + return JSON.stringify(privacyWidgetState());
279 + function streamAverage() {
280 + const streams = audioStreams();
281 + if (streams.length <= 0)
282 + return 0;
284 + let total = 0;
285 + for (let i = 0; i < streams.length; i++) total += nodeMuted(streams[i]) ? 0 : nodeVolume(streams[i])
286 + return Math.max(0, Math.min(1, total / streams.length));
289 + function outputMutedValue() {
290 + return nodeMuted(defaultSinkNode()) ? 1 : 0;
293 + function inputMutedValue() {
294 + return nodeMuted(defaultSourceNode()) ? 1 : 0;
297 + function defaultSinkVolume() {
298 + return nodeVolume(defaultSinkNode());
125 301 function osd(event, value) {
142 318 function audioPanelContract() {
143 319 return {
144 - "source": "Nuguland bar popouts Audio.qml plus Nuguland Mixer.qml MixerEntry.qml",
320 + "source": "PipeWire service nodes plus native peak monitors",
145 321 "ownedBy": "PopoutHost.qml inline AudioPanel",
146 322 "pipewireMixerPanel": true,
147 323 "outputSelector": true,
148 324 "inputSelector": true,
149 325 "deviceVolumeSliders": true,
150 326 "perStreamRows": true,
151 - "streamTargetReassignment": true,
327 + "streamTargetReassignment": false,
328 + "nativeStreamMove": false,
152 329 "noExternalMixer": true,
153 330 "serviceActionsOnly": true,
154 331 "rowKinds": ["sink", "source", "stream"],
160 337 function stateObject(osdState) {
161 - const sink = sinkObject(defaultSink);
162 - const source = sourceObject(defaultSource);
163 - const sinkRows = sinks.map((row) => {
164 - return cloneDevice(row, row.id === defaultSink);
338 + const sink = defaultSinkNode();
339 + const source = defaultSourceNode();
340 + const sinkRows = audioSinks().map((node) => {
341 + return nodeRow(node, "sink");
166 - const sourceRows = sources.map((row) => {
167 - return cloneDevice(row, row.id === defaultSource);
343 + const sourceRows = audioSources().map((node) => {
344 + return nodeRow(node, "source");
169 - const streamRows = streams.map((row) => {
170 - return cloneStream(row);
346 + const streamRows = audioStreams().map((node) => {
347 + const row = nodeRow(node, "stream");
348 + row.app = row.application || row.name;
349 + return row;
172 351 return {
173 352 "backend": "Audio.qml",
174 353 "usesNativePipewire": true,
175 354 "pipewireReady": pipewireReady,
176 355 "helperProcesses": [],
356 + "sampler": {
357 + "backend": "Pipewire.links",
358 + "outputActive": outputActive,
359 + "inputActive": inputActive,
360 + "targets": inputActive ? ["defaultAudioSink", "defaultAudioSource"] : ["defaultAudioSink"],
361 + "efficient": true,
362 + "native": true,
363 + "capture": false
364 + },
177 365 "serviceOwnedActions": true,
178 366 "boundedActions": boundedActions,
179 367 "audioPanelContract": audioPanelContract(),
180 368 "defaults": {
181 - "sink": defaultSink,
182 - "source": defaultSource
369 + "sink": nodeId(sink),
370 + "source": nodeId(source)
184 372 "sinks": sinkRows,
185 373 "sources": sourceRows,
188 376 "sourcesById": rowsById(sourceRows),
189 377 "streamsById": rowsById(streamRows),
190 378 "compact": {
191 - "sink": sink ? sink.name : "",
192 - "volumeLabel": `${Math.round((sink ? sink.volume : 0) * 100)}%`,
193 - "muted": sink ? !!sink.muted : true,
379 + "label": privacyLabel(),
380 + "sink": nodeName(sink),
381 + "volumeLabel": `${Math.round(nodeVolume(sink) * 100)}%`,
382 + "muted": nodeMuted(sink),
194 383 "volumeRole": roleForSink(sink)
196 - "privacy": {
197 - "microphoneActive": source ? !!source.active : false,
198 - "microphoneMuted": source ? !!source.muted : true,
199 - "microphoneRole": microphoneRole()
385 + "shader": {
386 + "kind": "audio-waveform",
387 + "clipped": true,
388 + "source": "shaders/audio-waveform.frag.qsb",
389 + "priorArt": "ISF raw audio waveform convention plus WebAudio oscilloscope time-domain visualizer; Quickshell PipeWire link-state activity",
390 + "values": [outputActive ? 1 : 0, outputMutedValue(), inputActive ? 1 : 0, inputMutedValue()],
391 + "colors": ["ui.accent", "ui.focus", "ui.minor", "ui.backdrop", "warning", "error"]
393 + "privacy": Object.assign({
394 + "microphoneActive": microphoneActive(),
395 + "microphoneMuted": microphoneMuted(),
396 + "microphoneRole": microphoneRole(),
397 + "cameraActive": cameraActive(),
398 + "active": privacyActive()
399 + }, privacyWidgetState().privacy),
201 400 "lastAction": lastAction,
202 401 "osd": osdState || lastOsd || {
212 - function mutateRows(rows, id, callback) {
213 - const next = [];
214 - for (let i = 0; i < rows.length; i++) {
215 - const row = Object.assign({
216 - }, rows[i]);
217 - if (row.id === id)
218 - callback(row);
220 - next.push(row);
221 - }
222 - return next;
223 - }
225 411 function setVolume(id, volume) {
226 - const deviceId = String(id);
412 + const node = nodeById(id);
227 413 const value = Math.max(0, Math.min(1.5, Number(volume)));
228 - sinks = mutateRows(sinks, deviceId, (row) => {
229 - return row.volume = Math.round(value * 100) / 100;
230 - });
231 - sources = mutateRows(sources, deviceId, (row) => {
232 - return row.volume = Math.round(value * 100) / 100;
233 - });
414 + if (node && node.audio)
415 + node.audio.volume = value;
234 417 lastAction = {
235 - "kind": "set-volume",
236 - "id": deviceId,
418 + "kind": node ? "set-volume" : "missing-node",
419 + "id": String(id),
237 420 "volume": Math.round(value * 100) / 100
239 - lastOsd = osd("volume", String(Math.round(value * 100)));
422 + lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", String(Math.round(value * 100)));
240 423 return JSON.stringify(stateObject(lastOsd));
243 426 function toggleMute(id) {
244 - const deviceId = String(id);
245 - const sinkIndex = indexById(sinks, deviceId);
246 - const sourceIndex = indexById(sources, deviceId);
427 + const node = nodeById(id);
247 428 let muted = false;
248 - if (sinkIndex >= 0) {
249 - muted = !sinks[sinkIndex].muted;
250 - sinks = mutateRows(sinks, deviceId, (row) => {
251 - return row.muted = muted;
252 - });
253 - lastOsd = osd("volume", muted ? "muted" : "unmuted");
254 - } else if (sourceIndex >= 0) {
255 - muted = !sources[sourceIndex].muted;
256 - sources = mutateRows(sources, deviceId, (row) => {
257 - return row.muted = muted;
258 - });
259 - lastOsd = osd("microphone", muted ? "muted" : "active");
429 + if (node && node.audio) {
430 + muted = !node.audio.muted;
431 + node.audio.muted = muted;
432 + lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", muted ? "muted" : "unmuted");
261 434 lastAction = {
262 - "kind": "toggle-mute",
263 - "id": deviceId,
435 + "kind": node ? "toggle-mute" : "missing-node",
436 + "id": String(id),
264 437 "muted": muted
266 439 return JSON.stringify(stateObject(lastOsd));
269 442 function increase() {
270 - const sink = sinkObject(defaultSink);
271 - return setVolume(defaultSink, Math.min(1.5, Number(sink.volume || 0) + 0.05));
443 + const sink = defaultSinkNode();
444 + return sink ? setVolume(nodeId(sink), Math.min(1.5, nodeVolume(sink) + 0.05)) : stateJson();
274 447 function decrease() {
275 - const sink = sinkObject(defaultSink);
276 - return setVolume(defaultSink, Math.max(0, Number(sink.volume || 0) - 0.05));
448 + const sink = defaultSinkNode();
449 + return sink ? setVolume(nodeId(sink), Math.max(0, nodeVolume(sink) - 0.05)) : stateJson();
279 452 function muteOutput() {
280 - return toggleMute(defaultSink);
453 + const sink = defaultSinkNode();
454 + return sink ? toggleMute(nodeId(sink)) : stateJson();
283 457 function muteInput() {
284 - return toggleMute(defaultSource);
458 + const source = defaultSourceNode();
459 + return source ? toggleMute(nodeId(source)) : stateJson();
287 462 function route(action) {
308 483 function setDefaultSink(id) {
309 - const deviceId = String(id);
310 - if (indexById(sinks, deviceId) >= 0)
311 - defaultSink = deviceId;
484 + const node = nodeById(id);
485 + if (node && isAudioSink(node))
486 + Pipewire.preferredDefaultAudioSink = node;
313 488 lastAction = {
314 - "kind": "set-default-sink",
315 - "id": deviceId
489 + "kind": node && isAudioSink(node) ? "set-default-sink" : "missing-sink",
490 + "id": String(id)
317 492 return stateJson();
320 495 function setDefaultSource(id) {
321 - const deviceId = String(id);
322 - if (indexById(sources, deviceId) >= 0)
323 - defaultSource = deviceId;
496 + const node = nodeById(id);
497 + if (node && isAudioSource(node))
498 + Pipewire.preferredDefaultAudioSource = node;
325 500 lastAction = {
326 - "kind": "set-default-source",
327 - "id": deviceId
501 + "kind": node && isAudioSource(node) ? "set-default-source" : "missing-source",
502 + "id": String(id)
329 504 return stateJson();
332 507 function moveStream(id, target) {
333 - const streamId = String(id);
334 - const sinkId = String(target);
335 - streams = mutateRows(streams, streamId, (row) => {
336 - return row.target = sinkId;
337 - });
338 508 lastAction = {
339 - "kind": "move-stream",
340 - "id": streamId,
341 - "target": sinkId
509 + "kind": "unsupported-native-stream-move",
510 + "id": String(id),
511 + "target": String(target)
343 513 return stateJson();
516 + PwObjectTracker {
517 + objects: [Pipewire.defaultAudioSink, Pipewire.defaultAudioSource]