mirror of
https://github.com/R0m1k3/EveFlow.git
synced 2026-10-11 17:29:03 +02:00
feat: fin de phrase Silero, vision d'écran et actions locales (v2.3.0) (#13)
Écoute permanente - Silero VAD dans le worker sherpa-onnx (vad.start/audio/stop) ; le renderer envoie des trames de 128 ms pendant la commande et reçoit le segment WAV complet (speech-start / segment). Repli sur le VAD énergétique si le modèle n'est pas installé ou si l'option est désactivée. - Modèle « silero-vad » (0,6 Mo) dans le catalogue, section « Fin de phrase » dans Modèles locaux, réglage dans Paramètres → Micro. Vision d'écran - IPC system:screen-capture (desktopCapturer, JPEG 1600 px) ; bouton dans la barre de commande ; « regarde mon écran… » joint la capture à la question envoyée à Hermes (transport chat completions pour les images). Actions locales - IPC system:action à liste blanche : verrouiller la session, touches média (volume, mute, lecture, piste), ouvrir une application connue ou une URL http(s), presse-papiers, recherche de fichiers dans les dossiers utilisateur. - Routeur d'intentions FR/EN exécuté avant Hermes (« coupe le son », « ouvre Spotify », « verrouille la session »…), résultat affiché et lu ; désactivable. Correctif - Chat completions : les premiers fragments SSE arrivés avant l'événement de démarrage étaient perdus (premier mot manquant) ; ils sont maintenant rejoués. Tests : 26 tests unitaires (intentions locales ajoutées), e2e Electron sous Xvfb avec les vrais modèles (Silero : un segment par phrase, 6,7 s d'audio traités en 190 ms ; capture 109 ko reçue par Hermes ; intention locale traitée sans Hermes). Claude-Session: https://claude.ai/code/session_017Wn5VX9HNbJ7N54hR24u9Y Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
4f6074183f
commit
af8bdc5e9d
24 files changed
+691
-47
No files matched your search
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* Local "JARVIS" actions: screenshot for vision, and an allow-list of system actions
|
||||
* (lock, open an application or URL, media keys, clipboard, file search).
|
||||
*/
|
||||
import { app, clipboard, desktopCapturer, ipcMain, screen, shell } from 'electron';
|
||||
import { execFile } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { IPC } from '../../shared/ipc';
|
||||
import type { SystemAction, SystemActionResult } from '../../shared/bridge';
|
||||
import { log } from '../logger';
|
||||
|
||||
const MEDIA_KEYS: Record<string, number> = {
|
||||
'volume-up': 175,
|
||||
'volume-down': 174,
|
||||
mute: 173,
|
||||
'play-pause': 179,
|
||||
next: 176,
|
||||
previous: 177
|
||||
};
|
||||
|
||||
/** Applications the assistant may launch by name (Windows aliases + common Linux/macOS names). */
|
||||
const APP_ALIASES: Record<string, string[]> = {
|
||||
'bloc-notes': ['notepad.exe', 'gedit', 'TextEdit'],
|
||||
notepad: ['notepad.exe', 'gedit', 'TextEdit'],
|
||||
calculatrice: ['calc.exe', 'gnome-calculator', 'Calculator'],
|
||||
calc: ['calc.exe', 'gnome-calculator', 'Calculator'],
|
||||
explorateur: ['explorer.exe', 'nautilus', 'Finder'],
|
||||
explorer: ['explorer.exe', 'nautilus', 'Finder'],
|
||||
terminal: ['wt.exe', 'cmd.exe', 'gnome-terminal', 'Terminal'],
|
||||
cmd: ['cmd.exe'],
|
||||
powershell: ['powershell.exe'],
|
||||
paint: ['mspaint.exe'],
|
||||
chrome: ['chrome', 'google-chrome', 'Google Chrome'],
|
||||
edge: ['msedge', 'microsoft-edge', 'Microsoft Edge'],
|
||||
firefox: ['firefox', 'Firefox'],
|
||||
vscode: ['code', 'Visual Studio Code'],
|
||||
code: ['code', 'Visual Studio Code'],
|
||||
spotify: ['spotify', 'Spotify'],
|
||||
discord: ['discord', 'Discord'],
|
||||
steam: ['steam', 'Steam'],
|
||||
word: ['winword', 'Microsoft Word'],
|
||||
excel: ['excel', 'Microsoft Excel'],
|
||||
outlook: ['outlook', 'Microsoft Outlook'],
|
||||
teams: ['ms-teams', 'Microsoft Teams'],
|
||||
'task manager': ['taskmgr.exe'],
|
||||
'gestionnaire des tâches': ['taskmgr.exe'],
|
||||
paramètres: ['ms-settings:'],
|
||||
settings: ['ms-settings:']
|
||||
};
|
||||
|
||||
function run(cmd: string, args: string[], timeoutMs = 8000): Promise<{ code: number; out: string }> {
|
||||
return new Promise((resolve) => {
|
||||
execFile(cmd, args, { timeout: timeoutMs, windowsHide: true }, (err, stdout, stderr) => {
|
||||
resolve({ code: err ? 1 : 0, out: `${stdout}${stderr}`.trim() });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function openApp(name: string): Promise<SystemActionResult> {
|
||||
const key = name.trim().toLowerCase();
|
||||
if (!key || key.length > 40) return { ok: false, message: 'Nom d’application invalide' };
|
||||
const candidates = APP_ALIASES[key] ?? [key.replace(/[^a-z0-9 ._-]/gi, '')];
|
||||
if (process.platform === 'win32') {
|
||||
for (const candidate of candidates) {
|
||||
if (candidate.endsWith(':')) {
|
||||
await shell.openExternal(candidate);
|
||||
return { ok: true, message: `${name} ouvert` };
|
||||
}
|
||||
// `start` resolves App Paths, PATH and Start Menu names.
|
||||
const result = await run('cmd.exe', ['/c', 'start', '', candidate]);
|
||||
if (result.code === 0) return { ok: true, message: `${name} lancé` };
|
||||
}
|
||||
return { ok: false, message: `Impossible de lancer ${name}` };
|
||||
}
|
||||
for (const candidate of candidates) {
|
||||
const result = process.platform === 'darwin' ? await run('open', ['-a', candidate]) : await run('sh', ['-c', `command -v ${JSON.stringify(candidate)} >/dev/null && (nohup ${JSON.stringify(candidate)} >/dev/null 2>&1 &)`]);
|
||||
if (result.code === 0) return { ok: true, message: `${name} lancé` };
|
||||
}
|
||||
return { ok: false, message: `Application introuvable : ${name}` };
|
||||
}
|
||||
|
||||
async function lockSession(): Promise<SystemActionResult> {
|
||||
if (process.platform === 'win32') {
|
||||
const r = await run('rundll32.exe', ['user32.dll,LockWorkStation']);
|
||||
return { ok: r.code === 0, message: r.code === 0 ? 'Session verrouillée' : r.out };
|
||||
}
|
||||
if (process.platform === 'darwin') {
|
||||
const r = await run('osascript', ['-e', 'tell application "System Events" to keystroke "q" using {command down, control down}']);
|
||||
return { ok: r.code === 0, message: r.out };
|
||||
}
|
||||
const r = await run('sh', ['-c', 'loginctl lock-session || xdg-screensaver lock || gnome-screensaver-command -l']);
|
||||
return { ok: r.code === 0, message: r.code === 0 ? 'Session verrouillée' : r.out };
|
||||
}
|
||||
|
||||
async function mediaKey(key: string): Promise<SystemActionResult> {
|
||||
const code = MEDIA_KEYS[key];
|
||||
if (!code) return { ok: false, message: 'Touche inconnue' };
|
||||
if (process.platform === 'win32') {
|
||||
const script = `$s=Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void keybd_event(byte b,byte s,uint f,UIntPtr e);' -Name K -Namespace W -PassThru; $s::keybd_event(${code},0,0,[UIntPtr]::Zero); $s::keybd_event(${code},0,2,[UIntPtr]::Zero)`;
|
||||
const r = await run('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script]);
|
||||
return { ok: r.code === 0, message: r.code === 0 ? key : r.out };
|
||||
}
|
||||
const xdo: Record<string, string> = { 'volume-up': 'XF86AudioRaiseVolume', 'volume-down': 'XF86AudioLowerVolume', mute: 'XF86AudioMute', 'play-pause': 'XF86AudioPlay', next: 'XF86AudioNext', previous: 'XF86AudioPrev' };
|
||||
const r = await run('sh', ['-c', `xdotool key ${xdo[key]}`]);
|
||||
return { ok: r.code === 0, message: r.code === 0 ? key : 'xdotool indisponible' };
|
||||
}
|
||||
|
||||
async function findFiles(query: string): Promise<SystemActionResult> {
|
||||
const needle = query.trim().toLowerCase();
|
||||
if (needle.length < 2) return { ok: false, message: 'Recherche trop courte' };
|
||||
const roots = ['Documents', 'Desktop', 'Downloads', 'Pictures'].map((d) => path.join(os.homedir(), d));
|
||||
const hits: string[] = [];
|
||||
const deadline = Date.now() + 4000;
|
||||
const walk = async (dir: string, depth: number) => {
|
||||
if (depth > 4 || hits.length >= 25 || Date.now() > deadline) return;
|
||||
let entries: fs.Dirent[] = [];
|
||||
try {
|
||||
entries = await fs.promises.readdir(dir, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const entry of entries) {
|
||||
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
|
||||
const full = path.join(dir, entry.name);
|
||||
if (entry.name.toLowerCase().includes(needle)) hits.push(full);
|
||||
if (entry.isDirectory()) await walk(full, depth + 1);
|
||||
if (hits.length >= 25) return;
|
||||
}
|
||||
};
|
||||
for (const root of roots) await walk(root, 0);
|
||||
return { ok: true, message: `${hits.length} résultat(s)`, data: hits };
|
||||
}
|
||||
|
||||
export async function captureScreen(maxWidth = 1600): Promise<string> {
|
||||
const display = screen.getPrimaryDisplay();
|
||||
const scale = Math.min(1, maxWidth / display.size.width);
|
||||
const sources = await desktopCapturer.getSources({
|
||||
types: ['screen'],
|
||||
thumbnailSize: { width: Math.round(display.size.width * scale), height: Math.round(display.size.height * scale) }
|
||||
});
|
||||
const primary = sources.find((s) => s.display_id === String(display.id)) ?? sources[0];
|
||||
if (!primary) throw new Error('Aucun écran capturable');
|
||||
return primary.thumbnail.toJPEG(82).toString('base64').replace(/^/, 'data:image/jpeg;base64,');
|
||||
}
|
||||
|
||||
export async function runSystemAction(action: SystemAction): Promise<SystemActionResult> {
|
||||
switch (action.type) {
|
||||
case 'lock':
|
||||
return lockSession();
|
||||
case 'open-app':
|
||||
return openApp(String(action.name ?? ''));
|
||||
case 'open-url': {
|
||||
const url = String(action.url ?? '');
|
||||
if (!/^https?:\/\//i.test(url) || url.length > 2048) return { ok: false, message: 'URL refusée' };
|
||||
await shell.openExternal(url);
|
||||
return { ok: true, message: 'Ouvert dans le navigateur' };
|
||||
}
|
||||
case 'media':
|
||||
return mediaKey(String(action.key));
|
||||
case 'clipboard-read':
|
||||
return { ok: true, data: (await clipboard.readText()).slice(0, 20_000) };
|
||||
case 'clipboard-write':
|
||||
await clipboard.writeText(String(action.text ?? '').slice(0, 100_000));
|
||||
return { ok: true, message: 'Copié dans le presse-papiers' };
|
||||
case 'find-files':
|
||||
return findFiles(String(action.query ?? ''));
|
||||
default:
|
||||
return { ok: false, message: 'Action inconnue' };
|
||||
}
|
||||
}
|
||||
|
||||
export function registerSystemIpc(): void {
|
||||
ipcMain.handle(IPC.screenCapture, (_e, maxWidth?: number) => captureScreen(Number(maxWidth) || 1600));
|
||||
ipcMain.handle(IPC.systemAction, async (_e, action: SystemAction) => {
|
||||
if (!action || typeof action.type !== 'string') throw new Error('Action invalide');
|
||||
log('INFO', 'system', `action ${action.type}`);
|
||||
const result = await runSystemAction(action);
|
||||
if (!result.ok) log('WARN', 'system', `action ${action.type} failed: ${result.message}`);
|
||||
return result;
|
||||
});
|
||||
void app;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { getSharedDirectory, registerFilesIpc } from './ipc/files';
|
||||
import { createMainWindow, getMainWindow, getWindowMode, setWindowMode, toggleWindowVisibility, windowEvents } from './window';
|
||||
import { getWebhookStatus, startWebhookServer, stopWebhookServer } from './webhook';
|
||||
import { registerVoiceIpc } from './voice/ipc';
|
||||
import { registerSystemIpc } from './ipc/system';
|
||||
import { stopEngine } from './voice/engine';
|
||||
|
||||
// Audio playback must never be blocked behind a user gesture (TTS starts on incoming events).
|
||||
@@ -159,6 +160,7 @@ async function boot(): Promise<void> {
|
||||
registerFilesIpc();
|
||||
registerCoreIpc();
|
||||
registerVoiceIpc();
|
||||
registerSystemIpc();
|
||||
createMainWindow();
|
||||
createTray();
|
||||
registerShortcuts();
|
||||
|
||||
+10
-4
@@ -13,8 +13,8 @@ import {
|
||||
type WebhookStatus,
|
||||
type WindowMode
|
||||
} from '../shared/ipc';
|
||||
import type { EveFlowBridge, Unsubscribe } from '../shared/bridge';
|
||||
import { VOICE_IPC, type KwsDetection, type KwsStartRequest, type SynthesizeRequest, type SynthesizeResult, type TranscribeRequest, type TranscribeResult, type VoiceDownloadProgress, type VoiceEngineStatus, type VoiceModelStatus } from '../shared/voice';
|
||||
import type { EveFlowBridge, SystemAction, SystemActionResult, Unsubscribe } from '../shared/bridge';
|
||||
import { VOICE_IPC, type KwsDetection, type KwsStartRequest, type VadEvent, type VadStartRequest, type SynthesizeRequest, type SynthesizeResult, type TranscribeRequest, type TranscribeResult, type VoiceDownloadProgress, type VoiceEngineStatus, type VoiceModelStatus } from '../shared/voice';
|
||||
|
||||
function subscribe<T>(channel: string, callback: (payload: T) => void): Unsubscribe {
|
||||
const listener = (_event: Electron.IpcRendererEvent, payload: T) => callback(payload);
|
||||
@@ -43,7 +43,9 @@ const api: EveFlowBridge = {
|
||||
},
|
||||
system: {
|
||||
metrics: () => ipcRenderer.invoke(IPC.metrics) as Promise<SystemMetrics>,
|
||||
appInfo: () => ipcRenderer.invoke(IPC.appInfo) as Promise<AppInfo>
|
||||
appInfo: () => ipcRenderer.invoke(IPC.appInfo) as Promise<AppInfo>,
|
||||
captureScreen: (maxWidth?: number) => ipcRenderer.invoke(IPC.screenCapture, maxWidth) as Promise<string>,
|
||||
action: (action: SystemAction) => ipcRenderer.invoke(IPC.systemAction, action) as Promise<SystemActionResult>
|
||||
},
|
||||
files: {
|
||||
readLocal: (filePath: string) => ipcRenderer.invoke(IPC.readLocalFile, filePath) as Promise<string>,
|
||||
@@ -73,7 +75,11 @@ const api: EveFlowBridge = {
|
||||
kwsStart: (req: KwsStartRequest) => ipcRenderer.invoke(VOICE_IPC.kwsStart, req) as Promise<{ accepted: string[]; rejected: string[] }>,
|
||||
kwsStop: () => ipcRenderer.invoke(VOICE_IPC.kwsStop) as Promise<void>,
|
||||
kwsAudio: (pcm: Uint8Array, sampleRate: number) => ipcRenderer.send(VOICE_IPC.kwsAudio, pcm, sampleRate),
|
||||
onKwsDetected: (cb: (detection: KwsDetection) => void) => subscribe<KwsDetection>(VOICE_IPC.kwsDetected, cb)
|
||||
onKwsDetected: (cb: (detection: KwsDetection) => void) => subscribe<KwsDetection>(VOICE_IPC.kwsDetected, cb),
|
||||
vadStart: (req: VadStartRequest) => ipcRenderer.invoke(VOICE_IPC.vadStart, req) as Promise<void>,
|
||||
vadStop: () => ipcRenderer.invoke(VOICE_IPC.vadStop) as Promise<void>,
|
||||
vadAudio: (pcm: Uint8Array, sampleRate: number) => ipcRenderer.send(VOICE_IPC.vadAudio, pcm, sampleRate),
|
||||
onVadEvent: (cb: (event: VadEvent) => void) => subscribe<VadEvent>(VOICE_IPC.vadEvent, cb)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -86,6 +86,19 @@ export const VOICE_CATALOG: VoiceModelSpec[] = [
|
||||
],
|
||||
recommended: true
|
||||
},
|
||||
{
|
||||
id: 'silero-vad',
|
||||
kind: 'vad',
|
||||
engine: 'silero',
|
||||
name: 'Silero VAD (fin de phrase neuronale, 0,6 Mo)',
|
||||
description: 'Détecte précisément le début et la fin de la parole pendant l’écoute permanente ; moins de faux départs sur le bruit.',
|
||||
languages: ['multi'],
|
||||
sizeMb: 1,
|
||||
url: `${ASR}/silero_vad.onnx`,
|
||||
dir: 'silero-vad',
|
||||
files: ['silero_vad.onnx'],
|
||||
recommended: true
|
||||
},
|
||||
{
|
||||
id: 'kokoro-v1',
|
||||
kind: 'tts',
|
||||
|
||||
@@ -6,7 +6,7 @@ import { utilityProcess, type UtilityProcess, type WebContents } from 'electron'
|
||||
import { createHash } from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { VOICE_IPC, type KwsDetection, type KwsStartRequest, type SynthesizeRequest, type SynthesizeResult, type TranscribeRequest, type TranscribeResult, type VoiceEngineStatus } from '../../shared/voice';
|
||||
import { VOICE_IPC, type KwsDetection, type KwsStartRequest, type SynthesizeRequest, type SynthesizeResult, type TranscribeRequest, type TranscribeResult, type VadEvent, type VadStartRequest, type VoiceEngineStatus } from '../../shared/voice';
|
||||
import { buildKeywordsFile, parseTokens } from '../../shared/keywords';
|
||||
import { findModel } from './catalog';
|
||||
import { isInstalled, modelDir, modelsDir } from './models';
|
||||
@@ -16,6 +16,8 @@ import { log } from '../logger';
|
||||
let kwsSubscriber: WebContents | null = null;
|
||||
let kwsActive = false;
|
||||
let kwsRequest: KwsStartRequest | null = null;
|
||||
let vadSubscriber: WebContents | null = null;
|
||||
let vadActive = false;
|
||||
|
||||
interface Pending {
|
||||
resolve: (value: unknown) => void;
|
||||
@@ -42,7 +44,20 @@ function spawn(): UtilityProcess {
|
||||
child = proc;
|
||||
proc.stdout?.on('data', (d: Buffer) => log('DEBUG', 'voice-worker', d.toString().trim()));
|
||||
proc.stderr?.on('data', (d: Buffer) => log('WARN', 'voice-worker', d.toString().trim()));
|
||||
proc.on('message', (msg: { id?: number; type?: string; ok?: boolean; result?: unknown; error?: string; keyword?: string; at?: number }) => {
|
||||
proc.on('message', (msg: { id?: number; type?: string; ok?: boolean; result?: unknown; error?: string; keyword?: string; at?: number; event?: { type: string; wav?: string; durationSec?: number } }) => {
|
||||
if (msg.type === 'vad.event' && msg.event) {
|
||||
if (vadSubscriber && !vadSubscriber.isDestroyed()) {
|
||||
const ev = msg.event;
|
||||
const payload: VadEvent =
|
||||
ev.type === 'segment' && ev.wav
|
||||
? { type: 'segment', wav: new Uint8Array(Buffer.from(ev.wav, 'base64')), durationSec: ev.durationSec ?? 0 }
|
||||
: ev.type === 'speech-start'
|
||||
? { type: 'speech-start' }
|
||||
: { type: 'error', message: 'événement VAD inconnu' };
|
||||
vadSubscriber.send(VOICE_IPC.vadEvent, payload);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (msg.type === 'kws.detected') {
|
||||
if (kwsSubscriber && !kwsSubscriber.isDestroyed()) {
|
||||
kwsSubscriber.send(VOICE_IPC.kwsDetected, { keyword: msg.keyword ?? '', at: msg.at ?? Date.now() } satisfies KwsDetection);
|
||||
@@ -180,6 +195,35 @@ export function kwsFeed(pcm: Uint8Array, sampleRate: number): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** Neural end-of-speech detection (Silero) on frames streamed by the renderer. */
|
||||
export async function vadStart(req: VadStartRequest, sender: WebContents): Promise<void> {
|
||||
const spec = findModel(req.modelId);
|
||||
if (!spec || spec.kind !== 'vad') throw new Error('Modèle VAD introuvable');
|
||||
if (!isInstalled(spec)) throw new Error('Silero VAD non installé (Paramètres → Modèles locaux).');
|
||||
vadSubscriber = sender;
|
||||
await request(
|
||||
{ type: 'vad.start', model: { id: spec.id, engine: spec.engine, dir: modelDir(spec), files: spec.files }, silenceMs: req.silenceMs, threshold: req.threshold, maxUtteranceSec: req.maxUtteranceSec },
|
||||
30_000
|
||||
);
|
||||
vadActive = true;
|
||||
}
|
||||
|
||||
export async function vadStop(): Promise<void> {
|
||||
vadActive = false;
|
||||
vadSubscriber = null;
|
||||
if (child) await request({ type: 'vad.stop' }, 10_000).catch(() => undefined);
|
||||
}
|
||||
|
||||
export function vadFeed(pcm: Uint8Array, sampleRate: number): void {
|
||||
if (!vadActive || !child) return;
|
||||
const b64 = Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength).toString('base64');
|
||||
try {
|
||||
child.postMessage({ id: -1, type: 'vad.audio', pcm: b64, sampleRate });
|
||||
} catch (err) {
|
||||
log('WARN', 'voice', `vad feed failed: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Native model memory is only released deterministically by restarting the worker. */
|
||||
export function unload(_modelId?: string): Promise<unknown> {
|
||||
stopEngine();
|
||||
|
||||
+11
-2
@@ -1,6 +1,6 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { VOICE_IPC, type KwsStartRequest, type SynthesizeRequest, type TranscribeRequest } from '../../shared/voice';
|
||||
import { engineStatus, kwsFeed, kwsStart, kwsStop, synthesize, transcribe, unload } from './engine';
|
||||
import { VOICE_IPC, type KwsStartRequest, type SynthesizeRequest, type TranscribeRequest, type VadStartRequest } from '../../shared/voice';
|
||||
import { engineStatus, kwsFeed, kwsStart, kwsStop, synthesize, transcribe, unload, vadFeed, vadStart, vadStop } from './engine';
|
||||
import { cancelDownload, downloadModel, listModels, removeModel } from './models';
|
||||
|
||||
export function registerVoiceIpc(): void {
|
||||
@@ -32,6 +32,15 @@ export function registerVoiceIpc(): void {
|
||||
return kwsStart({ ...req, keywords: req.keywords.filter((k) => typeof k === 'string'), sensitivity: Number(req.sensitivity) || 3 }, event.sender);
|
||||
});
|
||||
ipcMain.handle(VOICE_IPC.kwsStop, () => kwsStop());
|
||||
ipcMain.handle(VOICE_IPC.vadStart, (event, req: VadStartRequest) => {
|
||||
if (!req || typeof req.modelId !== 'string') throw new Error('Requête invalide');
|
||||
return vadStart({ modelId: req.modelId, silenceMs: Number(req.silenceMs) || 700, threshold: Number(req.threshold) || 0.5, maxUtteranceSec: Number(req.maxUtteranceSec) || 30 }, event.sender);
|
||||
});
|
||||
ipcMain.handle(VOICE_IPC.vadStop, () => vadStop());
|
||||
ipcMain.on(VOICE_IPC.vadAudio, (_e, pcm: unknown, sampleRate: unknown) => {
|
||||
if (!(pcm instanceof Uint8Array) || pcm.byteLength === 0 || pcm.byteLength > 1024 * 1024) return;
|
||||
vadFeed(pcm, typeof sampleRate === 'number' && sampleRate > 0 ? sampleRate : 16000);
|
||||
});
|
||||
ipcMain.on(VOICE_IPC.kwsAudio, (_e, pcm: unknown, sampleRate: unknown) => {
|
||||
if (!(pcm instanceof Uint8Array) || pcm.byteLength === 0 || pcm.byteLength > 1024 * 1024) return;
|
||||
kwsFeed(pcm, typeof sampleRate === 'number' && sampleRate > 0 ? sampleRate : 16000);
|
||||
|
||||
@@ -21,7 +21,10 @@ type Request =
|
||||
| { id: number; type: 'unload'; modelId?: string }
|
||||
| { id: number; type: 'kws.start'; model: ModelRef; keywordsFile: string; threshold: number; score: number }
|
||||
| { id: number; type: 'kws.audio'; pcm: string; sampleRate: number }
|
||||
| { id: number; type: 'kws.stop' };
|
||||
| { id: number; type: 'kws.stop' }
|
||||
| { id: number; type: 'vad.start'; model: ModelRef; silenceMs: number; threshold: number; maxUtteranceSec: number }
|
||||
| { id: number; type: 'vad.audio'; pcm: string; sampleRate: number }
|
||||
| { id: number; type: 'vad.stop' };
|
||||
|
||||
type Response = { id: number; ok: true; result: unknown } | { id: number; ok: false; error: string };
|
||||
|
||||
@@ -32,6 +35,16 @@ type Sherpa = {
|
||||
decode: (s: unknown) => void;
|
||||
getResult: (s: unknown) => { text: string; lang?: string };
|
||||
};
|
||||
Vad: new (config: unknown, bufferSizeInSeconds: number) => {
|
||||
acceptWaveform: (samples: Float32Array) => void;
|
||||
isEmpty: () => boolean;
|
||||
isDetected: () => boolean;
|
||||
pop: () => void;
|
||||
clear: () => void;
|
||||
front: (enableExternalBuffer?: boolean) => { start: number; samples: Float32Array };
|
||||
reset: () => void;
|
||||
flush: () => void;
|
||||
};
|
||||
KeywordSpotter: new (config: unknown) => {
|
||||
createStream: () => KwsStream;
|
||||
isReady: (s: KwsStream) => boolean;
|
||||
@@ -50,6 +63,7 @@ type Sherpa = {
|
||||
type KwsStream = { acceptWaveform: (w: { sampleRate: number; samples: Float32Array }) => void };
|
||||
let sherpa: Sherpa | null = null;
|
||||
let kws: { spotter: InstanceType<Sherpa['KeywordSpotter']>; stream: KwsStream } | null = null;
|
||||
let vad: { detector: InstanceType<Sherpa['Vad']>; speaking: boolean; windowSize: number; carry: Float32Array } | null = null;
|
||||
let notify: ((message: unknown) => void) | null = null;
|
||||
let loadError: string | null = null;
|
||||
|
||||
@@ -275,9 +289,74 @@ function feedKws(pcmBase64: string, sampleRate: number): void {
|
||||
}
|
||||
}
|
||||
|
||||
function startVad(model: ModelRef, silenceMs: number, threshold: number, maxUtteranceSec: number): void {
|
||||
const s = loadSherpa();
|
||||
const windowSize = 512;
|
||||
const detector = new s.Vad(
|
||||
{
|
||||
sileroVad: {
|
||||
model: path.join(model.dir, 'silero_vad.onnx'),
|
||||
threshold: Math.max(0.1, Math.min(0.95, threshold)),
|
||||
minSilenceDuration: Math.max(0.15, silenceMs / 1000),
|
||||
minSpeechDuration: 0.2,
|
||||
windowSize,
|
||||
maxSpeechDuration: Math.max(3, maxUtteranceSec)
|
||||
},
|
||||
sampleRate: 16000,
|
||||
numThreads: 1,
|
||||
provider: 'cpu',
|
||||
debug: 0
|
||||
},
|
||||
Math.max(10, maxUtteranceSec + 5)
|
||||
);
|
||||
vad = { detector, speaking: false, windowSize, carry: new Float32Array(0) };
|
||||
}
|
||||
|
||||
function feedVad(pcmBase64: string, sampleRate: number): void {
|
||||
if (!vad) return;
|
||||
const bytes = Buffer.from(pcmBase64, 'base64');
|
||||
const int16 = new Int16Array(bytes.buffer, bytes.byteOffset, Math.floor(bytes.byteLength / 2));
|
||||
let samples: Float32Array = new Float32Array(int16.length);
|
||||
for (let i = 0; i < int16.length; i++) samples[i] = int16[i] / 32768;
|
||||
if (sampleRate !== 16000) samples = resampleTo16k(samples, sampleRate);
|
||||
// Silero expects fixed windows: keep the remainder for the next frame.
|
||||
const merged = new Float32Array(vad.carry.length + samples.length);
|
||||
merged.set(vad.carry, 0);
|
||||
merged.set(samples, vad.carry.length);
|
||||
const usable = merged.length - (merged.length % vad.windowSize);
|
||||
for (let i = 0; i < usable; i += vad.windowSize) {
|
||||
vad.detector.acceptWaveform(merged.subarray(i, i + vad.windowSize));
|
||||
const detected = vad.detector.isDetected();
|
||||
if (detected && !vad.speaking) {
|
||||
vad.speaking = true;
|
||||
notify?.({ type: 'vad.event', event: { type: 'speech-start' } });
|
||||
}
|
||||
while (!vad.detector.isEmpty()) {
|
||||
const segment = vad.detector.front(false);
|
||||
vad.detector.pop();
|
||||
vad.speaking = false;
|
||||
const wav = encodeWav(segment.samples, 16000);
|
||||
notify?.({
|
||||
type: 'vad.event',
|
||||
event: { type: 'segment', wav: Buffer.from(wav.buffer, wav.byteOffset, wav.byteLength).toString('base64'), durationSec: segment.samples.length / 16000 }
|
||||
});
|
||||
}
|
||||
}
|
||||
vad.carry = merged.slice(usable);
|
||||
}
|
||||
|
||||
// ── request handling ───────────────────────────────────────────────────────
|
||||
function handle(req: Request): unknown {
|
||||
switch (req.type) {
|
||||
case 'vad.start':
|
||||
startVad(req.model, req.silenceMs, req.threshold, req.maxUtteranceSec);
|
||||
return { ok: true };
|
||||
case 'vad.audio':
|
||||
feedVad(req.pcm, req.sampleRate);
|
||||
return { ok: true };
|
||||
case 'vad.stop':
|
||||
vad = null;
|
||||
return { ok: true };
|
||||
case 'kws.start':
|
||||
startKws(req.model, req.keywordsFile, req.threshold, req.score);
|
||||
return { ok: true };
|
||||
@@ -331,6 +410,7 @@ function handle(req: Request): unknown {
|
||||
recognizers.clear();
|
||||
synthesizers.clear();
|
||||
kws = null;
|
||||
vad = null;
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user