Repository navigation
Providing Audio Data
pschatzmann edited this page Oct 3, 2026
·
1 revision
The A2DP Source sends the audio data that you provide to a Bluetooth speaker or headphones. The data is PCM: 44.1 kHz, two channels, 16 bits per sample.
#include "BluetoothA2DPSource.h"
BluetoothA2DPSource a2dp_source;
int32_t get_sound_data(uint8_t *data, int32_t byteCount) {
// fill data with your sound data
// and return the effective length in bytes
return byteCount;
}
void setup() {
a2dp_source.set_data_callback(get_sound_data);
a2dp_source.start("MyMusic");
}
void loop() {
delay(1000);
}A Frame contains the 16 bit samples of the two channels (channel1, channel2), which is often more convenient. Here is an example that generates a sine tone:
#include "BluetoothA2DPSource.h"
#include <math.h>
const float deltaAngle = 2.0 * PI * 440.0 / 44100.0;
BluetoothA2DPSource a2dp_source;
int32_t get_data_frames(Frame *frame, int32_t frame_count) {
static float angle = 0.0;
for (int j = 0; j < frame_count; j++) {
frame[j].channel1 = 10000.0 * sin(angle);
frame[j].channel2 = frame[j].channel1;
angle += deltaAngle;
if (angle > 2.0 * PI) angle -= 2.0 * PI;
}
return frame_count;
}
void setup() {
a2dp_source.set_data_callback_in_frames(get_data_frames);
a2dp_source.set_volume(30);
a2dp_source.start("MyMusic");
}
void loop() {
delay(1000);
}In Arduino you can also provide a Stream (e.g. a File) that contains the PCM data:
-
set_data_source(Stream&): a single Stream -
set_data_source_callback(Stream& (*next_stream)()): a callback that provides the next Stream when the current one has ended
- The callbacks are called from a separate task, so keep them fast and avoid any blocking operations.
-
set_volume()adjusts the volume of the data before it is sent. See Volume Control. - To use AAC or your own SBC encoder, see Custom Codecs.
- To select the device you connect to, see Device Discovery & Selection.
Getting Started
A2DP Sink (Receiver)
- Overview & Examples
- Audio Output & DACs
- Sample Rate
- Volume Control
- AVRC: Controlling your Phone
- Metadata & Notifications
- Performance Optimizations
A2DP Source (Sender)
Common
Background
Help