Arduino A2DP

Loading...
Searching...
No Matches
BluetoothA2DPOutput.h
1#pragma once
2#include "BluetoothA2DPCommon.h"
3
4#ifdef ARDUINO
5#include "Print.h"
6#endif
7
8#if A2DP_LEGACY_I2S_SUPPORT
9#include "driver/i2s.h"
10#endif
11
12#if A2DP_I2S_AUDIOTOOLS
13#include "AudioTools.h"
14#endif
15
22 class BluetoothA2DPOutput {
23 public:
24 virtual bool begin() = 0;
25 virtual size_t write(const uint8_t *data, size_t len) = 0;
26 virtual void end() = 0;
27 virtual void set_sample_rate(int rate) = 0;
28 virtual void set_output_active(bool active) = 0;
29
30#if A2DP_I2S_AUDIOTOOLS
32 virtual void set_output(audio_tools::AudioOutput &output) {}
34 virtual void set_output(audio_tools::AudioStream &output) {}
35#endif
36
37#ifdef ARDUINO
39 virtual void set_output(Print &output) {}
40#endif
41
42#if A2DP_LEGACY_I2S_SUPPORT
44 virtual void set_pin_config(i2s_pin_config_t pin_config) {}
46 virtual void set_i2s_port(i2s_port_t i2s_num) {}
47
49 virtual void set_i2s_config(i2s_config_t i2s_config) {}
50
53 virtual void set_bits_per_sample(int bps) {}
54
55#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 1)
57 virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin) { return ESP_FAIL; };
58#endif
59
60#endif
61};
62
69 class BluetoothA2DPOutputAudioTools : public BluetoothA2DPOutput {
70 public:
71 BluetoothA2DPOutputAudioTools() = default;
72 bool begin() override;
73 size_t write(const uint8_t *data, size_t len) override;
74 void end() override;
75 void set_sample_rate(int rate) override;
76 void set_output_active(bool active) override;
77
78 operator bool() {
79#if A2DP_I2S_AUDIOTOOLS || defined(ARDUINO)
80 return p_print != nullptr;
81#else
82 return false;
83#endif
84 }
85
86
87#if A2DP_I2S_AUDIOTOOLS
89 void set_output(audio_tools::AudioOutput &output) {
90 p_print = &output;
91 p_audio_print = &output;
92 }
93
95 void set_output(audio_tools::AudioStream &output) {
96 static audio_tools::AdapterAudioStreamToAudioOutput adapter(output);
97 adapter.setStream(output);
98 p_print = &output;
99 p_audio_print = &adapter;
100 }
101#endif
102
103#ifdef ARDUINO
105 void set_output(Print &output) { p_print = &output; }
106#endif
107
108 protected:
109#if defined(ARDUINO) || A2DP_I2S_AUDIOTOOLS
110 Print *p_print = nullptr;
111#endif
112
113#if A2DP_I2S_AUDIOTOOLS
114 audio_tools::AudioOutput *p_audio_print = nullptr;
115#endif
116};
117
118#ifdef ARDUINO
119
126 class BluetoothA2DPOutputPrint : public BluetoothA2DPOutput {
127 public:
128 BluetoothA2DPOutputPrint() = default;
129 bool begin() { return true;};
130 size_t write(const uint8_t *data, size_t len) override {
131 if (p_print==nullptr) return 0;
132 return p_print->write(data, len);
133 }
134 void end() override {}
135 void set_sample_rate(int rate) override {};
136 void set_output_active(bool active) override {};
137
138 operator bool() {
139 return p_print != nullptr;
140 }
141
143 void set_output(Print &output) { p_print = &output; }
144
145 protected:
146 Print *p_print = nullptr;
147
148};
149#endif
150
151
157 class BluetoothA2DPOutputLegacy : public BluetoothA2DPOutput {
158 public:
159 BluetoothA2DPOutputLegacy();
160 bool begin() override;
161 size_t write(const uint8_t *data, size_t len) override;
162 void end() override;
163 void set_sample_rate(int rate) override;
164 void set_output_active(bool active) override;
165
166#if A2DP_LEGACY_I2S_SUPPORT
168 virtual void set_pin_config(i2s_pin_config_t pin_config) {
169 this->pin_config = pin_config;
170 }
172 virtual void set_i2s_port(i2s_port_t i2s_num) { i2s_port = i2s_num; }
173
175 virtual void set_i2s_config(i2s_config_t i2s_config) {
176 this->i2s_config = i2s_config;
177 }
178
181 virtual void set_bits_per_sample(int bps) {
182 i2s_config.bits_per_sample = (i2s_bits_per_sample_t)bps;
183 }
184
185#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 1)
186 virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin);
187#endif
188
189 protected:
190 i2s_config_t i2s_config;
191 i2s_pin_config_t pin_config;
192 i2s_channel_t i2s_channels = I2S_CHANNEL_STEREO;
193 i2s_port_t i2s_port = I2S_NUM_0;
194#endif
195};
196
203 class BluetoothA2DPOutputDefault : public BluetoothA2DPOutput {
204 public:
205 BluetoothA2DPOutputDefault() = default;
206 bool begin() {
207 bool rc = false;
208 if (out_tools)
209 rc = out_tools.begin();
210 else
211 rc = out_legacy.begin();
212 return rc;
213 }
214
215 size_t write(const uint8_t *data, size_t len) {
216 size_t result = 0;
217 if (out_tools)
218 result = out_tools.write(data, len);
219 else
220 result = out_legacy.write(data, len);
221 return result;
222 }
223
224 void end() override {
225 if (out_tools)
226 out_tools.end();
227 else
228 out_legacy.end();
229 }
230
231 void set_sample_rate(int rate) override {
232 if (out_tools)
233 out_tools.set_sample_rate(rate);
234 else
235 out_legacy.set_sample_rate(rate);
236 }
237
238 void set_output_active(bool active) override {
239 if (out_tools)
240 out_tools.set_output_active(active);
241 else
242 out_legacy.set_output_active(active);
243 }
244
245#if A2DP_I2S_AUDIOTOOLS
247 void set_output(audio_tools::AudioOutput &output) override { out_tools.set_output(output); }
249 void set_output(audio_tools::AudioStream &output) override { out_tools.set_output(output); }
250#endif
251
252#ifdef ARDUINO
254 void set_output(Print &output) override { out_tools.set_output(output); }
255#endif
256
257#if A2DP_LEGACY_I2S_SUPPORT
259 virtual void set_pin_config(i2s_pin_config_t pin_config) override {
260 out_legacy.set_pin_config(pin_config);
261 }
263 virtual void set_i2s_port(i2s_port_t i2s_num) override {
264 out_legacy.set_i2s_port(i2s_num);
265 }
266
268 virtual void set_i2s_config(i2s_config_t i2s_config) override {
269 out_legacy.set_i2s_config(i2s_config);
270 }
271
274 virtual void set_bits_per_sample(int bps) override {
275 out_legacy.set_bits_per_sample(bps);
276 }
277
278#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 1)
279 virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin) override {
280 return out_legacy.i2s_mclk_pin_select(pin);
281 }
282#endif
283#endif // A2DP_LEGACY_I2S_SUPPORT
284 protected:
285 BluetoothA2DPOutputAudioTools out_tools;
286 BluetoothA2DPOutputLegacy out_legacy;
287};
BluetoothA2DPCommon.h
BluetoothA2DPOutputAudioTools
Output Class using AudioTools library: https://github.com/pschatzmann/arduino-audio-tools.
Definition BluetoothA2DPOutput.h:69
BluetoothA2DPOutputAudioTools::set_output
void set_output(Print &output)
Output to Arduino Print.
Definition BluetoothA2DPOutput.h:105
BluetoothA2DPOutputAudioTools::set_output
void set_output(audio_tools::AudioOutput &output)
Output AudioStream using AudioTools library.
Definition BluetoothA2DPOutput.h:89
BluetoothA2DPOutputAudioTools::set_output
void set_output(audio_tools::AudioStream &output)
Output AudioStream using AudioTools library.
Definition BluetoothA2DPOutput.h:95
BluetoothA2DPOutputDefault
Default Output Class providing both the Legacy I2S and the AudioTools I2S functionality.
Definition BluetoothA2DPOutput.h:203
BluetoothA2DPOutputDefault::set_output
void set_output(audio_tools::AudioOutput &output) override
Output AudioStream using AudioTools library.
Definition BluetoothA2DPOutput.h:247
BluetoothA2DPOutputDefault::set_output
void set_output(audio_tools::AudioStream &output) override
Output AudioStream using AudioTools library.
Definition BluetoothA2DPOutput.h:249
BluetoothA2DPOutputDefault::set_output
void set_output(Print &output) override
Output to Arduino Print.
Definition BluetoothA2DPOutput.h:254
BluetoothA2DPOutput
Abstract Output Class.
Definition BluetoothA2DPOutput.h:22
BluetoothA2DPOutput::set_output
virtual void set_output(audio_tools::AudioOutput &output)
Not implemented.
Definition BluetoothA2DPOutput.h:32
BluetoothA2DPOutput::set_output
virtual void set_output(audio_tools::AudioStream &output)
Not implemented.
Definition BluetoothA2DPOutput.h:34
BluetoothA2DPOutput::set_output
virtual void set_output(Print &output)
Not implemented.
Definition BluetoothA2DPOutput.h:39
BluetoothA2DPOutputLegacy
Legacy I2S Output Class.
Definition BluetoothA2DPOutput.h:157
BluetoothA2DPOutputPrint
Output Class using Print API:
Definition BluetoothA2DPOutput.h:126
BluetoothA2DPOutputPrint::set_output
void set_output(Print &output)
Output to Arduino Print.
Definition BluetoothA2DPOutput.h:143