/* Copyright 2023-2026 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /// /// import { Eagle } from './eagle'; import { EagleWorkerProcessRequest, EagleWorkerInitRequest, EagleWorkerRequest, PvStatus, } from './types'; import { EagleError } from "./eagle_errors"; let eagle: Eagle | null = null; const initRequest = async (request: EagleWorkerInitRequest): Promise => { if (eagle !== null) { return { command: 'error', status: PvStatus.INVALID_STATE, shortMessage: 'Eagle has already been initialized', }; } try { Eagle.setWasmSimd(request.wasmSimd); Eagle.setWasmSimdLib(request.wasmSimdLib); Eagle.setWasmPThread(request.wasmPThread); Eagle.setWasmPThreadLib(request.wasmPThreadLib); Eagle.setSdk(request.sdk); eagle = await Eagle._init( request.accessKey, request.modelPath, request.options ); return { command: 'ok', minProcessSamples: eagle.minProcessSamples, sampleRate: eagle.sampleRate, version: eagle.version, }; } catch (e: any) { if (e instanceof EagleError) { return { command: 'error', status: e.status, shortMessage: e.shortMessage, messageStack: e.messageStack }; } else { return { command: 'error', status: PvStatus.RUNTIME_ERROR, shortMessage: e.message }; } } }; const processRequest = async ( request: EagleWorkerProcessRequest ): Promise => { if (eagle === null) { return { command: 'error', status: PvStatus.INVALID_STATE, shortMessage: 'Eagle has not been initialized', }; } try { const scores = await eagle.process( request.inputFrame, request.speakerProfiles ); return { command: 'ok', scores, }; } catch (e: any) { if (e instanceof EagleError) { return { command: 'error', status: e.status, shortMessage: e.shortMessage, messageStack: e.messageStack }; } else { return { command: 'error', status: PvStatus.RUNTIME_ERROR, shortMessage: e.message }; } } }; const releaseRequest = async (): Promise => { if (eagle !== null) { await eagle.release(); eagle = null; close(); } return { command: 'ok', }; }; /** * Eagle worker handler. */ self.onmessage = async function ( event: MessageEvent ): Promise { switch (event.data.command) { case 'init': self.postMessage(await initRequest(event.data)); break; case 'process': self.postMessage(await processRequest(event.data)); break; case 'release': self.postMessage(await releaseRequest()); break; default: self.postMessage({ command: 'failed', // @ts-ignore message: `Unrecognized command: ${event.data.command}`, }); } };