
M5Stack で iPhone GarageBandの音を鳴らす ① BLE-MIDIでの接続・発音
目次
前書き:ChatGPTとの相談(読み飛ばし可)
M5Stack でなにをしてみようかなということをChatGPTと話していて、
GarageBandで音を鳴らしてみようとなった流れやBLE MIDIについての質問など。
ESP32-BLE-MIDI をインストール

サンプルコードを試すとエラー
Repository: max22-/ESP32-BLE-MIDI: An Arduino library to use Midi over BLE (Bluetooth Low Energy), on ESP32 boards
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <Arduino.h> #include <BLEMidi.h> void setup() { Serial.begin(115200); Serial.println("Initializing bluetooth"); BLEMidiServer.begin("Basic MIDI device"); Serial.println("Waiting for connections..."); //BLEMidiServer.enableDebugging(); // Uncomment if you want to see some debugging output from the library } void loop() { if(BLEMidiServer.isConnected()) { // If we've got a connection, we send an A4 during one second, at full velocity (127) BLEMidiServer.noteOn(0, 69, 127); delay(1000); BLEMidiServer.noteOff(0, 69, 127); // Then we stop the note and make a delay of one second before returning to the beginning of the loop delay(1000); } } |
記載のある上記のサンプルコードを試そうとすると以下のようなエラー…
1 2 |
error: 'void BLEMidiServerClass::onConnect(NimBLEServer*)' marked 'override', but does not override 18 | void onConnect(BLEServer* pServer) override; error: 'void BLEMidiServerClass::onDisconnect(NimBLEServer*)' marked 'override', but does not override 19 | void onDisconnect(BLEServer* pServer) override; | ^~~~~~~~~~~~ exit status 1 Compilation error: exit status 1 |
ChatGPTに聞いてみた…

リンクされてた参考URL:
ESP32 Bluetooth MIDI: Libraries not found despite being installed – Nano Family / Nano ESP32 – Arduino Forum
NimBLE-Arduino を 1.4.3 にダウングレード
NimBLE-Arduino を検索すると2.3.2が入っている。

1.4.3 を選択肢、Install

ちなみに最新バージョンへ消さなくて良いのか?という疑問。
自動的に上書きされるからいちいち消さなくても良いらしい。

アップデートをしないように注意
先ほど掲載したフォーラムで、
ptillisch さんが以下のように回答しています。(Google 翻訳)
Arduino IDE will periodically display a notification that offers to update the library for you:
Updates are available for some of your libraries.
If you click the “INSTALL MANUALLY” button in the notification, a list of each of the libraries that have available updates will be shown in the Arduino IDE Library Manager. It is generally a good idea to keep your libraries updated since the updates might provide important enhancements or bug fixes. So you should look through the list and update other libraries if appropriate, but you should avoid accepting the update for the NimBLE-Arduino library until such time as a new version of the ESP32-BLE-MIDI library comes out that is compatible with the latest version of NimBLE-Arduino.
翻訳:
Arduino IDE は、ライブラリの更新を提案する通知を定期的に表示します。
一部のライブラリに更新が利用可能です。
通知内の「手動でインストール」ボタンをクリックすると、Arduino IDEライブラリマネージャーに、利用可能なアップデートがある各ライブラリのリストが表示されます。アップデートには重要な機能強化やバグ修正が含まれる可能性があるため、ライブラリは常に最新の状態に保つことをお勧めします。リストを確認し、必要に応じて他のライブラリを更新してください。ただし、最新バージョンのNimBLE-Arduinoと互換性のある新しいバージョンのESP32-BLE-MIDIライブラリがリリースされるまでは、NimBLE-Arduinoライブラリのアップデートは受け入れないでください。
IDEのこの表示かと思います。

Install All をしてしまわないように注意しなきゃ💦
再度書き込み、、成功!

しかしM5Stackには何も表示されていない。。質問。。。
「M5Stackには何も表示されていない。これは一体なんのプログラムでどう確認すりゃええん?」

iOSのGarageBand で接続を確認
右上の⚙️ボタンを押して設定を開き、下の方にスクロール、「詳細」タップ
「Bluetooth MIDIデバイス」 をタップ
「Basic MIDI device」というのが出てきてる!これをタップ
接続できた!
M5Stackに接続表示を追加
M5Stack に何も表示されないのはなんだか寂しいので、
接続状態を表示させるプログラムを書いて貰いました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <M5Unified.h> #include <BLEMidi.h> void setup() { M5.begin(); M5.Lcd.setTextSize(2); M5.Lcd.setCursor(0, 0); M5.Lcd.println("Initializing BLE MIDI..."); Serial.begin(115200); Serial.println("Initializing bluetooth"); BLEMidiServer.begin("Basic MIDI device"); Serial.println("Waiting for connections..."); M5.Lcd.setCursor(0, 30); M5.Lcd.println("Waiting for connection..."); } bool wasConnected = false; void loop() { if (BLEMidiServer.isConnected()) { if (!wasConnected) { M5.Lcd.setCursor(0, 60); M5.Lcd.println("✅ Connected!"); wasConnected = true; } BLEMidiServer.noteOn(0, 69, 127); // A4 delay(1000); BLEMidiServer.noteOff(0, 69, 127); delay(1000); } } |
※上記に到達するまでちょい試行錯誤。
ChatGPTは最新の記述?をしてきてエラーになったので、
「まずはサンプルコードベースで動くように書いて」とお願いした。
表示されてるー!
そしてGarageBandの方で接続すると。。。
無事 Connected 表示ー!!
ボタンを押したら音を鳴らす
ボタンAを押したら音を鳴らすプログラムを追加してもらった。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <M5Unified.h> #include <BLEMidi.h> void setup() { auto cfg = M5.config(); M5.begin(cfg); M5.Lcd.setTextSize(2); M5.Lcd.setCursor(0, 0); M5.Lcd.println("Initializing BLE MIDI..."); Serial.begin(115200); Serial.println("Initializing bluetooth"); BLEMidiServer.begin("Basic MIDI device"); Serial.println("Waiting for connections..."); M5.Lcd.setCursor(0, 30); M5.Lcd.println("Waiting for connection..."); } bool wasConnected = false; bool notePlaying = false; void loop() { M5.update(); // これを忘れるとボタン状態更新されん if (BLEMidiServer.isConnected()) { if (!wasConnected) { M5.Lcd.setCursor(0, 60); M5.Lcd.println("✅ Connected!"); wasConnected = true; } // Aボタン押下時にノートオン if (M5.BtnA.wasPressed()) { BLEMidiServer.noteOn(0, 69, 127); // A4 notePlaying = true; M5.Lcd.setCursor(0, 90); M5.Lcd.println("Note On A4"); } // Aボタン離したらノートオフ if (M5.BtnA.wasReleased() && notePlaying) { BLEMidiServer.noteOff(0, 69, 127); notePlaying = false; M5.Lcd.setCursor(0, 120); M5.Lcd.println("Note Off"); } } } |
すんなり成功してしまいました!
ボタン押してガレバンの音を鳴らすまでできた〜
BLE-MIDI 全然遅延しない#M5Stack #MIDI pic.twitter.com/f9ibaOg4oV— ムプ- (@j6XX2IChZG99891) July 6, 2025
ChatGPTに頼り切りで進めていますが、
初回ビルド成功までは苦戦したものの、
その後は簡単に音を鳴らすまで出来てしまった….。
次回はセンサーを使って音を出すということをやってみたいと思います🤖