たま日記

たまに書く

BLE MIDIの通信

ESP32とWindows10の間でBLE MIDI通信したときに,頻度がどうなるか調べてみた.参考にしたのは

結果

  • ESP32側で13.333ms毎にnotify()した 全て届いた
  • onConnect()内でconn_params.max_int を0x20にすると40ms毎に、0x0cにすると15ms毎に届く(値 * 1.25ms)それより小さい値を設定しても15msより短くはならないみたい 設定値より遅れることもある
  • 同じ時刻に届く(MIDI-OXに認識される)こともある
 ===> MIDI-OX Version: 7.0.2.372
 ===> Log Opened: Tue 30-Jun-2020 11:56:51 ===>
 TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT               
        20  1  --    176     2    49    1  ---  CC: Breath            
        35  1  --    176     2    50    1  ---  CC: Breath            
        51  1  --    176     2    51    1  ---  CC: Breath            
        65  1  --    176     2    52    1  ---  CC: Breath            
        66  1  --    176     2    53    1  ---  CC: Breath            
        80  1  --    176     2    54    1  ---  CC: Breath            
        95  1  --    176     2    55    1  ---  CC: Breath            
       110  1  --    176     2    56    1  ---  CC: Breath            
       125  1  --    176     2    57    1  ---  CC: Breath            
       140  1  --    176     2    58    1  ---  CC: Breath            
       155  1  --    176     2    59    1  ---  CC: Breath            
       170  1  --    176     2    60    1  ---  CC: Breath            
       185  1  --    176     2    61    1  ---  CC: Breath            
       200  1  --    176     2    62    1  ---  CC: Breath            
       201  1  --    176     2    63    1  ---  CC: Breath            
       215  1  --    176     2    64    1  ---  CC: Breath            
       230  1  --    176     2    65    1  ---  CC: Breath            
       245  1  --    176     2    66    1  ---  CC: Breath            
       260  1  --    176     2    67    1  ---  CC: Breath            
       275  1  --    176     2    68    1  ---  CC: Breath            
       290  1  --    176     2    69    1  ---  CC: Breath            
       305  1  --    176     2    70    1  ---  CC: Breath            
       320  1  --    176     2    71    1  ---  CC: Breath            
       335  1  --    176     2    72    1  ---  CC: Breath            
       335  1  --    176     2    73    1  ---  CC: Breath            
       350  1  --    176     2    74    1  ---  CC: Breath            
       365  1  --    176     2     0    1  ---  CC: Breath            
       380  1  --    176     2     1    1  ---  CC: Breath            
       395  1  --    176     2     2    1  ---  CC: Breath            
       410  1  --    176     2     3    1  ---  CC: Breath            
       426  1  --    176     2     4    1  ---  CC: Breath            
       440  1  --    176     2     5    1  ---  CC: Breath            
       455  1  --    176     2     6    1  ---  CC: Breath            
       470  1  --    176     2     7    1  ---  CC: Breath            
       470  1  --    176     2     8    1  ---  CC: Breath            
       485  1  --    176     2     9    1  ---  CC: Breath            
       500  1  --    176     2    10    1  ---  CC: Breath            
       515  1  --    176     2    11    1  ---  CC: Breath            
       530  1  --    176     2    12    1  ---  CC: Breath            
       545  1  --    176     2    13    1  ---  CC: Breath            
       560  1  --    176     2    14    1  ---  CC: Breath            
       575  1  --    176     2    15    1  ---  CC: Breath            
       590  1  --    176     2    16    1  ---  CC: Breath            
       605  1  --    176     2    17    1  ---  CC: Breath            
       606  1  --    176     2    18    1  ---  CC: Breath            
       620  1  --    176     2    19    1  ---  CC: Breath            
       636  1  --    176     2    20    1  ---  CC: Breath            
       650  1  --    176     2    21    1  ---  CC: Breath            
       665  1  --    176     2    22    1  ---  CC: Breath            
       680  1  --    176     2    23    1  ---  CC: Breath            
       695  1  --    176     2    24    1  ---  CC: Breath            
       710  1  --    176     2    25    1  ---  CC: Breath            
       725  1  --    176     2    26    1  ---  CC: Breath            
       740  1  --    176     2    27    1  ---  CC: Breath            
       741  1  --    176     2    28    1  ---  CC: Breath            
       755  1  --    176     2    29    1  ---  CC: Breath            
       770  1  --    176     2    30    1  ---  CC: Breath            
       785  1  --    176     2    31    1  ---  CC: Breath            
       800  1  --    176     2    32    1  ---  CC: Breath            
       815  1  --    176     2    33    1  ---  CC: Breath            
       838  1  --    176     2    34    1  ---  CC: Breath            
       845  1  --    176     2    35    1  ---  CC: Breath            
       862  1  --    176     2    36    1  ---  CC: Breath            
       875  1  --    176     2    37    1  ---  CC: Breath            
       875  1  --    176     2    38    1  ---  CC: Breath            
       890  1  --    176     2    39    1  ---  CC: Breath            
       905  1  --    176     2    40    1  ---  CC: Breath            
       920  1  --    176     2    41    1  ---  CC: Breath            
       935  1  --    176     2    42    1  ---  CC: Breath            
       950  1  --    176     2    43    1  ---  CC: Breath            
       965  1  --    176     2    44    1  ---  CC: Breath            
       980  1  --    176     2    45    1  ---  CC: Breath            
       995  1  --    176     2    46    1  ---  CC: Breath            
      1010  1  --    176     2    47    1  ---  CC: Breath            
      1011  1  --    176     2    48    1  ---  CC: Breath            
      1025  1  --    176     2    49    1  ---  CC: Breath            
      1040  1  --    176     2    50    1  ---  CC: Breath            
      1055  1  --    176     2    51    1  ---  CC: Breath            
      1070  1  --    176     2    52    1  ---  CC: Breath            
      1085  1  --    176     2    53    1  ---  CC: Breath            
      1100  1  --    176     2    54    1  ---  CC: Breath            
      1115  1  --    176     2    55    1  ---  CC: Breath            
      1130  1  --    176     2    56    1  ---  CC: Breath            
      1145  1  --    176     2    57    1  ---  CC: Breath            
      1145  1  --    176     2    58    1  ---  CC: Breath            
      1160  1  --    176     2    59    1  ---  CC: Breath            
      1175  1  --    176     2    60    1  ---  CC: Breath            
      1190  1  --    176     2    61    1  ---  CC: Breath            
      1205  1  --    176     2    62    1  ---  CC: Breath            
      1227  1  --    176     2    63    1  ---  CC: Breath            
      1235  1  --    176     2    64    1  ---  CC: Breath            
      1250  1  --    176     2    65    1  ---  CC: Breath            
      1265  1  --    176     2    66    1  ---  CC: Breath            
      1280  1  --    176     2    67    1  ---  CC: Breath            
      1281  1  --    176     2    68    1  ---  CC: Breath            
      1295  1  --    176     2    69    1  ---  CC: Breath            
      1310  1  --    176     2    70    1  ---  CC: Breath            
      1325  1  --    176     2    71    1  ---  CC: Breath            
      1340  1  --    176     2    72    1  ---  CC: Breath            
      1355  1  --    176     2    73    1  ---  CC: Breath            
      1371  1  --    176     2    74    1  ---  CC: Breath            
      1385  1  --    176     2     0    1  ---  CC: Breath            
      1400  1  --    176     2     1    1  ---  CC: Breath            
      1415  1  --    176     2     2    1  ---  CC: Breath            
      1415  1  --    176     2     3    1  ---  CC: Breath            
      1430  1  --    176     2     4    1  ---  CC: Breath            
      1445  1  --    176     2     5    1  ---  CC: Breath            
      1460  1  --    176     2     6    1  ---  CC: Breath            
      1475  1  --    176     2     7    1  ---  CC: Breath            
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>

