add expo-audio. work on memory issue when reading file.

This commit is contained in:
Jordan Hewitt
2025-03-11 18:35:37 -07:00
parent dca3987e18
commit f0a722b3fb
10 changed files with 178 additions and 69 deletions

View File

@ -1,5 +1,9 @@
import { TextDecoder } from "util";
export async function arrbufToStr(arrayBuffer : ArrayBuffer) {
export function arrbufToStr(arrayBuffer : ArrayBuffer) {
return new TextDecoder().decode(new Uint8Array(arrayBuffer));
}
export function strToArrBuf(input : string) : Uint8Array<ArrayBufferLike> {
return new TextEncoder().encode(input)
}

View File

@ -3,7 +3,7 @@ import * as FileSystem from "expo-file-system";
import { File, Paths } from "expo-file-system/next";
import { getDb } from "./db";
import * as Crypto from "expo-crypto";
import { arrbufToStr } from "./util";
import { arrbufToStr, strToArrBuf } from "./util";
export const WHISPER_MODEL_PATH = Paths.join(
FileSystem.documentDirectory || "file:///",
@ -154,16 +154,24 @@ export class WhisperFile {
console.debug("%s does not exist", this.targetPath);
return undefined;
}
return await Crypto.digest(
const strData = await FileSystem.readAsStringAsync(this.targetPath, {
encoding: FileSystem.EncodingType.Base64,
});
const data = strToArrBuf(strData);
const digest = await Crypto.digest(
Crypto.CryptoDigestAlgorithm.SHA256,
this.targetFile.bytes()
data
);
return digest;
}
public async updateTargetHash() {
const targetSha = await this.getTargetSha();
if (!targetSha) return;
this.target_hash = await arrbufToStr(targetSha);
this.target_hash = arrbufToStr(targetSha);
}
get isHashValid() {
@ -239,8 +247,8 @@ export class WhisperFile {
options: {
onData?: DownloadCallback | undefined;
} = {
onData: undefined,
}
onData: undefined,
}
) {
await this.syncHfMetadata();
@ -254,24 +262,51 @@ export class WhisperFile {
// Check for the existence of the target file
// If it exists, load the existing data.
await this.updateTargetExistence();
const existingData = this.does_target_exist
? this.targetFile.text()
: undefined;
// Create the resumable.
return FileSystem.createDownloadResumable(
this.modelUrl,
this.targetPath,
{},
async (data: FileSystem.DownloadProgressData) => {
this.download_data = data;
await this.syncHfMetadata();
await this.updateTargetHash();
await this.updateTargetExistence();
if (options.onData) await options.onData(this);
},
existingData ? existingData : undefined
);
try {
const existingData = this.does_target_exist
? await FileSystem.readAsStringAsync(this.targetPath, {
encoding: FileSystem.EncodingType.Base64,
})
: undefined;
// Create the resumable.
return FileSystem.createDownloadResumable(
this.modelUrl,
this.targetPath,
{},
async (data: FileSystem.DownloadProgressData) => {
// console.debug("yes, I'm still downloading");
try {
this.download_data = data;
} catch (err) {
console.error("Failed to set downloadData: %s", err);
}
try {
await this.syncHfMetadata();
} catch (err) {
console.error("Failed to update HuggingFace metadata: %s", err)
}
try {
await this.updateTargetHash();
} catch (er) {
console.error("Failed to update target hash: %s", er);
}
try {
await this.updateTargetExistence();
} catch (err) {
console.error("Failed to update target existence: %s", err)
}
if (options.onData) await options.onData(this);
},
existingData ? existingData : undefined
);
} catch (err) {
console.error("Could not read %s: %s", this.targetPath, err);
}
}
}