Repository navigation
feat(desktop): add local voice input #8010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d1430ee
feat(desktop): add local voice input
t3-code[bot] 3ea876e
fix(desktop): align speech service and controls
t3-code[bot] 22db4c5
fix(desktop): harden speech cancellation and models
t3-code[bot] b76c225
fix(desktop): close speech input edge cases
t3-code[bot] e59f77c
fix(desktop): resolve speech data path from app identity
t3-code[bot] 7c038f5
fix(desktop): inject speech model http client
t3-code[bot] 5d92c0a
fix(web): align voice input feedback
t3-code[bot] 49ad918
fix(web): respect reduced motion for voice level
t3-code[bot] 2956e79
fix(web): preserve composer focus for voice controls
t3-code[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { DesktopSpeechStatusSchema } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| import * as ElectronWindow from "../../electron/ElectronWindow.ts"; | ||
| import * as DesktopSpeech from "../../speech/DesktopSpeech.ts"; | ||
| import * as IpcChannels from "../channels.ts"; | ||
| import * as DesktopIpc from "../DesktopIpc.ts"; | ||
|
|
||
| export const installSpeechEventForwarding = Effect.fn("desktop.ipc.speech.events")(function* () { | ||
| const windows = yield* ElectronWindow.ElectronWindow; | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| yield* speech.subscribe((event) => windows.sendAll(IpcChannels.SPEECH_EVENT_CHANNEL, event)); | ||
| }); | ||
|
|
||
| export const getSpeechStatus = DesktopIpc.makeIpcMethod({ | ||
| channel: IpcChannels.SPEECH_GET_STATUS_CHANNEL, | ||
| payload: Schema.Void, | ||
| result: DesktopSpeechStatusSchema, | ||
| handler: Effect.fn("desktop.ipc.speech.getStatus")(function* () { | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| return yield* speech.getStatus; | ||
| }), | ||
| }); | ||
|
|
||
| export const startSpeech = DesktopIpc.makeIpcMethod({ | ||
| channel: IpcChannels.SPEECH_START_CHANNEL, | ||
| payload: Schema.Void, | ||
| result: DesktopSpeechStatusSchema, | ||
| handler: Effect.fn("desktop.ipc.speech.start")(function* () { | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| return yield* speech.start; | ||
| }), | ||
| }); | ||
|
|
||
| export const stopSpeech = DesktopIpc.makeIpcMethod({ | ||
| channel: IpcChannels.SPEECH_STOP_CHANNEL, | ||
| payload: Schema.Void, | ||
| result: DesktopSpeechStatusSchema, | ||
| handler: Effect.fn("desktop.ipc.speech.stop")(function* () { | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| return yield* speech.stop; | ||
| }), | ||
| }); | ||
|
|
||
| export const cancelSpeech = DesktopIpc.makeIpcMethod({ | ||
| channel: IpcChannels.SPEECH_CANCEL_CHANNEL, | ||
| payload: Schema.Void, | ||
| result: DesktopSpeechStatusSchema, | ||
| handler: Effect.fn("desktop.ipc.speech.cancel")(function* () { | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| return yield* speech.cancel; | ||
| }), | ||
| }); | ||
|
|
||
| export const removeSpeechModelMethod = DesktopIpc.makeIpcMethod({ | ||
| channel: IpcChannels.SPEECH_REMOVE_MODEL_CHANNEL, | ||
| payload: Schema.Void, | ||
| result: DesktopSpeechStatusSchema, | ||
| handler: Effect.fn("desktop.ipc.speech.removeModel")(function* () { | ||
| const speech = yield* DesktopSpeech.DesktopSpeech; | ||
| return yield* speech.removeModel; | ||
| }), | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // @effect-diagnostics globalDate:off - native microphone callback is outside Effect runtime. | ||
| import { PvRecorder } from "@picovoice/pvrecorder-node"; | ||
|
|
||
| const SAMPLE_RATE = 16_000; | ||
| const FRAME_LENGTH = 512; | ||
|
|
||
| export class DesktopMicrophoneCapture { | ||
| private readonly onLevel: (level: number, elapsedMs: number) => void; | ||
| private recorder: PvRecorder | undefined; | ||
| private frames: Int16Array[] = []; | ||
| private readLoop: Promise<void> | undefined; | ||
| private stopping = false; | ||
| private readError: Error | undefined; | ||
| private startedAt = 0; | ||
| private lastLevelAt = 0; | ||
|
|
||
| constructor(onLevel: (level: number, elapsedMs: number) => void) { | ||
| this.onLevel = onLevel; | ||
| } | ||
|
|
||
| start(): void { | ||
| if (this.recorder) throw new Error("microphone capture is already active"); | ||
| const recorder = new PvRecorder(FRAME_LENGTH, -1); | ||
| try { | ||
| if (recorder.sampleRate !== SAMPLE_RATE) { | ||
| throw new Error( | ||
| `microphone reported ${recorder.sampleRate} Hz; expected ${SAMPLE_RATE} Hz`, | ||
| ); | ||
| } | ||
| this.frames = []; | ||
| this.stopping = false; | ||
| this.readError = undefined; | ||
| this.startedAt = Date.now(); | ||
| this.lastLevelAt = 0; | ||
| recorder.start(); | ||
| this.recorder = recorder; | ||
| this.readLoop = this.readFrames(recorder); | ||
| } catch (error) { | ||
| recorder.release(); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async stop(): Promise<Float32Array> { | ||
| const recorder = this.recorder; | ||
| if (!recorder) throw new Error("microphone capture is not active"); | ||
| this.stopping = true; | ||
| try { | ||
| if (recorder.isRecording) recorder.stop(); | ||
| await this.readLoop; | ||
| } finally { | ||
| recorder.release(); | ||
| this.recorder = undefined; | ||
| this.readLoop = undefined; | ||
| } | ||
| if (this.readError) throw this.readError; | ||
| const sampleCount = this.frames.reduce((total, frame) => total + frame.length, 0); | ||
| if (sampleCount === 0) throw new Error("no microphone audio was captured"); | ||
| const pcm = new Float32Array(sampleCount); | ||
| let offset = 0; | ||
| for (const frame of this.frames) { | ||
| for (let index = 0; index < frame.length; index += 1) { | ||
| pcm[offset + index] = (frame[index] ?? 0) / 32_768; | ||
| } | ||
| offset += frame.length; | ||
| } | ||
| this.frames = []; | ||
| return pcm; | ||
| } | ||
|
|
||
| async cancel(): Promise<void> { | ||
| if (!this.recorder) return; | ||
| await this.stop().catch(() => undefined); | ||
| this.frames = []; | ||
| } | ||
|
|
||
| private async readFrames(recorder: PvRecorder): Promise<void> { | ||
| try { | ||
| while (!this.stopping && recorder.isRecording) { | ||
| const frame = await recorder.read(); | ||
| if (this.stopping) continue; | ||
| this.frames.push(frame); | ||
| const now = Date.now(); | ||
| if (now - this.lastLevelAt >= 100) { | ||
| this.lastLevelAt = now; | ||
| let energy = 0; | ||
| for (const sample of frame) energy += (sample / 32_768) ** 2; | ||
| this.onLevel( | ||
| Math.min(1, Math.sqrt(energy / Math.max(1, frame.length)) * 8), | ||
| now - this.startedAt, | ||
| ); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| if (!this.stopping) | ||
| this.readError = error instanceof Error ? error : new Error(String(error)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.