Skip to content

Commit

Permalink
Dev (#535)
Browse files Browse the repository at this point in the history
* Output to AudioTools

* AudioKit example

* Update example to use new API

* doxygen

* Examples add header

* Warining if AudioTools is not installed

* Do not use legacy I2S when p_print is defined.

* BluetoothA2DPSinkQueued legacy only when p_print == nullptr

* Setup actual_bluetooth_a2dp_sink = this;

* doxygen

* Doxygen remove legacy API

* Remove is_i2s_output

* BluetoothA2DPSinkQueued add missing constructors

* add delay(1) on i2s_write_data

* doxygen
  • Loading branch information
pschatzmann committed Apr 6, 2024
1 parent d01e1bb commit 478aca9
Show file tree
Hide file tree
Showing 76 changed files with 2,509 additions and 48,631 deletions.
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = ESP_IDF_4 A2DP_I2S_SUPPORT
PREDEFINED = ESP_IDF_4 ARDUINO A2DP_I2S_AUDIOTOOLS

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
92 changes: 44 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ It also supports Audio/Video Remote Control Profile (AVRCP) together with A2DP.

The Hands-Free Profile (HFP), Headset Profile (HSP) and stand alone AVRCP without A2DP are __not__ supported!

## I2S API / Dependencies

Espressif is retiring the legacy I2S API: So with Arduino 3.0.0 [my old I2S integration](https://github.com/pschatzmann/ESP32-A2DP/wiki/Legacy-I2S-API) will not be available any more.

In order to support a unique output API which is version independent, it is recommended to install and use the [AudioTools](https://github.com/pschatzmann/arduino-audio-tools) library.

However you can output to any other class which inherits from Arduino Print: e.g. the Arduino ESP32 I2SClass.


## A2DP Sink (Music Receiver)

Expand All @@ -27,9 +35,11 @@ This can be used e.g. to build your own Bluetooth Speaker.
Here is the simplest example which just uses the proper default settings:

```
#include "AudioTools.h
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
I2SStream i2s;
BluetoothA2DPSink a2dp_sink(i2s);
void setup() {
a2dp_sink.start("MyMusic");
Expand All @@ -38,93 +48,79 @@ void setup() {
void loop() {
}
```
This creates a new Bluetooth device with the name “MyMusic” and the output will be sent to the following default I2S pins which need to be conected to an external DAC:
- bck_io_num = 26
- ws_io_num = 25
This creates a new Bluetooth device with the name “MyMusic” and the output will be sent to the following default I2S pins which need to be connected to an external DAC:

- bck_io_num = 14
- ws_io_num = 15
- data_out_num = 22

Please note that these default pins have changed compared to the legacy API!

### Defining Pins

You can define your own pins easily by calling the ```set_pin_config``` method in the setup before the ```start```.
You can define your own pins easily before the ```start```.

```
#include "AudioTools.h
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
I2SStream i2s;
BluetoothA2DPSink a2dp_sink(i2s);
void setup() {
i2s_pin_config_t my_pin_config = {
.mck_io_num = I2S_PIN_NO_CHANGE,
.bck_io_num = 26,
.ws_io_num = 25,
.data_out_num = 22,
.data_in_num = I2S_PIN_NO_CHANGE
};
a2dp_sink.set_pin_config(my_pin_config);
auto cfg = i2s.defaultConfig();
cfg.pin_bck = 14;
cfg.pin_ws = 15;
cfg.pin_data = 22;
i2s.begin(cfg);
a2dp_sink.start("MyMusic");
}
void loop() {
}
```

### Using your specific i2s_config
### Output Using the ESP32 I2S API

In some cases you might want to use your specific i2s_config settings. E.g. to request a different bits_per_sample (e.g. 32) or to use the use_apll or to optimize the dma buffer...
You can also use the Arduino ESP32 I2S API: You do not need to install any additional library for this.

```
#include "I2S.h"
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
BluetoothA2DPSink a2dp_sink(I2S);
void setup() {
I2S.setSckPin(14);
I2S.setFsPin(15);
I2S.setDataPin(22);
if (!I2S.begin(I2S_PHILIPS_MODE, 44100, 16)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
static i2s_config_t i2s_config = {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 44100, // updated automatically by A2DP
.bits_per_sample = (i2s_bits_per_sample_t)32,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t) (I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = true,
.tx_desc_auto_clear = true // avoiding noise in case of data unavailability
};
a2dp_sink.set_i2s_config(i2s_config);
a2dp_sink.start("MyMusic");
a2dp_sink.start("MyMusic");
}
void loop() {
}
```


### Output to the Internal DAC
You can also send the output directly to the internal DAC of the ESP32 by providing the corresponding i2s_config:

You can also send the output directly to the internal DAC of the ESP32 by using the AnalogAudioStream fro the AudioTools:

```
#include "AudioTools.h
#include "BluetoothA2DPSink.h"
BluetoothA2DPSink a2dp_sink;
AnalogAudioStream out;
BluetoothA2DPSink a2dp_sink(out);
void setup() {
static const i2s_config_t i2s_config = {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = 44100, // corrected by info from bluetooth
.bits_per_sample = (i2s_bits_per_sample_t) 16, /* the DAC module will only take the 8bits from MSB */
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_MSB,
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false
};
a2dp_sink.set_i2s_config(i2s_config);
a2dp_sink.start("MyMusic");
}
void loop() {
Expand Down
1 change: 0 additions & 1 deletion docs/html/_bluetooth_a2_d_p_common_8h.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<code>#include &quot;SoundData.h&quot;</code><br />
<code>#include &quot;A2DPVolumeControl.h&quot;</code><br />
<code>#include &quot;esp_task_wdt.h&quot;</code><br />
<code>#include &quot;driver/i2s.h&quot;</code><br />
<code>#include &quot;esp_log.h&quot;</code><br />
</div>
<p><a href="_bluetooth_a2_d_p_common_8h_source.html">Go to the source code of this file.</a></p>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/_bluetooth_a2_d_p_common_8h_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160;<span class="preprocessor">#include &quot;A2DPVolumeControl.h&quot;</span></div>
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160;<span class="preprocessor">#include &quot;esp_task_wdt.h&quot;</span></div>
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160; </div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160;<span class="preprocessor">#if A2DP_I2S_SUPPORT</span></div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160;<span class="preprocessor">#if A2DP_LEGACY_I2S_SUPPORT</span></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160;<span class="preprocessor"># include &quot;driver/i2s.h&quot;</span></div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; </div>
Expand Down
Loading

0 comments on commit 478aca9

Please sign in to comment.