#define SERVICE_UUID        "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;

uint8_t midiPacket[] = {
   0x80,  // header
   0x80,  // timestamp, not implemented 
   0x00,  // status
   0x3c,  // 0x3c == 60 == middle c
   0x00   // velocity
};

void setup() {
    delay(500);
    Serial.begin(115200);
    BLE_init();
}

void loop() {
    noteOn(60, 120);
    for (;;) {
        for (int i=0; i<75; i++) {
            setBreath(i);
            delayMicroseconds(13333); // 75Hz
        }
    }
}

void setBreath(int value) {
    setTimestamp();
    midiPacket[2] = 0xB0;   // control change
    midiPacket[3] = 2;      // breath
    midiPacket[4] = value;  // value
    pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes
    pCharacteristic->notify();
}

void setTimestamp() {
    auto currentTimestamp = millis() & 0x01fff;
    midiPacket[0] = ((currentTimestamp >> 7) & 0x3f) | 0x80;
    midiPacket[1] = (currentTimestamp & 0x7f) | 0x80;
}

class MyServerCallbacks: public BLEServerCallbacks {
    
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
      Serial.println("con0");
    };

    void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t* param) {
        Serial.println("con1");
        esp_ble_conn_update_params_t conn_params = {0};
        memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
        conn_params.latency = 0;
        conn_params.max_int = 0x0c;    // max_int = 0x0c*1.25ms = 15ms
        conn_params.min_int = 0x06;    // min_int = 0x06*1.25ms = 7.5ms
        conn_params.timeout = 400;     // timeout = 400*10ms = 4000ms
        //start sent the update connection parameters to the peer device.
        esp_ble_gap_update_conn_params(&conn_params);
    }

    void onDisconnect(BLEServer* pServer) {
      Serial.println("discon");
      deviceConnected = false;
    }
};

void BLE_init() {
    BLEDevice::init("ESP32 MIDI Example");
    
    // Create the BLE Server
    BLEServer *pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    // Create the BLE Service
    BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));

    // Create a BLE Characteristic
    pCharacteristic = pService->createCharacteristic(
        BLEUUID(CHARACTERISTIC_UUID),
        BLECharacteristic::PROPERTY_READ   |
        BLECharacteristic::PROPERTY_WRITE  |
        BLECharacteristic::PROPERTY_NOTIFY |
        BLECharacteristic::PROPERTY_WRITE_NR
    );

    // Create a BLE Descriptor
    pCharacteristic->addDescriptor(new BLE2902());

    // Start the service
    pService->start();

    // Start advertising
    BLEAdvertising *pAdvertising = pServer->getAdvertising();
    pAdvertising->addServiceUUID(pService->getUUID());
    pAdvertising->start();
}