Skip to content

Commit

Permalink
adapt addSoundfiles method, bug fixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Fr0stbyteR committed Apr 29, 2024
1 parent 67dd8a0 commit d9fbcb1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@babel/preset-typescript": "^7.18.6",
"@babel/runtime": "^7.18.9",
"@fortawesome/fontawesome-free": "^5.15.3",
"@grame/faustwasm": "^0.2.0",
"@grame/faustwasm": "^0.2.1",
"@shren/faust-ui": "^1.1.9",
"@types/bootstrap": "^4.6.0",
"@types/jquery": "^3.5.5",
Expand Down
4 changes: 2 additions & 2 deletions src/FileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type TOptions = {
path?: string;
mainFile?: string;
selectHandler?: (name: string, content: string, mainCode: string) => any;
saveHandler?: (name: string, content: string, mainCode: string) => any;
saveHandler?: (name: string, content: string | Uint8Array, mainCode: string) => any;
deleteHandler?: (name: string, mainCode: string) => any;
mainFileChangeHandler?: (name: string, mainCode: string) => any;
};
Expand Down Expand Up @@ -202,7 +202,7 @@ export class FileManager {
const file = e.dataTransfer.files[0];
const reader = new FileReader();
reader.onload = () => {
const content = reader.result instanceof ArrayBuffer ? new Uint8Array(reader.result) : reader.result.toString();
const content = typeof reader.result === "string" ? reader.result.toString() : new Uint8Array(reader.result);
const fileName = this.newFile(file.name, content);
this.select(fileName);
};
Expand Down
46 changes: 26 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
// snippets
// indexDB

import * as monaco from "monaco-editor";
import { initVimMode, VimMode } from "monaco-vim";
import type * as monaco from "monaco-editor";
import type { VimMode } from "monaco-vim";
import webmidi, { Input, WebMidiEventConnected, WebMidiEventDisconnected } from "webmidi";
import * as QRCode from "qrcode";
import * as WaveSurfer from "wavesurfer.js";
import * as JSZip from "jszip";
import * as BrowserFS from "browserfs";
import { FSModule } from "browserfs/dist/node/core/FS";
import { FaustAudioWorkletNode, FaustCompiler, FaustMonoDspGenerator, FaustScriptProcessorNode, FaustSvgDiagrams, LibFaust, instantiateFaustModuleFromFile, FaustPolyDspGenerator, AudioData } from "@grame/faustwasm";
import type { FSModule } from "browserfs/dist/node/core/FS";
import type { FaustAudioWorkletNode, FaustCompiler, FaustScriptProcessorNode, LibFaust, AudioData } from "@grame/faustwasm";
import { Key2Midi } from "./Key2Midi";
import { Scope } from "./Scope";
import "bootstrap/js/dist/dropdown";
Expand Down Expand Up @@ -103,13 +99,17 @@ let server = "https://faustservicecloud.grame.fr";
const PROJECT_DIR = "/usr/share/project/";

$(async () => {
const { instantiateFaustModuleFromFile, LibFaust, FaustCompiler, FaustSvgDiagrams, FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("@grame/faustwasm");
const faustModule = await instantiateFaustModuleFromFile("faustwasm/libfaust-wasm.js");
const libFaust = new LibFaust(faustModule);
const faustCompiler = new FaustCompiler(libFaust);
const faustSvgDiagrams = new FaustSvgDiagrams(faustCompiler);
const faustPrimitiveLibFile = await fetch("primitives.lib");
const faustPrimitiveLib = await faustPrimitiveLibFile.text();
libFaust.fs().writeFile("/usr/share/faust/primitives.lib", faustPrimitiveLib);

const BrowserFS = await import("browserfs");
const { Buffer } = BrowserFS.BFSRequire("buffer");
const bfs = await new Promise<FSModule>((resolve, reject) => BrowserFS.configure({
fs: "IndexedDB",
options: { storeName: "FaustIDE" }
Expand All @@ -118,11 +118,12 @@ $(async () => {
reject(e);
} else {
resolve(BrowserFS.BFSRequire("fs"));
// libFaust.fs().mkdir(PROJECT_DIR);
// libFaust.fs().mount(fs, { root: "/" }, PROJECT_DIR);
// resolve(bfs);
}
}));

const JSZip = (await import("jszip") as any).default as import("jszip");
const WaveSurfer = (await import("wavesurfer.js") as any).default as import("wavesurfer.js");
const QRCode = await import("qrcode");
// TODO(ijc): This previously set `window.faust`; what depends on that being set?
window.faustCompiler = faustCompiler;
/**
Expand Down Expand Up @@ -212,10 +213,10 @@ $(async () => {
}));
}
};
const loadSoundfiles = async (audioCtx: BaseAudioContext): Promise<Record<string, AudioData>> => {
const loadSoundfiles = async (audioCtx: BaseAudioContext, soundfileList: string[]): Promise<Record<string, AudioData>> => {
const map = {} as Record<string, AudioData>;
const files = libFaust.fs().readdir(PROJECT_DIR) as string[];
await Promise.all(files.filter(n => n.match(/\.(wav|mp3|ogg|flac|aac)$/)).map(async (filename) => {
await Promise.all(files.filter(n => soundfileList.indexOf(n) !== -1).map(async (filename) => {
const ui8Array = libFaust.fs().readFile(PROJECT_DIR + filename);
try {
const audioBuffer = await audioCtx.decodeAudioData(ui8Array.buffer);
Expand Down Expand Up @@ -332,17 +333,20 @@ $(async () => {
if (mediaLengthRaf) cancelAnimationFrame(mediaLengthRaf);
mediaLengthRaf = requestAnimationFrame(() => mediaLengthDisplay(t));
};
const soundfiles = await loadSoundfiles(audioCtx);
try {
// const getDiagramResult = getDiagram(code);
// if (!getDiagramResult.success) throw getDiagramResult.error;
if (voices) {
const factory = await new FaustPolyDspGenerator().compile(faustCompiler, "main", code, args.join(" "));
factory.voiceFactory.soundfiles = soundfiles;
const soundfileList = factory.getSoundfileList();
const soundfiles = await loadSoundfiles(audioCtx, soundfileList);
factory.addSoundfiles(soundfiles);
node = await factory.createNode(audioCtx, voices, "main", undefined, undefined, undefined, !useWorklet, bufferSize);
} else {
const factory = await new FaustMonoDspGenerator().compile(faustCompiler, "main", code, args.join(" "));
factory.factory.soundfiles = soundfiles;
const soundfileList = factory.getSoundfileList();
const soundfiles = await loadSoundfiles(audioCtx, soundfileList);
factory.addSoundfiles(soundfiles);
node = await factory.createNode(audioCtx, "main", undefined, !useWorklet, bufferSize);
}
node.setPlotHandler(plotHandler);
Expand Down Expand Up @@ -546,7 +550,7 @@ $(async () => {
path: PROJECT_DIR,
mainFile: compileOptions.mainFile,
selectHandler: (fileName, content) => editor.setValue(content),
saveHandler: async (fileName: string, content: string, mainCode: string) => {
saveHandler: async (fileName: string, content: string | Uint8Array, mainCode: string) => {
/*
let project: { [name: string]: string };
try {
Expand All @@ -569,7 +573,7 @@ $(async () => {
else resolve();
}));
}
await new Promise<void>((resolve, reject) => bfs.writeFile(fileName, content, { encoding: "utf8" }, (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();
}));
Expand Down Expand Up @@ -728,10 +732,11 @@ $(async () => {
if (compileOptions.plotMode === "offline") {
const code = uiEnv.fileManager.mainCode;
const { args, plot, plotSR } = compileOptions;
const soundfiles = await loadSoundfiles(new OfflineAudioContext({ sampleRate: plotSR, length: 0 }));
const generator = new FaustMonoDspGenerator();
await generator.compile(faustCompiler, "main", code, args.join(" "));
generator.factory.soundfiles = soundfiles;
const soundfileList = generator.getSoundfileList();
const soundfiles = await loadSoundfiles(new OfflineAudioContext({ sampleRate: plotSR, length: 0 }), soundfileList);
generator.addSoundfiles(soundfiles);
const processor = await generator.createOfflineProcessor(plotSR, 128);
const output = processor.render([], plot);
uiEnv.analyser.plotHandler(output, 0, undefined, true);
Expand Down Expand Up @@ -1924,6 +1929,7 @@ process = ba.pulsen(1, ba.hz2midikey(freq) * 1000) : pm.marimba(freq, 0, 7000, 0
};
effect = dm.freeverb_demo;`;
const monaco = await import("monaco-editor");
const { initVimMode } = await import("monaco-vim");
const { faustLang, providers } = await faustLangRegister(monaco, libFaust);
let saveCode = false;
try {
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const config = {
cleanupOutdatedCaches: true,
clientsClaim: true,
skipWaiting: true,
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024,
maximumFileSizeToCacheInBytes: 16 * 1024 * 1024,
})
]
};
Expand Down

0 comments on commit d9fbcb1

Please sign in to comment.