2016-05-13 128 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。