2 3 import Quickshell.Services.Pipewire
7 8 property QtObject osdService
8 - property string
defaultSink:
"sink-speakers" 9 - property string
defaultSource:
"source-mic" 9 + readonly property string
audioSamplerBackend:
"Pipewire.links" 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",
23 - "channels": [
"front-left",
"front-right"]
25 - "id":
"sink-headphones",
26 - "name":
"Nugu Headphones",
27 - "description":
"Bluetooth output",
31 - "channels": [
"front-left",
"front-right"]
33 - property
var sources: [{
35 - "name":
"Nugu Microphone",
36 - "description":
"Internal microphone",
41 - "channels": [
"mono"]
43 - property
var streams: [{
44 - "id":
"stream-browser",
46 - "title":
"Nuguland reference audio",
49 - "target":
"sink-speakers" 51 - "id":
"stream-music",
53 - "title":
"Nugu Line",
56 - "target":
"sink-speakers" 20 + readonly property
var boundedActions: [
"increase",
"decrease",
"muteOutput",
"muteInput",
"set-volume",
"toggle-mute",
"set-default-sink",
"set-default-source"]
59 - function cloneDevice(device, isDefault) {
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
22 + function pipewireNodes() {
23 + return Pipewire.nodes.values
|| [];
72 - function cloneStream(stream) {
76 - "title": stream.title,
77 - "volume":
Math.
round(stream.volume
* 100)
/ 100,
78 - "muted":
!!stream.muted,
79 - "target": stream.target
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) {
30 + function nodeTypeText(node) {
31 + return String(
PwNodeType.
toString(node
&& node.type
? node.type
: PwNodeType.Untracked));
34 + function nodeProps(node) {
35 + return node
&& node.properties
? node.properties
: ({
39 + function nodeName(node) {
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);
47 + function channelNames(node) {
48 + if (
!node
|| !node.audio)
51 + const channels
= node.audio.channels
|| [];
53 + for (
let i
= 0; i
< channels.length; i
++) names.push(
String(
PwAudioChannel.
toString(channels[i])).toLowerCase())
57 + function nodeVolume(node) {
58 + return Math.
max(
0,
Math.
min(
1.5,
Number(node
&& node.audio
? node.audio.volume
: 0)));
61 + function nodeMuted(node) {
62 + return !!(node
&& node.audio
&& node.audio.muted);
65 + function isAudioSink(node) {
66 + if (
!node
|| !node.audio
|| node.isStream)
69 + return !!node.isSink
|| nodeTypeText(node).indexOf(
"AudioSink")
>= 0;
72 + function isAudioSource(node) {
73 + if (
!node
|| !node.audio
|| node.isStream)
76 + const type
= nodeTypeText(node);
77 + return type.indexOf(
"AudioSource")
>= 0 || (
!node.isSink
&& type.indexOf(
"Source")
>= 0 && type.indexOf(
"Video")
< 0);
80 + function isAudioStream(node) {
81 + if (
!node
|| !node.audio)
84 + const type
= nodeTypeText(node);
85 + return !!node.isStream
|| type.indexOf(
"AudioOutStream")
>= 0 || type.indexOf(
"AudioInStream")
>= 0;
88 + function audioSinks() {
89 + return pipewireNodes().filter(isAudioSink);
92 + function audioSources() {
93 + return pipewireNodes().filter(isAudioSource);
96 + function audioStreams() {
97 + return pipewireNodes().filter(isAudioStream);
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) {
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))
129 + if (nodeId(link.source)
=== id)
130 + return nodeId(link.target);
132 + if (nodeId(link.target)
=== id)
133 + return nodeId(link.source);
139 + function nodeActive(node) {
140 + return linkedNodeId(node).length
> 0;
143 + function nodeRow(node, kind) {
144 + const props
= nodeProps(node);
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)
: "" 90 162 function rowsById(rows) {
97 - function sinkObject(id) {
98 - const index
= indexById(sinks, id);
99 - return index
>= 0 ? sinks[index]
: sinks[
0];
102 - function sourceObject(id) {
103 - const index
= indexById(sources, id);
104 - return index
>= 0 ? sources[index]
: sources[
0];
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])) {
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)
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])) {
222 + function cameraActive() {
223 + return nativeCameraActive();
226 + function privacyLabel() {
227 + const mic
= microphoneActive();
228 + const cam
= cameraActive();
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();
253 + "backend":
"Audio.qml",
255 + "label": privacyLabel(),
256 + "role": mic
|| cam
? "warning" : roleForSink(defaultSinkNode()),
260 + "active": mic
|| cam,
261 + "microphoneActive": mic,
262 + "cameraActive": cam,
264 + "kind":
"privacy-veil",
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"]
275 + function privacyWidgetJson() {
276 + return JSON.
stringify(privacyWidgetState());
279 + function streamAverage() {
280 + const streams
= audioStreams();
281 + if (streams.length
<= 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() {
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;
173 352 "backend":
"Audio.qml",
174 353 "usesNativePipewire":
true,
175 354 "pipewireReady": pipewireReady,
176 355 "helperProcesses": [],
357 + "backend":
"Pipewire.links",
358 + "outputActive": outputActive,
359 + "inputActive": inputActive,
360 + "targets": inputActive
? [
"defaultAudioSink",
"defaultAudioSource"]
: [
"defaultAudioSink"],
177 365 "serviceOwnedActions":
true,
178 366 "boundedActions": boundedActions,
179 367 "audioPanelContract": audioPanelContract(),
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)
, 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)
197 - "microphoneActive": source
? !!source.active
: false, 198 - "microphoneMuted": source
? !!source.muted
: true, 199 - "microphoneRole": microphoneRole()
386 + "kind":
"audio-waveform", 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) {
214 - for (
let i
= 0; i
< rows.length; i
++) {
215 - const row
= Object.
assign({
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;
231 - sources
= mutateRows(sources, deviceId, (row)
=> {
232 - return row.volume
= Math.
round(value
* 100)
/ 100;
414 + if (node
&& node.audio)
415 + node.audio.volume
= value;
235 - "kind":
"set-volume",
418 + "kind": node
? "set-volume" : "missing-node",
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;
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;
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");
262 - "kind":
"toggle-mute",
435 + "kind": node
? "toggle-mute" : "missing-node",
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;
314 - "kind":
"set-default-sink",
489 + "kind": node
&& isAudioSink(node)
? "set-default-sink" : "missing-sink",
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;
326 - "kind":
"set-default-source",
501 + "kind": node
&& isAudioSource(node)
? "set-default-source" : "missing-source",
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;
339 - "kind":
"move-stream",
509 + "kind":
"unsupported-native-stream-move",
511 + "target":
String(target)
343 513 return stateJson();
517 + objects: [
Pipewire.defaultAudioSink,
Pipewire.defaultAudioSource]