diff --git a/examples/sandbox/ble/client-as-source/communication-ble-client-send/communication-ble-client-send.ino b/examples/sandbox/ble/client-as-source/communication-ble-client-send/communication-ble-client-send.ino new file mode 100644 index 0000000000..be766c7062 --- /dev/null +++ b/examples/sandbox/ble/client-as-source/communication-ble-client-send/communication-ble-client-send.ino @@ -0,0 +1,39 @@ +/** + * @file communications-ble-client-send.ino + * @author Phil Schatzmann + * @brief Sending audio via BLE: the client acts as audio source + * @version 0.1 + * @date 2022-11-04 + * + * @copyright Copyright (c) 2022 + */ + + +#include "AudioTools.h" +#include "AudioCodecs/CodecADPCM.h" // https://github.com/pschatzmann/adpcm +#include "Sandbox/BLE/AudioBLE.h" + +AudioInfo info(16000, 1, 16); +SineWaveGenerator sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000 +GeneratedSoundStream sound( sineWave); // Stream generated from sine wave +Throttle throttle(sound); +AudioBLEClient ble; +ADPCMEncoder adpcm(AV_CODEC_ID_ADPCM_IMA_WAV); +EncodedAudioStream encoder(&ble, &adpcm); +StreamCopy copier(encoder, throttle); + +void setup() { + Serial.begin(115200); + AudioLogger::instance().begin(Serial, AudioLogger::Info); + + sineWave.begin(info, N_B4); + throttle.begin(info); + encoder.begin(info); + ble.begin("ble-send", 60*10); + + Serial.println("started..."); +} + +void loop() { + copier.copy(); +} \ No newline at end of file diff --git a/examples/sandbox/ble/client-as-source/communication-ble-server-receive/communication-ble-server-receive.ino b/examples/sandbox/ble/client-as-source/communication-ble-server-receive/communication-ble-server-receive.ino new file mode 100644 index 0000000000..63ab3185d5 --- /dev/null +++ b/examples/sandbox/ble/client-as-source/communication-ble-server-receive/communication-ble-server-receive.ino @@ -0,0 +1,38 @@ +/** + * @file communications-ble-server-receive.ino + * @author Phil Schatzmann + * @brief Receiving audio via BLE: the server acts as audio sink + * @version 0.1 + * @date 2022-11-04 + * + * @copyright Copyright (c) 2022 + */ + +#include "AudioTools.h" +#include "AudioCodecs/CodecADPCM.h" // https://github.com/pschatzmann/adpcm +#include "Sandbox/BLE/AudioBLE.h" +//#include "AudioLibs/AudioKit.h" + +AudioInfo info(16000, 1, 16); +ADPCMDecoder adpcm(AV_CODEC_ID_ADPCM_IMA_WAV); +I2SStream i2s; // or AudioKitStream ... +EncodedAudioStream decoder(&i2s, &adpcm); +AudioBLEServer ble; +StreamCopy copier(decoder, ble); + +void setup() { + Serial.begin(115200); + AudioLogger::instance().begin(Serial, AudioLogger::Warning); + + decoder.begin(info); + + auto cfg = i2s.defaultConfig(); + cfg.copyFrom(info); + i2s.begin(cfg); + + ble.begin("ble-receive"); +} + +void loop() { + copier.copy(); +} \ No newline at end of file diff --git a/examples/sandbox/ble/communication-ble-client/communication-ble-client.ino b/examples/sandbox/ble/server-as-source/communication-ble-client-receive/communication-ble-client-receive.ino similarity index 81% rename from examples/sandbox/ble/communication-ble-client/communication-ble-client.ino rename to examples/sandbox/ble/server-as-source/communication-ble-client-receive/communication-ble-client-receive.ino index 3f2269e5d8..f4ef9cff92 100644 --- a/examples/sandbox/ble/communication-ble-client/communication-ble-client.ino +++ b/examples/sandbox/ble/server-as-source/communication-ble-client-receive/communication-ble-client-receive.ino @@ -1,11 +1,11 @@ /** - * @file example-serial-receive.ino + * @file communications-ble-client-receive.ino * @author Phil Schatzmann - * @brief Receiving audio via BLE and writing to I2S + * @brief Receiving audio via BLE: The client acts as audio sink and is writing to I2S * @version 0.1 - * @date 2022-03-09 + * @date 2022-11-04 * - * @copyright Copyright (c) 2022 + * @copyright Copyright (c) 2023 */ diff --git a/examples/sandbox/ble/communication-ble-server/communication-ble-server.ino b/examples/sandbox/ble/server-as-source/communication-ble-server-send/communication-ble-server-send.ino similarity index 83% rename from examples/sandbox/ble/communication-ble-server/communication-ble-server.ino rename to examples/sandbox/ble/server-as-source/communication-ble-server-send/communication-ble-server-send.ino index 1f3a1545dd..233b917cac 100644 --- a/examples/sandbox/ble/communication-ble-server/communication-ble-server.ino +++ b/examples/sandbox/ble/server-as-source/communication-ble-server-send/communication-ble-server-send.ino @@ -1,11 +1,11 @@ /** - * @file communications-ble-send.ino + * @file communications-ble-server-send.ino * @author Phil Schatzmann - * @brief Sending audio via BLE + * @brief Sending audio via BLE: The server acts as audio source * @version 0.1 - * @date 2022-03-09 + * @date 2022-11-04 * - * @copyright Copyright (c) 2022 + * @copyright Copyright (c) 2023 */ #include "AudioTools.h" diff --git a/src/Sandbox/BLE/AudioBLEClient.h b/src/Sandbox/BLE/AudioBLEClient.h index 009971c7d3..89224e56bf 100644 --- a/src/Sandbox/BLE/AudioBLEClient.h +++ b/src/Sandbox/BLE/AudioBLEClient.h @@ -70,17 +70,31 @@ class AudioBLEClient : public AudioBLEStream, size_t write(const uint8_t *data, size_t dataSize) override { TRACED(); setupBLEClient(); - int result = 0; if (!is_client_connected || !is_client_set_up) return 0; - if (ch02_char->canWrite()) { - ch02_char->writeValue((uint8_t *)data, dataSize, false); - result = dataSize; + if (!ch02_char->canWrite()){ + return 0; + } + + if (is_framed){ + writeChannel2Characteristic(data, dataSize); + delay(1); + } else { + // send only data with max mtu + for (int j=0; j write_buffer{0}; + int write_throttle = 0; + bool write_confirmation_flag = false; volatile bool is_client_connected = false; bool is_client_set_up = false; @@ -125,6 +148,13 @@ class AudioBLEClient : public AudioBLEStream, info_char->writeValue((uint8_t *)&info, sizeof(AudioInfo)); } + void writeChannel2Characteristic(const uint8_t*data, size_t len){ + if (ch02_char->canWrite()) { + ch02_char->writeValue((uint8_t *)data, len, write_confirmation_flag); + delay(write_throttle); + } + } + bool readAudioInfoCharacteristic(){ if (!info_char->canRead()) return false; @@ -166,6 +196,11 @@ class AudioBLEClient : public AudioBLEStream, TRACEI(); + // setup buffer + if (write_buffer.size()==0){ + write_buffer.resize(getMTU() - BLE_MTU_OVERHEAD); + } + if (p_client == nullptr) p_client = BLEDevice::createClient();