arduino-audio-tools it doesn't want to work with BLE #765
-
**** I am working on an esp32 based device with the following libraries: BLE from esp32 in arduino ide and arduino-audio-tools. I created a BLEServer and its working and it worked well for me, I need the device when it receives data from the client (phone) to play a sound on the internal dac and I installed the arduino-audio-tools library I took an example streams-memory_raw-i2s, I adapted it as needed, but when I send 1 from the phone, the sound is heard, then it freezes until disconnection and connection to the power source. please help me, any idea is welcome. thank you in advance. I'm a beginner. I am attaching my aii code. |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 56 replies
-
Just try to test each functionality separatly: e.g the sound processing triggered by some touch pins. If you want to watch in detail what's happening on the audio side you can set the log level to debug... |
Beta Was this translation helpful? Give feedback.
-
Oh, so many errors:
if (StatusBeep && millis() < timeout){
copier.copy())
} |
Beta Was this translation helpful? Give feedback.
-
The thing that you need to make sure is that your logic for triggering is only called once. I assumed that this is working because this has nothing to do with my library! You should be able to verify this if you see"sa livrat 1" only once I don't think that you want it to be played once per second (this is what it is doing now and means that it will play every second) but once for a second I guess! |
Beta Was this translation helpful? Give feedback.
-
I took the BLE server example from Arduino and added the audio playing function that I described in my comments above. The frequency is read from the BLE attribute and the sound is played for 1 second. You can adapt the example for your purpose.... #include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "AudioTools.h"
#include "AudioLibs/AudioKit.h"
// BLE
const char* SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
const char* CHARACTERISTIC_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
uint64_t timeout = 0;
String value="0";
// audio output
AudioInfo info(44100, 1, 16);
AudioKitStream out;
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> music(sineWave); // Stream generated from sine wave
StreamCopy copier(out, music);
class ToneCallback: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
Serial.print(rxValue.c_str());
float frequency = std::stof(rxValue);
if (frequency != 0.0) {
// start audio
Serial.print("playing ");
Serial.println(frequency);
sineWave.begin(info, frequency);
music.begin(info);
// limit to 1 second
timeout = millis() + 1000;
// reset to initial value
value = "0";
pCharacteristic->setValue(value.c_str());
}
}
} toneCallback;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
// setup BLE
BLEDevice::init("Tone Player");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(&toneCallback);
pCharacteristic->setBroadcastProperty(true);
pCharacteristic->setIndicateProperty(true);
pCharacteristic->setNotifyProperty(true);
pCharacteristic->setReadProperty(true);
pCharacteristic->setValue(value.c_str());
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
// setup audio output
auto cfg = out.defaultConfig();
cfg.copyFrom(info);
out.begin(cfg);
}
void loop() {
// output audio
if (timeout > 0 && millis() < timeout) {
copier.copy();
} else {
// if nothing is played we give BLE a chance to do something
delay(50);
}
} |
Beta Was this translation helpful? Give feedback.
-
I don't understand your question. The internal DAC of the ESP32 is cool to do some simple tests, but the audio quality is pretty bad. A better way is to use a DAC which is connected via I2S, but for that you need to connect 5 cables: 3 for the signal and 2 for the power. This was getting annoying, so my preferred way is to use an audio board that has everything on one board. The Espressif LyraT or the AI Thinker AudioKit have an Audio Chip which consists of an ADC and DAC with an amplifier and a built in SD drive. You just need to plug in the speakers or you can use audio cables for the input or output. |
Beta Was this translation helpful? Give feedback.
-
But you already know the answer of this question, because you already used the AnalogAudioStream it in your sketch! I also replaced your audio source (MemoryStream) with a sine wave because I did not have the related file and because this was just more convenient to test. Your MemoryStream is not endless, so it will automatically stop to play at the end you you don't need to do anything. As explained above however you need to care at the start when you want to play it again: The actual position must be re-set to the starting position, by calling begin()! |
Beta Was this translation helpful? Give feedback.
-
I already provided you with a working example: all you have to do is to replace the audio source and audio destination whith what you want to use! |
Beta Was this translation helpful? Give feedback.
-
The AnalogAudioStream and I2SStream are part of the basic API which is automatically available when you include #include "AudioTools.h". Your mentioned includes are absolutely unnecessary! |
Beta Was this translation helpful? Give feedback.
-
Just read the error message: bool begin(): candidate expects 0 arguments, 1 provided |
Beta Was this translation helpful? Give feedback.
-
look in this example, does the sound need to be played once? after that you do nothing. but the audio plays continuously as if set to repeat. one question, where to read about wav decoder how to use it? #include "AudioTools.h" uint8_t channels = 1; MemoryStream music(StarWars30_raw, StarWars30_raw_len); void setup() { auto config = out.defaultConfig(TX_MODE); } void loop() { |
Beta Was this translation helpful? Give feedback.
-
I reviewed the actual code and made a test: it stops after 30 seconds when it reaches the end. #include "AudioTools.h"
#include "StarWars30.h"
AudioInfo info(22050, 1, 16);
I2SStream i2s; // Output to I2S
MemoryStream music(StarWars30_raw, StarWars30_raw_len);
StreamCopy copier(i2s, music); // copies sound into i2s
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
auto config = i2s.defaultConfig(TX_MODE);
config.copyFrom(info);
i2s.begin(config);
}
void loop(){
copier.copy();
} In the Wiki there is a chapter about encoding and decoding... Are you sure that you use the actual version of the library ? |
Beta Was this translation helpful? Give feedback.
-
Can you describe the hardware that you are using ? I don't understand your comment about USB... |
Beta Was this translation helpful? Give feedback.
-
Maybe the easiest would be to add a good portion of silence in the beginning and the end of your file (in the form of 0 values) |
Beta Was this translation helpful? Give feedback.
-
Why don't you look at this example which uses a local config file |
Beta Was this translation helpful? Give feedback.
Just try to test each functionality separatly: e.g the sound processing triggered by some touch pins.
My gut feeling is that you need to do the audio output and BLE processing both in the loop. If you only do all the audio output you cause the BLE not to receive any events and it might hang.
If you want to watch in detail what's happening on the audio side you can set the log level to debug...