|
| 1 | +import type { VoiceDuckingOutputsFork, VoiceDuckingSettingsFork } from "@t3tools/contracts"; |
| 2 | +import * as Context from "effect/Context"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Schema from "effect/Schema"; |
| 6 | +import * as Electron from "electron"; |
| 7 | +import { DesktopEnvironment } from "../app/DesktopEnvironment.ts"; |
| 8 | +import { linuxAudioOutputsFork } from "./audioOutputs.fork.ts"; |
| 9 | +import { SpeakerDuckingFork } from "./ducking.fork.ts"; |
| 10 | + |
| 11 | +export class VoiceDuckingErrorFork extends Schema.TaggedError<VoiceDuckingErrorFork>()( |
| 12 | + "VoiceDuckingErrorFork", |
| 13 | + { message: Schema.String }, |
| 14 | +) {} |
| 15 | +const attempt = <A>(run: () => Promise<A>) => |
| 16 | + Effect.tryPromise({ |
| 17 | + try: run, |
| 18 | + catch: (error) => |
| 19 | + new VoiceDuckingErrorFork({ |
| 20 | + message: error instanceof Error ? error.message : "Speaker ducking failed.", |
| 21 | + }), |
| 22 | + }); |
| 23 | + |
| 24 | +export class DesktopVoiceDuckingFork extends Context.Service< |
| 25 | + DesktopVoiceDuckingFork, |
| 26 | + { |
| 27 | + readonly list: Effect.Effect<VoiceDuckingOutputsFork>; |
| 28 | + readonly start: ( |
| 29 | + owner: number, |
| 30 | + sessionId: string, |
| 31 | + settings: VoiceDuckingSettingsFork, |
| 32 | + ) => Effect.Effect<void, VoiceDuckingErrorFork>; |
| 33 | + readonly stop: (owner: number, sessionId: string) => Effect.Effect<void, VoiceDuckingErrorFork>; |
| 34 | + readonly shutdown: Effect.Effect<void>; |
| 35 | + } |
| 36 | +>()("@t3tools/desktop/voice-input/DesktopVoiceDucking.fork/DesktopVoiceDuckingFork") {} |
| 37 | + |
| 38 | +export const layer = Layer.effect( |
| 39 | + DesktopVoiceDuckingFork, |
| 40 | + Effect.gen(function* () { |
| 41 | + const environment = yield* DesktopEnvironment; |
| 42 | + const unsupported = |
| 43 | + environment.platform === "linux" |
| 44 | + ? null |
| 45 | + : "Speaker ducking currently supports Linux with PipeWire or PulseAudio."; |
| 46 | + const runBackground = Effect.runForkWith(yield* Effect.context<never>()); |
| 47 | + const warn = (error: unknown) => { |
| 48 | + runBackground(Effect.logWarning("Speaker ducking:", error)); |
| 49 | + }; |
| 50 | + const manager = new SpeakerDuckingFork(linuxAudioOutputsFork, warn); |
| 51 | + const watchers = new Map<number, () => void>(); |
| 52 | + const watchOwner = (id: number) => { |
| 53 | + if (watchers.has(id)) return; |
| 54 | + const contents = Electron.webContents.fromId(id); |
| 55 | + if (!contents || contents.isDestroyed()) |
| 56 | + throw new Error("Recording window is no longer available."); |
| 57 | + const release = () => { |
| 58 | + void manager.stop(id).catch(warn); |
| 59 | + }; |
| 60 | + const navigation = ( |
| 61 | + _event: Electron.Event, |
| 62 | + _url: string, |
| 63 | + _inPlace: boolean, |
| 64 | + mainFrame: boolean, |
| 65 | + ) => { |
| 66 | + if (mainFrame) release(); |
| 67 | + }; |
| 68 | + const cleanup = () => { |
| 69 | + contents.removeListener("destroyed", destroyed); |
| 70 | + contents.removeListener("render-process-gone", release); |
| 71 | + contents.removeListener("did-start-navigation", navigation); |
| 72 | + watchers.delete(id); |
| 73 | + }; |
| 74 | + const destroyed = () => { |
| 75 | + release(); |
| 76 | + cleanup(); |
| 77 | + }; |
| 78 | + contents.once("destroyed", destroyed); |
| 79 | + contents.on("render-process-gone", release); |
| 80 | + contents.on("did-start-navigation", navigation); |
| 81 | + watchers.set(id, cleanup); |
| 82 | + }; |
| 83 | + const shutdown = attempt(async () => { |
| 84 | + for (const cleanup of watchers.values()) cleanup(); |
| 85 | + await manager.close(); |
| 86 | + }).pipe( |
| 87 | + Effect.catch((error) => |
| 88 | + Effect.logWarning("Could not restore speaker volume on shutdown.", error), |
| 89 | + ), |
| 90 | + ); |
| 91 | + yield* Effect.addFinalizer(() => shutdown); |
| 92 | + return DesktopVoiceDuckingFork.of({ |
| 93 | + list: unsupported |
| 94 | + ? Effect.succeed({ unavailableReason: unsupported, outputs: [] }) |
| 95 | + : attempt(async () => ({ |
| 96 | + unavailableReason: null, |
| 97 | + outputs: (await linuxAudioOutputsFork.list()).map(({ id, label, isDefault }) => ({ |
| 98 | + id, |
| 99 | + label, |
| 100 | + isDefault, |
| 101 | + })), |
| 102 | + })).pipe( |
| 103 | + Effect.catch((error) => |
| 104 | + Effect.succeed({ unavailableReason: error.message, outputs: [] }), |
| 105 | + ), |
| 106 | + ), |
| 107 | + start: Effect.fn("DesktopVoiceDuckingFork.start")(function (owner, sessionId, settings) { |
| 108 | + return attempt(async () => { |
| 109 | + if (unsupported) throw new Error(unsupported); |
| 110 | + watchOwner(owner); |
| 111 | + await manager.start(owner, sessionId, settings); |
| 112 | + if (Electron.webContents.fromId(owner)?.isDestroyed() !== false) |
| 113 | + await manager.stop(owner); |
| 114 | + }); |
| 115 | + }), |
| 116 | + stop: Effect.fn("DesktopVoiceDuckingFork.stop")(function (owner, sessionId) { |
| 117 | + return attempt(() => manager.stop(owner, sessionId)); |
| 118 | + }), |
| 119 | + shutdown, |
| 120 | + }); |
| 121 | + }), |
| 122 | +); |
| 123 | + |
| 124 | +/** Register after the completion finalizer so restoration runs before Electron may quit. */ |
| 125 | +export const registerShutdown = Effect.gen(function* () { |
| 126 | + const ducking = yield* DesktopVoiceDuckingFork; |
| 127 | + yield* Effect.addFinalizer(() => ducking.shutdown); |
| 128 | +}); |
0 commit comments