@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Connor Prussin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @domicile-desktop/system-audio
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A shell's mixer: devices, streams, cards, volume, mute and level meters, read
|
|
4
|
+
from PulseAudio or PipeWire through `pactl` and `parec`.
|
|
5
|
+
|
|
6
|
+
- Runs them with `domicile.processes`' `spawn` and `run`, in the C
|
|
7
|
+
locale. They must be on the compositor's `PATH`; the flake's wrapper adds
|
|
8
|
+
PulseAudio's.
|
|
9
|
+
- Published to npm. Ships built JavaScript and `.d.ts` from `dist/`.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { soundServer } from "@domicile-desktop/system-audio/sound-server";
|
|
15
|
+
|
|
16
|
+
const server = soundServer(domicile);
|
|
17
|
+
const stop = server.watch((audio) => draw(audio));
|
|
18
|
+
await server.setVolume(audio.outputs[0].id, 0.5);
|
|
19
|
+
const meters = server.meters((levels) => drawLevels(levels));
|
|
20
|
+
meters.meter(new Map([[id, audio.meters.get(id)]]));
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `watch` holds `pactl -f json subscribe` open and reports the whole state on
|
|
24
|
+
each settled burst of changes. Without a sound server it reports nothing,
|
|
25
|
+
logs once and retries every 5 s.
|
|
26
|
+
- Requests resolve `Result<Asked, AudioError>`. They run one at a time; a
|
|
27
|
+
volume overtaken by a later one for the same id is skipped
|
|
28
|
+
(`Asked.Overtaken`).
|
|
29
|
+
- Volumes are kept between 0 and 1.5 (pavucontrol's maximum); NaN is refused.
|
|
30
|
+
Ids not of the form `watch` reports are refused before `pactl` runs.
|
|
31
|
+
- Meters run one `parec` per id and report peaks 20 times a second. Metering a
|
|
32
|
+
microphone records it: call `stop` when the meters are not shown.
|
|
33
|
+
- The library's own meters, other mixers' meters and PipeWire filter streams
|
|
34
|
+
are left out of the stream lists.
|
|
35
|
+
- While the desktop is locked, only `watch`, and `setVolume` and `setMuted` on
|
|
36
|
+
an output, work ([LOCK.md](/docs/LOCK.md)). Everything else fails with
|
|
37
|
+
`locked`.
|
|
38
|
+
|
|
39
|
+
## Modules
|
|
40
|
+
|
|
41
|
+
| Module | What it is |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| `./sound-server` | `soundServer(domicile)` and its types. |
|
|
44
|
+
| `./audio` | `Audio` and its devices, streams, cards and `Meter`s. |
|
|
45
|
+
| `./audio-error` | `AudioError`: why a request was refused. |
|
|
46
|
+
| `./asked` | `Asked`: what became of a request. |
|
|
47
|
+
|
|
48
|
+
## React
|
|
49
|
+
|
|
50
|
+
`@domicile-desktop/system-audio/react`, for a shell on
|
|
51
|
+
[`@domicile-desktop/react`](../react/README.md):
|
|
52
|
+
|
|
53
|
+
- `useAudio()`: the devices and streams, `undefined` until `pactl` answers.
|
|
54
|
+
- `useHasSoundCard()`: whether `/sys` lists a sound card, looked at every
|
|
55
|
+
10s.
|
|
56
|
+
- `deskSound(domicile)`: the shared `audio` watch and the `sound` requests,
|
|
57
|
+
one per desktop. Its `meters` are shared among open mixers by id
|
|
58
|
+
(`sharedMeters`), so a device shown twice is recorded once.
|
|
59
|
+
- `deskSoundHardware(domicile)`: the watch behind `useHasSoundCard`.
|
|
60
|
+
|
|
61
|
+
## Test
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
bun run turbo test --filter @domicile-desktop/system-audio
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Unit tests read recorded `pactl -f json` output (`src/pactl.fixture.ts`)
|
|
68
|
+
through fake `processes` (`src/system.fixture.ts`).
|
package/dist/asked.d.ts
ADDED
package/dist/asked.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare enum AudioErrorKind {
|
|
2
|
+
/** A volume that is NaN or infinite. */
|
|
3
|
+
NotANumber = 0,
|
|
4
|
+
/** An id that must name a device and names a stream. */
|
|
5
|
+
NotADevice = 1,
|
|
6
|
+
/** A stream moved to a device of the other direction. */
|
|
7
|
+
Mismatched = 2,
|
|
8
|
+
/** An id `watchAudio` never reported. */
|
|
9
|
+
UnknownId = 3,
|
|
10
|
+
/** `pactl` could not run, or the sound server refused. */
|
|
11
|
+
Refused = 4
|
|
12
|
+
}
|
|
13
|
+
/** Why the sound server was not asked, or said no. */
|
|
14
|
+
export declare const AudioError: {
|
|
15
|
+
Mismatched: (stream: string, device: string) => {
|
|
16
|
+
device: string;
|
|
17
|
+
kind: AudioErrorKind.Mismatched;
|
|
18
|
+
stream: string;
|
|
19
|
+
};
|
|
20
|
+
NotADevice: (id: string) => {
|
|
21
|
+
id: string;
|
|
22
|
+
kind: AudioErrorKind.NotADevice;
|
|
23
|
+
};
|
|
24
|
+
NotANumber: () => {
|
|
25
|
+
kind: AudioErrorKind.NotANumber;
|
|
26
|
+
};
|
|
27
|
+
/** `message` is `pactl`'s error output, or why it did not run. */
|
|
28
|
+
Refused: (message: string) => {
|
|
29
|
+
kind: AudioErrorKind.Refused;
|
|
30
|
+
message: string;
|
|
31
|
+
};
|
|
32
|
+
UnknownId: (id: string) => {
|
|
33
|
+
id: string;
|
|
34
|
+
kind: AudioErrorKind.UnknownId;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export type AudioError = ReturnType<(typeof AudioError)[keyof typeof AudioError]>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export var AudioErrorKind;
|
|
2
|
+
(function (AudioErrorKind) {
|
|
3
|
+
/** A volume that is NaN or infinite. */
|
|
4
|
+
AudioErrorKind[AudioErrorKind["NotANumber"] = 0] = "NotANumber";
|
|
5
|
+
/** An id that must name a device and names a stream. */
|
|
6
|
+
AudioErrorKind[AudioErrorKind["NotADevice"] = 1] = "NotADevice";
|
|
7
|
+
/** A stream moved to a device of the other direction. */
|
|
8
|
+
AudioErrorKind[AudioErrorKind["Mismatched"] = 2] = "Mismatched";
|
|
9
|
+
/** An id `watchAudio` never reported. */
|
|
10
|
+
AudioErrorKind[AudioErrorKind["UnknownId"] = 3] = "UnknownId";
|
|
11
|
+
/** `pactl` could not run, or the sound server refused. */
|
|
12
|
+
AudioErrorKind[AudioErrorKind["Refused"] = 4] = "Refused";
|
|
13
|
+
})(AudioErrorKind || (AudioErrorKind = {}));
|
|
14
|
+
/** Why the sound server was not asked, or said no. */
|
|
15
|
+
export const AudioError = {
|
|
16
|
+
Mismatched: (stream, device) => ({
|
|
17
|
+
device,
|
|
18
|
+
kind: AudioErrorKind.Mismatched,
|
|
19
|
+
stream,
|
|
20
|
+
}),
|
|
21
|
+
NotADevice: (id) => ({
|
|
22
|
+
id,
|
|
23
|
+
kind: AudioErrorKind.NotADevice,
|
|
24
|
+
}),
|
|
25
|
+
NotANumber: () => ({ kind: AudioErrorKind.NotANumber }),
|
|
26
|
+
/** `message` is `pactl`'s error output, or why it did not run. */
|
|
27
|
+
Refused: (message) => ({
|
|
28
|
+
kind: AudioErrorKind.Refused,
|
|
29
|
+
message,
|
|
30
|
+
}),
|
|
31
|
+
UnknownId: (id) => ({ id, kind: AudioErrorKind.UnknownId }),
|
|
32
|
+
};
|
package/dist/audio.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** A port of a device, or a profile of a card: something to switch it to. */
|
|
2
|
+
export type AudioChoice = {
|
|
3
|
+
/** What `setPort` and `setProfile` name it by. */
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
/**
|
|
7
|
+
* `false` for a port with an empty jack, or a profile that needs one. It can
|
|
8
|
+
* still be selected.
|
|
9
|
+
*/
|
|
10
|
+
available: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** An output (a sink) or an input (a source). */
|
|
13
|
+
export type AudioDevice = {
|
|
14
|
+
id: string;
|
|
15
|
+
description: string;
|
|
16
|
+
/** The loudest channel's volume as a fraction of 100%. Can exceed 1. */
|
|
17
|
+
volume: number;
|
|
18
|
+
muted: boolean;
|
|
19
|
+
/** Whether new streams go to it. */
|
|
20
|
+
default: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether it is an output's monitor source, which records what the output
|
|
23
|
+
* plays. Always `false` for an output.
|
|
24
|
+
*/
|
|
25
|
+
monitor: boolean;
|
|
26
|
+
/** E.g. speakers, headphones, line in. Often empty. */
|
|
27
|
+
ports: readonly AudioChoice[];
|
|
28
|
+
/** The {@link AudioChoice.name} of the port in use, if it has ports. */
|
|
29
|
+
port: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
/** Something playing (a sink input) or recording (a source output). */
|
|
32
|
+
export type AudioStream = {
|
|
33
|
+
id: string;
|
|
34
|
+
/** The application playing or recording it. */
|
|
35
|
+
application: string;
|
|
36
|
+
/** The stream title, if the application set one. */
|
|
37
|
+
title: string | undefined;
|
|
38
|
+
volume: number;
|
|
39
|
+
muted: boolean;
|
|
40
|
+
/** The {@link AudioDevice.id} it plays to or records from, if listed. */
|
|
41
|
+
device: string | undefined;
|
|
42
|
+
};
|
|
43
|
+
/** A sound card and its profiles, which select the enabled devices. */
|
|
44
|
+
export type AudioCard = {
|
|
45
|
+
id: string;
|
|
46
|
+
description: string;
|
|
47
|
+
/** Sorted best first. */
|
|
48
|
+
profiles: readonly AudioChoice[];
|
|
49
|
+
/** The {@link AudioChoice.name} of the profile in use. */
|
|
50
|
+
profile: string | undefined;
|
|
51
|
+
};
|
|
52
|
+
export declare enum MeterKind {
|
|
53
|
+
/** A source by name: an input, or an output's monitor. */
|
|
54
|
+
Source = 0,
|
|
55
|
+
/** A playback stream by index, as pavucontrol meters one. */
|
|
56
|
+
Stream = 1
|
|
57
|
+
}
|
|
58
|
+
/** Where a level meter records from. */
|
|
59
|
+
export declare const Meter: {
|
|
60
|
+
Source: (name: string) => {
|
|
61
|
+
kind: MeterKind.Source;
|
|
62
|
+
name: string;
|
|
63
|
+
};
|
|
64
|
+
Stream: (index: number) => {
|
|
65
|
+
index: number;
|
|
66
|
+
kind: MeterKind.Stream;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export type Meter = ReturnType<(typeof Meter)[keyof typeof Meter]>;
|
|
70
|
+
/** Every output, input, stream and card. */
|
|
71
|
+
export type Audio = {
|
|
72
|
+
outputs: readonly AudioDevice[];
|
|
73
|
+
inputs: readonly AudioDevice[];
|
|
74
|
+
playback: readonly AudioStream[];
|
|
75
|
+
recording: readonly AudioStream[];
|
|
76
|
+
cards: readonly AudioCard[];
|
|
77
|
+
/** Where each meterable device or stream id is metered from. */
|
|
78
|
+
meters: ReadonlyMap<string, Meter>;
|
|
79
|
+
};
|
package/dist/audio.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The sound server's state, as `watchAudio` reports it.
|
|
2
|
+
//
|
|
3
|
+
// Ids are opaque. A device id is `output:` or `input:` plus the device name,
|
|
4
|
+
// which survives a server restart. A stream id is `playback:` or `recording:`
|
|
5
|
+
// plus the stream index, which lasts as long as the stream.
|
|
6
|
+
export var MeterKind;
|
|
7
|
+
(function (MeterKind) {
|
|
8
|
+
/** A source by name: an input, or an output's monitor. */
|
|
9
|
+
MeterKind[MeterKind["Source"] = 0] = "Source";
|
|
10
|
+
/** A playback stream by index, as pavucontrol meters one. */
|
|
11
|
+
MeterKind[MeterKind["Stream"] = 1] = "Stream";
|
|
12
|
+
})(MeterKind || (MeterKind = {}));
|
|
13
|
+
/** Where a level meter records from. */
|
|
14
|
+
export const Meter = {
|
|
15
|
+
Source: (name) => ({ kind: MeterKind.Source, name }),
|
|
16
|
+
Stream: (index) => ({ index, kind: MeterKind.Stream }),
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { Files } from "@domicile-desktop/sdk/desktop/files";
|
|
3
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
4
|
+
/** The calls the check makes. Each works while the desktop is locked. */
|
|
5
|
+
export type SysfsDesktop = {
|
|
6
|
+
readonly files: Pick<Files, "readDir" | "readText">;
|
|
7
|
+
};
|
|
8
|
+
/** Whether ALSA has a sound card. */
|
|
9
|
+
export declare const hasSoundCard: (domicile: SysfsDesktop) => Promise<Result<boolean, SystemError>>;
|
package/dist/hardware.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Whether the machine has sound hardware, from /sys, whether or not a sound
|
|
2
|
+
// server runs.
|
|
3
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
4
|
+
import { SystemErrorKind } from "@domicile-desktop/sdk/system";
|
|
5
|
+
/** ALSA's cards, `card0` and on; the class's other entries are their devices and the timer. */
|
|
6
|
+
const CARD = /^card[0-9]+$/;
|
|
7
|
+
/** Whether ALSA has a sound card. */
|
|
8
|
+
export const hasSoundCard = async (domicile) => (await entriesOf(domicile, "/sys/class/sound")).map((names) => names.some((name) => CARD.test(name)));
|
|
9
|
+
/** The names in `dir`; none when it does not exist, as when no driver made it. */
|
|
10
|
+
const entriesOf = async (domicile, dir) => (await domicile.files.readDir(dir)).match({
|
|
11
|
+
Err: (error) => error.kind === SystemErrorKind.NotFound ? Ok([]) : Err(error),
|
|
12
|
+
Ok: (entries) => Ok(entries.map(({ name }) => name)),
|
|
13
|
+
});
|
package/dist/ids.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import { AudioError } from "./audio-error";
|
|
3
|
+
export declare enum Target {
|
|
4
|
+
Output = 0,
|
|
5
|
+
Input = 1,
|
|
6
|
+
Playback = 2,
|
|
7
|
+
Recording = 3
|
|
8
|
+
}
|
|
9
|
+
export declare const targetId: (target: Target, key: string | number) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Splits an id into its target and key. A stream's key must be its index, or
|
|
12
|
+
* `pactl` would look it up as a name.
|
|
13
|
+
*/
|
|
14
|
+
export declare const parseId: (id: string) => Result<{
|
|
15
|
+
target: Target;
|
|
16
|
+
key: string;
|
|
17
|
+
}, AudioError>;
|
package/dist/ids.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The kinds of thing an id names, and the ids themselves: the kind's prefix,
|
|
2
|
+
// a colon, then a device name or stream index.
|
|
3
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
4
|
+
import { AudioError } from "./audio-error";
|
|
5
|
+
export var Target;
|
|
6
|
+
(function (Target) {
|
|
7
|
+
Target[Target["Output"] = 0] = "Output";
|
|
8
|
+
Target[Target["Input"] = 1] = "Input";
|
|
9
|
+
Target[Target["Playback"] = 2] = "Playback";
|
|
10
|
+
Target[Target["Recording"] = 3] = "Recording";
|
|
11
|
+
})(Target || (Target = {}));
|
|
12
|
+
export const targetId = (target, key) => `${prefix(target)}:${key.toString()}`;
|
|
13
|
+
/**
|
|
14
|
+
* Splits an id into its target and key. A stream's key must be its index, or
|
|
15
|
+
* `pactl` would look it up as a name.
|
|
16
|
+
*/
|
|
17
|
+
export const parseId = (id) => {
|
|
18
|
+
const colon = id.indexOf(":");
|
|
19
|
+
const target = TARGETS.find((candidate) => prefix(candidate) === id.slice(0, colon));
|
|
20
|
+
const key = id.slice(colon + 1);
|
|
21
|
+
return colon === -1 || target === undefined || !fits(target, key)
|
|
22
|
+
? Err(AudioError.UnknownId(id))
|
|
23
|
+
: Ok({ key, target });
|
|
24
|
+
};
|
|
25
|
+
const TARGETS = [
|
|
26
|
+
Target.Output,
|
|
27
|
+
Target.Input,
|
|
28
|
+
Target.Playback,
|
|
29
|
+
Target.Recording,
|
|
30
|
+
];
|
|
31
|
+
const fits = (target, key) => target === Target.Playback || target === Target.Recording
|
|
32
|
+
? /^\d+$/.test(key)
|
|
33
|
+
: key !== "";
|
|
34
|
+
const prefix = (target) => {
|
|
35
|
+
switch (target) {
|
|
36
|
+
case Target.Output:
|
|
37
|
+
return "output";
|
|
38
|
+
case Target.Input:
|
|
39
|
+
return "input";
|
|
40
|
+
case Target.Playback:
|
|
41
|
+
return "playback";
|
|
42
|
+
case Target.Recording:
|
|
43
|
+
return "recording";
|
|
44
|
+
}
|
|
45
|
+
};
|
package/dist/meters.d.ts
ADDED
package/dist/meters.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Level meters: one `parec` per metered id, sampled for its peak.
|
|
2
|
+
//
|
|
3
|
+
// `parec` cannot use the server's own peak detection, so it records 1000 mono
|
|
4
|
+
// samples a second and the peak is taken here.
|
|
5
|
+
import { MeterKind } from "./audio";
|
|
6
|
+
import { C_LOCALE } from "./pactl";
|
|
7
|
+
import { peak } from "./peak";
|
|
8
|
+
import { METER_APPLICATION } from "./reading";
|
|
9
|
+
/** High enough to catch transients, low enough to run one per device. */
|
|
10
|
+
const RATE = 1000;
|
|
11
|
+
/** See `SoundServer.meters`. */
|
|
12
|
+
export const meters = (domicile, onLevels, tickMs, report) => {
|
|
13
|
+
const running = new Map();
|
|
14
|
+
const said = { cannotRecord: false };
|
|
15
|
+
const ticking = setInterval(() => {
|
|
16
|
+
const levels = levelsOf(running);
|
|
17
|
+
if (levels.size > 0) {
|
|
18
|
+
onLevels(levels);
|
|
19
|
+
}
|
|
20
|
+
}, tickMs);
|
|
21
|
+
return {
|
|
22
|
+
// A meter whose source changed restarts: a stream moved to another
|
|
23
|
+
// device keeps its id.
|
|
24
|
+
meter: (wanted) => {
|
|
25
|
+
for (const [id, recording] of running) {
|
|
26
|
+
const meter = wanted.get(id);
|
|
27
|
+
if (meter === undefined || !sameMeter(meter, recording.meter)) {
|
|
28
|
+
stop(recording);
|
|
29
|
+
running.delete(id);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const [id, meter] of wanted) {
|
|
33
|
+
if (!running.has(id)) {
|
|
34
|
+
running.set(id, record(domicile, meter, report, said));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
stop: () => {
|
|
39
|
+
clearInterval(ticking);
|
|
40
|
+
for (const recording of running.values()) {
|
|
41
|
+
stop(recording);
|
|
42
|
+
}
|
|
43
|
+
running.clear();
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/** `parec` arguments to record `meter` as mono floats, named as a meter. */
|
|
48
|
+
const argv = (meter) => [
|
|
49
|
+
"parec",
|
|
50
|
+
meter.kind === MeterKind.Source
|
|
51
|
+
? `--device=${meter.name}`
|
|
52
|
+
: `--monitor-stream=${meter.index.toString()}`,
|
|
53
|
+
"--raw",
|
|
54
|
+
"--format=float32le",
|
|
55
|
+
"--channels=1",
|
|
56
|
+
`--rate=${RATE.toString()}`,
|
|
57
|
+
"--latency-msec=30",
|
|
58
|
+
`--property=application.id=${METER_APPLICATION}`,
|
|
59
|
+
];
|
|
60
|
+
/** Starts a `parec` for `meter`. One that cannot start stays silent. */
|
|
61
|
+
const record = (domicile, meter, report, said) => {
|
|
62
|
+
const recording = {
|
|
63
|
+
loudest: undefined,
|
|
64
|
+
meter,
|
|
65
|
+
process: undefined,
|
|
66
|
+
stopped: false,
|
|
67
|
+
};
|
|
68
|
+
domicile.processes
|
|
69
|
+
.spawn(argv(meter), C_LOCALE)
|
|
70
|
+
.then(async (started) => {
|
|
71
|
+
await started.match({
|
|
72
|
+
Err: (error) => {
|
|
73
|
+
if (!said.cannotRecord) {
|
|
74
|
+
said.cannotRecord = true;
|
|
75
|
+
report("the meters cannot record; they read nothing", error.message);
|
|
76
|
+
}
|
|
77
|
+
return Promise.resolve();
|
|
78
|
+
},
|
|
79
|
+
Ok: (process) => {
|
|
80
|
+
recording.process = process;
|
|
81
|
+
if (recording.stopped) {
|
|
82
|
+
process.kill();
|
|
83
|
+
}
|
|
84
|
+
return listen(process.stdout, recording);
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
})
|
|
88
|
+
.catch((error) => {
|
|
89
|
+
// biome-ignore lint/suspicious/noConsole: a meter has no caller to return to
|
|
90
|
+
console.error("a level meter broke", error);
|
|
91
|
+
});
|
|
92
|
+
return recording;
|
|
93
|
+
};
|
|
94
|
+
/** Keeps `recording.loudest` at the loudest sample `samples` carried. */
|
|
95
|
+
const listen = async (samples, recording) => {
|
|
96
|
+
let carried = new Uint8Array();
|
|
97
|
+
for await (const read of samples) {
|
|
98
|
+
const { loudest, rest } = peak(carried, read);
|
|
99
|
+
recording.loudest = Math.max(recording.loudest ?? 0, loudest);
|
|
100
|
+
carried = rest;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const stop = (recording) => {
|
|
104
|
+
recording.stopped = true;
|
|
105
|
+
recording.process?.kill();
|
|
106
|
+
};
|
|
107
|
+
/** Each meter's peak since the last call, resetting it. Silent ones are left out. */
|
|
108
|
+
const levelsOf = (running) => new Map([...running].flatMap(([id, recording]) => {
|
|
109
|
+
const loudest = recording.loudest;
|
|
110
|
+
recording.loudest = loudest === undefined ? undefined : 0;
|
|
111
|
+
return loudest === undefined ? [] : [[id, loudest]];
|
|
112
|
+
}));
|
|
113
|
+
const sameMeter = (a, b) => {
|
|
114
|
+
switch (a.kind) {
|
|
115
|
+
case MeterKind.Source:
|
|
116
|
+
return b.kind === MeterKind.Source && a.name === b.name;
|
|
117
|
+
case MeterKind.Stream:
|
|
118
|
+
return b.kind === MeterKind.Stream && a.index === b.index;
|
|
119
|
+
}
|
|
120
|
+
};
|
package/dist/pactl.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import { AudioError } from "./audio-error";
|
|
3
|
+
import type { AudioDesktop } from "./sound-server";
|
|
4
|
+
/**
|
|
5
|
+
* The C locale, because some of `pactl`'s JSON values are read as English
|
|
6
|
+
* words.
|
|
7
|
+
*/
|
|
8
|
+
export declare const C_LOCALE: {
|
|
9
|
+
env: {
|
|
10
|
+
LC_ALL: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
/** Runs `pactl args` and returns its output, or its error output as the error. */
|
|
14
|
+
export declare const pactl: (domicile: AudioDesktop, args: readonly string[]) => Promise<Result<string, AudioError>>;
|
package/dist/pactl.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Err, Ok } from "@cprussin/option-result";
|
|
2
|
+
import { AudioError } from "./audio-error";
|
|
3
|
+
/**
|
|
4
|
+
* The C locale, because some of `pactl`'s JSON values are read as English
|
|
5
|
+
* words.
|
|
6
|
+
*/
|
|
7
|
+
export const C_LOCALE = { env: { LC_ALL: "C" } };
|
|
8
|
+
/** Runs `pactl args` and returns its output, or its error output as the error. */
|
|
9
|
+
export const pactl = async (domicile, args) => (await domicile.processes.run(["pactl", ...args], C_LOCALE))
|
|
10
|
+
.mapErr((error) => AudioError.Refused(error.message))
|
|
11
|
+
.andThen((ran) => ran.code === 0
|
|
12
|
+
? Ok(ran.stdout)
|
|
13
|
+
: Err(AudioError.Refused(ran.stderr.trim())));
|
package/dist/peak.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loudest of the `f32` samples in `carried` then `read`, clipped to 1.
|
|
3
|
+
* `rest` is a trailing partial sample, to pass as `carried` with the next read.
|
|
4
|
+
*/
|
|
5
|
+
export declare const peak: (carried: Uint8Array, read: Uint8Array) => {
|
|
6
|
+
loudest: number;
|
|
7
|
+
rest: Uint8Array;
|
|
8
|
+
};
|
package/dist/peak.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Bytes in one little-endian `f32` sample. */
|
|
2
|
+
const SAMPLE = 4;
|
|
3
|
+
/**
|
|
4
|
+
* The loudest of the `f32` samples in `carried` then `read`, clipped to 1.
|
|
5
|
+
* `rest` is a trailing partial sample, to pass as `carried` with the next read.
|
|
6
|
+
*/
|
|
7
|
+
export const peak = (carried, read) => {
|
|
8
|
+
const bytes = new Uint8Array([...carried, ...read]);
|
|
9
|
+
const whole = bytes.length - (bytes.length % SAMPLE);
|
|
10
|
+
const view = new DataView(bytes.buffer, 0, whole);
|
|
11
|
+
const samples = Array.from({ length: whole / SAMPLE }, (_, at) => Math.abs(view.getFloat32(at * SAMPLE, true)));
|
|
12
|
+
return {
|
|
13
|
+
loudest: Math.min(1, Math.max(0, ...samples)),
|
|
14
|
+
rest: bytes.slice(whole),
|
|
15
|
+
};
|
|
16
|
+
};
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { SharedWatch } from "@domicile-desktop/react/shared-watch";
|
|
3
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
4
|
+
import type { Audio } from "./audio";
|
|
5
|
+
import type { SoundServer } from "./sound-server";
|
|
6
|
+
/** The sound server's requests. Its state is {@link DeskSound.audio}. */
|
|
7
|
+
export type SoundControls = Omit<SoundServer, "watch">;
|
|
8
|
+
export type DeskSound = {
|
|
9
|
+
audio: SharedWatch<Audio>;
|
|
10
|
+
/** Its meters are shared by id among open mixers. */
|
|
11
|
+
sound: SoundControls;
|
|
12
|
+
};
|
|
13
|
+
/** The sound server, watched once per desktop. */
|
|
14
|
+
export declare const deskSound: (domicile: import("@domicile-desktop/sdk/desktop/desktop").Domicile) => DeskSound;
|
|
15
|
+
/** Whether the machine has a sound card, looked for once per desktop. */
|
|
16
|
+
export declare const deskSoundHardware: (domicile: import("@domicile-desktop/sdk/desktop/desktop").Domicile) => SharedWatch<Result<boolean, SystemError>>;
|
|
17
|
+
/** The sound server's devices and streams, or `undefined` before it says. */
|
|
18
|
+
export declare const useAudio: () => Audio | undefined;
|
|
19
|
+
/** Whether the machine has a sound card, or `undefined` before it was looked for. */
|
|
20
|
+
export declare const useHasSoundCard: () => Result<boolean, SystemError> | undefined;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Sound for a React shell: one sound server watch per desktop, however many
|
|
2
|
+
// parts read it.
|
|
3
|
+
import { perDesktop } from "@domicile-desktop/react/per-desktop";
|
|
4
|
+
import { sharedWatch } from "@domicile-desktop/react/shared-watch";
|
|
5
|
+
import { useDeskWatch } from "@domicile-desktop/react/use-shared-watch";
|
|
6
|
+
import { watchPresence } from "@domicile-desktop/react/watch-presence";
|
|
7
|
+
import { hasSoundCard } from "./hardware";
|
|
8
|
+
import { sharedMeters } from "./shared-meters";
|
|
9
|
+
import { soundServer } from "./sound-server";
|
|
10
|
+
/** The sound server, watched once per desktop. */
|
|
11
|
+
export const deskSound = perDesktop((domicile) => {
|
|
12
|
+
const { watch, ...sound } = soundServer(domicile);
|
|
13
|
+
return {
|
|
14
|
+
audio: sharedWatch(watch),
|
|
15
|
+
sound: { ...sound, meters: sharedMeters(sound.meters) },
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
/** Whether the machine has a sound card, looked for once per desktop. */
|
|
19
|
+
export const deskSoundHardware = perDesktop((domicile) => sharedWatch((onPresent) => watchPresence(() => hasSoundCard(domicile), onPresent)));
|
|
20
|
+
/** The sound server's devices and streams, or `undefined` before it says. */
|
|
21
|
+
export const useAudio = () => useDeskWatch((domicile) => deskSound(domicile).audio);
|
|
22
|
+
/** Whether the machine has a sound card, or `undefined` before it was looked for. */
|
|
23
|
+
export const useHasSoundCard = () => useDeskWatch(deskSoundHardware);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { Audio } from "./audio";
|
|
3
|
+
/** The raw volume `pactl` reports for 100%. */
|
|
4
|
+
export declare const NORMAL = 65536;
|
|
5
|
+
/** Application id of this library's meters, so they are hidden from the lists. */
|
|
6
|
+
export declare const METER_APPLICATION = "org.domicile.meter";
|
|
7
|
+
/** Parses the output of `pactl -f json info` and `pactl -f json list`. */
|
|
8
|
+
export declare const reading: (info: string, list: string) => Result<Audio, string>;
|