Arduino A2DP

Loading...
Searching...
No Matches
BluetoothA2DPSinkQueued.h
1#pragma once
2
3#include "BluetoothA2DPSink.h"
4
5#if IS_VALID_PLATFORM
6
7#define RINGBUF_HIGHEST_WATER_LEVEL (32 * 1024)
8#define RINGBUF_PREFETCH_PERCENT 65
9
10 enum A2DPRingBufferMode : char {
11 RINGBUFFER_MODE_PROCESSING, /* ringbuffer is buffering incoming audio data,
12 I2S is working */
13 RINGBUFFER_MODE_PREFETCHING, /* ringbuffer is buffering incoming audio data,
14 I2S is waiting */
15 RINGBUFFER_MODE_DROPPING /* ringbuffer is not buffering (dropping) incoming
16 audio data, I2S is working */
17};
18
26 class BluetoothA2DPSinkQueued : public BluetoothA2DPSink {
27 public:
28 BluetoothA2DPSinkQueued() = default;
29
30#if A2DP_I2S_AUDIOTOOLS
32 BluetoothA2DPSinkQueued(audio_tools::AudioOutput &output) {
33 actual_bluetooth_a2dp_sink = this;
34 out->set_output(output);
35 }
37 BluetoothA2DPSinkQueued(audio_tools::AudioStream &output) {
38 actual_bluetooth_a2dp_sink = this;
39 out->set_output(output);
40 }
41#endif
42
43#ifdef ARDUINO
45 BluetoothA2DPSinkQueued(Print &output) {
46 actual_bluetooth_a2dp_sink = this;
47 out->set_output(output);
48 }
49#endif
50
52 void set_i2s_stack_size(int size) { i2s_stack_size = size; }
53
55 void set_i2s_ringbuffer_size(int size) { i2s_ringbuffer_size = size; }
56
58 void set_i2s_ringbuffer_prefetch_percent(int percent) {
59 if (percent < 0) return;
60 if (percent > 100) return;
61 ringbuffer_prefetch_percent = percent;
62 }
63
65 void set_i2s_task_priority(UBaseType_t prio) { i2s_task_priority = prio; }
66
67 void set_i2s_write_size_upto(size_t size) { i2s_write_size_upto = size; }
68
69 void set_i2s_ticks(int ticks) { i2s_ticks = ticks; }
70
71 protected:
72 TaskHandle_t s_bt_i2s_task_handle = nullptr; /* handle of I2S task */
73 RingbufHandle_t s_ringbuf_i2s = nullptr; /* handle of ringbuffer for I2S */
74 SemaphoreHandle_t s_i2s_write_semaphore = nullptr;
75 // I2S task
76 int i2s_stack_size = 2048;
77 int i2s_ringbuffer_size = RINGBUF_HIGHEST_WATER_LEVEL;
78 UBaseType_t i2s_task_priority = configMAX_PRIORITIES - 3;
79 volatile A2DPRingBufferMode ringbuffer_mode = RINGBUFFER_MODE_PROCESSING;
80 volatile bool is_starting = true;
81 size_t i2s_write_size_upto = 240 * 6;
82 int i2s_ticks = 20;
83 int ringbuffer_prefetch_percent = RINGBUF_PREFETCH_PERCENT;
84
85 void bt_i2s_task_start_up(void) override;
86 void bt_i2s_task_shut_down(void) override;
87 void i2s_task_handler(void *arg) override;
88 size_t write_audio(const uint8_t *data, size_t size) override;
89
90 void set_i2s_active(bool active) override {
91 BluetoothA2DPSink::set_i2s_active(active);
92 if (active) {
93 ringbuffer_mode = RINGBUFFER_MODE_PREFETCHING;
94 is_starting = true;
95 }
96 }
97
98 int i2s_ringbuffer_prefetch_size() {
99 int bytes = i2s_ringbuffer_size * ringbuffer_prefetch_percent / 100;
100 return (bytes / 4 * 4);
101 }
102};
103
104#endif // platform
BluetoothA2DPOutput::set_output
virtual void set_output(audio_tools::AudioOutput &output)
Not implemented.
Definition BluetoothA2DPOutput.h:32
BluetoothA2DPSink
A2DP Bluethooth Sink - We initialize and start the Bluetooth A2DP Sink. The example https://github....
Definition BluetoothA2DPSink.h:71
BluetoothA2DPSinkQueued
The BluetoothA2DPSinkQueued is using a separate Task with an additinal Queue to write the I2S data....
Definition BluetoothA2DPSinkQueued.h:26
BluetoothA2DPSinkQueued::write_audio
size_t write_audio(const uint8_t *data, size_t size) override
output audio data e.g. to i2s or to queue
Definition BluetoothA2DPSinkQueued.cpp:91
BluetoothA2DPSinkQueued::BluetoothA2DPSinkQueued
BluetoothA2DPSinkQueued(audio_tools::AudioOutput &output)
Output AudioOutput using AudioTools library.
Definition BluetoothA2DPSinkQueued.h:32
BluetoothA2DPSinkQueued::BluetoothA2DPSinkQueued
BluetoothA2DPSinkQueued(audio_tools::AudioStream &output)
Output AudioStream using AudioTools library.
Definition BluetoothA2DPSinkQueued.h:37
BluetoothA2DPSinkQueued::set_i2s_task_priority
void set_i2s_task_priority(UBaseType_t prio)
Defines the priority of the I2S task.
Definition BluetoothA2DPSinkQueued.h:65
BluetoothA2DPSinkQueued::BluetoothA2DPSinkQueued
BluetoothA2DPSinkQueued(Print &output)
Output to Arduino Print.
Definition BluetoothA2DPSinkQueued.h:45
BluetoothA2DPSinkQueued::set_i2s_ringbuffer_prefetch_percent
void set_i2s_ringbuffer_prefetch_percent(int percent)
Audio starts to play when limit exeeded.
Definition BluetoothA2DPSinkQueued.h:58
BluetoothA2DPSinkQueued::set_i2s_stack_size
void set_i2s_stack_size(int size)
Defines the stack size of the i2s task (in bytes)
Definition BluetoothA2DPSinkQueued.h:52
BluetoothA2DPSinkQueued::i2s_task_handler
void i2s_task_handler(void *arg) override
dummy functions needed for BluetoothA2DPSinkQueued
Definition BluetoothA2DPSinkQueued.cpp:45
BluetoothA2DPSinkQueued::set_i2s_ringbuffer_size
void set_i2s_ringbuffer_size(int size)
Defines the ringbuffer size used by the i2s task (in bytes)
Definition BluetoothA2DPSinkQueued.h:55