@domicile-desktop/system-audio 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/dist/asked.d.ts +6 -0
- package/dist/asked.js +7 -0
- package/dist/audio-error.d.ts +37 -0
- package/dist/audio-error.js +32 -0
- package/dist/audio.d.ts +79 -0
- package/dist/audio.js +17 -0
- package/dist/hardware.d.ts +9 -0
- package/dist/hardware.js +13 -0
- package/dist/ids.d.ts +17 -0
- package/dist/ids.js +45 -0
- package/dist/meters.d.ts +3 -0
- package/dist/meters.js +120 -0
- package/dist/pactl.d.ts +14 -0
- package/dist/pactl.js +13 -0
- package/dist/peak.d.ts +8 -0
- package/dist/peak.js +16 -0
- package/dist/react.d.ts +20 -0
- package/dist/react.js +23 -0
- package/dist/reading.d.ts +8 -0
- package/dist/reading.js +218 -0
- package/dist/request-queue.d.ts +10 -0
- package/dist/request-queue.js +38 -0
- package/dist/request.d.ts +53 -0
- package/dist/request.js +127 -0
- package/dist/shared-meters.d.ts +6 -0
- package/dist/shared-meters.js +44 -0
- package/dist/sound-server.d.ts +49 -0
- package/dist/sound-server.js +30 -0
- package/dist/subscription.d.ts +5 -0
- package/dist/subscription.js +19 -0
- package/dist/watch-audio.d.ts +4 -0
- package/dist/watch-audio.js +118 -0
- package/package.json +69 -4
package/dist/reading.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Parses `pactl -f json info` and `pactl -f json list` into `Audio`.
|
|
2
|
+
//
|
|
3
|
+
// Run `pactl` under `LC_ALL=C`: it translates some values, such as a port's
|
|
4
|
+
// availability, that this module matches as English words.
|
|
5
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { Meter } from "./audio";
|
|
8
|
+
import { Target, targetId } from "./ids";
|
|
9
|
+
/** The raw volume `pactl` reports for 100%. */
|
|
10
|
+
export const NORMAL = 65_536;
|
|
11
|
+
/** Application id of this library's meters, so they are hidden from the lists. */
|
|
12
|
+
export const METER_APPLICATION = "org.domicile.meter";
|
|
13
|
+
/** Applications whose streams are level meters. Matches pavucontrol's list. */
|
|
14
|
+
const MIXERS = new Set([
|
|
15
|
+
METER_APPLICATION,
|
|
16
|
+
"org.PulseAudio.pavucontrol",
|
|
17
|
+
"org.gnome.VolumeControl",
|
|
18
|
+
"org.kde.kmixd",
|
|
19
|
+
]);
|
|
20
|
+
/** PipeWire property shared by a filter's device and its stream. */
|
|
21
|
+
const LINK_GROUP = "node.link-group";
|
|
22
|
+
/** Parses the output of `pactl -f json info` and `pactl -f json list`. */
|
|
23
|
+
export const reading = (info, list) => {
|
|
24
|
+
const parsedInfo = parseJson(infoSchema, info);
|
|
25
|
+
const parsedList = parseJson(listSchema, list);
|
|
26
|
+
return parsedInfo.andThen((server) => parsedList.map((devices) => audioOf(server, devices)));
|
|
27
|
+
};
|
|
28
|
+
const volumeSchema = z.record(z.string(), z.unknown());
|
|
29
|
+
const propertiesSchema = z.record(z.string(), z.string()).default({});
|
|
30
|
+
const infoSchema = z.object({
|
|
31
|
+
default_sink_name: z.string().nullish(),
|
|
32
|
+
default_source_name: z.string().nullish(),
|
|
33
|
+
});
|
|
34
|
+
const deviceSchema = z.object({
|
|
35
|
+
active_port: z.string().nullish(),
|
|
36
|
+
description: z.string().nullish(),
|
|
37
|
+
index: z.number(),
|
|
38
|
+
/** On a source, the sink it monitors; on a sink, its monitor source. */
|
|
39
|
+
monitor_source: z.string().nullish(),
|
|
40
|
+
mute: z.boolean(),
|
|
41
|
+
name: z.string(),
|
|
42
|
+
ports: z
|
|
43
|
+
.array(z.object({
|
|
44
|
+
availability: z.string(),
|
|
45
|
+
description: z.string(),
|
|
46
|
+
name: z.string(),
|
|
47
|
+
}))
|
|
48
|
+
.default([]),
|
|
49
|
+
properties: propertiesSchema,
|
|
50
|
+
volume: volumeSchema,
|
|
51
|
+
});
|
|
52
|
+
const streamSchema = z.object({
|
|
53
|
+
index: z.number(),
|
|
54
|
+
mute: z.boolean(),
|
|
55
|
+
properties: propertiesSchema,
|
|
56
|
+
/** A sink input's device. */
|
|
57
|
+
sink: z.number().optional(),
|
|
58
|
+
/** A source output's device. */
|
|
59
|
+
source: z.number().optional(),
|
|
60
|
+
volume: volumeSchema,
|
|
61
|
+
});
|
|
62
|
+
const cardSchema = z.object({
|
|
63
|
+
active_profile: z.string().nullish(),
|
|
64
|
+
name: z.string(),
|
|
65
|
+
profiles: z.record(z.string(), z.object({
|
|
66
|
+
available: z.boolean(),
|
|
67
|
+
description: z.string(),
|
|
68
|
+
priority: z.number().default(0),
|
|
69
|
+
})),
|
|
70
|
+
properties: propertiesSchema,
|
|
71
|
+
});
|
|
72
|
+
const listSchema = z.object({
|
|
73
|
+
cards: z.array(cardSchema).default([]),
|
|
74
|
+
sink_inputs: z.array(streamSchema).default([]),
|
|
75
|
+
sinks: z.array(deviceSchema).default([]),
|
|
76
|
+
source_outputs: z.array(streamSchema).default([]),
|
|
77
|
+
sources: z.array(deviceSchema).default([]),
|
|
78
|
+
});
|
|
79
|
+
const parseJson = (schema, text) => {
|
|
80
|
+
const json = z.string().transform(jsonOf).pipe(schema).safeParse(text);
|
|
81
|
+
return json.success ? Ok(json.data) : Err(z.prettifyError(json.error));
|
|
82
|
+
};
|
|
83
|
+
const jsonOf = (text, context) => {
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(text);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
context.addIssue({ code: "custom", input: text, message: "not JSON" });
|
|
89
|
+
return z.NEVER;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const audioOf = (info, list) => {
|
|
93
|
+
const outputsByIndex = new Map(list.sinks.map((sink) => [sink.index, targetId(Target.Output, sink.name)]));
|
|
94
|
+
const inputsByIndex = new Map(list.sources.map((source) => [
|
|
95
|
+
source.index,
|
|
96
|
+
targetId(Target.Input, source.name),
|
|
97
|
+
]));
|
|
98
|
+
const plumbing = new Set([...list.sinks, ...list.sources].flatMap((device) => {
|
|
99
|
+
const group = device.properties[LINK_GROUP];
|
|
100
|
+
return group === undefined ? [] : [group];
|
|
101
|
+
}));
|
|
102
|
+
const listed = (stream) => !isAMixers(stream) && !isPlumbing(stream, plumbing);
|
|
103
|
+
return {
|
|
104
|
+
cards: list.cards.map(card),
|
|
105
|
+
inputs: list.sources.map((source) => device(source, Target.Input, info.default_source_name)),
|
|
106
|
+
meters: meters(list),
|
|
107
|
+
outputs: list.sinks.map((sink) => device(sink, Target.Output, info.default_sink_name)),
|
|
108
|
+
playback: list.sink_inputs
|
|
109
|
+
.filter(listed)
|
|
110
|
+
.map((playing) => stream(playing, Target.Playback, outputsByIndex.get(playing.sink ?? -1))),
|
|
111
|
+
recording: list.source_outputs
|
|
112
|
+
.filter(listed)
|
|
113
|
+
.map((recording) => stream(recording, Target.Recording, inputsByIndex.get(recording.source ?? -1))),
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
/** An output's monitor source (if any), an input itself, or a stream. */
|
|
117
|
+
const meters = (list) => new Map([
|
|
118
|
+
...list.sinks.flatMap((sink) => sink.monitor_source === undefined || sink.monitor_source === null
|
|
119
|
+
? []
|
|
120
|
+
: [
|
|
121
|
+
[
|
|
122
|
+
targetId(Target.Output, sink.name),
|
|
123
|
+
Meter.Source(sink.monitor_source),
|
|
124
|
+
],
|
|
125
|
+
]),
|
|
126
|
+
...list.sources.map((source) => [
|
|
127
|
+
targetId(Target.Input, source.name),
|
|
128
|
+
Meter.Source(source.name),
|
|
129
|
+
]),
|
|
130
|
+
...list.sink_inputs.map((playing) => [
|
|
131
|
+
targetId(Target.Playback, playing.index),
|
|
132
|
+
Meter.Stream(playing.index),
|
|
133
|
+
]),
|
|
134
|
+
]);
|
|
135
|
+
const device = (from, kind, defaultName) => ({
|
|
136
|
+
default: defaultName === from.name,
|
|
137
|
+
description: from.description ?? from.name,
|
|
138
|
+
id: targetId(kind, from.name),
|
|
139
|
+
monitor: kind === Target.Input && isAMonitor(from),
|
|
140
|
+
muted: from.mute,
|
|
141
|
+
port: from.active_port ?? undefined,
|
|
142
|
+
ports: from.ports.map((port) => ({
|
|
143
|
+
available: port.availability !== "not available",
|
|
144
|
+
description: port.description,
|
|
145
|
+
name: port.name,
|
|
146
|
+
})),
|
|
147
|
+
volume: loudest(from.volume),
|
|
148
|
+
});
|
|
149
|
+
/**
|
|
150
|
+
* Whether a source monitors a sink. Uses `device.class` when the server sets
|
|
151
|
+
* it, else whether `monitor_source` names a sink. Some `pactl` versions write
|
|
152
|
+
* "none" there as `""` or `"n/a"`.
|
|
153
|
+
*/
|
|
154
|
+
const isAMonitor = (source) => {
|
|
155
|
+
const deviceClass = source.properties["device.class"];
|
|
156
|
+
return deviceClass === undefined
|
|
157
|
+
? namesASink(source.monitor_source)
|
|
158
|
+
: deviceClass === "monitor";
|
|
159
|
+
};
|
|
160
|
+
const namesASink = (sink) => sink !== undefined && sink !== null && sink !== "" && sink !== "n/a";
|
|
161
|
+
/**
|
|
162
|
+
* Uses the application name with the media name as title. Without an
|
|
163
|
+
* application name, the media name becomes the name and there is no title.
|
|
164
|
+
*/
|
|
165
|
+
const stream = (from, kind, device) => {
|
|
166
|
+
const media = from.properties["media.name"];
|
|
167
|
+
const application = from.properties["application.name"];
|
|
168
|
+
return {
|
|
169
|
+
application: application ?? media ?? "Unknown",
|
|
170
|
+
device,
|
|
171
|
+
id: targetId(kind, from.index),
|
|
172
|
+
muted: from.mute,
|
|
173
|
+
title: application === undefined ? undefined : media,
|
|
174
|
+
volume: loudest(from.volume),
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
const isAMixers = (stream) => {
|
|
178
|
+
const application = stream.properties["application.id"];
|
|
179
|
+
return application !== undefined && MIXERS.has(application);
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Whether a stream carries a filter device's audio to the hardware, such as a
|
|
183
|
+
* PipeWire filter-chain for speaker correction. It shares the device's link
|
|
184
|
+
* group.
|
|
185
|
+
*/
|
|
186
|
+
const isPlumbing = (stream, plumbing) => {
|
|
187
|
+
const group = stream.properties[LINK_GROUP];
|
|
188
|
+
return group !== undefined && plumbing.has(group);
|
|
189
|
+
};
|
|
190
|
+
const card = (from) => ({
|
|
191
|
+
description: from.properties["device.description"] ?? from.name,
|
|
192
|
+
id: from.name,
|
|
193
|
+
profile: from.active_profile ?? undefined,
|
|
194
|
+
profiles: Object.entries(from.profiles)
|
|
195
|
+
.toSorted(([aName, a], [bName, b]) => b.priority - a.priority || byCodeUnits(aName, bName))
|
|
196
|
+
.map(([name, profile]) => ({
|
|
197
|
+
available: profile.available,
|
|
198
|
+
description: profile.description,
|
|
199
|
+
name,
|
|
200
|
+
})),
|
|
201
|
+
});
|
|
202
|
+
/**
|
|
203
|
+
* The loudest channel as a fraction of 100%. An invalid volume, reported as
|
|
204
|
+
* `{"error": ...}`, reads as 0.
|
|
205
|
+
*/
|
|
206
|
+
const loudest = (volume) => Math.max(0, ...Object.values(volume).flatMap((channel) => {
|
|
207
|
+
const parsed = channelSchema.safeParse(channel);
|
|
208
|
+
return parsed.success ? [parsed.data.value / NORMAL] : [];
|
|
209
|
+
}));
|
|
210
|
+
const channelSchema = z.object({ value: z.number() });
|
|
211
|
+
const byCodeUnits = (a, b) => {
|
|
212
|
+
if (a < b) {
|
|
213
|
+
return -1;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
return a > b ? 1 : 0;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import { Asked } from "./asked";
|
|
3
|
+
import type { AudioError } from "./audio-error";
|
|
4
|
+
import type { Request } from "./request";
|
|
5
|
+
import type { AudioDesktop } from "./sound-server";
|
|
6
|
+
/**
|
|
7
|
+
* Runs requests one at a time, in order. Requests that wait while another
|
|
8
|
+
* runs are coalesced: see {@link coalesce}.
|
|
9
|
+
*/
|
|
10
|
+
export declare const requestQueue: (domicile: AudioDesktop) => (request: Request) => Promise<Result<Asked, AudioError>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
2
|
+
import { Asked } from "./asked";
|
|
3
|
+
import { pactl } from "./pactl";
|
|
4
|
+
import { argv, coalesce } from "./request";
|
|
5
|
+
/**
|
|
6
|
+
* Runs requests one at a time, in order. Requests that wait while another
|
|
7
|
+
* runs are coalesced: see {@link coalesce}.
|
|
8
|
+
*/
|
|
9
|
+
export const requestQueue = (domicile) => {
|
|
10
|
+
const waiting = [];
|
|
11
|
+
const state = { running: false };
|
|
12
|
+
return (request) => argv(request).match({
|
|
13
|
+
Err: (error) => Promise.resolve(Err(error)),
|
|
14
|
+
Ok: (args) => new Promise((answer) => {
|
|
15
|
+
waiting.push({ answer, args, request });
|
|
16
|
+
if (!state.running) {
|
|
17
|
+
state.running = true;
|
|
18
|
+
drain(domicile, waiting, state).catch((error) => {
|
|
19
|
+
// biome-ignore lint/suspicious/noConsole: no caller is waiting on the queue itself
|
|
20
|
+
console.error("the mixer's request queue broke", error);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
/** Runs what is waiting, then marks the queue idle in the same turn. */
|
|
27
|
+
const drain = async (domicile, waiting, state) => {
|
|
28
|
+
while (waiting.length > 0) {
|
|
29
|
+
const batch = waiting.splice(0);
|
|
30
|
+
const kept = coalesce(batch.map(({ request }) => request));
|
|
31
|
+
for (const { answer, args, request } of batch) {
|
|
32
|
+
answer(kept.includes(request)
|
|
33
|
+
? (await pactl(domicile, args)).map(() => Asked.Done)
|
|
34
|
+
: Ok(Asked.Overtaken));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
state.running = false;
|
|
38
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import { AudioError } from "./audio-error";
|
|
3
|
+
export declare enum RequestKind {
|
|
4
|
+
Volume = 0,
|
|
5
|
+
Muted = 1,
|
|
6
|
+
Default = 2,
|
|
7
|
+
Move = 3,
|
|
8
|
+
Port = 4,
|
|
9
|
+
Profile = 5
|
|
10
|
+
}
|
|
11
|
+
export declare const Request: {
|
|
12
|
+
Default: (id: string) => {
|
|
13
|
+
id: string;
|
|
14
|
+
kind: RequestKind.Default;
|
|
15
|
+
};
|
|
16
|
+
Move: (id: string, device: string) => {
|
|
17
|
+
device: string;
|
|
18
|
+
id: string;
|
|
19
|
+
kind: RequestKind.Move;
|
|
20
|
+
};
|
|
21
|
+
Muted: (id: string, muted: boolean) => {
|
|
22
|
+
id: string;
|
|
23
|
+
kind: RequestKind.Muted;
|
|
24
|
+
muted: boolean;
|
|
25
|
+
};
|
|
26
|
+
Port: (id: string, port: string) => {
|
|
27
|
+
id: string;
|
|
28
|
+
kind: RequestKind.Port;
|
|
29
|
+
port: string;
|
|
30
|
+
};
|
|
31
|
+
Profile: (card: string, profile: string) => {
|
|
32
|
+
card: string;
|
|
33
|
+
kind: RequestKind.Profile;
|
|
34
|
+
profile: string;
|
|
35
|
+
};
|
|
36
|
+
Volume: (id: string, volume: number) => {
|
|
37
|
+
id: string;
|
|
38
|
+
kind: RequestKind.Volume;
|
|
39
|
+
volume: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export type Request = ReturnType<(typeof Request)[keyof typeof Request]>;
|
|
43
|
+
/**
|
|
44
|
+
* `pactl` arguments for `request`. They start with `--` so a name cannot be
|
|
45
|
+
* read as an option.
|
|
46
|
+
*/
|
|
47
|
+
export declare const argv: (request: Request) => Result<string[], AudioError>;
|
|
48
|
+
/**
|
|
49
|
+
* Drops each volume request whose next request for the same target is also a
|
|
50
|
+
* volume. A dragged slider sends many requests a second; only the last
|
|
51
|
+
* matters.
|
|
52
|
+
*/
|
|
53
|
+
export declare const coalesce: (requests: readonly Request[]) => Request[];
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// What a mixer asks the sound server, and the `pactl` arguments for it.
|
|
2
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
3
|
+
import { AudioError } from "./audio-error";
|
|
4
|
+
import { parseId, Target } from "./ids";
|
|
5
|
+
import { NORMAL } from "./reading";
|
|
6
|
+
/** The highest volume a mixer may set: 150%, matching pavucontrol. */
|
|
7
|
+
const CEILING = 1.5;
|
|
8
|
+
export var RequestKind;
|
|
9
|
+
(function (RequestKind) {
|
|
10
|
+
RequestKind[RequestKind["Volume"] = 0] = "Volume";
|
|
11
|
+
RequestKind[RequestKind["Muted"] = 1] = "Muted";
|
|
12
|
+
RequestKind[RequestKind["Default"] = 2] = "Default";
|
|
13
|
+
RequestKind[RequestKind["Move"] = 3] = "Move";
|
|
14
|
+
RequestKind[RequestKind["Port"] = 4] = "Port";
|
|
15
|
+
RequestKind[RequestKind["Profile"] = 5] = "Profile";
|
|
16
|
+
})(RequestKind || (RequestKind = {}));
|
|
17
|
+
export const Request = {
|
|
18
|
+
Default: (id) => ({ id, kind: RequestKind.Default }),
|
|
19
|
+
Move: (id, device) => ({
|
|
20
|
+
device,
|
|
21
|
+
id,
|
|
22
|
+
kind: RequestKind.Move,
|
|
23
|
+
}),
|
|
24
|
+
Muted: (id, muted) => ({
|
|
25
|
+
id,
|
|
26
|
+
kind: RequestKind.Muted,
|
|
27
|
+
muted,
|
|
28
|
+
}),
|
|
29
|
+
Port: (id, port) => ({
|
|
30
|
+
id,
|
|
31
|
+
kind: RequestKind.Port,
|
|
32
|
+
port,
|
|
33
|
+
}),
|
|
34
|
+
Profile: (card, profile) => ({
|
|
35
|
+
card,
|
|
36
|
+
kind: RequestKind.Profile,
|
|
37
|
+
profile,
|
|
38
|
+
}),
|
|
39
|
+
Volume: (id, volume) => ({
|
|
40
|
+
id,
|
|
41
|
+
kind: RequestKind.Volume,
|
|
42
|
+
volume,
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* `pactl` arguments for `request`. They start with `--` so a name cannot be
|
|
47
|
+
* read as an option.
|
|
48
|
+
*/
|
|
49
|
+
export const argv = (request) => words(request).map((args) => ["--", ...args]);
|
|
50
|
+
/**
|
|
51
|
+
* Drops each volume request whose next request for the same target is also a
|
|
52
|
+
* volume. A dragged slider sends many requests a second; only the last
|
|
53
|
+
* matters.
|
|
54
|
+
*/
|
|
55
|
+
export const coalesce = (requests) => requests.filter((request, at) => {
|
|
56
|
+
const next = requests
|
|
57
|
+
.slice(at + 1)
|
|
58
|
+
.find((later) => subject(later) === subject(request));
|
|
59
|
+
return !(request.kind === RequestKind.Volume && next?.kind === RequestKind.Volume);
|
|
60
|
+
});
|
|
61
|
+
const words = (request) => {
|
|
62
|
+
switch (request.kind) {
|
|
63
|
+
case RequestKind.Volume:
|
|
64
|
+
return rawVolume(request.volume).andThen((raw) => parseId(request.id).map(({ key, target }) => [
|
|
65
|
+
`set-${noun(target)}-volume`,
|
|
66
|
+
key,
|
|
67
|
+
raw.toString(),
|
|
68
|
+
]));
|
|
69
|
+
case RequestKind.Muted:
|
|
70
|
+
return parseId(request.id).map(({ key, target }) => [
|
|
71
|
+
`set-${noun(target)}-mute`,
|
|
72
|
+
key,
|
|
73
|
+
request.muted ? "1" : "0",
|
|
74
|
+
]);
|
|
75
|
+
case RequestKind.Default:
|
|
76
|
+
return device(request.id).map(({ key, target }) => [
|
|
77
|
+
`set-default-${noun(target)}`,
|
|
78
|
+
key,
|
|
79
|
+
]);
|
|
80
|
+
case RequestKind.Move:
|
|
81
|
+
return parseId(request.id).andThen((stream) => device(request.device).andThen((to) => moveVerb(stream.target, to.target).match({
|
|
82
|
+
Err: () => Err(AudioError.Mismatched(request.id, request.device)),
|
|
83
|
+
Ok: (verb) => Ok([verb, stream.key, to.key]),
|
|
84
|
+
})));
|
|
85
|
+
case RequestKind.Port:
|
|
86
|
+
return device(request.id).map(({ key, target }) => [
|
|
87
|
+
`set-${noun(target)}-port`,
|
|
88
|
+
key,
|
|
89
|
+
request.port,
|
|
90
|
+
]);
|
|
91
|
+
case RequestKind.Profile:
|
|
92
|
+
return Ok(["set-card-profile", request.card, request.profile]);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
/** The id or card a request is about, for {@link coalesce}. */
|
|
96
|
+
const subject = (request) => request.kind === RequestKind.Profile ? request.card : request.id;
|
|
97
|
+
const rawVolume = (volume) => Number.isFinite(volume)
|
|
98
|
+
? Ok(Math.round(Math.min(Math.max(volume, 0), CEILING) * NORMAL))
|
|
99
|
+
: Err(AudioError.NotANumber());
|
|
100
|
+
/** Parses an id that must name a device. */
|
|
101
|
+
const device = (id) => parseId(id).andThen((parsed) => parsed.target === Target.Output || parsed.target === Target.Input
|
|
102
|
+
? Ok(parsed)
|
|
103
|
+
: Err(AudioError.NotADevice(id)));
|
|
104
|
+
const moveVerb = (stream, device) => {
|
|
105
|
+
if (stream === Target.Playback && device === Target.Output) {
|
|
106
|
+
return Ok("move-sink-input");
|
|
107
|
+
}
|
|
108
|
+
else if (stream === Target.Recording && device === Target.Input) {
|
|
109
|
+
return Ok("move-source-output");
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
return Err("mismatched");
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/** The `pactl` name for a target. */
|
|
116
|
+
const noun = (target) => {
|
|
117
|
+
switch (target) {
|
|
118
|
+
case Target.Output:
|
|
119
|
+
return "sink";
|
|
120
|
+
case Target.Input:
|
|
121
|
+
return "source";
|
|
122
|
+
case Target.Playback:
|
|
123
|
+
return "sink-input";
|
|
124
|
+
case Target.Recording:
|
|
125
|
+
return "source-output";
|
|
126
|
+
}
|
|
127
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SoundServer } from "./sound-server";
|
|
2
|
+
/**
|
|
3
|
+
* Shares `meters` among every open mixer, so a device shown in two mixers is
|
|
4
|
+
* recorded once. The meters run while any mixer is open.
|
|
5
|
+
*/
|
|
6
|
+
export declare const sharedMeters: (meters: SoundServer["meters"]) => SoundServer["meters"];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shares `meters` among every open mixer, so a device shown in two mixers is
|
|
3
|
+
* recorded once. The meters run while any mixer is open.
|
|
4
|
+
*/
|
|
5
|
+
export const sharedMeters = (meters) => {
|
|
6
|
+
const mixers = new Set();
|
|
7
|
+
const state = { running: undefined };
|
|
8
|
+
const sync = () => {
|
|
9
|
+
if (mixers.size === 0) {
|
|
10
|
+
state.running?.stop();
|
|
11
|
+
state.running = undefined;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
state.running ??= meters((levels) => {
|
|
15
|
+
tell(mixers, levels);
|
|
16
|
+
});
|
|
17
|
+
state.running.meter(new Map([...mixers].flatMap(({ wanted }) => [...wanted])));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
return (onLevels) => {
|
|
21
|
+
const mixer = { onLevels, wanted: new Map() };
|
|
22
|
+
mixers.add(mixer);
|
|
23
|
+
sync();
|
|
24
|
+
return {
|
|
25
|
+
meter: (wanted) => {
|
|
26
|
+
mixer.wanted = wanted;
|
|
27
|
+
sync();
|
|
28
|
+
},
|
|
29
|
+
stop: () => {
|
|
30
|
+
mixers.delete(mixer);
|
|
31
|
+
sync();
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/** Hands each mixer the levels of the ids it meters, if any. */
|
|
37
|
+
const tell = (mixers, levels) => {
|
|
38
|
+
for (const { onLevels, wanted } of mixers) {
|
|
39
|
+
const its = new Map([...levels].filter(([id]) => wanted.has(id)));
|
|
40
|
+
if (its.size > 0) {
|
|
41
|
+
onLevels(its);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { Processes } from "@domicile-desktop/sdk/desktop/processes";
|
|
3
|
+
import type { Asked } from "./asked";
|
|
4
|
+
import type { Audio, Meter } from "./audio";
|
|
5
|
+
import type { AudioError } from "./audio-error";
|
|
6
|
+
/** The calls this library makes. */
|
|
7
|
+
export type AudioDesktop = {
|
|
8
|
+
readonly processes: Pick<Processes, "run" | "spawn">;
|
|
9
|
+
};
|
|
10
|
+
/** Each meter's loudest sample since the last report, 0 through 1, by id. */
|
|
11
|
+
export type Levels = ReadonlyMap<string, number>;
|
|
12
|
+
/** Running meters. Metering a microphone records it: stop when not shown. */
|
|
13
|
+
export type Meters = {
|
|
14
|
+
/** Meter these ids from these sources, and stop metering the rest. */
|
|
15
|
+
meter: (wanted: ReadonlyMap<string, Meter>) => void;
|
|
16
|
+
stop: () => void;
|
|
17
|
+
};
|
|
18
|
+
/** Ids come from {@link SoundServer.watch}; volumes are fractions of 100%. */
|
|
19
|
+
export type SoundServer = {
|
|
20
|
+
/**
|
|
21
|
+
* Calls `onAudio` with the server's state and again after each change.
|
|
22
|
+
* Never calls it without a sound server. Returns a stop function.
|
|
23
|
+
*/
|
|
24
|
+
watch: (onAudio: (audio: Audio) => void) => () => void;
|
|
25
|
+
/** Kept between 0 and 1.5, pavucontrol's maximum. */
|
|
26
|
+
setVolume: (id: string, volume: number) => Promise<Result<Asked, AudioError>>;
|
|
27
|
+
setMuted: (id: string, muted: boolean) => Promise<Result<Asked, AudioError>>;
|
|
28
|
+
/** Make a device the default for new streams. */
|
|
29
|
+
setDefault: (id: string) => Promise<Result<Asked, AudioError>>;
|
|
30
|
+
/** Move a stream to a device of its own direction. */
|
|
31
|
+
moveStream: (id: string, device: string) => Promise<Result<Asked, AudioError>>;
|
|
32
|
+
setPort: (id: string, port: string) => Promise<Result<Asked, AudioError>>;
|
|
33
|
+
setProfile: (card: string, profile: string) => Promise<Result<Asked, AudioError>>;
|
|
34
|
+
/** Level meters, reported to `onLevels` while any is running. */
|
|
35
|
+
meters: (onLevels: (levels: Levels) => void) => Meters;
|
|
36
|
+
};
|
|
37
|
+
/** How long to wait; shortened by tests. */
|
|
38
|
+
export type Timing = {
|
|
39
|
+
/** Quiet after a change before the server is read again. */
|
|
40
|
+
settleMs: number;
|
|
41
|
+
/** Delay before subscribing again after a subscription ends. */
|
|
42
|
+
reopenMs: number;
|
|
43
|
+
/** Interval between meter reports. */
|
|
44
|
+
tickMs: number;
|
|
45
|
+
};
|
|
46
|
+
/** Says that something keeps failing in the background. */
|
|
47
|
+
export type Report = (message: string, detail: unknown) => void;
|
|
48
|
+
/** The sound server, reached through `domicile`'s processes. */
|
|
49
|
+
export declare const soundServer: (domicile: AudioDesktop, timing?: Timing, report?: Report) => SoundServer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// The sound server (PulseAudio or PipeWire) for a shell's mixer, through
|
|
2
|
+
// `pactl` and `parec` run by `@domicile-desktop/sdk/system`.
|
|
3
|
+
//
|
|
4
|
+
// - `pactl` and `parec` work with both PulseAudio and `pipewire-pulse`. The
|
|
5
|
+
// compositor's `PATH` must have them.
|
|
6
|
+
// - Requests run one at a time, in order, so the last volume a slider sent is
|
|
7
|
+
// the one the server keeps.
|
|
8
|
+
import { meters } from "./meters";
|
|
9
|
+
import { Request } from "./request";
|
|
10
|
+
import { requestQueue } from "./request-queue";
|
|
11
|
+
import { watchAudio } from "./watch-audio";
|
|
12
|
+
const TIMING = { reopenMs: 5000, settleMs: 30, tickMs: 50 };
|
|
13
|
+
/** The sound server, reached through `domicile`'s processes. */
|
|
14
|
+
export const soundServer = (domicile, timing = TIMING, report = warn) => {
|
|
15
|
+
const ask = requestQueue(domicile);
|
|
16
|
+
return {
|
|
17
|
+
meters: (onLevels) => meters(domicile, onLevels, timing.tickMs, report),
|
|
18
|
+
moveStream: (id, device) => ask(Request.Move(id, device)),
|
|
19
|
+
setDefault: (id) => ask(Request.Default(id)),
|
|
20
|
+
setMuted: (id, muted) => ask(Request.Muted(id, muted)),
|
|
21
|
+
setPort: (id, port) => ask(Request.Port(id, port)),
|
|
22
|
+
setProfile: (card, profile) => ask(Request.Profile(card, profile)),
|
|
23
|
+
setVolume: (id, volume) => ask(Request.Volume(id, volume)),
|
|
24
|
+
watch: (onAudio) => watchAudio(domicile, onAudio, timing, report),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const warn = (message, detail) => {
|
|
28
|
+
// biome-ignore lint/suspicious/noConsole: background failures have no caller to return to
|
|
29
|
+
console.warn(message, detail);
|
|
30
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* `pactl subscribe` facilities that change mixer state. Client events are
|
|
4
|
+
* left out: `pactl list` is itself a client, so rereading on them would loop.
|
|
5
|
+
*/
|
|
6
|
+
const NEWS = new Set([
|
|
7
|
+
"sink",
|
|
8
|
+
"source",
|
|
9
|
+
"sink-input",
|
|
10
|
+
"source-output",
|
|
11
|
+
"card",
|
|
12
|
+
"server",
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* Whether a `pactl -f json subscribe` line means mixer state changed. Throws
|
|
16
|
+
* on a line that is not an event.
|
|
17
|
+
*/
|
|
18
|
+
export const announcesAChange = (line) => NEWS.has(eventSchema.parse(JSON.parse(line)).on);
|
|
19
|
+
const eventSchema = z.object({ on: z.string() });
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Audio } from "./audio";
|
|
2
|
+
import type { AudioDesktop, Report, Timing } from "./sound-server";
|
|
3
|
+
/** See `SoundServer.watch`. */
|
|
4
|
+
export declare const watchAudio: (domicile: AudioDesktop, onAudio: (audio: Audio) => void, timing: Timing, report: Report) => (() => void);
|