UNPKG

react-native-executorch

Version:

An easy way to run AI models in React Native with ExecuTorch

28 lines (25 loc) 935 B
import { ResourceFetcher } from '../../utils/ResourceFetcher'; import { ResourceSource } from '../../types/common'; import { Segment } from '../../types/vad'; import { ETError, getError } from '../../Error'; import { BaseModule } from '../BaseModule'; export class VADModule extends BaseModule { async load( model: { modelSource: ResourceSource }, onDownloadProgressCallback: (progress: number) => void = () => {} ): Promise<void> { const paths = await ResourceFetcher.fetch( onDownloadProgressCallback, model.modelSource ); if (paths === null || paths.length < 1) { throw new Error('Download interrupted.'); } this.nativeModule = global.loadVAD(paths[0] || ''); } async forward(waveform: Float32Array): Promise<Segment[]> { if (this.nativeModule == null) throw new Error(getError(ETError.ModuleNotLoaded)); return await this.nativeModule.generate(waveform); } }