Skip to content

STK Output to A2DP Bluetooth

Phil Schatzmann edited this page Dec 25, 2021 · 6 revisions

I have demonstrated the MIDI Bluetooth functionality where we can receive MIDI messages over BLE in one of my last posts. Today I will investigate if it is possible to combine this with the output of sound to Bluetooth with A2DP that I have demonstrated in my first post.

The Challenge

So far I have never been using 2 Bluetooth Server processes running in parallel on a ESP32 and this is exactly what we would need: The Bluetooth Sink is starting a Bluetooth Server process for sending sound data and the MIDI BLE Server is running in another process at the same time receiving MIDI messages over BLE.

Precondition

The functionality is working on a ESP32: I used a Node MCU ESP32 Development Board. Make sure that you have the latest versions of following libraries installed

You also need to uncomment the #define BT_A2DP in ArdConfig.h!

The Sketch

We receive MIDI messages via BLE, convert them to sound and send the output to the Bluetooth Sink (e.g. Bluetooth loudspeakers).

The sketch is surprisingly short:

#include "StkAll.h"

Clarinet clarinet(440);
Voicer voicer;
ArdMidiBleServer midi("MidiServer");
ArdBtSource btSource;

void setup() {
  Serial.begin(115200);
  Stk::setSampleRate( 44100.0 );
  voicer.addInstrument(&clarinet, 0);
  
  midi.start(voicer);

  btSource.setNVSInit(false);
  btSource.setResetBLE(false);
  btSource.start("RadioPlayer", voicer);
}

void loop() {
}

The Voicer is used to communicate between the two processes. It is used to handle the MIDI input by the ArdMidiBleServer and in the ArdBtSource it is used to get and process the sound data.

The startup logic of the ArdMidiBleServer and the ArdBtSource were indeed not compatible. Therfore I added some switches so that we can deactivate the disturbing functionality: setNVSInit(false) is makeing sure that we do not reset the NVS and setResetBLE(false) is making sure that we do not deactivate the BLE MIDI functionality that we have just started before...

The Testing Environment

I thought that it might be helpful to know how I tested this functionality on my old McBook Air.

Unfortunately my old hardware does not show any BLE devices in the list of Bluetooth devices. I installed BlueSee where they are showing up properly and it is possible to connect from there!

In order to generate the Midi Messages I used Pocket MIDI which is the perfect tool to use if you want to monitor inbound MIDI messages and generate MIDI message for any selected output device. Both are available for free in the Apple App Store.

Conclusion

I think it is pretty impressive that it is possible to define a synthesizer that receives MIDI messages via BLE and sends the output to a Bluetooth Speaker in just with a few lines of code on a cheap 5 USD device...

Clone this wiki locally