p5.js声音库:如何添加/删除p5.第5页中的短语().零件()

p5.js sound library: how to add/remove p5. Phrases() from p5. Part()?

本文关键字:p5 删除 5页 零件 短语 添加 声音 js 何添加      更新时间:2023-09-26

我正在开发一个带有声音库的p5.js,并使用p5 Phrase和p5 Part使多个声音文件同时播放。

我能够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);
}

你是对的,p5.sound中有一个错误,导致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);
        }
    }
};

这将用实际的工作代码替换有缺陷的函数。

我会让p5.js的开发人员知道,这样这个错误就可以在官方版本中得到修复。