我是 JavaScript 初学者。 我的母语不是英语,很抱歉我的英语不好。
现在我想用 Node.js + Typescript 进行声音可视化,但我不知道如何在 setup()
中加载声音。请教我如何在 Node.js + Typescript 中加载声音。
我尝试导入'p5/lib/addons/p5.sound'
。但我不知道如何在下面代码的 sketch
中使用这个模块。
main.ts
import * as p5 from 'p5';
const sketch = (p: p5) => {
p.setup = () => {
p.resizeCanvas(100, 100);
// want to load sound here
};
p.draw = () => {
p.background(100);
}
};
const sketchP = new p5(sketch);
全局.d.ts
import module = require('p5');
export = module;
export as namespace p5;
declare global {
interface Window {
p5: typeof module
}
}
最佳答案
现在我解决了这个问题。谢谢。
main.ts
require('p5/lib/addons/p5.sound')
const sketch = function(p: p5) {
let sound: p5.SoundFile;
p.preload = () => {
const loadSound = (path: string) =>
((p as any) as p5.SoundFile).loadSound(path);
sound = loadSound("../audio/demo.mp3")
};
p.setup = () => {
p.resizeCanvas(100, 100);
sound.play()
};
}
关于javascript - 如何在 Typescript + Node.js 中使用 p5.sound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56199144/