Example 16 · Anything can be an instrument範例 16 · 什麼都能當樂器

p5 keys with soundp5 彈出聲音

startSound('piano') in setup(): every key from a MIDI keyboard, the 🎹 on screen or a phone is heard, and bursts into particles.在 setup() 寫 startSound('piano'):MIDI 鍵盤、螢幕上的 🎹、手機彈的每個音都聽得到,也炸開一團粒子。

Try these three things試試這三件事

  1. Click the page once (browsers keep sound off until then), then play the keys of the controller that opens at the bottom.
  2. Pick another instrument in that panel: marimba, strings, a synth. 🔊 turns the sound off and on.
  3. Hit a pad: notes on MIDI channel 10 play the drum kit, not the piano.
  1. 先點一下畫面(瀏覽器要點過才出聲),再彈下方打開的控制器上的琴鍵。
  2. 在面板裡換樂器:馬林巴、弦樂、合成器。🔊 可以關掉、再打開聲音。
  3. 敲打擊墊:第 10 軌的音打鼓,不會彈成鋼琴。

Needs: MIDI controller.需要:MIDI 控制器。 No hardware? Use On-screen controller · Phone or iPad.沒有硬體?用螢幕上的控制器、手機或 iPad。

Read these lines讀這幾行

  • sketch.js:2 The one line. Everything below it is plain p5.js.那一行。它下面全是普通的 p5.js。
  • sketch.js:13–14 Sound on, and the controller on screen open: two calls in setup().開聲音、打開螢幕上的控制器:setup() 裡兩行。
  • sketch.js:35–46 Every note, from any of the three inputs, calls notePressed(): the pitch picks the colour and the place, the velocity how far it bursts.三種輸入彈的每個音都呼叫 notePressed():音高決定顏色和位置,力度決定散多開。

In the docs文件裡: Sound: what is played is heard聲音:彈什麼就聽到什麼

Next: 17 p5 microphone →下一個:17 p5 麥克風 → All examples所有範例