micro:bitはシリアルも扱えるので、midiで受けた音をシリアル経由で鳴らせるようにしてみました。
micro:bitで音を鳴らすにはP0-GND間にスピーカーをつなぐ必要があります。
自分はこちらを使用しました。
シリアルから 0-127の数値を受け取るとオクターブ-1のC#1からオクターブ9のGまでの音を鳴らします。0は音を止めるのに使用します。
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
music.stopMelody(MelodyStopOptions.All)
AA = serial.readString()
iAA = parseFloat(AA.substr(0, AA.length - 1))
if (iAA >= 0 && iAA < 128) {
music.rest(20)
music.ringTone(fArr[iAA])
led.unplot(px, py)
if (iAA > 0) {
px = iAA % 12 % 5
py = iAA % 12 / 5 % 5
led.plot(px, py)
}
}
})
let py = 0
let px = 0
let iAA = 0
let AA = ""
let fArr: number[] = []
let f = 0
let tmp = 0
fArr = [0, 8.661958, 9.177023, 9.722718, 10.30086, 10.91338, 11.56233, 12.24986, 12.97827, 13.75, 14.56762, 15.43385, 16.3516, 17.32392, 18.35405, 19.44544, 20.60172, 21.82676, 23.12465, 24.49972, 25.95654, 27.5, 29.13523, 30.86771, 32.70319, 34.64783, 36.7081, 38.89087, 41.20344, 43.65353, 46.2493, 48.99942, 51.91309, 55, 58.27047, 61.73542, 65.40639, 69.29565, 73.4162, 77.78175, 82.40688, 87.30706, 92.4986, 97.99885, 103.8262, 110, 116.5409, 123.4708, 130.8128, 138.5913, 146.8324, 155.5635, 164.8138, 174.6141, 184.9972, 195.9977, 207.6523, 220, 233.0819, 246.9417, 261.6255, 277.1826, 293.6648, 311.127, 329.6276, 349.2282, 369.9944, 391.9954, 415.3047, 440, 466.1638, 493.8833, 523.2511, 554.3653, 587.3295, 622.254, 659.2551, 698.4565, 739.9888, 783.9908, 830.6094, 880, 932.3276, 987.7666, 1046.502, 1108.731, 1174.659, 1244.508, 1318.51, 1396.913, 1479.978, 1567.982, 1661.219, 1760, 1864.655, 1975.533, 2093.004, 2217.461, 2349.318, 2489.016, 2637.02, 2793.826, 2959.955, 3135.963, 3322.438, 3520, 3729.31, 3951.067, 4186.009, 4434.922, 4698.637, 4978.032, 5274.041, 5587.652, 5919.911, 6271.926, 6644.875, 7040, 7458.622, 7902.132, 8372.018, 8869.845, 9397.271, 9956.063, 10548.08, 11175.3, 11839.82, 12543.86]
serial.redirect(
SerialPin.USB_TX,
SerialPin.USB_RX,
BaudRate.BaudRate115200
)
for (let index = 0; index <= 50; index++) {
music.ringTone(fArr[index + 40])
basic.pause(5)
}
music.rest(1)
動画1:arduinoのシリアルモニタから直接
動画2:Unity経由で曲を再生
周波数は下記の式で計算できますが、実機だと精度が出ないようです
let fArr: number[] = []
for (let index = 0; index <= 127; index++) {
fArr.push(440 * 2 ** ((index - 69) / 12))
}