forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw into calculator
This commit is contained in:
commit
e1c4628566
46 files changed
+1445
-304
No files matched your search
@@ -83,6 +83,13 @@ export class FaceclawVoiceControlBridge {
|
||||
// when the last one releases. Transcript events broadcast to every
|
||||
// listener, so push-to-talk and the Transcribe window both receive text.
|
||||
private readonly captureHolders = new Set<CaptureHolder>();
|
||||
// Holders whose stream died with the glasses session (transport drop,
|
||||
// charging case, EvenHub suspend). They still want the mic, so the next
|
||||
// session restarts it for them; see handleSessionEnded/resumeCapture.
|
||||
private readonly suspendedHolders = new Set<CaptureHolder>();
|
||||
private suspendedRaw = false;
|
||||
// Endpointing of the live capture, replayed when it is resumed.
|
||||
private captureEndpointing = false;
|
||||
// Non-null while a cloud provider owns the transcript; Java only decodes PCM.
|
||||
private cloudClient: CloudSttClient | null = null;
|
||||
// Raw-PCM tap (EvenHub mic apps): when active, the controller runs in the
|
||||
@@ -156,8 +163,13 @@ export class FaceclawVoiceControlBridge {
|
||||
*/
|
||||
startRawCapture(options: { communicator: any }): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
if (this.rawActive) return true;
|
||||
if (this.captureHolders.size > 0 || this.started) return false;
|
||||
if (this.rawActive && this.micIsLive()) return true;
|
||||
// An STT holder owns the mic even while its stream waits on a new glasses
|
||||
// session; the tap re-tries when the app's eligibility is re-evaluated.
|
||||
if (this.captureHolders.size > 0 || this.suspendedHolders.size > 0) return false;
|
||||
// A tap left over from a dead session is not something to share.
|
||||
if (this.started || this.rawActive) this.teardownCapture();
|
||||
this.suspendedRaw = false;
|
||||
this.ensureController();
|
||||
this.controller?.setCommunicator(options.communicator);
|
||||
this.controller?.setSaveRecordings(false);
|
||||
@@ -177,6 +189,7 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
|
||||
stopRawCapture(): void {
|
||||
this.suspendedRaw = false;
|
||||
if (!this.rawActive) return;
|
||||
this.rawActive = false;
|
||||
this.started = false;
|
||||
@@ -188,17 +201,26 @@ export class FaceclawVoiceControlBridge {
|
||||
// STT preempts the raw-PCM tap: a live transcription session owns the mic,
|
||||
// and an EvenHub app gets no audio for its duration.
|
||||
if (this.rawActive) this.stopRawCapture();
|
||||
const wasIdle = this.captureHolders.size === 0;
|
||||
// Whether the mic is already running is a question about the stream, not
|
||||
// about this set: a holder whose capture died with the glasses session
|
||||
// (or whose mic enable was refused) is still in it. Trusting the set here
|
||||
// let one stale holder silently swallow every later request for the
|
||||
// process's lifetime.
|
||||
const micLive = this.micIsLive();
|
||||
this.suspendedHolders.delete(holder);
|
||||
this.captureHolders.add(holder);
|
||||
if (!wasIdle) {
|
||||
if (micLive) {
|
||||
// Mic already running; the new holder just shares the existing stream
|
||||
// (transcripts are already broadcast to its listeners).
|
||||
return;
|
||||
}
|
||||
// Any leftover capture is dead; drop it before starting the new one.
|
||||
if (this.started) this.teardownCapture();
|
||||
this.ensureController();
|
||||
this.controller?.setCommunicator(options.communicator);
|
||||
this.controller?.setSaveRecordings(options.saveRecording);
|
||||
this.controller?.setEndpointing(Boolean(options.endpointing));
|
||||
this.captureEndpointing = Boolean(options.endpointing);
|
||||
this.controller?.setNoiseSuppression(Boolean(options.noiseSuppression));
|
||||
if (options.beamFilter) {
|
||||
this.controller?.setBeamFilter(true, Math.round(options.beamFilter.centerDeg), Math.round(options.beamFilter.halfWidthDeg));
|
||||
@@ -271,6 +293,9 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
|
||||
private releaseCapture(holder: CaptureHolder, commit: boolean): void {
|
||||
// A holder that gives up while its capture is parked must not be resumed
|
||||
// by the next session.
|
||||
this.suspendedHolders.delete(holder);
|
||||
if (!this.captureHolders.delete(holder)) {
|
||||
// Never held; still finish a dangling cloud commit if one is pending.
|
||||
if (commit) this.cloudClient?.finish();
|
||||
@@ -298,6 +323,75 @@ export class FaceclawVoiceControlBridge {
|
||||
|
||||
stop(): void {
|
||||
this.captureHolders.clear();
|
||||
this.suspendedHolders.clear();
|
||||
this.suspendedRaw = false;
|
||||
this.teardownCapture();
|
||||
this.setStatus("Voice control stopped.");
|
||||
}
|
||||
|
||||
/**
|
||||
* The glasses session ended under a live capture (transport drop, charging
|
||||
* case, EvenHub suspend). Its mic enable did not survive, so drop the local
|
||||
* capture state — leaving it in place would make the holder set claim a
|
||||
* stream that no longer exists — and remember who was holding so
|
||||
* resumeCapture() can bring the mic back on the next session.
|
||||
*/
|
||||
handleSessionEnded(): void {
|
||||
if (this.captureHolders.size === 0 && !this.started && !this.rawActive) return;
|
||||
for (const holder of this.captureHolders) {
|
||||
this.suspendedHolders.add(holder);
|
||||
}
|
||||
this.captureHolders.clear();
|
||||
this.suspendedRaw = this.rawActive;
|
||||
this.teardownCapture();
|
||||
if (this.suspendedHolders.size > 0) {
|
||||
this.setStatus("Waiting for the glasses...");
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a capture is parked waiting for a session to come back. */
|
||||
hasSuspendedCapture(): boolean {
|
||||
return this.suspendedHolders.size > 0 || this.suspendedRaw;
|
||||
}
|
||||
|
||||
/** Whether anyone holds the mic, including across a session outage. */
|
||||
isCaptureHeld(): boolean {
|
||||
return this.captureHolders.size > 0 || this.suspendedHolders.size > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* A fresh glasses session is ready (its display path warmed up, so a mic
|
||||
* enable will be accepted): restart the mic for every holder that was
|
||||
* capturing when the last session ended. Mic permission was granted for the
|
||||
* original capture and cannot be revoked without restarting the process, so
|
||||
* this does not ask again.
|
||||
*/
|
||||
resumeCapture(options: Omit<PushToTalkOptions, "endpointing">): void {
|
||||
const holders = Array.from(this.suspendedHolders);
|
||||
const resumeRaw = this.suspendedRaw;
|
||||
this.suspendedHolders.clear();
|
||||
this.suspendedRaw = false;
|
||||
for (const holder of holders) {
|
||||
this.acquireCapture(holder, { ...options, endpointing: this.captureEndpointing });
|
||||
}
|
||||
if (resumeRaw && !holders.length) {
|
||||
this.startRawCapture({ communicator: options.communicator });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the glasses mic is actually streaming. The holder set and
|
||||
* `started` are phone-side bookkeeping; the enable itself lives in the
|
||||
* glasses session, so the controller (via the communicator) is the only
|
||||
* authority on whether audio is still flowing.
|
||||
*/
|
||||
private micIsLive(): boolean {
|
||||
if (!global.isAndroid || !this.started) return false;
|
||||
return Boolean(this.controller?.isCapturing());
|
||||
}
|
||||
|
||||
/** Stop the native capture and any cloud client; leaves holders alone. */
|
||||
private teardownCapture(): void {
|
||||
this.rawActive = false;
|
||||
if (global.isAndroid) {
|
||||
this.controller?.stop();
|
||||
@@ -305,7 +399,6 @@ export class FaceclawVoiceControlBridge {
|
||||
this.started = false;
|
||||
this.cloudClient?.stop();
|
||||
this.cloudClient = null;
|
||||
this.setStatus("Voice control stopped.");
|
||||
}
|
||||
|
||||
private ensureController(): void {
|
||||
|
||||
Reference in new issue
Block a user