forked from Blendman974/faceclaw
Add initial voice-command support
Adds a single wakeword-style voice command, "screen on", and deals with all the wiring and permissions that entails. Currently this only supports using the phone's mic (or a bluetooth headset), not the G2 mic.
This commit is contained in:
1 parent
c4cb4f9f01
commit
9fbb985db6
35 files changed
+1722
-16
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
export type VoiceControlState = {
|
||||
status: string;
|
||||
};
|
||||
|
||||
export class FaceclawVoiceControlBridge {
|
||||
private readonly statusListeners = new Set<(state: VoiceControlState) => void>();
|
||||
private readonly wakeWordListeners = new Set<(keyword: string) => void>();
|
||||
private controller: any | null = null;
|
||||
private listenerProxy: any | null = null;
|
||||
private status = "Voice control stopped.";
|
||||
private started = false;
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statusListeners.add(listener);
|
||||
listener({ status: this.status });
|
||||
return () => this.statusListeners.delete(listener);
|
||||
}
|
||||
|
||||
onWakeWord(listener: (keyword: string) => void): () => void {
|
||||
this.wakeWordListeners.add(listener);
|
||||
return () => this.wakeWordListeners.delete(listener);
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.started || !global.isAndroid) return;
|
||||
this.ensureController();
|
||||
this.started = true;
|
||||
this.controller?.start();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (!this.started || !global.isAndroid) return;
|
||||
this.started = false;
|
||||
this.controller?.stop();
|
||||
this.setStatus("Voice control stopped.");
|
||||
}
|
||||
|
||||
private ensureController(): void {
|
||||
if (!global.isAndroid || this.controller) return;
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) {
|
||||
throw new Error("Android application context unavailable");
|
||||
}
|
||||
this.controller = new com.faceclaw.app.FaceclawVoiceController(context);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawVoiceControllerListener({
|
||||
onStatus: (status: string) => {
|
||||
this.setStatus(String(status));
|
||||
},
|
||||
onWakeWord: (keyword: string) => {
|
||||
for (const listener of this.wakeWordListeners) {
|
||||
listener(String(keyword));
|
||||
}
|
||||
},
|
||||
});
|
||||
this.controller.setListener(this.listenerProxy);
|
||||
}
|
||||
|
||||
private setStatus(status: string): void {
|
||||
this.status = status;
|
||||
for (const listener of this.statusListeners) {
|
||||
listener({ status });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const voiceControlBridge = new FaceclawVoiceControlBridge();
|
||||
Reference in new issue
Block a user