@sdeck/plugin-audio 0.0.0-stage → 0.2.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/bin/sdeck-audio.exe +0 -0
- package/dist/helper.d.ts +95 -0
- package/dist/helper.d.ts.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +681 -0
- package/dist/index.js.map +11 -0
- package/native/Cargo.lock +230 -0
- package/native/Cargo.toml +29 -0
- package/native/src/main.rs +28 -0
- package/native/src/policy.rs +41 -0
- package/native/src/protocol.rs +132 -0
- package/native/src/windows_audio.rs +609 -0
- package/package.json +65 -4
- package/src/helper.ts +270 -0
- package/src/index.tsx +541 -0
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.tsx
|
|
3
|
+
import { existsSync as existsSync2 } from "fs";
|
|
4
|
+
import {
|
|
5
|
+
defineAction,
|
|
6
|
+
definePlugin,
|
|
7
|
+
useHostState,
|
|
8
|
+
useImage,
|
|
9
|
+
useKeyRepeat,
|
|
10
|
+
useSensorPressed,
|
|
11
|
+
useSensorRepeat
|
|
12
|
+
} from "@sdeck/plugin";
|
|
13
|
+
import { iconLayer, textLayer } from "@sdeck/schema";
|
|
14
|
+
|
|
15
|
+
// src/helper.ts
|
|
16
|
+
import { spawn } from "child_process";
|
|
17
|
+
import { existsSync } from "fs";
|
|
18
|
+
import { join } from "path";
|
|
19
|
+
|
|
20
|
+
class AudioUnavailable extends Error {
|
|
21
|
+
}
|
|
22
|
+
var BUILD_HINT = "Build the audio helper with `bun run build:audio`";
|
|
23
|
+
function findHelper(env = process.env, base = import.meta.dir, exists = existsSync) {
|
|
24
|
+
const name = process.platform === "win32" ? "sdeck-audio.exe" : "sdeck-audio";
|
|
25
|
+
return [
|
|
26
|
+
env.SDECK_AUDIO_BIN,
|
|
27
|
+
join(base, "..", "native", "target", "release", name),
|
|
28
|
+
join(base, "..", "bin", name),
|
|
29
|
+
join(base, "..", "native", "target", "debug", name)
|
|
30
|
+
].find((path) => Boolean(path) && exists(path));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class AudioHelper {
|
|
34
|
+
#options;
|
|
35
|
+
#listeners = new Set;
|
|
36
|
+
#pending = new Map;
|
|
37
|
+
#child;
|
|
38
|
+
#snapshot;
|
|
39
|
+
#users = 0;
|
|
40
|
+
#next = 1;
|
|
41
|
+
#linger;
|
|
42
|
+
#restart;
|
|
43
|
+
#delay = 1000;
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.#options = options;
|
|
46
|
+
}
|
|
47
|
+
get snapshot() {
|
|
48
|
+
return this.#snapshot;
|
|
49
|
+
}
|
|
50
|
+
get running() {
|
|
51
|
+
return this.#child !== undefined;
|
|
52
|
+
}
|
|
53
|
+
subscribe(listener) {
|
|
54
|
+
this.#listeners.add(listener);
|
|
55
|
+
const release = this.#acquire();
|
|
56
|
+
if (this.#snapshot)
|
|
57
|
+
listener(this.#snapshot);
|
|
58
|
+
return () => {
|
|
59
|
+
this.#listeners.delete(listener);
|
|
60
|
+
release();
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async request(method, params) {
|
|
64
|
+
const release = this.#acquire();
|
|
65
|
+
try {
|
|
66
|
+
const child = this.#child ?? this.#start();
|
|
67
|
+
const id = this.#next++;
|
|
68
|
+
return await new Promise((resolve, reject) => {
|
|
69
|
+
const timer = setTimeout(() => {
|
|
70
|
+
this.#pending.delete(id);
|
|
71
|
+
reject(new Error("the audio helper did not answer"));
|
|
72
|
+
}, this.#options.timeoutMs ?? 5000);
|
|
73
|
+
this.#pending.set(id, { resolve, reject, timer });
|
|
74
|
+
child.stdin?.write(`${JSON.stringify({ id, method, params })}
|
|
75
|
+
`);
|
|
76
|
+
});
|
|
77
|
+
} finally {
|
|
78
|
+
release();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
close() {
|
|
82
|
+
this.#users = 0;
|
|
83
|
+
this.#listeners.clear();
|
|
84
|
+
clearTimeout(this.#linger);
|
|
85
|
+
this.#stop();
|
|
86
|
+
}
|
|
87
|
+
#acquire() {
|
|
88
|
+
this.#users++;
|
|
89
|
+
clearTimeout(this.#linger);
|
|
90
|
+
if (!this.#child && !this.#restart) {
|
|
91
|
+
try {
|
|
92
|
+
this.#start();
|
|
93
|
+
} catch (error) {
|
|
94
|
+
this.#options.onProblem?.(error);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
let released = false;
|
|
98
|
+
return () => {
|
|
99
|
+
if (released)
|
|
100
|
+
return;
|
|
101
|
+
released = true;
|
|
102
|
+
this.#users--;
|
|
103
|
+
if (this.#users > 0)
|
|
104
|
+
return;
|
|
105
|
+
this.#linger = setTimeout(() => this.#stop(), this.#options.lingerMs ?? 3000);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
#start() {
|
|
109
|
+
clearTimeout(this.#restart);
|
|
110
|
+
this.#restart = undefined;
|
|
111
|
+
const [file, ...args] = this.#options.command();
|
|
112
|
+
if (!file)
|
|
113
|
+
throw new AudioUnavailable(BUILD_HINT);
|
|
114
|
+
const child = spawn(file, args, { stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
|
|
115
|
+
this.#child = child;
|
|
116
|
+
let buffer = "";
|
|
117
|
+
let errors = "";
|
|
118
|
+
child.stdout?.setEncoding("utf8");
|
|
119
|
+
child.stdout?.on("data", (chunk) => {
|
|
120
|
+
buffer += chunk;
|
|
121
|
+
const lines = buffer.split(`
|
|
122
|
+
`);
|
|
123
|
+
buffer = lines.pop() ?? "";
|
|
124
|
+
for (const line of lines)
|
|
125
|
+
this.#receive(line);
|
|
126
|
+
});
|
|
127
|
+
child.stderr?.setEncoding("utf8");
|
|
128
|
+
child.stderr?.on("data", (chunk) => {
|
|
129
|
+
errors = (errors + chunk).slice(-2000);
|
|
130
|
+
});
|
|
131
|
+
child.stdin?.on("error", () => {});
|
|
132
|
+
const ended = (reason) => {
|
|
133
|
+
if (this.#child !== child)
|
|
134
|
+
return;
|
|
135
|
+
this.#child = undefined;
|
|
136
|
+
this.#fail(new Error(reason));
|
|
137
|
+
this.#publish(undefined);
|
|
138
|
+
this.#options.onProblem?.(new AudioUnavailable(reason));
|
|
139
|
+
if (this.#users <= 0)
|
|
140
|
+
return;
|
|
141
|
+
this.#restart = setTimeout(() => {
|
|
142
|
+
this.#restart = undefined;
|
|
143
|
+
if (this.#users <= 0 || this.#child)
|
|
144
|
+
return;
|
|
145
|
+
try {
|
|
146
|
+
this.#start();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
this.#options.onProblem?.(error);
|
|
149
|
+
}
|
|
150
|
+
}, this.#delay);
|
|
151
|
+
this.#delay = Math.min(this.#delay * 2, 30000);
|
|
152
|
+
};
|
|
153
|
+
child.once("error", (error) => ended(`the audio helper cannot run: ${error.message}`));
|
|
154
|
+
child.once("close", (code) => ended(errors.trim() || `the audio helper stopped (exit ${code})`));
|
|
155
|
+
return child;
|
|
156
|
+
}
|
|
157
|
+
#stop() {
|
|
158
|
+
clearTimeout(this.#restart);
|
|
159
|
+
this.#restart = undefined;
|
|
160
|
+
const child = this.#child;
|
|
161
|
+
this.#child = undefined;
|
|
162
|
+
this.#fail(new Error("the audio helper was stopped"));
|
|
163
|
+
this.#publish(undefined);
|
|
164
|
+
if (!child)
|
|
165
|
+
return;
|
|
166
|
+
child.stdin?.end();
|
|
167
|
+
const kill = setTimeout(() => child.kill(), 2000);
|
|
168
|
+
child.once("close", () => clearTimeout(kill));
|
|
169
|
+
}
|
|
170
|
+
#receive(line) {
|
|
171
|
+
let message;
|
|
172
|
+
try {
|
|
173
|
+
message = JSON.parse(line);
|
|
174
|
+
} catch {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (message.event === "snapshot") {
|
|
178
|
+
this.#delay = 1000;
|
|
179
|
+
this.#options.onProblem?.(undefined);
|
|
180
|
+
this.#publish(message.data);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const pending = this.#pending.get(message.id ?? -1);
|
|
184
|
+
if (!pending)
|
|
185
|
+
return;
|
|
186
|
+
this.#pending.delete(message.id);
|
|
187
|
+
clearTimeout(pending.timer);
|
|
188
|
+
if (message.error !== undefined)
|
|
189
|
+
pending.reject(new Error(message.error));
|
|
190
|
+
else
|
|
191
|
+
pending.resolve(message.result);
|
|
192
|
+
}
|
|
193
|
+
#publish(snapshot) {
|
|
194
|
+
if (this.#snapshot === undefined && snapshot === undefined)
|
|
195
|
+
return;
|
|
196
|
+
this.#snapshot = snapshot;
|
|
197
|
+
for (const listener of [...this.#listeners])
|
|
198
|
+
listener(snapshot);
|
|
199
|
+
}
|
|
200
|
+
#fail(error) {
|
|
201
|
+
for (const pending of this.#pending.values()) {
|
|
202
|
+
clearTimeout(pending.timer);
|
|
203
|
+
pending.reject(error);
|
|
204
|
+
}
|
|
205
|
+
this.#pending.clear();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/index.tsx
|
|
210
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
211
|
+
function appKey(name) {
|
|
212
|
+
return name.trim().toLowerCase().replace(/\.exe$/, "");
|
|
213
|
+
}
|
|
214
|
+
function audioState(snapshot) {
|
|
215
|
+
if (!snapshot) {
|
|
216
|
+
return {
|
|
217
|
+
master: undefined,
|
|
218
|
+
mic: undefined,
|
|
219
|
+
output: undefined,
|
|
220
|
+
input: undefined,
|
|
221
|
+
outputs: undefined,
|
|
222
|
+
inputs: undefined,
|
|
223
|
+
apps: undefined
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const level = (d) => d && { volume: d.volume, muted: d.muted, name: d.name };
|
|
227
|
+
const output = snapshot.outputs.find((d) => d.default);
|
|
228
|
+
const input = snapshot.inputs.find((d) => d.default);
|
|
229
|
+
return {
|
|
230
|
+
master: level(output),
|
|
231
|
+
mic: level(input),
|
|
232
|
+
output: output?.name,
|
|
233
|
+
input: input?.name,
|
|
234
|
+
outputs: snapshot.outputs,
|
|
235
|
+
inputs: snapshot.inputs,
|
|
236
|
+
apps: Object.fromEntries(snapshot.apps.map((app) => [appKey(app.name), app]))
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
var context;
|
|
240
|
+
var helper2 = new AudioHelper({
|
|
241
|
+
command: () => {
|
|
242
|
+
if (process.platform !== "win32") {
|
|
243
|
+
throw new AudioUnavailable("Audio control is available on Windows only");
|
|
244
|
+
}
|
|
245
|
+
const path = String(context?.settings().helper || "") || findHelper();
|
|
246
|
+
if (!path)
|
|
247
|
+
throw new AudioUnavailable(`${BUILD_HINT}.`);
|
|
248
|
+
return [path];
|
|
249
|
+
},
|
|
250
|
+
onProblem: (problem) => context?.status(problem?.message, "error")
|
|
251
|
+
});
|
|
252
|
+
var stateOf = (id, name, type, description) => ({
|
|
253
|
+
id,
|
|
254
|
+
name,
|
|
255
|
+
type,
|
|
256
|
+
description,
|
|
257
|
+
start: (set) => helper2.subscribe((snapshot) => set(audioState(snapshot)[id]))
|
|
258
|
+
});
|
|
259
|
+
var useIcon = (icon, color) => useImage(`icon:${icon}`, color);
|
|
260
|
+
var TARGETS = [
|
|
261
|
+
{ value: "output", label: "Speakers (default output)" },
|
|
262
|
+
{ value: "input", label: "Microphone (default input)" },
|
|
263
|
+
{ value: "app", label: "One app" }
|
|
264
|
+
];
|
|
265
|
+
function targetOf(s) {
|
|
266
|
+
if (s.target !== "app")
|
|
267
|
+
return { kind: s.target };
|
|
268
|
+
if (!s.app.trim())
|
|
269
|
+
throw new Error("choose an app");
|
|
270
|
+
return { kind: "app", app: s.app.trim() };
|
|
271
|
+
}
|
|
272
|
+
function targetPath(s) {
|
|
273
|
+
if (s.target === "output")
|
|
274
|
+
return "audio.master";
|
|
275
|
+
if (s.target === "input")
|
|
276
|
+
return "audio.mic";
|
|
277
|
+
return `audio.apps[${JSON.stringify(appKey(s.app))}]`;
|
|
278
|
+
}
|
|
279
|
+
var targetName = (s) => s.target === "output" ? "Volume" : s.target === "input" ? "Mic" : appKey(s.app) || "App";
|
|
280
|
+
var APP_ICONS = {
|
|
281
|
+
spotify: "si/SiSpotify",
|
|
282
|
+
discord: "si/SiDiscord",
|
|
283
|
+
chrome: "si/SiGooglechrome",
|
|
284
|
+
firefox: "si/SiFirefoxbrowser",
|
|
285
|
+
msedge: "fa6/FaEdge",
|
|
286
|
+
brave: "si/SiBrave",
|
|
287
|
+
opera: "si/SiOpera",
|
|
288
|
+
steam: "si/SiSteam",
|
|
289
|
+
vlc: "si/SiVlcmediaplayer",
|
|
290
|
+
obs64: "si/SiObsstudio",
|
|
291
|
+
slack: "fa6/FaSlack",
|
|
292
|
+
zoom: "si/SiZoom",
|
|
293
|
+
system: "lucide/bell"
|
|
294
|
+
};
|
|
295
|
+
function targetIcons(s) {
|
|
296
|
+
if (s.target === "output")
|
|
297
|
+
return { on: "lucide/volume-2", off: "lucide/volume-x" };
|
|
298
|
+
if (s.target === "input")
|
|
299
|
+
return { on: "lucide/mic", off: "lucide/mic-off" };
|
|
300
|
+
const brand = APP_ICONS[appKey(s.app)];
|
|
301
|
+
return brand ? { on: brand, off: brand } : { on: "lucide/app-window", off: "lucide/volume-x" };
|
|
302
|
+
}
|
|
303
|
+
var targetFields = {
|
|
304
|
+
target: { type: "select", label: "Control", default: "output", options: TARGETS },
|
|
305
|
+
app: {
|
|
306
|
+
type: "text",
|
|
307
|
+
label: "App",
|
|
308
|
+
source: "apps",
|
|
309
|
+
placeholder: "spotify.exe",
|
|
310
|
+
description: "The executable name, as it appears while the app plays sound.",
|
|
311
|
+
visibleIf: { target: "app" }
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
async function adjust(target, operation, step, level) {
|
|
315
|
+
switch (operation) {
|
|
316
|
+
case "up":
|
|
317
|
+
return helper2.request("setVolume", { target, delta: step });
|
|
318
|
+
case "down":
|
|
319
|
+
return helper2.request("setVolume", { target, delta: -step });
|
|
320
|
+
case "set":
|
|
321
|
+
return helper2.request("setVolume", { target, volume: level });
|
|
322
|
+
case "toggle":
|
|
323
|
+
return helper2.request("setMute", { target });
|
|
324
|
+
default:
|
|
325
|
+
return helper2.request("setMute", { target, muted: operation === "mute" });
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
var OPERATION_NAMES = {
|
|
329
|
+
up: "Volume up",
|
|
330
|
+
down: "Volume down",
|
|
331
|
+
set: "Set volume",
|
|
332
|
+
mute: "Mute",
|
|
333
|
+
unmute: "Unmute",
|
|
334
|
+
toggle: "Toggle mute"
|
|
335
|
+
};
|
|
336
|
+
var volume = defineAction({
|
|
337
|
+
id: "volume",
|
|
338
|
+
name: "Volume",
|
|
339
|
+
description: "Changes or mutes the speakers, the microphone or one app, and shows its level.",
|
|
340
|
+
category: "Audio",
|
|
341
|
+
icon: "volume-2",
|
|
342
|
+
slots: ["key", "sensor"],
|
|
343
|
+
settings: {
|
|
344
|
+
...targetFields,
|
|
345
|
+
operation: {
|
|
346
|
+
type: "select",
|
|
347
|
+
label: "When pressed",
|
|
348
|
+
default: "toggle",
|
|
349
|
+
options: Object.entries(OPERATION_NAMES).map(([value, label]) => ({ value, label }))
|
|
350
|
+
},
|
|
351
|
+
step: {
|
|
352
|
+
type: "number",
|
|
353
|
+
label: "Step (%)",
|
|
354
|
+
default: 5,
|
|
355
|
+
min: 1,
|
|
356
|
+
max: 50,
|
|
357
|
+
visibleIf: { operation: ["up", "down"] }
|
|
358
|
+
},
|
|
359
|
+
level: {
|
|
360
|
+
type: "number",
|
|
361
|
+
label: "Level (%)",
|
|
362
|
+
default: 50,
|
|
363
|
+
min: 0,
|
|
364
|
+
max: 100,
|
|
365
|
+
visibleIf: { operation: "set" }
|
|
366
|
+
},
|
|
367
|
+
background: { type: "color", label: "Background", default: "#172554", appearance: true }
|
|
368
|
+
},
|
|
369
|
+
run: async ({ settings }) => {
|
|
370
|
+
const target = targetOf(settings);
|
|
371
|
+
const { before, after } = await adjust(target, settings.operation, settings.step, settings.level);
|
|
372
|
+
return {
|
|
373
|
+
result: after,
|
|
374
|
+
label: `${OPERATION_NAMES[settings.operation]} (${targetName(settings)})`,
|
|
375
|
+
undo: async () => {
|
|
376
|
+
await helper2.request("setVolume", { target, volume: before.volume });
|
|
377
|
+
await helper2.request("setMute", { target, muted: before.muted });
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
},
|
|
381
|
+
look: (s) => {
|
|
382
|
+
const path = targetPath(s);
|
|
383
|
+
const muted = `${path}.muted`;
|
|
384
|
+
const icons = targetIcons(s);
|
|
385
|
+
const step = s.operation === "up" ? "plus" : s.operation === "down" ? "minus" : "";
|
|
386
|
+
return {
|
|
387
|
+
background: s.background,
|
|
388
|
+
layers: [
|
|
389
|
+
iconLayer("icon", icons.on, {
|
|
390
|
+
size: 42,
|
|
391
|
+
position: "top",
|
|
392
|
+
visible: `!${muted}`,
|
|
393
|
+
row: "icon"
|
|
394
|
+
}),
|
|
395
|
+
iconLayer("muted", icons.off, {
|
|
396
|
+
color: "#f87171",
|
|
397
|
+
size: 42,
|
|
398
|
+
position: "top",
|
|
399
|
+
visible: muted,
|
|
400
|
+
row: "icon"
|
|
401
|
+
}),
|
|
402
|
+
...step ? [iconLayer("step", `lucide/${step}`, { size: 22, position: "top", row: "icon" })] : [],
|
|
403
|
+
textLayer("level", `{{${path}.volume == null ? "N/A" : ${path}.volume + "%"}}`, {
|
|
404
|
+
size: 18,
|
|
405
|
+
position: "bottom"
|
|
406
|
+
}),
|
|
407
|
+
{
|
|
408
|
+
id: "bar",
|
|
409
|
+
type: "bar",
|
|
410
|
+
value: `${path}.volume ?? 0`,
|
|
411
|
+
color: "#60a5fa",
|
|
412
|
+
track: "#1e293b",
|
|
413
|
+
position: "bottom",
|
|
414
|
+
thickness: 6
|
|
415
|
+
}
|
|
416
|
+
]
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
var slider = defineAction({
|
|
421
|
+
id: "slider",
|
|
422
|
+
name: "Volume slider",
|
|
423
|
+
description: "A level bar on a sensor strip: touch it to step the volume (the left one lowers, the right one raises), hold to keep going.",
|
|
424
|
+
category: "Audio",
|
|
425
|
+
icon: "sliders-horizontal",
|
|
426
|
+
slots: ["sensor", "window", "key"],
|
|
427
|
+
settings: {
|
|
428
|
+
...targetFields,
|
|
429
|
+
direction: {
|
|
430
|
+
type: "select",
|
|
431
|
+
label: "Touching it",
|
|
432
|
+
default: "auto",
|
|
433
|
+
options: [
|
|
434
|
+
{ value: "auto", label: "Left lowers, right raises" },
|
|
435
|
+
{ value: "down", label: "Lowers" },
|
|
436
|
+
{ value: "up", label: "Raises" }
|
|
437
|
+
]
|
|
438
|
+
},
|
|
439
|
+
step: { type: "number", label: "Step (%)", default: 4, min: 1, max: 50 },
|
|
440
|
+
color: { type: "color", label: "Bar colour", default: "#60a5fa", appearance: true }
|
|
441
|
+
},
|
|
442
|
+
run: async ({ settings }) => {
|
|
443
|
+
const target = targetOf(settings);
|
|
444
|
+
const delta = settings.direction === "down" ? -settings.step : settings.step;
|
|
445
|
+
const { before, after } = await helper2.request("setVolume", { target, delta });
|
|
446
|
+
return {
|
|
447
|
+
result: after,
|
|
448
|
+
label: `${targetName(settings)} ${after.volume}%`,
|
|
449
|
+
undo: () => helper2.request("setVolume", { target, volume: before.volume })
|
|
450
|
+
};
|
|
451
|
+
},
|
|
452
|
+
component: ({ settings, slot, plugin }) => {
|
|
453
|
+
const name = settings.target === "output" ? "master" : settings.target === "input" ? "mic" : "";
|
|
454
|
+
const whole = useHostState(name ? `audio.${name}` : "audio.apps");
|
|
455
|
+
const level = name ? whole : whole?.[appKey(settings.app)];
|
|
456
|
+
const down = settings.direction === "down" || settings.direction === "auto" && slot.kind !== "window" && slot.index % 2 === 0;
|
|
457
|
+
const step = () => {
|
|
458
|
+
helper2.request("setVolume", {
|
|
459
|
+
target: targetOf(settings),
|
|
460
|
+
delta: down ? -settings.step : settings.step
|
|
461
|
+
}).catch((error) => plugin.log("slider:", error.message));
|
|
462
|
+
};
|
|
463
|
+
const sensorPressed = useSensorPressed(slot.kind === "sensor" ? slot.index : 0);
|
|
464
|
+
const index = slot.kind === "window" ? -1 : slot.index;
|
|
465
|
+
useSensorRepeat((i) => slot.kind === "sensor" && i === index && step(), {
|
|
466
|
+
sensor: slot.kind === "sensor" ? index : -1,
|
|
467
|
+
intervalMs: 150
|
|
468
|
+
});
|
|
469
|
+
useKeyRepeat((i) => slot.kind === "key" && i === index && step(), {
|
|
470
|
+
key: slot.kind === "key" ? index : -1,
|
|
471
|
+
intervalMs: 150
|
|
472
|
+
});
|
|
473
|
+
const strip = slot.kind === "sensor";
|
|
474
|
+
const text = !level ? "N/A" : `${level.volume}%`;
|
|
475
|
+
const icons = targetIcons(settings);
|
|
476
|
+
const size = strip ? 12 : 24;
|
|
477
|
+
const icon = useIcon(level?.muted ? icons.off : icons.on, "#ffffff");
|
|
478
|
+
const arrow = useIcon(down ? "lucide/minus" : "lucide/plus", "#ffffff");
|
|
479
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
480
|
+
style: {
|
|
481
|
+
display: "flex",
|
|
482
|
+
position: "relative",
|
|
483
|
+
width: "100%",
|
|
484
|
+
height: "100%",
|
|
485
|
+
background: strip && sensorPressed ? "#334155" : "#0f172a",
|
|
486
|
+
alignItems: "center",
|
|
487
|
+
justifyContent: "center"
|
|
488
|
+
},
|
|
489
|
+
children: [
|
|
490
|
+
/* @__PURE__ */ jsx("div", {
|
|
491
|
+
style: {
|
|
492
|
+
display: "flex",
|
|
493
|
+
position: "absolute",
|
|
494
|
+
left: 0,
|
|
495
|
+
top: 0,
|
|
496
|
+
bottom: 0,
|
|
497
|
+
width: `${level?.volume ?? 0}%`,
|
|
498
|
+
background: level?.muted ? "#7f1d1d" : settings.color,
|
|
499
|
+
opacity: 0.6
|
|
500
|
+
}
|
|
501
|
+
}),
|
|
502
|
+
/* @__PURE__ */ jsxs("div", {
|
|
503
|
+
style: {
|
|
504
|
+
display: "flex",
|
|
505
|
+
flexDirection: "row",
|
|
506
|
+
alignItems: "center",
|
|
507
|
+
gap: strip ? 3 : 6,
|
|
508
|
+
color: "#ffffff",
|
|
509
|
+
fontWeight: 700,
|
|
510
|
+
fontSize: 20
|
|
511
|
+
},
|
|
512
|
+
children: [
|
|
513
|
+
strip && arrow ? /* @__PURE__ */ jsx("img", {
|
|
514
|
+
src: arrow,
|
|
515
|
+
width: 10,
|
|
516
|
+
height: 10,
|
|
517
|
+
alt: ""
|
|
518
|
+
}) : null,
|
|
519
|
+
icon ? /* @__PURE__ */ jsx("img", {
|
|
520
|
+
src: icon,
|
|
521
|
+
width: size,
|
|
522
|
+
height: size,
|
|
523
|
+
alt: ""
|
|
524
|
+
}) : null,
|
|
525
|
+
strip ? null : /* @__PURE__ */ jsx("div", {
|
|
526
|
+
style: { display: "flex" },
|
|
527
|
+
children: text
|
|
528
|
+
})
|
|
529
|
+
]
|
|
530
|
+
})
|
|
531
|
+
]
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
var NEXT = "__next";
|
|
536
|
+
function nextDevice(devices) {
|
|
537
|
+
const at = devices.findIndex((d) => d.default);
|
|
538
|
+
return devices[(at + 1) % devices.length];
|
|
539
|
+
}
|
|
540
|
+
var defaultDevice = defineAction({
|
|
541
|
+
id: "default-device",
|
|
542
|
+
name: "Switch audio device",
|
|
543
|
+
description: "Makes another speaker or microphone the default, or steps through them.",
|
|
544
|
+
category: "Audio",
|
|
545
|
+
icon: "speaker",
|
|
546
|
+
slots: ["key", "sensor"],
|
|
547
|
+
settings: {
|
|
548
|
+
flow: {
|
|
549
|
+
type: "select",
|
|
550
|
+
label: "Switch the",
|
|
551
|
+
default: "output",
|
|
552
|
+
options: [
|
|
553
|
+
{ value: "output", label: "Output (speakers)" },
|
|
554
|
+
{ value: "input", label: "Input (microphone)" }
|
|
555
|
+
]
|
|
556
|
+
},
|
|
557
|
+
output: {
|
|
558
|
+
type: "select",
|
|
559
|
+
label: "To",
|
|
560
|
+
default: NEXT,
|
|
561
|
+
source: "outputs",
|
|
562
|
+
options: [{ value: NEXT, label: "The next device" }],
|
|
563
|
+
visibleIf: { flow: "output" }
|
|
564
|
+
},
|
|
565
|
+
input: {
|
|
566
|
+
type: "select",
|
|
567
|
+
label: "To",
|
|
568
|
+
default: NEXT,
|
|
569
|
+
source: "inputs",
|
|
570
|
+
options: [{ value: NEXT, label: "The next device" }],
|
|
571
|
+
visibleIf: { flow: "input" }
|
|
572
|
+
},
|
|
573
|
+
background: { type: "color", label: "Background", default: "#3b0764", appearance: true }
|
|
574
|
+
},
|
|
575
|
+
run: async ({ settings }) => {
|
|
576
|
+
const wanted = settings.flow === "output" ? settings.output : settings.input;
|
|
577
|
+
const snapshot = await helper2.request("snapshot");
|
|
578
|
+
const devices = settings.flow === "output" ? snapshot.outputs : snapshot.inputs;
|
|
579
|
+
const device = wanted === NEXT ? nextDevice(devices) : devices.find((d) => d.id === wanted);
|
|
580
|
+
if (!device)
|
|
581
|
+
throw new Error("that audio device is not connected");
|
|
582
|
+
const { previous } = await helper2.request("setDefault", { device: device.id });
|
|
583
|
+
return {
|
|
584
|
+
result: device.name,
|
|
585
|
+
label: `Audio ${settings.flow}: ${device.name}`,
|
|
586
|
+
...previous && {
|
|
587
|
+
undo: () => helper2.request("setDefault", { device: previous })
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
},
|
|
591
|
+
look: (s) => ({
|
|
592
|
+
background: s.background,
|
|
593
|
+
layers: [
|
|
594
|
+
iconLayer("icon", s.flow === "output" ? "lucide/headphones" : "lucide/mic", {
|
|
595
|
+
size: 44,
|
|
596
|
+
position: "top",
|
|
597
|
+
row: "icon"
|
|
598
|
+
}),
|
|
599
|
+
iconLayer("switch", "lucide/arrow-left-right", {
|
|
600
|
+
color: "#c4b5fd",
|
|
601
|
+
size: 22,
|
|
602
|
+
position: "top",
|
|
603
|
+
row: "icon"
|
|
604
|
+
}),
|
|
605
|
+
textLayer("device", `{{audio.${s.flow} ?? "N/A"}}`, { size: 11, position: "bottom" })
|
|
606
|
+
]
|
|
607
|
+
})
|
|
608
|
+
});
|
|
609
|
+
var deviceOptions = async (flow) => (await helper2.request("snapshot"))[flow].map((d) => ({ value: d.id, label: d.name }));
|
|
610
|
+
var src_default = definePlugin({
|
|
611
|
+
id: "@sdeck/plugin-audio",
|
|
612
|
+
name: "Audio",
|
|
613
|
+
version: "0.2.0",
|
|
614
|
+
namespace: "audio",
|
|
615
|
+
description: "Volume and mute for speakers, microphone and each app, and device switching.",
|
|
616
|
+
author: "sdeck",
|
|
617
|
+
permissions: ["audio", "process"],
|
|
618
|
+
settings: {
|
|
619
|
+
helper: {
|
|
620
|
+
type: "text",
|
|
621
|
+
label: "Audio helper",
|
|
622
|
+
placeholder: "Found automatically",
|
|
623
|
+
description: "Full path of sdeck-audio, if it is somewhere unusual."
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
onboarding: {
|
|
627
|
+
steps: [
|
|
628
|
+
{
|
|
629
|
+
id: "helper",
|
|
630
|
+
title: "The audio helper",
|
|
631
|
+
body: "sdeck reaches your speakers, microphone and apps through a small helper that comes with this plugin on Windows. Leave this empty unless it is somewhere else.",
|
|
632
|
+
settings: ["helper"],
|
|
633
|
+
done: (activated) => {
|
|
634
|
+
const custom = String(activated.settings().helper ?? "").trim();
|
|
635
|
+
return custom ? existsSync2(custom) : findHelper() !== undefined;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
]
|
|
639
|
+
},
|
|
640
|
+
actions: [volume, slider, defaultDevice],
|
|
641
|
+
optionSources: {
|
|
642
|
+
outputs: () => deviceOptions("outputs"),
|
|
643
|
+
inputs: () => deviceOptions("inputs"),
|
|
644
|
+
apps: async () => (await helper2.request("snapshot")).apps.map((a) => ({ value: a.name, label: a.name }))
|
|
645
|
+
},
|
|
646
|
+
state: [
|
|
647
|
+
stateOf("master", "Speakers", "json", "audio.master.volume, .muted, .name"),
|
|
648
|
+
stateOf("mic", "Microphone", "json", "audio.mic.volume, .muted, .name"),
|
|
649
|
+
stateOf("output", "Output device", "string", "The default output's name."),
|
|
650
|
+
stateOf("input", "Input device", "string", "The default input's name."),
|
|
651
|
+
stateOf("outputs", "Output devices", "json", "Every output with its level."),
|
|
652
|
+
stateOf("inputs", "Input devices", "json", "Every input with its level."),
|
|
653
|
+
stateOf("apps", "App volumes", "json", "audio.apps.spotify.volume, .muted, .active")
|
|
654
|
+
],
|
|
655
|
+
activate(activated) {
|
|
656
|
+
context = activated;
|
|
657
|
+
if (process.platform !== "win32") {
|
|
658
|
+
activated.status("Audio control is available on Windows only", "info");
|
|
659
|
+
} else if (!findHelper() && !activated.settings().helper) {
|
|
660
|
+
activated.status(`${BUILD_HINT}.`, "info");
|
|
661
|
+
}
|
|
662
|
+
return () => {
|
|
663
|
+
helper2.close();
|
|
664
|
+
context = undefined;
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
export {
|
|
669
|
+
AudioHelper,
|
|
670
|
+
AudioUnavailable,
|
|
671
|
+
BUILD_HINT,
|
|
672
|
+
appKey,
|
|
673
|
+
audioState,
|
|
674
|
+
src_default as default,
|
|
675
|
+
findHelper,
|
|
676
|
+
nextDevice,
|
|
677
|
+
targetIcons,
|
|
678
|
+
targetPath
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
//# debugId=2DB11882DB7A76C964756E2164756E21
|