open-audiovisual
搜尋 GitHub

oav-p5 參考 · OAVp5 物件(進階)

OAVp5.sound.shell#

變數oav-p5
OAVp5.sound.shell

同一條也包含: OAVp5.sound.engine OAVp5.sound.module

載入進來的 @openav/sound 各部分:Sound 外殼、它的 Tone.js 引擎,以及模組本身。聲音載入之前三個都是 null。

OAVp5.sound.shell 是 @openav/sound 的 Sound 實例,OAVp5.sound.engine 是它用來發聲的 Tone.js 引擎(noteOn、noteOff、sustain、releaseAll、setInstrument),OAVp5.sound.module 是整個模組,例如 OAVp5.sound.module.listInstruments() 會列出所有樂器代號。它們是進階的把手:草圖層級的名稱(startSound()、soundIsOn)已經涵蓋草圖平常需要的一切。

型別
Object | null

startSound() 載入 @openav/sound 之後設定一次(引擎和外殼同時,模組稍微早一點)。在那之前,或載入失敗之後,都是 null。

要先做什麼才有值

先呼叫 startSound(),等 OAVp5.sound.state 離開 'loading' 之後再用,也可以在使用之前先檢查它是不是 null。

範例

jsOpenProcessing ↗
let ids = [];

function setup() {
  createCanvas(400, 300);
  startSound('synth/pluck');
}

function draw() {
  background(20);
  fill(255);
  textSize(12);
  if (!ids.length && OAVp5.sound.module) ids = OAVp5.sound.module.listInstruments().map((d) => d.id);
  text(ids.length ? ids.join(', ') : 'loading…', 20, 20, width - 40, height - 40);
}
模組一載入,就列出 startSound() 接受的所有樂器代號。

相關

  • OAVp5.sound 聲音相關名稱背後的物件:狀態、樂器、錯誤、載入進度,以及載入進來的 @openav/sound 各部分。
  • startSound() 讓你彈的鍵都聽得到,用你指定的樂器發聲,並回傳一個 promise,告訴你聲音有沒有開起來。
  • OAVp5.sound.state 聲音現在在哪個階段:'off'、'waiting'(等點擊)、'loading'、'on' 或 'error'。

p5/oav-p5.js 第 1347 行

↑ 回到頂端 · 所有名稱