【发布时间】:2021-04-19 03:03:27
【问题描述】:
我正在尝试使用 P5.js 创建类似于音频循环播放器的东西——也就是说,您录制一段 sn-p 音频,它作为循环播放给您,然后您可以录制其他 sn-ps音频一起播放以创作一首歌曲。
我想出了如何录制音频、将其作为循环播放并停止循环,但我不确定重复该功能的最佳方式,以便它可以用于相互独立运行的按钮并录制单独的音频文件,因为我想录制多个循环。
我对 P5.js 还是很陌生,所以可能有一种简单的方法可以做到这一点——任何想法都会有所帮助!一般来说,如果您对如何实现这个项目作为一个整体(音频循环器)有任何想法,我很乐意听到。
这是我的代码:
let mic, recorder, soundFile, button;
let state = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
background(200);
mic = new p5.AudioIn();
mic.start();
recorder = new p5.SoundRecorder();
recorder.setInput(mic);
soundFile = new p5.SoundFile();
button = createButton("record");
button.size(200, 100);
button.style("font-family", "Bodoni");
button.style("font-size", "48px");
button.position(10, 10, 10);
button.mouseClicked(loopRecord);
}
// this is the looper
function loopRecord() {
if (state === 0 && mic.enabled) {
recorder.record(soundFile);
background(255, 0, 0);
state++;
} else if (state === 1) {
recorder.stop();
background(0, 255, 0);
state++;
} else if (state === 2) {
soundFile.loop();
state++;
} else if (state === 3) {
soundFile.stop();
state++;
} else if (state === 4) {
state === 0;
}
}
【问题讨论】: