This commit is contained in:
2026-02-12 21:00:02 -08:00
parent 77f8236347
commit 8bdbf227ca
1141 changed files with 1010880 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
## Using the AI Thinker ESP32 Audio Kit as A2DP Receiver
I found some cheap [AI Thinker ESP32 Audio Kit V2.2](https://docs.ai-thinker.com/en/esp32-audio-kit) on AliExpress.
<img src="https://pschatzmann.github.io/Resources/img/audio-toolkit.png" alt="Audio Kit" />
I am using the data callback of the A2DP library to feed the AudioBoardStream
You dont need to bother about any wires because everything is on one nice board. Just just need to install the dependencies:
## Dependencies
You need to install the following libraries:
- https://github.com/pschatzmann/arduino-audio-tools
- https://github.com/pschatzmann/ESP32-A2DP
- https://github.com/pschatzmann/arduino-audio-driver

View File

@@ -0,0 +1,37 @@
/**
* @file basic-a2dp-audiokit.ino
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-audiokit/basic-a2dp-audiokit/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h" // install https://github.com/pschatzmann/ESP32-A2DP
#include "AudioTools/AudioLibs/AudioBoardStream.h" // install https://github.com/pschatzmann/arduino-audio-driver
BluetoothA2DPSink a2dp_sink;
AudioBoardStream kit(AudioKitEs8388V1);
// Write data to AudioKit in callback
void read_data_stream(const uint8_t *data, uint32_t length) {
kit.write(data, length);
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup output
auto cfg = kit.defaultConfig(TX_MODE);
cfg.sd_active = false;
kit.begin(cfg);
// register callback
a2dp_sink.set_stream_reader(read_data_stream, false);
a2dp_sink.start("AudioKit");
}
void loop() {
}

View File

@@ -0,0 +1,17 @@
## Using the AI Thinker ESP32 Audio Kit as A2DP Receiver with Equalizer
I found some cheap [AI Thinker ESP32 Audio Kit V2.2](https://docs.ai-thinker.com/en/esp32-audio-kit) on AliExpress.
<img src="https://pschatzmann.github.io/Resources/img/audio-toolkit.png" alt="Audio Kit" />
I am using the data callback of the A2DP library to feed the AudioBoardStream that will be controlled by an Equalizer
You dont need to bother about any wires because everything is on one nice board. Just just need to install the dependencies:
## Dependencies
You need to install the following libraries:
- https://github.com/pschatzmann/arduino-audio-tools
- https://github.com/pschatzmann/ESP32-A2DP
- https://github.com/pschatzmann/arduino-audio-driver

View File

@@ -0,0 +1,51 @@
/**
* @file basic-a2dp-audiokit.ino
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-audiokit/basic-a2dp-audiokit/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h" // install https://github.com/pschatzmann/ESP32-A2DP
#include "AudioTools/AudioLibs/AudioBoardStream.h" // install https://github.com/pschatzmann/arduino-audio-driver
BluetoothA2DPSink a2dp_sink;
AudioBoardStream kit(AudioKitEs8388V1);
Equalizer3Bands eq(kit);
ConfigEqualizer3Bands cfg_eq;
// Write data to AudioKit in callback
void read_data_stream(const uint8_t *data, uint32_t length) {
eq.write(data, length);
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup output
auto cfg = kit.defaultConfig(TX_MODE);
cfg.sd_active = false;
kit.begin(cfg);
// max volume
kit.setVolume(1.0);
// setup equilizer
cfg_eq = eq.defaultConfig();
cfg_eq.setAudioInfo(cfg); // use channels, bits_per_sample and sample_rate from kit
cfg_eq.gain_low = 0.5;
cfg_eq.gain_medium = 0.5;
cfg_eq.gain_high = 1.0;
eq.begin(cfg_eq);
// register callback
a2dp_sink.set_stream_reader(read_data_stream, false);
a2dp_sink.start("AudioKit");
}
void loop() {
}

View File

@@ -0,0 +1,68 @@
/**
* @file basic-a2dp-fft-led.ino
* @brief A2DP Sink with output of the FFT result to the LED matrix
* For details see the FFT Wiki: https://github.com/pschatzmann/arduino-audio-tools/wiki/FFT
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include <FastLED.h> // to prevent conflicts introduced with 3.9
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioRealFFT.h" // or any other supported inplementation
#include "AudioTools/AudioLibs/LEDOutput.h"
#include "BluetoothA2DPSink.h"
#define PIN_LEDS 22
#define LED_X 32
#define LED_Y 8
BluetoothA2DPSink a2dp_sink;
AudioRealFFT fft; // or any other supported inplementation
FFTDisplay fft_dis(fft);
LEDOutput led(fft_dis); // output to LED matrix
// Provide data to FFT
void writeDataStream(const uint8_t *data, uint32_t length) {
fft.write(data, length);
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// Setup FFT
auto tcfg = fft.defaultConfig();
tcfg.length = 1024;
tcfg.channels = 2;
tcfg.sample_rate = a2dp_sink.sample_rate();;
tcfg.bits_per_sample = 16;
fft.begin(tcfg);
// Setup LED matrix output
auto lcfg = led.defaultConfig();
lcfg.x = LED_X;
lcfg.y = LED_Y;
led.begin(lcfg);
fft_dis.fft_group_bin = 3;
fft_dis.fft_start_bin = 0;
fft_dis.fft_max_magnitude = 40000;
fft_dis.begin();
// add LEDs
FastLED.addLeds<WS2812B, PIN_LEDS, GRB>(led.ledData(), led.ledCount());
// register A2DP callback
a2dp_sink.set_stream_reader(writeDataStream, false);
// Start Bluetooth Audio Receiver
Serial.print("starting a2dp-fft...");
a2dp_sink.set_auto_reconnect(false);
a2dp_sink.start("a2dp-fft");
}
void loop() {
led.update();
delay(50);
}

View File

@@ -0,0 +1,59 @@
/**
* @file basic-a2dp-fft.ino
* @brief A2DP Sink with output to FFT.
* For details see the FFT Wiki: https://github.com/pschatzmann/arduino-audio-tools/wiki/FFT
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioRealFFT.h" // or any other supported inplementation
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
AudioRealFFT fft; // or any other supported inplementation
// Provide data to FFT
void writeDataStream(const uint8_t *data, uint32_t length) {
fft.write(data, length);
}
// display fft result
void fftResult(AudioFFTBase &fft){
float diff;
auto result = fft.result();
if (result.magnitude>100){
Serial.print(result.frequency);
Serial.print(" ");
Serial.print(result.magnitude);
Serial.print(" => ");
Serial.print(result.frequencyAsNote(diff));
Serial.print( " diff: ");
Serial.println(diff);
}
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// Setup FFT
auto tcfg = fft.defaultConfig();
tcfg.length = 4096;
tcfg.channels = 2;
tcfg.sample_rate = a2dp_sink.sample_rate();;
tcfg.bits_per_sample = 16;
tcfg.callback = &fftResult;
fft.begin(tcfg);
// register callback
a2dp_sink.set_stream_reader(writeDataStream, false);
// Start Bluetooth Audio Receiver
Serial.print("starting a2dp-fft...");
a2dp_sink.set_auto_reconnect(false);
a2dp_sink.start("a2dp-fft");
}
void loop() { delay(100); }

View File

@@ -0,0 +1,43 @@
/**
* @file basic-a2dp-audioi2s.ino
* @brief A2DP Sink with output to I2SStream. This example is of small value
* since my Bluetooth Library already provides I2S output out of the box.
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
I2SStream i2s;
// Write data to I2S
void read_data_stream(const uint8_t *data, uint32_t length) {
i2s.write(data, length);
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// register callback
a2dp_sink.set_stream_reader(read_data_stream, false);
// Start Bluetooth Audio Receiver
a2dp_sink.set_auto_reconnect(false);
a2dp_sink.start("a2dp-i2s");
// setup output
auto cfg = i2s.defaultConfig();
cfg.pin_data = 23;
cfg.sample_rate = a2dp_sink.sample_rate();
cfg.channels = 2;
cfg.bits_per_sample = 16;
cfg.buffer_count = 8;
cfg.buffer_size = 256;
i2s.begin(cfg);
}
void loop() { delay(100); }

View File

@@ -0,0 +1,73 @@
/**
* @file basic-a2dp-mixer-i2s.ino
* @brief A2DP Sink with output to mixer: I think this is the most efficient way
* of mixing a signal that is coming from A2DP which requires only 160 byte of additional RAM.
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "BluetoothA2DPSink.h"
AudioInfo info(44100, 2, 16);
BluetoothA2DPSink a2dp_sink;
I2SStream i2s;
SineWaveGenerator<int16_t> sineWave(10000); // subclass of SoundGenerator with max amplitude of 10000
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
OutputMixer<int16_t> mixer(i2s, 2); // output mixer with 2 outputs
const int buffer_size = 80; // split up the output into small chunks
uint8_t sound_buffer[buffer_size];
// Write data to mixer
void read_data_stream(const uint8_t *data, uint32_t length) {
// To keep the mixing buffer small we split up the output into small chunks
int count = length / buffer_size + 1;
for (int j = 0; j < count; j++) {
const uint8_t *start = data + (j * buffer_size);
const uint8_t *end = min(data + length, start + buffer_size);
int len = end - start;
if (len > 0) {
// write a2dp
mixer.write(start, len);
// write sine tone with identical length
sound.readBytes(sound_buffer, len);
mixer.write(sound_buffer, len);
// We could flush to force the output but this is not necessary because we
// were already writing all 2 streams mixer.flushMixer();
}
}
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// setup Output mixer with min necessary memory
mixer.begin(buffer_size);
// Register data callback
a2dp_sink.set_stream_reader(read_data_stream, false);
// Start Bluetooth Audio Receiver
a2dp_sink.set_auto_reconnect(false);
a2dp_sink.start("a2dp-i2s");
// Update sample rate
info.sample_rate = a2dp_sink.sample_rate();
// start sine wave
sineWave.begin(info, N_B4);
// setup output
auto cfg = i2s.defaultConfig();
cfg.copyFrom(info);
// cfg.pin_data = 23;
cfg.buffer_count = 8;
cfg.buffer_size = 256;
i2s.begin(cfg);
}
void loop() { delay(100); }

View File

@@ -0,0 +1,23 @@
# Stream Analog input to A2DP Bluetooth
We can read an analog signal from a microphone and and send it to a Bluetooth A2DP device. To test the functionality I am using a MCP6022 microphone module.
![MCP6022](https://pschatzmann.github.io/Resources/img/mcp6022.jpeg)
![MCP6022](https://pschatzmann.github.io/Resources/img/mcp6022-1.jpeg)
The MCP6022 is a anlog microphone which operates at 3.3 V
We sample the sound signal with the help of the ESP32 I2S ADC input functionality.
### Pins:
| MCP6022 | ESP32
|---------|---------------
| VCC | 3.3
| GND | GND
| OUT | GPIO34
### Dependencies
- https://github.com/pschatzmann/ESP32-A2DP.git

View File

@@ -0,0 +1,47 @@
/**
* @file basic-adc-a2dp.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-communication/a2dp/basic-adc-a2dp/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*
*/
#include "AudioTools.h"
#include "BluetoothA2DPSource.h"
/**
* @brief We use a mcp6022 analog microphone as input and send the data to A2DP
*/
AnalogAudioStream adc;
BluetoothA2DPSource a2dp_source;
// callback used by A2DP to provide the sound data
int32_t get_sound_data(Frame* frames, int32_t frameCount) {
uint8_t *data = (uint8_t*)frames;
int frameSize = 4;
size_t resultBytes = adc.readBytes(data, frameCount*frameSize);
return resultBytes/frameSize;
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
// start i2s input with default configuration
Serial.println("starting I2S-ADC...");
adc.begin(adc.defaultConfig(RX_MODE));
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.set_auto_reconnect(false);
a2dp_source.set_data_callback_in_frames(get_sound_data);
a2dp_source.start("MyMusic");
}
// Arduino loop - repeated processing
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,44 @@
/**
* @file base-audiokit-a2dp.ino
* @author Phil Schatzmann
* @brief We play the input from the ADC to an A2DP speaker
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/Communication/A2DPStream.h"
AudioInfo info(44100, 2, 16);
BluetoothA2DPSource a2dp_source;
AudioBoardStream i2s(AudioKitEs8388V1);
const int16_t BYTES_PER_FRAME = 4;
// callback used by A2DP to provide the sound data - usually len is 128 2 channel int16 frames
int32_t get_sound_data(Frame* data, int32_t frameCount) {
return i2s.readBytes((uint8_t*)data, frameCount*BYTES_PER_FRAME)/BYTES_PER_FRAME;
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// start i2s input with default configuration
Serial.println("starting I2S...");
auto cfg = i2s.defaultConfig(RX_MODE);
cfg.i2s_format = I2S_STD_FORMAT; // or try with I2S_LSB_FORMAT
cfg.copyFrom(info);
cfg.input_device = ADC_INPUT_LINE2; // microphone
i2s.begin(cfg);
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.set_data_callback_in_frames(get_sound_data);
a2dp_source.start("LEXON MINO L");
}
// Arduino loop - repeated processing
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,53 @@
// We use the decoding on the input side to provid pcm data
#include "SPI.h"
#include "SD.h"
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
//#include "AudioTools/AudioLibs/AudioBoardStream.h" // for SPI pins
File file;
MP3DecoderHelix mp3; // or change to MP3DecoderMAD
EncodedAudioStream decoder(&file, &mp3);
BluetoothA2DPSource a2dp_source;
const int chipSelect = SS; //PIN_AUDIO_KIT_SD_CARD_CS;
// callback used by A2DP to provide the sound data - usually len is 128 2 channel int16 frames
int32_t get_sound_data(uint8_t* data, int32_t size) {
int32_t result = decoder.readBytes((uint8_t*)data, size);
delay(1); // feed the dog
return result;
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// open file
//SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
SD.begin(chipSelect);
file = SD.open("/test.mp3", FILE_READ);
if (!file) {
Serial.println("file failed");
stop();
}
// make sure we have enough space for the pcm data
decoder.transformationReader().resizeResultQueue(1024 * 8);
if (!decoder.begin()) {
Serial.println("decoder failed");
stop();
}
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.set_data_callback(get_sound_data);
a2dp_source.start("LEXON MINO L");
}
// Arduino loop - repeated processing
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,45 @@
/**
* @file basic-generator-a2dp.ino
* @author Phil Schatzmann
* @brief We send a test sine signal to a bluetooth speaker
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
const char* name = "LEXON MINO L"; // Replace with your bluetooth speaker name
SineWaveGenerator<int16_t> sineWave(15000); // subclass of SoundGenerator, set max amplitude (=volume)
GeneratedSoundStream<int16_t> in_stream(sineWave); // Stream generated from sine wave
BluetoothA2DPSource a2dp_source; // A2DP Sender
// callback used by A2DP to provide the sound data - usually len is 128 * 2 channel int16 frames
int32_t get_sound_data(uint8_t * data, int32_t len) {
return in_stream.readBytes((uint8_t*)data, len);
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// start input
auto cfg = in_stream.defaultConfig();
cfg.bits_per_sample = 16;
cfg.channels = 2;
cfg.sample_rate = 44100;
in_stream.begin(cfg);
sineWave.begin(cfg, N_B4);
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.set_auto_reconnect(false);
a2dp_source.set_data_callback(get_sound_data);
a2dp_source.start(name);
}
// Arduino loop - repeated processing
void loop() {
delay(1000);
}

View File

@@ -0,0 +1,35 @@
# Stream I2S Input to A2DP Bluetooth
## General Description:
We implement a A2DP source: We stream the sound input which we read in from the I2S interface to a A2DP sink. We can use any device which provides the sound data via I2S. In order to test the functionality we use the INMP441 microphone.
![INMP441](https://pschatzmann.github.io/Resources/img/inmp441.jpeg)
The INMP441 is a high-performance, low power, digital-output, omnidirectional MEMS microphone with a bottom port. The complete INMP441 solution consists of a MEMS sensor, signal conditioning, an analog-to-digital converter, anti-aliasing filters, power management, and an industry-standard 24-bit I²S interface. The I²S interface allows the INMP441 to connect directly to digital processors, such as DSPs and microcontrollers, without the need for an audio codec in the system.
## Pins
| INMP441 | ESP32
| --------| ---------------
| VDD | 3.3
| GND | GND
| SD | IN (GPIO32)
| L/R | GND
| WS | WS (GPIO15)
| SCK | BCK (GPIO14)
SCK: Serial data clock for I²S interface
WS: Select serial data words for the I²S interface
L/R: Left / right channel selection
When set to low, the microphone emits signals on one channel of the I²S frame.
When the high level is set, the microphone will send signals on the other channel.
ExSD: Serial data output of the I²S interface
VCC: input power 1.8V to 3.3V
GND: Power groundHigh PSR: -75 dBFS.
### Dependencies
- https://github.com/pschatzmann/ESP32-A2DP.git

View File

@@ -0,0 +1,51 @@
/**
* @file basic-i2s-a2dp.ino
* @author Phil Schatzmann
* @brief We use a INMP441 I2S microphone as input and send the data to A2DP
* Unfortunatly the data type from the microphone (int32_t) does not match with
* the required data type by A2DP (int16_t), so we need to convert.
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
AudioInfo info32(44100, 2, 32);
AudioInfo info16(44100, 2, 16);
BluetoothA2DPSource a2dp_source;
I2SStream i2s;
FormatConverterStream conv(i2s);
const int BYTES_PER_FRAME = 4;
int32_t get_sound_data(Frame* data, int32_t frameCount) {
return conv.readBytes((uint8_t*)data, frameCount*BYTES_PER_FRAME)/BYTES_PER_FRAME;
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// setup conversion
conv.begin(info32, info16);
// start i2s input with default configuration
Serial.println("starting I2S...");
auto cfg = i2s.defaultConfig(RX_MODE);
cfg.i2s_format = I2S_STD_FORMAT; // or try with I2S_LSB_FORMAT
cfg.copyFrom(info32);
cfg.is_master = true;
i2s.begin(cfg);
// start the bluetooth
Serial.println("starting A2DP...");
// a2dp_source.set_auto_reconnect(false);
a2dp_source.set_data_callback_in_frames(get_sound_data);
a2dp_source.start("LEXON MINO L");
Serial.println("A2DP started");
}
// Arduino loop - repeated processing
void loop() { delay(1000); }

View File

@@ -0,0 +1,66 @@
/**
* @file basic-player-a2dp.ino
* @author Phil Schatzmann
* @brief Sketch which uses the A2DP callback to provide data from the AudioPlayer via a Queue
* The queue is filled by the Arduino loop.
* @version 0.1
* @date 2022-12-04
*
* @copyright Copyright (c) 2022
*
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
#include "AudioTools/Disk/AudioSourceSDFAT.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
//#include "AudioTools/AudioLibs/AudioBoardStream.h" // for SD Pins
const int cs = 33; //PIN_AUDIO_KIT_SD_CARD_CS;
const int buffer_size = 15*1024;
const char *startFilePath = "/";
const char *ext = "mp3";
AudioSourceSDFAT source(startFilePath, ext, cs);
MP3DecoderHelix decoder;
//Setup of synchronized buffer
BufferRTOS<uint8_t> buffer(0);
QueueStream<uint8_t> out(buffer); // convert Buffer to Stream
AudioPlayer player(source, out, decoder);
BluetoothA2DPSource a2dp;
// Provide data to A2DP
int32_t get_data(uint8_t *data, int32_t bytes) {
size_t result_bytes = buffer.readArray(data, bytes);
//LOGI("get_data_channels %d -> %d of (%d)", bytes, result_bytes , buffer.available());
return result_bytes;
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// allocate in PSRAM only possible in setup or loop
buffer.resize(buffer_size);
// sd_active is setting up SPI with the right SD pins by calling
// SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
// start QueueStream when 95% full
out.begin(95);
// setup player
player.setDelayIfOutputFull(0);
player.setVolume(0.1);
player.begin();
// start a2dp source
Serial.println("starting A2DP...");
a2dp.set_data_callback(get_data);
a2dp.start("LEXON MINO L");
Serial.println("Started!");
}
void loop() {
// decode data to buffer
player.copy();
}

View File

@@ -0,0 +1,87 @@
/**
* @file player-sdfat_a2dp-audiokit.ino
* @author Phil Schatzmann
* @brief Swithcing between Player and A2DP
* @version 0.1
* @date 2022-04-21
*
* @copyright Copyright (c) 2022
*
*/
// install https://github.com/greiman/SdFat.git
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/Disk/AudioSourceSDFAT.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
const char *startFilePath="/";
const char* ext="mp3";
AudioBoardStream kit(AudioKitEs8388V1);
SdSpiConfig sdcfg(PIN_AUDIO_KIT_SD_CARD_CS, DEDICATED_SPI, SD_SCK_MHZ(10) , &SPI);
AudioSourceSDFAT source(startFilePath, ext, sdcfg);
MP3DecoderHelix decoder;
AudioPlayer player(source, kit, decoder);
BluetoothA2DPSink a2dp_sink;
bool player_active = true;
// Write data to AudioKit in callback
void read_data_stream(const uint8_t *data, uint32_t length) {
kit.write(data, length);
}
// switch between a2dp and sd player
void mode(bool, int, void*) {
player_active = !player_active;
if (player_active){
Serial.println("Stopping A2DP");
a2dp_sink.end();
// setup player
player.setVolume(0.7);
player.begin();
Serial.println("Player started");
} else {
Serial.println("Stopping player..");
player.end();
// start sink
a2dp_sink.start("AudioKit");
// update sample rate
auto cfg = kit.defaultConfig();
cfg.sample_rate = a2dp_sink.sample_rate();
kit.setAudioInfo(cfg);
Serial.println("A2DP started");
}
}
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// provide a2dp data
a2dp_sink.set_stream_reader(read_data_stream, false);
// setup output
auto cfg = kit.defaultConfig(TX_MODE);
kit.setVolume(40);
kit.begin(cfg);
// setup additional buttons
kit.addDefaultActions();
kit.addAction(kit.getKey(4), mode);
// setup player
player.setVolume(0.7);
player.begin();
}
void loop() {
if (player) {
player.copy();
} else {
// feed watchdog
delay(10);
}
kit.processActions();
}

View File

@@ -0,0 +1,28 @@
# A Simple SdFat Audio Player
The example demonstrates how to implement an __MP3 Player__: which provides the data from a SD drive and provides the audio via A2DP (e.g. to a Bluetooth Speaker): The __AudioSourceSdFat class__ builds on the [SdFat Library](https://github.com/greiman/SdFat) from Bill Greiman which provides FAT16/FAT32 and exFAT support.
## SD Card
Here is the information how to wire the SD card to the ESP32
| SD | ESP32
|-------|-----------------------
| CS | VSPI-CS0 (GPIO 05)
| SCK | VSPI-CLK (GPIO 18)
| MOSI | VSPI-MOSI (GPIO 23)
| MISO | VSPI-MISO (GPIO 19)
| VCC | VIN (5V)
| GND | GND
![SD](https://www.pschatzmann.ch/wp-content/uploads/2021/04/sd-module.jpeg)
## Dependencies
- https://github.com/pschatzmann/arduino-audio-tools
- https://github.com/pschatzmann/arduino-libhelix
- https://github.com/greiman/SdFat
- https://github.com/pschatzmann/ESP32-A2DP

View File

@@ -0,0 +1,46 @@
/**
* @file player-sdfat-a2dp.ino
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-player/player-sdfat-a2dp/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
//remove this, to find issues regarding mp3 decoding
#define HELIX_LOGGING_ACTIVE false
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
#include "AudioTools/Disk/AudioSourceSDFAT.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
const char *startFilePath="/";
const char* ext="mp3";
AudioSourceSDFAT source(startFilePath, ext); // , PIN_AUDIO_KIT_SD_CARD_CS);
A2DPStream out;
MP3DecoderHelix decoder;
AudioPlayer player(source, out, decoder);
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// setup player
// Setting up SPI if necessary with the right SD pins by calling
// SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
player.setVolume(0.1);
player.begin();
// setup output - We send the test signal via A2DP - so we conect to a Bluetooth Speaker
auto cfg = out.defaultConfig(TX_MODE);
cfg.silence_on_nodata = true; // prevent disconnect when there is no audio data
cfg.name = "LEXON MINO L"; // set the device here. Otherwise the first available device is used for output
//cfg.auto_reconnect = true; // if this is use we just quickly connect to the last device ignoring cfg.name
out.begin(cfg);
}
void loop() {
player.copy();
}

View File

@@ -0,0 +1,41 @@
/**
* @file streams-a2dp-serial.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-a2dp-serial/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
A2DPStream in;
AudioBoardStream kit(AudioKitEs8388V1);
StreamCopy copier(kit, in); // copy in to out
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// start the bluetooth audio receiver
Serial.println("starting A2DP...");
auto cfg = in.defaultConfig(RX_MODE);
cfg.name = "AudioKit";
in.begin(cfg);
// setup the audioKit
auto cfgk = kit.defaultConfig(TX_MODE);
cfgk.copyFrom(in.audioInfo());
kit.begin(cfgk);
}
// Arduino loop
void loop() {
copier.copy();
}

View File

@@ -0,0 +1,3 @@
# Receive Sound Data from Bluetooth A2DP
We receive some music via Bluetooth e.g. from your mobile phone and display it as CSV

View File

@@ -0,0 +1,33 @@
/**
* @file streams-a2dp-serial.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-a2dp-serial/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
A2DPStream in;
CsvOutput<int16_t> out(Serial, 2); // ASCII stream as csv
StreamCopy copier(out, in); // copy in to out
// Arduino Setup
void setup(void) {
Serial.begin(115200);
// start the bluetooth audio receiver
Serial.println("starting A2DP...");
auto cfg = in.defaultConfig(RX_MODE);
cfg.name = "MyReceiver";
in.begin(cfg);
}
// Arduino loop
void loop() {
copier.copy();
}

View File

@@ -0,0 +1,35 @@
/**
* @file basic-a2dp-audiospdif.ino
* @brief A2DP Sink with output to SPDIFOutput
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/SPDIFOutput.h"
#include "BluetoothA2DPSink.h"
AudioInfo info(44100, 2, 16);
SPDIFOutput spdif;
BluetoothA2DPSink a2dp_sink(spdif);
void setup() {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// setup output
auto cfg = spdif.defaultConfig();
cfg.copyFrom(info);
cfg.buffer_size = 384;
cfg.buffer_count = 30;
cfg.pin_data = 23;
spdif.begin(cfg);
// Start Bluetooth Audio Receiver
a2dp_sink.start("a2dp-spdif");
}
void loop() {
delay(100);
}

View File

@@ -0,0 +1,10 @@
# Test Signal to Bluetooth Speaker
Sometimes it is quite useful to be able to generate a test tone.
We can use the GeneratedSoundStream class together with a SoundGenerator class. In my example I use a SineWaveGenerator.
To test the output I'm using this generated signal and write it to A2DP (e.g. a Bluetooth Speaker).
### Dependencies
- https://github.com/pschatzmann/ESP32-A2DP.git

View File

@@ -0,0 +1,48 @@
/**
* @file streams-generator-a2dp.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-generator-a2dp/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
const char* name = "LEXON MINO L"; // Replace with your device name
AudioInfo info(44100, 2, 16);
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in(sineWave); // Stream generated from sine wave
A2DPStream out; // A2DP output
StreamCopy copier(out, in); // copy in to out
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// set the frequency
sineWave.setFrequency(N_B4);
// Setup sine wave
auto cfg = in.defaultConfig();
cfg.copyFrom(info);
in.addNotifyAudioChange(out);
in.begin(cfg);
// We send the test signal via A2DP - so we conect to the MyMusic Bluetooth Speaker
auto cfgA2DP = out.defaultConfig(TX_MODE);
cfgA2DP.name = name;
//cfgA2DP.auto_reconnect = false;
out.begin(cfgA2DP);
out.setVolume(0.3);
Serial.println("A2DP is connected now...");
}
// Arduino loop
void loop() {
copier.copy();
}

View File

@@ -0,0 +1,34 @@
# Stream I2S Input to A2DP Bluetooth
## General Description:
We implement a A2DP source: We stream the sound input which we read in from the I2S interface to a A2DP sink. We can use any device which provides the sound data via I2S. In order to test the functionality we use the INMP441 microphone. Because the Microphone only provides data on one channel, we the ConverterFillLeftAndRight class to copy the data to the other channel as well.
In this Sketch we are using Streams!
![INMP441](https://pschatzmann.github.io/Resources/img/inmp441.jpeg)
The INMP441 is a high-performance, low power, digital-output, omnidirectional MEMS microphone with a bottom port. The complete INMP441 solution consists of a MEMS sensor, signal conditioning, an analog-to-digital converter, anti-aliasing filters, power management, and an industry-standard 24-bit I²S interface. The I²S interface allows the INMP441 to connect directly to digital processors, such as DSPs and microcontrollers, without the need for an audio codec in the system.
## Pins
| INMP441 | ESP32
| --------| ---------------
| VDD | 3.3
| GND | GND
| SD | IN (GPIO32)
| L/R | GND
| WS | WS (GPIO15)
| SCK | BCK (GPIO14)
- SCK: Serial data clock for I²S interface
- WS: Select serial data words for the I²S interface
- L/R: Left / right channel selection
When set to low, the microphone emits signals on the left channel of the I²S frame.
When the high level is set, the microphone will send signals on the right channel.
- ExSD: Serial data output of the I²S interface
- VCC: input power 1.8V to 3.3V
- GND: Power groundHigh PSR: -75 dBFS.

View File

@@ -0,0 +1,43 @@
/**
* @file streams-i2s-a2dp.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-i2s-a2dp/README.md
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
#include "AudioTools.h"
#include "AudioTools/Communication/A2DPStream.h"
I2SStream i2sStream; // Access I2S as stream
A2DPStream a2dpStream; // access A2DP as stream
StreamCopy copier(a2dpStream, i2sStream); // copy i2sStream to a2dpStream
ConverterFillLeftAndRight<int16_t> filler(LeftIsEmpty); // fill both channels
// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// start bluetooth
Serial.println("starting A2DP...");
auto cfgA2DP = a2dpStream.defaultConfig(TX_MODE);
cfgA2DP.name = "LEXON MINO L";
a2dpStream.begin(cfgA2DP);
// set intial volume
a2dpStream.setVolume(0.3);
// start i2s input with default configuration
Serial.println("starting I2S...");
a2dpStream.addNotifyAudioChange(i2sStream); // i2s is using the info from a2dp
i2sStream.begin(i2sStream.defaultConfig(RX_MODE));
}
// Arduino loop - copy data
void loop() {
// copier.copy(filler);
copier.copy();
}

View File

@@ -0,0 +1,15 @@
# A Simple Synthesizer for the AI Thinker AudioKit to A2DP Bluetooth Speaker
I was taking the streams-synth-audiokit example and converted the output to go to A2DP. In order to minimize the lag
I am not using the Stream API but directly the A2DP Callbacks - which avoids any buffers.
The delay howver is still too big to be really useful...
### Dependencies
You need to install the following libraries:
- [Arduino Audio Tools](https://github.com/pschatzmann/arduino-audio-tools)
- [Midi](https://github.com/pschatzmann/arduino-midi)
- [ESP32-A2DP](https://github.com/pschatzmann/ESP32-A2DP)

View File

@@ -0,0 +1,52 @@
/**
* @file streams-synth-audiokit.ino
* @author Phil Schatzmann
* @copyright GPLv3
*
*/
#define USE_MIDI
#include "AudioTools.h" // must be first
#include "AudioTools/AudioLibs/AudioBoardStream.h" // https://github.com/pschatzmann/arduino-audio-driver
#include "BluetoothA2DPSource.h" // https://github.com/pschatzmann/ESP32-A2DP
BluetoothA2DPSource a2dp_source;
int channels = 2;
AudioBoardStream kit(AudioKitEs8388V1);
Synthesizer synthesizer;
GeneratedSoundStream<int16_t> in(synthesizer);
SynthesizerKey keys[] = {{kit.getKey(1), N_C3},{kit.getKey(2), N_D3},{kit.getKey(3), N_E3},{kit.getKey(4), N_F3},{kit.getKey(5), N_G3},{kit.getKey(6), N_A3},{0,0}};
int32_t get_sound_data(Frame *data, int32_t frameCount) {
int frame_size = sizeof(int16_t)*channels;
int16_t samples = synthesizer.readBytes((uint8_t*)data,frameCount*frame_size);
//esp_task_wdt_reset();
delay(1);
return samples/frame_size;
}
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial,AudioLogger::Info);
// setup synthezizer keys
synthesizer.setKeys(kit.audioActions(), keys, AudioActions::ActiveLow);
// define synthesizer keys for AudioKit
synthesizer.setKeys(kit.audioActions(), keys, AudioActions::ActiveLow);
synthesizer.setMidiName("AudioKit Synthesizer");
auto cfg = in.defaultConfig();
cfg.channels = channels;
cfg.sample_rate = 44100;
in.begin(cfg);
a2dp_source.set_data_callback_in_frames(get_sound_data);
a2dp_source.start("LEXON MINO L");
a2dp_source.set_volume(20);
}
void loop() {
kit.processActions();
delay(1);
}