Created
December 4, 2023 12:55
-
-
Save RyoKosaka/39c2d0de13fc9efc4fd732a7fb18d79d to your computer and use it in GitHub Desktop.
プロダクトデザイン応用実習サンプルコード - Bluetooth経由で単純なMIDI(ノートオンオフ)を受ける
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// プロダクトデザイン応用実習サンプルコード - Bluetooth経由で単純なMIDI(ノートオンオフ)を受ける | |
// 参考 : switch case文 http://www.musashinodenpa.com/arduino/ref/index.php?f=0&pos=126 | |
// BLE経由でMIDIを扱うライブラリ「BLE-MIDI」を使いますという宣言 | |
#include <BLEMIDI_Transport.h> | |
#include <hardware/BLEMIDI_ESP32.h> | |
BLEMIDI_CREATE_DEFAULT_INSTANCE() | |
void setup() | |
{ | |
// MIDIの初期設定と受け付けるチャンネルの指定 | |
MIDI.begin(1); // チャンネルが1のMIDIだけを受け付ける | |
// 13番ピン(内蔵LED)を出力に使うという宣言 | |
pinMode(13, OUTPUT); | |
} | |
void loop() | |
{ | |
// 送られてきたMIDIの情報を格納する変数を用意する | |
int note; // ノート番号 | |
int velocity; // ベロシティ(強さ) | |
// MIDIが送られてきたときの動きを定義するif文 | |
if (MIDI.read() == true) | |
{ | |
// swich case文でMIDIの中身を読んで場合わけする | |
switch (MIDI.getType()) | |
{ | |
case midi::NoteOn: // MIDIの中身がノートオンだったとき | |
note = MIDI.getData1(); // 変数noteにノート番号を格納 | |
velocity = MIDI.getData2(); // 変数velocityにベロシティを格納 | |
if (note == 60) | |
{ // さらにif文でノート番号が60のときの動きを定義する | |
digitalWrite(13, HIGH); | |
} | |
break; | |
case midi::NoteOff: // MIDIの中身がノートオフだったとき | |
note = MIDI.getData1(); // 変数noteにノート番号を格納 | |
velocity = MIDI.getData2(); // 変数velocityにベロシティを格納 | |
if (note == 60) | |
{ | |
digitalWrite(13, LOW); | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment