@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.
@@ -0,0 +1,118 @@
1
+ // Holds `pactl subscribe` open and reads the server again after each burst of
2
+ // changes settles, subscribing again when the subscription ends.
3
+ import { AudioError } from "./audio-error";
4
+ import { C_LOCALE, pactl } from "./pactl";
5
+ import { reading } from "./reading";
6
+ import { announcesAChange } from "./subscription";
7
+ const SUBSCRIBE = ["pactl", "-f", "json", "subscribe"];
8
+ /** See `SoundServer.watch`. */
9
+ export const watchAudio = (domicile, onAudio, timing, report) => {
10
+ const stopping = new AbortController();
11
+ const told = (audio) => {
12
+ if (!stopping.signal.aborted) {
13
+ onAudio(audio);
14
+ }
15
+ };
16
+ keepSubscribed(domicile, told, timing, report, stopping.signal).catch((error) => {
17
+ // biome-ignore lint/suspicious/noConsole: a broken watch has no caller to return to
18
+ console.error("the sound server watch broke", error);
19
+ });
20
+ return () => {
21
+ stopping.abort();
22
+ };
23
+ };
24
+ /** Subscribes until stopped, saying once if there is no sound server. */
25
+ const keepSubscribed = async (domicile, onAudio, timing, report, signal) => {
26
+ let said = false;
27
+ while (!signal.aborted) {
28
+ const ended = await subscribe(domicile, onAudio, timing, report, signal);
29
+ if (!said) {
30
+ said = ended.match({
31
+ Err: (error) => {
32
+ report("no sound server to read; the mixer is empty until there is", error);
33
+ return true;
34
+ },
35
+ Ok: () => false,
36
+ });
37
+ }
38
+ await sleep(timing.reopenMs, signal);
39
+ }
40
+ };
41
+ /**
42
+ * One subscription: reads the server, then again after each settled burst of
43
+ * changes, until `pactl` exits. Fails if the first read does.
44
+ */
45
+ const subscribe = async (domicile, onAudio, timing, report, signal) => (await domicile.processes.spawn(SUBSCRIBE, C_LOCALE))
46
+ .mapErr((error) => AudioError.Refused(error.message))
47
+ .andThenAsync(async (subscription) => {
48
+ const kill = () => {
49
+ subscription.kill();
50
+ };
51
+ signal.addEventListener("abort", kill);
52
+ const settling = {
53
+ reads: Promise.resolve(),
54
+ timer: undefined,
55
+ };
56
+ const changed = () => {
57
+ clearTimeout(settling.timer);
58
+ settling.timer = setTimeout(() => {
59
+ settling.reads = settling.reads.then(async () => {
60
+ (await readServer(domicile)).match({
61
+ Err: (error) => {
62
+ report("the sound server could not be read", error);
63
+ },
64
+ Ok: onAudio,
65
+ });
66
+ });
67
+ }, timing.settleMs);
68
+ };
69
+ // Read after subscribing, so no change in between is missed.
70
+ const first = await readServer(domicile);
71
+ first.match({
72
+ // Returned below, once the subscription ends.
73
+ Err: () => undefined,
74
+ Ok: onAudio,
75
+ });
76
+ await forEachLine(subscription.stdout, (line) => {
77
+ if (announcesAChange(line)) {
78
+ changed();
79
+ }
80
+ });
81
+ await subscription.exited;
82
+ clearTimeout(settling.timer);
83
+ await settling.reads;
84
+ signal.removeEventListener("abort", kill);
85
+ return first.map(() => "ended");
86
+ });
87
+ const readServer = async (domicile) => {
88
+ const info = await pactl(domicile, ["-f", "json", "info"]);
89
+ const list = await pactl(domicile, ["-f", "json", "list"]);
90
+ return info.andThen((server) => list.map((devices) => parsed(server, devices)));
91
+ };
92
+ /** Throws on output that is not `pactl`'s JSON: a format this does not know. */
93
+ const parsed = (info, list) => reading(info, list).match({
94
+ Err: (why) => {
95
+ throw new Error(`unreadable pactl JSON: ${why}`);
96
+ },
97
+ Ok: (audio) => audio,
98
+ });
99
+ const forEachLine = async (stream, onLine) => {
100
+ const decoder = new TextDecoder();
101
+ let carried = "";
102
+ for await (const bytes of stream) {
103
+ const text = decoder.decode(bytes, { stream: true });
104
+ const lines = (carried + text).split("\n");
105
+ carried = lines.slice(-1).join("");
106
+ for (const line of lines.slice(0, -1)) {
107
+ onLine(line);
108
+ }
109
+ }
110
+ };
111
+ /** Resolves after `ms`, or at once when `signal` aborts. */
112
+ const sleep = (ms, signal) => new Promise((resolve) => {
113
+ const timer = setTimeout(resolve, ms);
114
+ signal.addEventListener("abort", () => {
115
+ clearTimeout(timer);
116
+ resolve();
117
+ });
118
+ });
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
+ "dependencies": {
3
+ "@cprussin/option-result": "^2.0.1",
4
+ "@domicile-desktop/react": "0.0.0",
5
+ "@domicile-desktop/sdk": "1.0.0",
6
+ "zod": "^4.5.4"
7
+ },
8
+ "description": "Volume, mute, devices, streams and level meters for a Domicile shell, through pactl and parec.",
9
+ "devDependencies": {
10
+ "@cprussin/tsconfig": "^5.0.0",
11
+ "@domicile-desktop/test-support": "0.0.1",
12
+ "@types/bun": "^1.4.2",
13
+ "@types/react": "^19.2.18",
14
+ "react": "^19.2.8",
15
+ "typescript": "^7.0.2"
16
+ },
17
+ "engines": {
18
+ "bun": ">=1.4.2",
19
+ "node": ">=24"
20
+ },
21
+ "exports": {
22
+ "./asked": {
23
+ "types": "./dist/asked.d.ts",
24
+ "default": "./dist/asked.js"
25
+ },
26
+ "./audio": {
27
+ "types": "./dist/audio.d.ts",
28
+ "default": "./dist/audio.js"
29
+ },
30
+ "./audio-error": {
31
+ "types": "./dist/audio-error.d.ts",
32
+ "default": "./dist/audio-error.js"
33
+ },
34
+ "./hardware": {
35
+ "types": "./dist/hardware.d.ts",
36
+ "default": "./dist/hardware.js"
37
+ },
38
+ "./sound-server": {
39
+ "types": "./dist/sound-server.d.ts",
40
+ "default": "./dist/sound-server.js"
41
+ },
42
+ "./react": {
43
+ "types": "./dist/react.d.ts",
44
+ "default": "./dist/react.js"
45
+ }
46
+ },
47
+ "files": [
48
+ "dist/**"
49
+ ],
50
+ "license": "MIT",
2
51
  "name": "@domicile-desktop/system-audio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
52
+ "peerDependencies": {
53
+ "react": "^19.2.8"
54
+ },
55
+ "private": false,
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "scripts": {
60
+ "build": "tsc -p tsconfig.build.json",
61
+ "test:types": "tsc --noEmit",
62
+ "test:unit": "bun test"
63
+ },
64
+ "type": "module",
65
+ "version": "1.0.0",
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "git+https://github.com/cprussin/domicile.git",
69
+ "directory": "packages/system-audio"
70
+ }
71
+ }