Skip to content

Commit

Permalink
bug fixes: use timeout for async file saving / offline plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Fr0stbyteR authored and sletz committed Apr 30, 2024
1 parent 7f039c9 commit bbd0bbc
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ let server = "https://faustservicecloud.grame.fr";
const PROJECT_DIR = "/usr/share/project/";

$(async () => {
const { setTimeout } = window;
const { instantiateFaustModuleFromFile, LibFaust, FaustCompiler, FaustSvgDiagrams, FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("@grame/faustwasm");
const faustModule = await instantiateFaustModuleFromFile("faustwasm/libfaust-wasm.js");
const libFaust = new LibFaust(faustModule);
Expand Down Expand Up @@ -490,7 +491,7 @@ $(async () => {
isCompilingDsp = false;
return { success: true };
};
let rtCompileTimer: NodeJS.Timeout;
let rtCompileTimer: number;
const audioEnv: FaustEditorAudioEnv = {
dspConnectedToInput: false,
dspConnectedToOutput: false,
Expand Down Expand Up @@ -544,6 +545,7 @@ $(async () => {
uiEnv.analyser.drawHandler = uiEnv.plotScope.draw;
uiEnv.analyser.getSampleRate = () => (compileOptions.plotMode === "offline" ? compileOptions.plotSR : audioEnv.audioCtx.sampleRate);
await loadProject();
let saveTimeout: number;
uiEnv.fileManager = new FileManager({
container: $<HTMLDivElement>("#filemanager")[0],
fs: libFaust.fs(),
Expand All @@ -565,21 +567,24 @@ $(async () => {
showError(e);
}
*/
try {
const exist = await new Promise<boolean>((resolve, reject) => bfs.exists(fileName, resolve));
if (exist) {
await new Promise<void>((resolve, reject) => bfs.unlink(fileName, (e) => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(async () => {
try {
const exist = await new Promise<boolean>((resolve, reject) => bfs.exists(fileName, resolve));
if (exist) {
await new Promise<void>((resolve, reject) => bfs.unlink(fileName, (e) => {
if (e) reject(e);
else resolve();
}));
}
await new Promise<void>((resolve, reject) => bfs.writeFile(fileName, typeof content === "string" ? content : Buffer.from(content), typeof content === "string" ? { encoding: "utf8" } : {}, (e) => {
if (e) reject(e);
else resolve();
}));
} catch (e) {
showError(e);
}
await new Promise<void>((resolve, reject) => bfs.writeFile(fileName, typeof content === "string" ? content : Buffer.from(content), typeof content === "string" ? { encoding: "utf8" } : {}, (e) => {
if (e) reject(e);
else resolve();
}));
} catch (e) {
showError(e);
}
}, 1000);
clearTimeout(rtCompileTimer);
if (compileOptions.realtimeCompile) rtCompileTimer = setTimeout(audioEnv.dsp ? runDsp : updateDiagram, 1000, mainCode);
},
Expand Down Expand Up @@ -735,7 +740,7 @@ $(async () => {
const generator = new FaustMonoDspGenerator();
await generator.compile(faustCompiler, "main", code, args.join(" "));
const soundfileList = generator.getSoundfileList();
const soundfiles = await loadSoundfiles(new OfflineAudioContext({ sampleRate: plotSR, length: 0 }), soundfileList);
const soundfiles = await loadSoundfiles(new OfflineAudioContext({ sampleRate: plotSR, length: 1 }), soundfileList);
generator.addSoundfiles(soundfiles);
const processor = await generator.createOfflineProcessor(plotSR, 128);
const output = processor.render([], plot);
Expand Down

0 comments on commit bbd0bbc

Please sign in to comment.