Arduino A2DP

BluetoothOutput.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 BluetoothOutput {
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);
28 virtual void set_output_active(bool active);
29
30 #if A2DP_I2S_AUDIOTOOLS
32 void set_output(AudioOutput &output) {}
34 void set_output(AudioStream &output) {}
35 #endif
36
37 #ifdef ARDUINO
39 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 BluetoothOutputAudioTools : public BluetoothOutput {
70 public:
71 BluetoothOutputAudioTools() = 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() { return p_print != nullptr; }
79
80 #if A2DP_I2S_AUDIOTOOLS
81
83 void set_output(AudioOutput &output) {
84 p_print = &output;
85 p_audio_print = &output;
86 }
87
89 void set_output(AudioStream &output) {
90 static AdapterAudioStreamToAudioOutput adapter(output);
91 p_print = &output;
92 p_audio_print = &adapter;
93 }
94 #endif
95
96 #ifdef ARDUINO
98 void set_output(Print &output) { p_print = &output; }
99 #endif
100
101 protected:
102 #if defined(ARDUINO) || defined(A2DP_I2S_AUDIOTOOLS)
103 Print *p_print = nullptr;
104 #endif
105
106 #if A2DP_I2S_AUDIOTOOLS
107 AudioOutput *p_audio_print = nullptr;
108 #endif
109 };
110
116 class BluetoothOutputLegacy : public BluetoothOutput {
117 public:
118 BluetoothOutputLegacy();
119 bool begin() override;
120 size_t write(const uint8_t *data, size_t len) override;
121 void end() override;
122 void set_sample_rate(int rate) override;
123 void set_output_active(bool active) override;
124
125 #if A2DP_LEGACY_I2S_SUPPORT
127 virtual void set_pin_config(i2s_pin_config_t pin_config) {
128 this->pin_config = pin_config;
129 }
131 virtual void set_i2s_port(i2s_port_t i2s_num) { i2s_port = i2s_num; }
132
134 virtual void set_i2s_config(i2s_config_t i2s_config) {
135 this->i2s_config = i2s_config;
136 }
137
140 virtual void set_bits_per_sample(int bps) {
141 i2s_config.bits_per_sample = (i2s_bits_per_sample_t)bps;
142 }
143
144 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 1)
145 virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin);
146 #endif
147
148 protected:
149 i2s_config_t i2s_config;
150 i2s_pin_config_t pin_config;
151 i2s_channel_t i2s_channels = I2S_CHANNEL_STEREO;
152 i2s_port_t i2s_port = I2S_NUM_0;
153 #endif
154 };
155
162 class BluetoothOutputDefault : public BluetoothOutput {
163 public:
164 BluetoothOutputDefault() = default;
165 bool begin() {
166 bool rc = true;
167 if (out_tools)
168 rc = out_tools.begin();
169 else
170 rc = out_legacy.begin();
171 return rc;
172 }
173 size_t write(const uint8_t *data, size_t len) {
174 size_t result = 0;
175 if (out_tools)
176 result = out_tools.write(data, len);
177 else
178 result = out_legacy.write(data, len);
179 return result;
180 }
181 void end() override {
182 if (out_tools)
183 out_tools.end();
184 else
185 out_legacy.end();
186 }
187 void set_sample_rate(int rate) override {
188 if (out_tools)
189 out_tools.set_sample_rate(rate);
190 else
191 out_legacy.set_sample_rate(rate);
192 }
193 void set_output_active(bool active) override {
194 if (out_tools)
195 out_tools.set_output_active(active);
196 else
197 out_legacy.set_output_active(active);
198 }
199
200 #if A2DP_I2S_AUDIOTOOLS
202 void set_output(AudioOutput &output) { out_tools.set_output(output); }
204 void set_output(AudioStream &output) { out_tools.set_output(output); }
205 #endif
206
207 #ifdef ARDUINO
209 void set_output(Print &output) { out_tools.set_output(output); }
210 #endif
211
212 #if A2DP_LEGACY_I2S_SUPPORT
214 virtual void set_pin_config(i2s_pin_config_t pin_config) {
215 out_legacy.set_pin_config(pin_config);
216 }
218 virtual void set_i2s_port(i2s_port_t i2s_num) {
219 out_legacy.set_i2s_port(i2s_num);
220 }
221
223 virtual void set_i2s_config(i2s_config_t i2s_config) {
224 out_legacy.set_i2s_config(i2s_config);
225 }
226
229 virtual void set_bits_per_sample(int bps) {
230 out_legacy.set_bits_per_sample(bps);
231 }
232
233 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 1)
234 virtual esp_err_t i2s_mclk_pin_select(const uint8_t pin) {
235 return out_legacy.i2s_mclk_pin_select(pin);
236 }
237 #endif
238 #endif // A2DP_LEGACY_I2S_SUPPORT
239 protected:
240 BluetoothOutputAudioTools out_tools;
241 BluetoothOutputLegacy out_legacy;
242 };
BluetoothA2DPCommon.h
BluetoothOutputAudioTools
Output Class using AudioTools library: https://github.com/pschatzmann/arduino-audio-tools.
Definition: BluetoothOutput.h:69
BluetoothOutputAudioTools::set_output
void set_output(AudioOutput &output)
Output AudioStream using AudioTools library.
Definition: BluetoothOutput.h:83
BluetoothOutputAudioTools::set_output
void set_output(AudioStream &output)
Output AudioStream using AudioTools library.
Definition: BluetoothOutput.h:89
BluetoothOutputAudioTools::set_output
void set_output(Print &output)
Output to Arduino Print.
Definition: BluetoothOutput.h:98
BluetoothOutputDefault
Default Output Class providing both the Legacy I2S and the AudioTools I2S functionality.
Definition: BluetoothOutput.h:162
BluetoothOutputDefault::set_output
void set_output(AudioStream &output)
Output AudioStream using AudioTools library.
Definition: BluetoothOutput.h:204
BluetoothOutputDefault::set_output
void set_output(AudioOutput &output)
Output AudioStream using AudioTools library.
Definition: BluetoothOutput.h:202
BluetoothOutputDefault::set_output
void set_output(Print &output)
Output to Arduino Print.
Definition: BluetoothOutput.h:209
BluetoothOutput
Abstract Output Class.
Definition: BluetoothOutput.h:22
BluetoothOutput::set_output
void set_output(AudioStream &output)
Not implemented.
Definition: BluetoothOutput.h:34
BluetoothOutput::set_output
void set_output(AudioOutput &output)
Not implemented.
Definition: BluetoothOutput.h:32
BluetoothOutput::set_output
void set_output(Print &output)
Not implemented.
Definition: BluetoothOutput.h:39
BluetoothOutputLegacy
Legacy I2S Output Class.
Definition: BluetoothOutput.h:116