startSound()#
函式oav-p5startSound([id])同一條也包含: OAVp5.sound.start
讓你彈的鍵都聽得到,用你指定的樂器發聲,並回傳一個 promise,告訴你聲音有沒有開起來。
在 setup() 裡呼叫一次:startSound('organ')。從這時候起,你彈的每個音都聽得到:MIDI 鍵盤、螢幕上的 🎹、手機、midi.play(60, 0.8) 都算。你自己寫的 notePressed() 一樣會收到音符,而且是先發聲、再通知草圖,所以 callback 寫得慢也不會拖慢聲音。MIDI 第 10 軌(General MIDI 的鼓軌)的打擊墊打的是鼓組,不是樂器,延音踏板則會讓音延續。
樂器代號有 piano、epiano、organ、strings、choir、harp、marimba、vibraphone、xylophone、glockenspiel、music-box,以及合成器音色 synth/pad、synth/saw-lead、synth/square-lead、synth/pluck、synth/supersaw、synth/bass、synth/acid、synth/brass、synth/bell、synth/sub、synth/chip。聲音在播的時候再呼叫 startSound(id),就是換樂器。給了不存在的代號,主控台會連同真正的代號清單一起列出來:已經在播的樂器不變,第一次呼叫就給錯的話,會是溫暖的 synth/pad。
在第一次呼叫之前(或按 🎹 面板裡的 🔊 之前)什麼都不會載入:@openav/sound 從 oav-p5.js 旁邊載入,Tone.js 15 從 jsDelivr 載入(載入選項 tone 可以改成別的地方)。只有 piano 播的是錄好的取樣,第一次大約 2 MB,載好之前由電鋼琴先頂著(如果是從別的樂器切過來,就由原本那個繼續響),取樣到齊的時候 OAVp5.sound.instrument 會變成 piano。其他樂器都是 Tone.js 即時合成的,沒有檔案要載。
呼叫之前
瀏覽器要頁面上先有一次點擊或按鍵才讓頁面出聲,MIDI 鍵盤上的鍵不算。在 setup() 裡呼叫 startSound() 沒有問題:OAVp5.sound.state 會是 'waiting',🎹 旁邊有一張卡片說「聲音準備好了,還沒響」,第一次點擊或按鍵就會把聲音打開。soundIsOn 會告訴你什麼時候開了。
參數
| 名稱 | 型別 | 說明 |
|---|---|---|
id | String | 樂器的代號,例如 'organ' 或 'synth/pluck'(完整清單在說明裡)。不傳就沿用之前選的樂器,第一次呼叫時就是鋼琴。不是非空字串的值,視同沒傳。 可省略 預設 'piano' |
回傳
Promise<Boolean> 聲音開起來之後 resolve 成 true。瀏覽器要先有一次點擊的話,就是在頁面上第一次點擊或按鍵之後。載入選項 sound 是 off、stopSound() 先被呼叫、或 Tone.js 載入失敗的時候,resolve 成 false。
注意
- 完全不寫程式也行:在網址後面加
?oav_sound=piano(任何樂器代號都可以,在 OpenProcessing 上是?sketch_oav_sound=piano),任何草圖從第一次點擊起就聽得到,?oav_sound=off則讓它保持安靜,這時startSound()什麼都不做,並 resolve 成false。🎹 面板裡的 🔊 可以開關聲音,並列出所有樂器讓你挑。 - 聲音和鼓機是分開的:
startDrums()有自己的鼓組,不用startSound(),第一次點擊之後就聽得到。
範例
let playing = 60;
function setup() {
createCanvas(400, 300);
startSound('synth/pluck'); // every key you play is heard
}
function mousePressed() {
playing = 48 + floor(mouseX / 25); // low on the left, high on the right
midi.play(playing, 0.8);
}
function mouseReleased() {
midi.play(playing, 0);
}
function draw() {
background(soundIsOn ? 30 : 90);
fill(255);
textAlign(CENTER, CENTER);
text(soundIsOn ? 'click to play a note' : 'click once to turn sound on', width / 2, height / 2);
}const KEYS = { a: 60, s: 62, d: 64, f: 65, g: 67, h: 69, j: 71, k: 72 };
let dots = [];
function setup() {
createCanvas(400, 300);
noStroke();
startSound('epiano');
}
function keyPressed() {
if (KEYS[key]) midi.play(KEYS[key], 0.8);
}
function keyReleased() {
if (KEYS[key]) midi.play(KEYS[key], 0);
}
function notePressed(n, v) {
dots.push({ x: map(n, 60, 72, 30, width - 30), y: height, v });
}
function draw() {
background(20);
for (const d of dots) {
fill(255, 160, 80);
circle(d.x, d.y, 20 + d.v * 30);
d.y -= 2;
}
dots = dots.filter((d) => d.y > -40);
fill(255);
text(soundIsOn ? 'play with a s d f g h j k' : 'press a key once to turn sound on', 20, 24);
}const IDS = ['synth/pluck', 'organ', 'marimba', 'piano'];
function setup() {
createCanvas(400, 300);
startSound(IDS[0]);
}
function keyPressed() {
const i = Number(key) - 1;
if (IDS[i]) startSound(IDS[i]); // switches while it plays
}
function draw() {
background(20);
fill(255);
const s = OAVp5.sound.status; // the instrument being loaded
text('keys 1-4 pick an instrument', 20, 30);
text('playing: ' + OAVp5.sound.instrument, 20, 60);
text('loading: ' + s.id + ' (' + s.state + ', ' + nf(s.progress * 100, 1, 0) + '%)', 20, 90);
}相關
stopSound()關掉聲音,讓正在響的音全部放開,樂器還留在記憶體裡,所以再呼叫startSound()馬上就回來。soundIsOn現在彈的音有沒有被聽到:startSound()完成之後是 true,之前和之後都是 false。OAVp5.sound.state聲音現在在哪個階段:'off'、'waiting'(等點擊)、'loading'、'on'或'error'。OAVp5.sound.instrument你現在聽到的樂器代號,聲音還沒載入的時候是null。notePressed()你來定義這個函式。每當有一個鍵(或打擊墊)按下,oav-p5 就呼叫它。startDrums()啟動鼓機:頁面上出現一個 16 格的鼓機,播放預設節奏、你自己畫的格子或一行字寫的節奏,並替草圖數小節。