Skip to content

Commit daa77e7

Browse files
committed
Midi support
1 parent 0f94ecb commit daa77e7

11 files changed

+55
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Further information can be found in my Blogs
1616
- [Overview](https://www.pschatzmann.ch/home/2020/09/24/the-synthesis-toolkit-skt-library-for-the-arduino-esp32/)
1717
- [Sound without Files](https://www.pschatzmann.ch/home/2020/09/26/the-synthesis-toolkit-stk-w-o-files/)
1818
- [Using Midi](https://www.pschatzmann.ch/home/2020/09/28/the-synthesis-toolkit-skt-library-for-the-arduino-esp32-midi/)
19+
- [Bluetooth Support](https://www.pschatzmann.ch/home/2020/10/02/the-synthesis-toolkit-skt-library-for-the-arduino-esp32-bluetooth-support/)
1920

2021
## Installation
2122
Download the project as zip and install the file in the Arduino IDE via -> Sketch -> Include Library -> Add ZIP Library or execute the following command in the Arduino Library Folder

src/ArdBtSource.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
namespace stk {
55

6+
char * APP_ArdBtSource = "ArdBtSource";
7+
68
// Used to make the voicer available to the callback
79
Voicer *pArdBtSourceVoicer;
810

911
//! Default Constructor
1012
ArdBtSource :: ArdBtSource( ) {
13+
a2dp_source = new BluetoothA2DPSource();
1114
}
1215

1316
//! Destructor
@@ -29,6 +32,9 @@ StkFloat& clipTest( StkFloat& sample )
2932
return sample;
3033
}
3134

35+
bool hasSoundData;
36+
long logTime;
37+
3238
//! Callback: we just get the ticks from the pVoicer converted to Int16
3339
int32_t get_stk_data(uint8_t *data, int32_t len) {
3440

@@ -53,26 +59,40 @@ int32_t get_stk_data(uint8_t *data, int32_t len) {
5359
ptr++;
5460
// Channel 2
5561
*ptr = *(ptr-1);
62+
if (*ptr!=0){
63+
hasSoundData = true;
64+
}
5665
ptr++;
5766
}
67+
68+
if (logTime < millis()){
69+
logTime = millis()+5000;
70+
ESP_LOGD(APP_ArdBtSource, "Playing %s sound!", hasSoundData ? "" : "no" );
71+
hasSoundData = false;
72+
}
73+
5874
return len;
5975
}
6076

6177
void ArdBtSource :: start(char* name, Voicer &voicer) {
6278
this->pVoicer = &voicer;
6379
pArdBtSourceVoicer = &voicer;
6480

65-
if (a2dp_source != NULL) {
66-
delete a2dp_source;
67-
}
68-
a2dp_source = new BluetoothA2DPSource();
6981
a2dp_source->startRaw(name, get_stk_data);
7082
}
7183

7284
bool ArdBtSource :: isConnected(){
7385
return a2dp_source->isConnected();
7486
}
7587

88+
void ArdBtSource :: setNVSInit(bool doInit){
89+
a2dp_source->setNVSInit(doInit);
90+
}
91+
92+
void ArdBtSource :: setResetBLE(bool doInit){
93+
a2dp_source->setResetBLE(doInit);
94+
}
95+
7696
} // stk namespace
7797

7898
#endif

src/ArdBtSource.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#include "Stk.h"
3-
#ifdef __ESP__
3+
#ifdef __BT_A2DP__
44

55
#ifndef ARDBTSOURCE_H
66
#define ARDBTSOURCE_H
@@ -9,6 +9,7 @@
99
#include "ArdCommonOut.h"
1010
#include "BluetoothA2DPSource.h"
1111
#include "Voicer.h"
12+
#include "esp_log.h"
1213

1314
namespace stk {
1415

@@ -38,6 +39,13 @@ class ArdBtSource : public Stk
3839
//! Checks if this device is still connnected
3940
bool isConnected();
4041

42+
//! Defines if the Flash NVS should be reset on start
43+
void setNVSInit(bool doInit);
44+
45+
//! Defines if the BLE should be reset on start
46+
void setResetBLE(bool doInit);
47+
48+
4149
protected:
4250
Voicer *pVoicer;
4351
BluetoothA2DPSource *a2dp_source;

src/ArdConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#if defined(ESP32)
1010
#define __RAW_ARRAYS__ // raw files as compilable c arrays
1111
#define __STREAMS__ // output to Ardiono Streams
12+
#define __BT_A2DP__ // output to A2DP sink
1213
#define __MIDI__ // support Midi
1314
#define __MIDI_BLE__ // support BLE
1415
#define __RTOS__ // supports Free RTOS

src/ArdMidiBleEventHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace stk {
55

66
char * APP_EVENT_HDLR = "ArdMidiBleEventHandler";
77

8-
ArdMidiBleEventHandler::ArdMidiBleEventHandler(Voicer *p_voicer, uint8_t *p_channel)
8+
ArdMidiBleEventHandler::ArdMidiBleEventHandler(Voicer *p_voicer, int *p_channel)
99
: ArdMidiEventHandler(p_voicer, p_channel) {
1010
};
1111

src/ArdMidiBleEventHandler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Voicer.h"
88
#include "ArdMidiEventHandler.h"
99
#include "BLECharacteristic.h"
10+
#include "esp_log.h"
1011

1112
namespace stk {
1213

@@ -26,7 +27,7 @@ namespace stk {
2627
class ArdMidiBleEventHandler
2728
: public BLECharacteristicCallbacks , public ArdMidiEventHandler {
2829
public:
29-
ArdMidiBleEventHandler(Voicer *voicer, uint8_t *p_channel = NULL );
30+
ArdMidiBleEventHandler(Voicer *voicer, int *p_channel = NULL );
3031
~ArdMidiBleEventHandler();
3132
void onRead(BLECharacteristic* pCharacteristic);
3233
void onWrite(BLECharacteristic* pCharacteristic);

src/ArdMidiBleServer.cpp

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

66
const char* APP_SERVER = "ArdMidiBleServer";
77

8-
ArdMidiBleServer::ArdMidiBleServer(char* name, ArdMidiBleEventHandler* pEventHandler){
8+
ArdMidiBleServer::ArdMidiBleServer(char* name, ArdMidiBleEventHandler* pEventHandler)
9+
: ArdMidiCommon() {
910
this->name = name;
1011
this->pEventHandler = pEventHandler;
1112
this->connectionStatus = Unconnected;
@@ -34,10 +35,12 @@ void ArdMidiBleServer :: start() {
3435
);
3536

3637

37-
if (this->pVoicer != nullptr) {
38-
if (this->pEventHandler == nullptr){
38+
if (this->pVoicer != NULL) {
39+
if (this->pEventHandler == NULL){
40+
ESP_LOGD(APP_SERVER, "Creating new ArdMidiBleEventHandler for voicer");
3941
this->pEventHandler = new ArdMidiBleEventHandler(pVoicer, &(this->receivingChannel));
4042
}
43+
ESP_LOGD(APP_SERVER, "Setting callback for characteristic");
4144
pCharacteristic->setCallbacks(this->pEventHandler);
4245
}
4346

src/ArdMidiCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void ArdMidiCommon :: setDefaultSendingChannel(int8_t channel) {
1717
this->sendingChannel = channel;
1818
}
1919

20-
void ArdMidiCommon :: setFilterReceivingChannel(int8_t channel){
20+
void ArdMidiCommon :: setFilterReceivingChannel(int channel){
2121
this->receivingChannel = channel;
2222
}
2323

src/ArdMidiCommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ArdMidiCommon {
7575
ArdMidiCommon();
7676

7777
//! Activates a filter on receiving messages to the indicated channel
78-
void setFilterReceivingChannel(int8_t channel);
78+
void setFilterReceivingChannel(int channel);
7979

8080
//! Sets the default channel for the sending commands.
8181
void setDefaultSendingChannel(int8_t channel);
@@ -131,7 +131,7 @@ class ArdMidiCommon {
131131
ConnectionStatus connectionStatus;
132132
Voicer *pVoicer;
133133
MidiMessage outMessage;
134-
uint8_t receivingChannel = -1;
134+
int receivingChannel = -1;
135135
uint8_t sendingChannel = 0;
136136
uint8_t timestampLow;
137137
uint8_t timestampHigh;

src/ArdMidiEventHandler.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const char* APP = "ArdMidiEventHandler";
99

1010
//ArdMidiEventHandler *self_ArdMidiEventHandler;
1111

12-
ArdMidiEventHandler::ArdMidiEventHandler(Voicer *p_voicer, uint8_t *p_channel){
12+
ArdMidiEventHandler::ArdMidiEventHandler(Voicer *p_voicer, int *p_channel){
1313
this->p_voicer = p_voicer;
1414
this->p_channel = p_channel;
1515

@@ -70,7 +70,8 @@ void ArdMidiEventHandler::parse(uint8_t* msg, uint8_t len){
7070

7171
void ArdMidiEventHandler::onCommand(uint8_t channel, uint8_t status, uint8_t p1,uint8_t p2 ){
7272
ESP_LOGD(APP, "onCommand channel:%d, status:%d, p1:%d, p2:%d", (int)channel, (int)status, (int)p1, (int)p2);
73-
if (p_channel==NULL || *p_channel == -1 || *p_channel == channel) {
73+
ESP_LOGD(APP, "onCommand filtered channel: %d ", *p_channel);
74+
if (p_channel==NULL || *p_channel < 0 || *p_channel == channel) {
7475
switch (status) {
7576
case 0b1001:
7677
onNoteOn(channel, p1, p2);
@@ -92,10 +93,12 @@ void ArdMidiEventHandler::onCommand(uint8_t channel, uint8_t status, uint8_t p1,
9293
}
9394

9495
void ArdMidiEventHandler::onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity){
96+
ESP_LOGD(APP, "noteOn note:%d, velocity:%d, channel:%d", (int)note, (int)velocity, (int)channel);
9597
p_voicer->noteOn(note, velocity, channel);
9698
};
9799

98100
void ArdMidiEventHandler::onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity){
101+
ESP_LOGD(APP, "noteOff note:%d, velocity:%d, channel:%d", (int)note, (int)velocity, (int)channel);
99102
p_voicer->noteOff(note, velocity, channel);
100103
};
101104
void ArdMidiEventHandler::onControlChange(uint8_t channel, uint8_t controller, uint8_t value){

src/ArdMidiEventHandler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define ARDMIDIEVENTHANDLER_H
66

77
#include "Voicer.h"
8+
89
namespace stk {
910

1011
/***************************************************/
@@ -27,7 +28,7 @@ namespace stk {
2728
/***************************************************/
2829
class ArdMidiEventHandler {
2930
public:
30-
ArdMidiEventHandler(Voicer *voicer, uint8_t *p_channel = NULL );
31+
ArdMidiEventHandler(Voicer *voicer, int *p_channel = NULL );
3132
~ArdMidiEventHandler();
3233
void addInstrument(Instrmnt &instrument, int channel);
3334
void parse(uint8_t* msg, uint8_t len);
@@ -39,7 +40,7 @@ class ArdMidiEventHandler {
3940

4041
protected:
4142
Voicer *p_voicer;
42-
uint8_t *p_channel;
43+
int *p_channel;
4344
};
4445

4546

0 commit comments

Comments
 (0)