浮出水面
FaustWasm 图书馆提供了一个方便的高级API 浮士德 编译器. 这个库的界面主要设计于 类型脚本 使用,虽然它也为纯JavaScript提供了API描述和文档. Faust 编译器的WebAssembly版本与两者兼容 节点.js 和网页浏览器,已使用 标记 6.0.3 (英语).
库提供了将Faust DSP代码汇编到WebAssembly的功能,使其能在标准WebAudio节点图中作为WebAudio节点使用. 此外,它支持离线渲染情景。 此外,还可以使用补充工具,从Faust DSP方案生成SVG.
合成器和效应单节点,以及 多声道 可以创建多节点. 一旦启动 MIDI 支持 MIDI 元数据 用于单节点的DSP编码中,并始终采用多声模式.
传感器 支持(加速计和陀螺仪)元数据以及 渐进式网络应用 型号.
使用量
请使用稳定版本 节点.js 16+与该项目合作.
使用命令行接口
克隆人进入这个项目:
git clone https://github.com/grame-cncm/faustwasm.git
cd faustwasm
安装开发依赖性 :
npm install
也许:
npm update
构建文件 :
npm run build
源代码使用 物质的高级代码 正确格式化所有TS和JS文件。 使用 :
npm run format
应用格式规则 src,并:
npm run lint
以检查格式规则 src。 。 。 .
版本
你必须提高软件包的版本号 package.json 在此之前 npm run build 以适当的工作.
生成 Faust DSP 的 Web Assembly 版本
例如:
rm -rf test/out # make sure you are under the faustwasm directory.
node scripts/faust2wasm.js test/mono.dsp test/out
将创建一组文件 : index.js, (中文(简体) ), create-node.js, (中文(简体) ), dsp-module.wasm, (中文(简体) ), dsp-meta.json, (中文(简体) ), index.html 编辑 faustwasm 文件夹中 out 文件夹。 这个 -no-template 选项省略所有事项,但 dsp-module.wasm 财务报告和审定财务报表 dsp-meta.json。 。 。 .
配有:
rm -rf test/out # make sure you are under the faustwasm directory.
node scripts/faust2wasm.js test/poly.dsp test/out -poly
将创建一组文件 : index.js, (中文(简体) ), create-node.js, (中文(简体) ), dsp-module.wasm, (中文(简体) ), dsp-meta.json, (中文(简体) ), mixer-module.wasm, (中文(简体) ), index.html 编辑 faustwasm 文件夹(和,当 DSP 源定义 effect, (中文(简体) ), effect-module.wasm 财务报告和审定财务报表 effect-meta.json)在 out 文件夹.
本地文件 -I
CLI 在 Web Assembly 文件系统中运行 Faust 编译器, 因此它不能直接读取您的主机文件系统 。 让本地 import("something.lib") 任务:
- 包含输入的目录
.dsp自动复制到内存FS并添加到编译器中包含路径. - 任意
-I <dir>您的通过被镜像 入内膜FS 并添加到包含路径. - 仅
.dsp财务报告和审定财务报表.lib文件被镜像.
这意味着您可以从任何工作目录中运行 CLI , 只要您的本地地址包含 DSP 旁边的活字或列表中 -I。 。 。 .
示例,使用 test/foo.dsp 财务报告和审定财务报表 test/includes/mylib.lib 与存放处同船的固定装置mylib.lib 故意不坐旁边 foo.dsp,因此该建筑只与 -I:
node scripts/faust2wasm.js -I test/includes test/foo.dsp test/out
创建Faust DSP的渐进网络应用程序
您可以使用命令行创建独立的进步网络应用程序 :
node scripts/faust2wasm.js test/rev.dsp test/rev -pwa
将创建一组文件 : icon.png, (中文(简体) ), service-worker.js, (中文(简体) ), manifest.json, (中文(简体) ), index.js, (中文(简体) ), create-node.js, (中文(简体) ), faust-pwa.js, (中文(简体) ), dsp-module.wasm, (中文(简体) ), dsp-meta.json, (中文(简体) ), index.html,则 faustwasm, (中文(简体) ), faust-ui 文件夹在 rev 文件夹.
该文件夹包含将浮士德应用程序作为PWA部署在服务器上的必要源代码,将安装并下线使用. 注意: soundfile dSP代码中的原始语必须被曼努尔添加到文件夹中.
可通过以下方式创建独立的多声道和MIDI独立渐进网络应用程序:
node scripts/faust2wasm.js test/organ1.dsp test/organ1 -poly -pwa
将创建相同的文件集,加 mixer-module.wasm 并且,自从 organ1.dsp 定义 effect, (中文(简体) ), effect-module.wasm 财务报告和审定财务报表 effect-meta.json,在 organ1 文件夹.
创建独立版本的 Faust DSP, 配有音频和 MIDI 设备选择器
您可以使用命令行创建一个独立的 :
node scripts/faust2wasm.js test/rev.dsp test/rev -standalone
将创建一组文件 : icon.png, (中文(简体) ), service-worker.js, (中文(简体) ), manifest.json, (中文(简体) ), index.js, (中文(简体) ), create-node.js, (中文(简体) ), dsp-module.wasm, (中文(简体) ), dsp-meta.json, (中文(简体) ), index.html,则 faustwasm, (中文(简体) ), faust-ui 文件夹在 rev 文件夹.
该文件夹包含将浮士德应用程序作为PWA部署在服务器上的必要源代码,将安装并下线使用. 注意: soundfile dSP代码中的原始语必须被曼努尔添加到文件夹中.
可通过以下方式创建独立的多声道和MIDI独立渐进网络应用程序:
node scripts/faust2wasm.js test/organ1.dsp test/organ1 -poly -standalone
将创建相同的文件集,加 mixer-module.wasm 并且,自从 organ1.dsp 定义 effect, (中文(简体) ), effect-module.wasm 财务报告和审定财务报表 effect-meta.json,在 organ1 文件夹.
生成 Faust DSP 的 SVG 图
例如:
rm -rf test/out # make sure you are under the faustwasm directory.
node scripts/faust2svg.js test/mono.dsp test/out
主图应该包含在 test/out/process.svg。 。 。 .
在 Cmajor 文件内编译 Faust DSP
例如:
rm -rf test/out # make sure you are under the faustwasm directory.
node scripts/faust2cmajor.js test/organ.dsp test/out
Cmajor 文档应包含在 test/out/organ.cmajor。 。 。 .
生成或处理音频文件
选项 :
-bs <num>设置帧中的渲染缓冲大小( 默认: 64)-bd 16|24|32设置输出文件位深( 默认: 16)-c <samples>在框中设置输出文件长度, 当 -ct 不使用时( 默认: SR* 5)-in <inputWav.wav>指定要处理的输入文件-sr <num>设置输出文件样本率( 默认: 44100) 见此帮助:
node scripts/faust2sndfile.js -h
例如:
rm -rf test/out # make sure you are under the faustwasm directory.
node scripts/faust2sndfile.js test/djembe.dsp test/out/djembe.wav -c 192000 -sr 48000 -bd 24
现在 test/out/djembe.wav 应生成。 根据需要创建了输出文件的文件夹.
生成的文件又可以由另一个 DSP 处理 -in数字 :
node scripts/faust2sndfile.js test/rev.dsp test/out/djembe-rev.wav -c 192000 -sr 48000 -bd 24 -in test/out/djembe.wav
使用 JavaScript 模块
npm i -D @grame/faustwasm
在 Java 脚本中 :
const FaustWasm = require("@grame/faustwasm");
const path = require("path");
const fs = require("fs");
const {
instantiateFaustModuleFromFile,
LibFaust,
WavEncoder,
FaustMonoDspGenerator,
FaustCompiler,
FaustSvgDiagrams
} = FaustWasm;
(async () => {
const faustModulePath = path.join(__dirname, "../node_modules/@grame/faustwasm/libfaust-wasm/libfaust-wasm.js");
// initialize the libfaust wasm
const faustModule = await instantiateFaustModuleFromFile(faustModulePath);
// Get the Faust compiler
const libFaust = new LibFaust(faustModule);
console.log(libFaust.version());
const compiler = new FaustCompiler(libFaust);
const generator = new FaustMonoDspGenerator();
const sampleRate = 48000;
const name = "Djembe"
const argv = ["-I", "libraries/"];
const code = `
import("stdfaust.lib");
process = ba.pulsen(1, 10000) : pm.djembe(60, 0.3, 0.4, 1);
`;
// Compile the DSP
await generator.compile(compiler, name, code, argv.join(" "));
const processor = await generator.createOfflineProcessor(sampleRate, 1024);
// Generate SVG diagrams.
const svgDiagrams = new FaustSvgDiagrams(compiler);
const svgs = svgDiagrams.from(name, code, argv.join(" "));
console.log(Object.keys(svgs));
const out = processor.render(null, 192000);
const wav = WavEncoder.encode(out, { sampleRate, bitDepth: 24 });
// The wav file is generated
fs.writeFileSync(`${__dirname}/out.wav`, new Uint8Array(wav));
})();
在网页浏览器中使用
(async () => {
const {
instantiateFaustModuleFromFile,
LibFaust,
WavEncoder,
FaustMonoDspGenerator,
FaustCompiler,
FaustSvgDiagrams
} = await import("../node_modules/@grame/faustwasm/dist/esm/index.js");
// initialize the libfaust wasm
const faustModule = await instantiateFaustModuleFromFile("../node_modules/@grame/faustwasm/libfaust-wasm/libfaust-wasm.js");
// Get the Faust compiler
const libFaust = new LibFaust(faustModule);
window.libFaust = libFaust;
console.log(libFaust.version());
const compiler = new FaustCompiler(libFaust);
const generator = new FaustMonoDspGenerator();
const sampleRate = 48000;
const name = "Djembe"
const argv = ["-I", "libraries/"];
const code = `
import("stdfaust.lib");
process = ba.pulsen(1, 10000) : pm.djembe(60, 0.3, 0.4, 1);
`;
// Compile the DSP
await generator.compile(compiler, name, code, argv.join(" "));
const processor = await generator.createOfflineProcessor(sampleRate, 1024);
// Generate SVG diagrams.
const svgDiagrams = new FaustSvgDiagrams(compiler);
const svgs = svgDiagrams.from(name, code, argv.join(" "));
console.log(Object.keys(svgs));
const out = processor.render(null, 192000);
const wav = WavEncoder.encode(out, { sampleRate, bitDepth: 24 });
// The wav file is generated
const blob = new Blob([wav], { type: "audio/wav" });
const player = document.createElement("audio");
player.controls = true;
player.src = URL.createObjectURL(blob);
document.body.appendChild(player);
const svg = document.createElement("div");
svg.innerHTML = svgs["process.svg"];
document.body.appendChild(svg);
})();
多声乐器的平滑控制
空闲多声调没有计算,所以控制平滑 si.smoo 可以保留一个旧值,直到声音被再利用。 对于在笔记开始时应当取出其当前值的控件,使用 si.polySmooth 与乐器的 gate数字 :
import("stdfaust.lib");
freq = nentry("freq", 440, 20, 20000, 1);
gain = nentry("gain", 1, 0, 1, 0.01);
gate = button("gate");
level = hslider("level", 1, 0, 1, 0.01)
: si.polySmooth(gate, 0.999, 32);
process = os.osc(freq) * gain
* en.adsr(0.01, 0.1, 0.8, 0.2, gate) * level <: _, _;
这里, 0.999 是平滑系数,而且 32 是闸口起伏后平滑恢复前的样品数量。 所持音符的后期控制变化仍然平滑.
正在计算活动声音
多声节点和离线处理器有一个 getNumActiveVoices() 方法,它返回声音仍然发声的数:播放,在放出时,或在legato. 释放出的声音数到它死了, 这使得它适合一个语音表:
const meter = () => {
voicesLabel.textContent = `${node.getNumActiveVoices()} voices`;
requestAnimationFrame(meter);
};
meter();
使用 AudioWorklet 节点,声音会活在音频线索上:处理器在它所变化的每个块的结尾报告其编号,节点返回最新的报告. 脚本处理器节点和离线处理器直接计数. 吻合点 getNumActiveVoices() c++ 多声道架构faust/dsp/poly-dsp.h) (中文(简体) ).
早点释放出声音
释放出的声音被计算到其水平下至-90分贝,然后其插槽再次被自由. 认为更高水平的无声主机可以给出自己的阈值,即 停止关卡,并更快地将CPU调回. 这是 stopLevel 会 议 日 程 和 议 程 processorOptions 联 合 国 createNode,对于一个音频Worklet节点,对于一个脚本处理器,以及最后一个参数 createOfflineProcessor数字 :
// Voices are freed under -60 dB
const node = await generator.createNode(
audioContext, 16, "synth", undefined, undefined, undefined, false, 1024, undefined,
{ stopLevel: 0.001 }
);
const processor = await generator.createOfflineProcessor(
sampleRate, 1024, 16, undefined, undefined, undefined, undefined, 0.001
);
停止水平是线性的振幅, 10^(dB/20),与每个块上的声音的RMS级别相比,在其所有输出通道上. 它必须在里面 ]0, 0.01],即最多-40分贝:在控制台上发出警告而拒绝任何其他值,并使用-90分贝。 它是在节点创建后设定的,之后无法被更改. 和那个差不多 stoplevel 论点 mydsp_poly c++ 多声道架构中faust/dsp/poly-dsp.h) (中文(简体) ).
样本准确的时间安排
Faust AudioWorklet 节点上的每个控制方法都有一个可选 time 参数: setParamValue(path, value, time), (中文(简体) ), keyOn(channel, pitch, velocity, time), (中文(简体) ), keyOff(channel, pitch, velocity, time), (中文(简体) ), midiMessage(data, time), (中文(简体) ), ctrlChange(channel, ctrl, value, time), (中文(简体) ), pitchWheel(channel, wheel, time)。 。 。 . time 以 AudioContext 秒表示——与 audioCtx.currentTime 财务报告和审定财务报表 AudioParam.setValueAtTime 动作在即时名称的精确样本上, 在包含它的音频块内。 单位为第二(一个JavaScript双人),颗粒性为样本:处理器将其与 Math.round(time * sampleRate) 在音频时钟上的帧,所以一个瞬间被纪念到最近的样本(大约21μs at 48 kHz):
const t0 = audioCtx.currentTime + 0.1;
// A parameter change on the exact sample
node.setParamValue("/mydsp/gate", 1, t0);
node.setParamValue("/mydsp/gate", 0, t0 + 0.05);
// A polyphonic pattern scheduled ahead of time
node.keyOn(0, 60, 100, t0);
node.keyOff(0, 60, 0, t0 + 0.5);
node.keyOn(0, 63, 100, t0 + 0.25);
经常 AudioParam 节点参数上的自动化(setValueAtTime, (中文(简体) ), linearRampToValueAtTime,.)也被精确地跟踪样本,因此可以安排一个注释和一个参数斜坡对接. 由Web音频模块(WAM)活动所携带的时标同样受到尊重.
别管了, time 行为如前: 消息在到达时, 在下一个音频块开始时应用 。 A 类 time 已经在过去的应用 尽快,顺序。 注释:
- fFT 处理器荣誉
time(一) 块颗粒性,而不是样品颗粒性; - 脚本处理器节点( S)
sp: true接受论点,并在抵达时应用信息; setParamValue负数time抛出( 它被拒绝 )AudioParam.setValueAtTime) (中文(简体) ).
页面 test/web/poly-schedule.html 显示API,以及 test/timing/measure.mjs 衡量样本的准确性(见下文).
运行测试
单位测试 test/unit 并运行在平原节点(不需要浏览器). 首先建立无害环境管理捆绑,然后逐一运行 *.test.mjs 带有节点内置测试运行器的文件 :
npm test
计时标准 test/timing/measure.mjs:它通过一个无头铬中真正的AudioWorklet使MIDI注释和参数改变,并检查每一个都降落在它预定的确切样本上. 这需要 剧作家,故意不依赖此套件:
npm i --no-save playwright
npx playwright install chromium
npm run measure
使用CLI测试 test/cli 并运行脚本 scripts/ 检查磁盘上留下了什么 它们涵盖了每个输出模式 faust2wasm.js (默认模板, -no-template, (中文(简体) ), -standalone, (中文(简体) ), -pwa,每个有和没有 -poly页:1 .dsp 文件输入 test/ - 每一个编译,然后从生成的装入 dsp-module.wasm 财务报告和审定财务报表 dsp-meta.json 和边缘案件: -I 包括处理、论证、失败模式和同时援引. faust2svg, (中文(简体) ), faust2cmajor 财务报告和审定财务报表 faust2sndfile 后者也包括在内,通过阅读 -sr, (中文(简体) ), -bd 财务报告和审定财务报表 -c 返回到生成的 WAV 页眉。 他们不需要浏览器 :
npm run test-cli
npm run test-all 进行单位测试和CLI测试。 都写在 test/out,当套房通过时,该套房被取出并被取出;故障运行时,该套房被取出,以便检查产出。 CLI测试需要几秒钟.
test/web 包含在浏览器中手动检查的交互式测试页面:单声道和多声道仪器、FFT处理器、音效文件以及 createFaustNode API (英语). 构建软件包,为 repo 根服务(所以) dist/ 财务报告和审定财务报表 libfaust-wasm/ ),然后打开诸如 http://localhost:8000/test/web/mono.html 页面存档备份,存于互联网档案馆数字 :
npm run build
python3 -m http.server 8000
项目实例
几个例子可以通过在 faustwasm 根级别启动本地网络服务器进行测试,然后进入 test/faustlive-wasm 财务报告和审定财务报表 libfaust-in-worklet 文件夹.
该软件包用于以下项目:
- faust-web-组件,一个包提供了两个网络组件,用于将交互式浮士德片段嵌入到网页中.
- 浮游游戏场,一个网络平台,旨在使儿童能够以简单而生动的方式学习基本的音频编程.
- 浮士德编辑器在WebAssembly支持下,一个简单的在线编辑器用于编辑、编译和运行任何最近Web浏览器的Faust代码.
- 浮起网络 IDE在WebAssembly支持下,用于编辑、编译和运行任何最近Web浏览器的Faust代码.
- 测试嵌入式动态 Faust 编译器 页面允许测试编译器, 与此相关 HTML 源代码 使用 此 Java 脚本代码。 。 。 .
文档
API 组织
如下图所示,API的组织水平从低到高.
Faust 编译器 Web Assembly 模块
第一个关卡是 Faust 编译器, 编译为 asum 库, 命名为 libfaust-wasm。 。 。 .
它包括三个不同的文件:
libfaust-wasm.wasm: 作为WebAssembly模块提供的浮士德编译器libfaust-wasm.js: Web Assembly 模块的 Javascript 载入器libfaust-wasm.data:包含"浮士德"图书馆的虚拟文件系统.
C++ 代码由 标记 和接口 LibFaust.ts 财务报告和审定财务报表 types.ts文档。 装入器将负责提供浮士德WebAssembly模块以及相关虚拟文件系统(libfaust-wasm.data)的实例.
Faust 编译器 Javascript 接口
Faust 编译器 Javascript 界面描述于 FaustCompiler.ts。 。 。 .
它规定 经典 Faust编译服务,其输出是一个原始的WebAssembly模块,并附带了该模块的JSON描述.
浮士德·瓦斯姆实例
这个关卡需要由Faust编译器制作的WebAssemsem模块或从文件中装入的预编译模块,并用适当的Wasm内存布局构建一个模块实例,准备运行. 其说明见: FaustDspGenerator.ts, (中文(简体) ), FaustWasmInstantiator.ts, (中文(简体) ), FaustWebAudioDsp.ts 财务报告和审定财务报表 FaustDspInstance.ts 文档.
Faust 音频节点实例和离线处理器
这个关卡需要一个浮士德·瓦斯姆实例来构建一个音频节点. 音频工作 财务报告和审定财务报表 脚本处理器 节点被支持. 其说明见: FaustAudioWorkletNode.ts 财务报告和审定财务报表 FaustAudioWorkletProcessor.ts 文档.
3⁄4 ̄ ̧漯B: AudioWorklet是一种最新技术,可能不会得到所有浏览器的支持. 检查 兼容性 图表.
基础音频节点 API 文档记录在 IFAustBase 微信公众号 接口。 这个 互联网档案馆的存檔,存档日期2013-12-04 接口文档多声扩展名.
注意脚本处理器标记为 已贬值 但它是唯一在更古老的Safari版本中可用的音频架构. 单声道(发音,效果. )或多声道(仪器)节点都可以被创建. 其说明见: FaustScriptProcessorNode.ts 文档.
创建的音频节点有一个 start 财务报告和审定财务报表 stop 方法。 。 。 启动时(默认情况下完成),它们正在处理音频缓冲. 您可能必须明确阻止它们来保存 CPU( 并在需要时再次启动) , 例如, 在实际使用前的inition时间创建了多个节点 .
一个在非实时上下文中渲染出DSP并获得计算框架的离线处理器可供使用. 其说明见: FaustOfflineProcessor.ts。它将自动使用 start 财务报告和审定财务报表 stop 在内部激活其实际渲染的方法 plot 方法.
高级API
高级API提供了方便的功能来编译一个Faust DSP程序并创建出相应的音频节点,无论是单音节还是多音节.
- 使用适当的
FaustMonoDspGenerator或者说FaustPolyDspGenerator班级,createNode明确建立单声道或多声道节点. createFaustNode通过检查 :[nvoices:N]dSP 元数据中的字段.
对于离线渲染, createOfflineProcessor 具有并支持单声道和多声道处理。 FFT 处理节点可以使用 createFFTNode。 。 。 .
此API由FaustDspGenerator.ts执行.
如何使用打字符
简单地包括以下内容以获取类型和功能:
///<reference types="@grame/faustwasm"/>
动态和静态实例
Faust Wasm 和 Audio Node 级别使得从 Faust dsp 代码以及预编译的 Web Assembly 模块生成实例成为可能.
在后一种情况下,没有必要包括: libfaust-wasm.js 图书馆, index.js 足以提供所需的服务.
这可以生成更轻快的HTML页面.
杂项服务
FaustSvgDiagrams.ts: 提供浏览浮士德生成的 SVG 图表的设施FaustFFTAudioWorkletProcessor: 提供 FFT 处理
重要说明
Html 页面嵌入了 Faust 编译器,必须使用 https 服务,除非使用 http://localhost .
