2016-05-13 116 views
0

我正在与声音库p5.js一起工作,并使用p5短语和p5声部一次播放多个声音文件。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); 

} 
+0

该引用表示'removePhrase(phraseName)',其中_phraseName_是一个字符串标识符。所以在你的情况下,它应该是'myPart.removePhrase('box');' – michaPau

+0

我已经试过这样做了,但不起作用。 –

回答

1

你说得对,p5.sound有一个错误,所以p5.Part.removePhrase不起作用。

这里的修复:添加下面的代码片段在你安装结束()函数:

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开发人员知道,以便可以在官方版本中修复该bug。