我正在开发一个p5.js的声音库,并使用p5短语和p5部件使多个声音文件一次播放。
我可以addPhrase(),但mousePressed函数中的removePhrase()函数根本不起作用。如何在p5部件中添加和删除短语之间切换,这将打开/关闭特定的声音文件?
var box, drum, myPart;
var drumPhrase;
var boxPhrase;
var boxPat = [1, 0, 0, 2, 0, 2, 0, 0];
var drumPat = [0, 1, 1, 0, 2, 0, 1, 0];
function preload() {
box = loadSound('sound/noise.mp3');
drum = loadSound('sound/drum1.wav');
}
function setup() {
noStroke();
fill(255);
textAlign(CENTER);
masterVolume(0.1);
boxPhrase = new p5.Phrase('box', playBox, boxPat);
drumPhrase = new p5.Phrase('drum', playDrum, drumPat);
myPart = new p5.Part();
myPart.addPhrase(boxPhrase);
myPart.addPhrase(drumPhrase);
myPart.setBPM(60);
masterVolume(0.1);
myPart.start();
}
function draw() {
background(0);
}
function playBox(time, playbackRate) {
box.rate(playbackRate);
box.play(time);
}
function playDrum(time, playbackRate) {
drum.rate(playbackRate);
drum.play(time);
}
function mousePressed() {
myPart.removePhrase(box);
}发布于 2016-07-28 06:38:54
你说得对,p5.ound中有一个bug,所以p5.Part.removePhrase不能工作。
这里有一个解决方法:在setup()函数的末尾添加以下代码片段:
p5.Part.prototype.removePhrase = function (name) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
this.phrases.splice(i, 1);
}
}
};这将用实际的工作代码替换buggy函数。
我会让p5.js开发人员知道,这样就可以在官方版本中修复这个bug。
https://stackoverflow.com/questions/37215410
复制相似问题