简体   繁体   English

p5.j​​s声音库:如何添加/删除p5。 来自p5的短语()。 部分()?

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

I am working on a p5.js with sound library, and using p5 Phrase and p5 Part for making multiple sound files play at once. 我正在使用带有声音库的p5.js,并使用p5短语和p5 Part来一次播放多个声音文件。

I was able to addPhrase() but the removePhrase() function inside the mousePressed function does not work at all. 我能addPhrase()removePhrase()的函数里面mousePressed功能不能在所有的工作。 How can I toggle between adding and removing phrases from the p5 Part, which would turn on/off the specific sound file? 如何在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);

}

You're right, there is a bug in p5.sound so that p5.Part.removePhrase does not work. 没错,p5.sound中存在一个错误,因此p5.Part.removePhrase不起作用。

Here's a fix: Add the following snippet at the end of your setup() function: 解决方法:在setup()函数末尾添加以下代码段:

p5.Part.prototype.removePhrase = function (name) {
    for (var i in this.phrases) {
        if (this.phrases[i].name === name) {
            this.phrases.splice(i, 1);
        }
    }
};

This replaces the buggy function with the actual working code. 这将越野车功能替换为实际的工作代码。

I'll let the p5.js developers know so that the bug can be fixed in the official version. 我会让p5.js开发人员知道,以便可以在正式版本中修复该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM