Skip to content

Commit 0f94ecb

Browse files
committed
BLE MIDI Corrections
1 parent 90b8636 commit 0f94ecb

File tree

7 files changed

+44
-68
lines changed

7 files changed

+44
-68
lines changed

examples/MidiBLEServer/MidiBLEServer.ino

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ Clarinet clarinet(440);
1010
ArdMidiBleEventHandler handler(&voicer);
1111
ArdMidiBleServer ble("MidiServer", &handler);
1212

13-
using namespace stk;
14-
15-
ArdMidiBleServer ble("MidiServer");
16-
1713
StkFloat note = 64; // 0 to 128
1814
StkFloat amplitude = 100; // 0 to 128
1915

2016
void setup() {
2117
Serial.begin(115200);
2218

23-
voicer.addInstrument(&clarinet, 0);
19+
voicer.addInstrument(&clarinet, 1);
2420
ble.start(voicer);
21+
ble.setDefaultSendingChannel(0);
2522
}
2623

2724
void loop() {
@@ -37,3 +34,4 @@ void loop() {
3734
}
3835
}
3936

37+

examples/MidiFromUDP/MidiFromUDP

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
using namespace stk;
99

10-
1110
WiFiUDP udp;
1211
Clarinet clarinet(440);
1312
Voicer voicer;
@@ -33,10 +32,5 @@ void setup() {
3332

3433
void loop() {
3534
in.loop();
36-
}```
37-
38-
## BLE Server
39-
The MIDI BLE Server functionality is provided by the __ArdMidiBleServer__ class. Above you have already seen all relevant components and how they work together.
40-
41-
### Sending of MIDI Messages over BLE
35+
}
4236

src/ArdMidiBleEventHandler.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace stk {
55

6+
char * APP_EVENT_HDLR = "ArdMidiBleEventHandler";
7+
68
ArdMidiBleEventHandler::ArdMidiBleEventHandler(Voicer *p_voicer, uint8_t *p_channel)
79
: ArdMidiEventHandler(p_voicer, p_channel) {
810
};
@@ -15,7 +17,7 @@ ArdMidiBleEventHandler::~ArdMidiBleEventHandler(){
1517
* @param [in] pCharacteristic The characteristic that is the source of the event.
1618
*/
1719
void ArdMidiBleEventHandler::onRead(BLECharacteristic* pCharacteristic) {
18-
//ESP_LOGD(APP, "onRead");
20+
ESP_LOGD(APP_EVENT_HDLR, "onRead");
1921
} // onRead
2022

2123

@@ -24,8 +26,9 @@ void ArdMidiBleEventHandler::onRead(BLECharacteristic* pCharacteristic) {
2426
* @param [in] pCharacteristic The characteristic that is the source of the event.
2527
*/
2628
void ArdMidiBleEventHandler::onWrite(BLECharacteristic* pCharacteristic) {
29+
ESP_LOGD(APP_EVENT_HDLR, "onWrite");
2730
const char* str = pCharacteristic->getValue().c_str();
28-
int len = strlen(str);
31+
int len = pCharacteristic->getValue().length();
2932
parse((uint8_t*)str, len);
3033
}
3134

src/ArdMidiBleServer.cpp

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,6 @@ void ArdMidiBleServer :: start() {
1919
pServer = BLEDevice::createServer();
2020
pServer->setCallbacks(new ArdMidiBleServerCallback(&connectionStatus));
2121

22-
// Create the BLE Service
23-
BLEService *pService = pServer->createService(BLEUUID(MIDI_SERVICE_UUID));
24-
// Create a BLE Characteristic
25-
pCharacteristic = pService->createCharacteristic(
26-
BLEUUID(MIDI_CHARACTERISTIC_UUID),
27-
BLECharacteristic::PROPERTY_READ |
28-
BLECharacteristic::PROPERTY_WRITE_NR |
29-
BLECharacteristic::PROPERTY_NOTIFY |
30-
BLECharacteristic::PROPERTY_INDICATE
31-
);
32-
33-
pCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
34-
pCharacteristic->addDescriptor(new BLE2902());
35-
36-
// Start the service
37-
pService->start();
38-
39-
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
40-
pAdvertising->addServiceUUID(MIDI_SERVICE_UUID);
41-
pAdvertising->setScanResponse(true);
42-
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
43-
pAdvertising->setMinPreferred(0x12);
44-
pAdvertising->start();
45-
46-
ESP_LOGD(APP_SERVER, "started");
47-
48-
}
49-
50-
void ArdMidiBleServer :: start(Voicer &voicer) {
51-
setVoicer(voicer);
52-
53-
// Create the BLE Device
54-
BLEDevice::init(this->name);
55-
56-
// Create the BLE Server
57-
pServer = BLEDevice::createServer();
58-
pServer->setCallbacks(new ArdMidiBleServerCallback(&connectionStatus));
59-
6022
//BLEDevice::setEncryptionLevel((esp_ble_sec_act_t)ESP_LE_AUTH_REQ_SC_BOND);
6123

6224
// Create the BLE Service
@@ -70,13 +32,18 @@ void ArdMidiBleServer :: start(Voicer &voicer) {
7032
BLECharacteristic::PROPERTY_NOTIFY |
7133
BLECharacteristic::PROPERTY_INDICATE
7234
);
35+
7336

74-
if (this->pEventHandler == NULL){
75-
this->pEventHandler = new ArdMidiBleEventHandler(&voicer, &(this->receivingChannel));
37+
if (this->pVoicer != nullptr) {
38+
if (this->pEventHandler == nullptr){
39+
this->pEventHandler = new ArdMidiBleEventHandler(pVoicer, &(this->receivingChannel));
40+
}
41+
pCharacteristic->setCallbacks(this->pEventHandler);
7642
}
77-
pCharacteristic->setCallbacks(this->pEventHandler);
78-
pCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
43+
44+
//pCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
7945
pCharacteristic->addDescriptor(new BLE2902());
46+
pCharacteristic->setBroadcastProperty(true);
8047

8148
// Start the service
8249
pService->start();
@@ -98,6 +65,12 @@ void ArdMidiBleServer :: start(Voicer &voicer) {
9865

9966
}
10067

68+
void ArdMidiBleServer :: start(Voicer &voicer) {
69+
ESP_LOGD(APP_SERVER, "start");
70+
setVoicer(voicer);
71+
start();
72+
}
73+
10174
void ArdMidiBleServer :: writeData(MidiMessage *pMsg, int len) {
10275
updateTimestamp(&outMessage);
10376

@@ -120,10 +93,12 @@ ArdMidiBleServerCallback :: ArdMidiBleServerCallback(ConnectionStatus *pStatus){
12093
this->pConnectionStatus = pStatus;
12194
}
12295
void ArdMidiBleServerCallback :: onConnect(BLEServer* pServer) {
96+
ESP_LOGD(APP_SERVER, "onConnect");
12397
*pConnectionStatus = Connected;
12498
};
12599

126100
void ArdMidiBleServerCallback :: onDisconnect(BLEServer* pServer) {
101+
ESP_LOGD(APP_SERVER, "onDisconnect");
127102
*pConnectionStatus = Disconnected;
128103
}
129104

src/ArdMidiCommon.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace stk {
55

66
ArdMidiCommon::ArdMidiCommon(){
77
this->connectionStatus = Unconnected;
8+
this->timestampLow = 0;
9+
this->timestampHigh = 0;
810
}
911

1012
void ArdMidiCommon :: setVoicer(Voicer &voicer) {
@@ -28,8 +30,8 @@ void ArdMidiCommon :: updateTimestamp(MidiMessage *pMsg) {
2830
timestampHigh = 0;
2931
}
3032
}
31-
pMsg->timestampHigh = 0b10000000 | timestampHigh;
32-
pMsg->timestampHigh = 0b10000000 | timestampLow;
33+
pMsg->timestampHigh = 0b10000000 + timestampHigh;
34+
pMsg->timestampLow = 0b10000000 + timestampLow;
3335
}
3436

3537

src/ArdMidiCommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#include "esp_log.h"
3030
#include "BLE2902.h"
3131

32-
#define MIDI_SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
33-
#define MIDI_CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
32+
#define MIDI_SERVICE_UUID "03B80E5A-EDE8-4B33-A751-6CE34EC4C700"
33+
#define MIDI_CHARACTERISTIC_UUID "7772E5DB-3868-4112-A1A9-F2669D106BF3"
3434

3535
#endif
3636

src/ArdMidiEventHandler.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,22 @@ void ArdMidiEventHandler :: addInstrument(Instrmnt &instrument, int channel){
3030
*/
3131

3232
void ArdMidiEventHandler::parse(uint8_t* msg, uint8_t len){
33+
ESP_LOGD(APP, "parse: len: %d", len);
34+
3335
int pos = 0;
34-
uint8_t status;
35-
uint8_t channel;
36-
uint8_t p1;
37-
uint8_t p2;
36+
uint8_t status=0;
37+
uint8_t channel=0;
38+
uint8_t p1=0;
39+
uint8_t p2=0;
3840

39-
while (pos>len){
41+
while (pos<len){
4042
// find status ingnoring headers and timestamps
4143
while (msg[pos]>>7 == 1) {
4244
// if next is data we have a status record
4345
if (pos+1<len && msg[pos+1]>>7 == 0){
44-
status = msg[pos] > 4;
45-
channel = status | 0b1111;
46+
// status: 0b1001 << 4 | channel;
47+
status = msg[pos] >> 4; // high 4 bits
48+
channel = msg[pos] & 0x0F; // lower 4 bits
4649
p1 = 0;
4750
p2 = 0;
4851
}
@@ -52,6 +55,7 @@ void ArdMidiEventHandler::parse(uint8_t* msg, uint8_t len){
5255
// process data bytes
5356
if (msg[pos]>>7 == 0) { // data
5457
p1 = msg[pos];
58+
// check if we have 2 data bytes
5559
if (pos+1<len && msg[pos+1]>>7 == 0) {
5660
pos++;
5761
p2 = msg[pos];
@@ -65,7 +69,7 @@ void ArdMidiEventHandler::parse(uint8_t* msg, uint8_t len){
6569
}
6670

6771
void ArdMidiEventHandler::onCommand(uint8_t channel, uint8_t status, uint8_t p1,uint8_t p2 ){
68-
//ESP_LOGD(APP, "onCommand");
72+
ESP_LOGD(APP, "onCommand channel:%d, status:%d, p1:%d, p2:%d", (int)channel, (int)status, (int)p1, (int)p2);
6973
if (p_channel==NULL || *p_channel == -1 || *p_channel == channel) {
7074
switch (status) {
7175
case 0b1001:

0 commit comments

Comments
 (0)