open-audiovisual
搜尋 GitHub

oav-p5 參考 · 聲音與樂器

OAVp5.sound.instrument#

變數oav-p5
OAVp5.sound.instrument

你現在聽到的樂器代號,聲音還沒載入的時候是 null。

第一次呼叫 startSound('piano') 的時候,鋼琴的取樣(約 2 MB)要先下載,這段時間由電鋼琴頂著,所以第一個鍵不會沒聲音:鋼琴準備好之前 instrument 是 'epiano',之後才是 'piano'。如果是從別的樂器切到鋼琴,取樣到齊之前原本那個樂器繼續響,instrument 也還是它的名字。載到哪裡、載的是哪個樂器,看 OAVp5.sound.status。

型別
String | null

樂器換了才會變,取樣型的樂器則是等取樣到齊的時候才換。

範例

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startSound('piano');
  textAlign(CENTER, CENTER);
}

function draw() {
  background(20);
  fill(255);
  text(OAVp5.sound.instrument || 'nothing loaded yet', width / 2, height / 2);
}
顯示你聽到的樂器:鋼琴取樣下載的時候是 epiano,之後變成 piano。

相關

  • startSound() 讓你彈的鍵都聽得到,用你指定的樂器發聲,並回傳一個 promise,告訴你聲音有沒有開起來。
  • OAVp5.sound.status 樂器載入得怎麼樣了:{ id, state, progress, error },其中 progress 從 0 到 1。
  • OAVp5.sound.state 聲音現在在哪個階段:'off'、'waiting'(等點擊)、'loading'、'on' 或 'error'。

p5/oav-p5.js 第 1347 行

↑ 回到頂端 · 所有名稱