Skip to content

Commit a50897c

Browse files
authored
Merge pull request #943 from schreibfaul1/V3.1
V3.1
2 parents 979823b + 9638f01 commit a50897c

File tree

4 files changed

+60
-184
lines changed

4 files changed

+60
-184
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,52 @@ void audio_eof_speech(const char *info){
110110

111111
````
112112
113+
````c++
114+
/* ESP32-S3 EXAMPLE */
115+
116+
#include "Arduino.h"
117+
#include "Audio.h"
118+
#include "WiFi.h"
119+
#include "SD_MMC.h"
120+
121+
#define I2S_DOUT 9
122+
#define I2S_BCLK 3
123+
#define I2S_LRC 1
124+
#define SD_MMC_D0 11
125+
#define SD_MMC_CLK 13
126+
#define SD_MMC_CMD 14
127+
128+
Audio audio;
129+
130+
String ssid = "*****";
131+
String password = "*****";
132+
133+
void setup() {
134+
Serial.begin(115200);
135+
// WiFi.begin(ssid.c_str(), password.c_str());
136+
// while (WiFi.status() != WL_CONNECTED) delay(1500);
137+
138+
pinMode(SD_MMC_D0, INPUT_PULLUP);
139+
SD_MMC.setPins(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
140+
SD_MMC.begin("/sdcard", true);
141+
142+
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
143+
audio.setVolume(12); // default 0...21
144+
// audio.connecttohost("http://stream.antennethueringen.de/live/aac-64/stream.antennethueringen.de/"); // aac
145+
audio.connecttoFS(SD_MMC, "/test.wav");
146+
}
147+
148+
void loop() {
149+
audio.loop();
150+
vTaskDelay(1);
151+
}
152+
153+
// optional
154+
void audio_info(const char *info){
155+
Serial.print("info "); Serial.println(info);
156+
}
157+
````
158+
113159
<br>
114160

115161
|Codec |ESP32 |ESP32 PSRAM |ESP32-S3 PSRAM | |
@@ -125,10 +171,10 @@ void audio_eof_speech(const char *info){
125171

126172
<br>
127173

128-
Breadboard
129-
![Breadboard](https://github.com/schreibfaul1/ESP32-audioI2S/blob/master/additional_info/Breadboard.jpg)
174+
***
130175
Wiring
131-
![Wiring](https://github.com/schreibfaul1/ESP32-audioI2S/blob/master/additional_info/ESP32_I2S_PCM5102A.JPG)
176+
![Wiring ESP32-S3](https://github.com/user-attachments/assets/15dd1766-0fc1-4079-b378-bc566583e80d)
177+
***
132178
Impulse diagram
133179
![Impulse diagram](https://github.com/schreibfaul1/ESP32-audioI2S/blob/master/additional_info/Impulsdiagramm.jpg)
134180
***

examples/Internal DAC/internalDAC.ino

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Audio.cpp

Lines changed: 7 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 28.2018
55
*
6-
* Version 3.0.13zi
7-
* Updated on: Jan 06.2025
6+
* Version 3.1.0
7+
* Updated on: Jan 07.2025
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -158,7 +158,7 @@ uint32_t AudioBuffer::getWritePos() { return m_writePtr - m_buffer; }
158158
uint32_t AudioBuffer::getReadPos() { return m_readPtr - m_buffer; }
159159
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
160160
// clang-format off
161-
Audio::Audio(bool internalDAC /* = false */, uint8_t channelEnabled /* = I2S_SLOT_MODE_STEREO */, uint8_t i2sPort) {
161+
Audio::Audio(uint8_t i2sPort) {
162162

163163
mutex_playAudioData = xSemaphoreCreateMutex();
164164
mutex_audioTask = xSemaphoreCreateMutex();
@@ -177,12 +177,9 @@ Audio::Audio(bool internalDAC /* = false */, uint8_t channelEnabled /* = I2S_SLO
177177
#define AUDIO_INFO(...) { snprintf(m_ibuff, m_ibuffSize, __VA_ARGS__); if(audio_info) audio_info(m_ibuff); }
178178

179179
clientsecure.setInsecure();
180-
m_f_channelEnabled = channelEnabled;
181-
m_f_internalDAC = internalDAC;
182180
m_i2s_num = i2sPort; // i2s port number
183181

184182
// -------- I2S configuration -------------------------------------------------------------------------------------------
185-
#if ESP_IDF_VERSION_MAJOR == 5
186183
m_i2s_chan_cfg.id = (i2s_port_t)m_i2s_num; // I2S_NUM_AUTO, I2S_NUM_0, I2S_NUM_1
187184
m_i2s_chan_cfg.role = I2S_ROLE_MASTER; // I2S controller master role, bclk and lrc signal will be set to output
188185
m_i2s_chan_cfg.dma_desc_num = 32; // number of DMA buffer
@@ -206,45 +203,6 @@ Audio::Audio(bool internalDAC /* = false */, uint8_t channelEnabled /* = I2S_SLO
206203
I2Sstart(m_i2s_num);
207204
m_sampleRate = 44100;
208205

209-
if (internalDAC) {
210-
#ifdef CONFIG_IDF_TARGET_ESP32 // ESP32S3 has no DAC
211-
printf("internal DAC is not supported");
212-
// no support in V5 ???
213-
#endif
214-
}
215-
#else
216-
m_i2s_config.sample_rate = 44100;
217-
m_i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
218-
m_i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
219-
m_i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; // interrupt priority
220-
m_i2s_config.dma_buf_count = 16;
221-
m_i2s_config.dma_buf_len = 512;
222-
m_i2s_config.use_apll = APLL_DISABLE;
223-
m_i2s_config.tx_desc_auto_clear = true;
224-
m_i2s_config.fixed_mclk = true;
225-
m_i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_128;
226-
227-
if (internalDAC) {
228-
#ifdef CONFIG_IDF_TARGET_ESP32 // ESP32S3 has no DAC
229-
printf("internal DAC");
230-
m_i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN );
231-
m_i2s_config.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_MSB); // vers >= 2.0.5
232-
i2s_driver_install((i2s_port_t)m_i2s_num, &m_i2s_config, 0, NULL);
233-
i2s_set_dac_mode((i2s_dac_mode_t)m_f_channelEnabled);
234-
if(m_f_channelEnabled != I2S_DAC_CHANNEL_BOTH_EN) {
235-
m_f_forceMono = true;
236-
}
237-
#endif
238-
}
239-
else {
240-
m_i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
241-
m_i2s_config.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S); // Arduino vers. > 2.0.0
242-
i2s_driver_install((i2s_port_t)m_i2s_num, &m_i2s_config, 0, NULL);
243-
m_f_forceMono = false;
244-
}
245-
i2s_zero_dma_buffer((i2s_port_t) m_i2s_num);
246-
247-
#endif // ESP_IDF_VERSION_MAJOR == 5
248206
for(int i = 0; i < 3; i++) {
249207
m_filter[i].a0 = 1;
250208
m_filter[i].a1 = 0;
@@ -261,12 +219,8 @@ Audio::~Audio() {
261219
// InBuff.~AudioBuffer(); #215 the AudioBuffer is automatically destroyed by the destructor
262220
setDefaults();
263221

264-
#if ESP_IDF_VERSION_MAJOR == 5
265222
i2s_channel_disable(m_i2s_tx_handle);
266223
i2s_del_channel(m_i2s_tx_handle);
267-
#else
268-
i2s_driver_uninstall((i2s_port_t)m_i2s_num); // #215 free I2S buffer
269-
#endif
270224

271225
x_ps_free(&m_playlistBuff);
272226
x_ps_free(&m_chbuf);
@@ -300,21 +254,11 @@ void Audio::initInBuff() {
300254

301255
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
302256
esp_err_t Audio::I2Sstart(uint8_t i2s_num) {
303-
#if ESP_IDF_VERSION_MAJOR == 5
304257
return i2s_channel_enable(m_i2s_tx_handle);
305-
#else
306-
// It is not necessary to call this function after i2s_driver_install() (it is started automatically),
307-
// however it is necessary to call it after i2s_stop()
308-
return i2s_start((i2s_port_t)i2s_num);
309-
#endif
310258
}
311259

312260
esp_err_t Audio::I2Sstop(uint8_t i2s_num) {
313-
#if ESP_IDF_VERSION_MAJOR == 5
314261
return i2s_channel_disable(m_i2s_tx_handle);
315-
#else
316-
return i2s_stop((i2s_port_t)i2s_num);
317-
#endif
318262
}
319263
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
320264
void Audio::setDefaults() {
@@ -2343,12 +2287,6 @@ void Audio::playChunk() {
23432287
(*sample)[LEFTCHANNEL] = (int16_t)xy;
23442288
}
23452289
Gain(*sample);
2346-
2347-
if(m_f_internalDAC) {
2348-
s2 = *sample;
2349-
s2[LEFTCHANNEL] += 0x8000;
2350-
s2[RIGHTCHANNEL] += 0x8000;
2351-
}
23522290
i += 2;
23532291
validSamples -= 1;
23542292
}
@@ -2367,13 +2305,7 @@ void Audio::playChunk() {
23672305

23682306
validSamples = m_validSamples;
23692307

2370-
2371-
#if(ESP_IDF_VERSION_MAJOR == 5)
23722308
err = i2s_channel_write(m_i2s_tx_handle, (int16_t*)m_outBuff + count, validSamples * sampleSize, &i2s_bytesConsumed, 10);
2373-
#else
2374-
err = i2s_write((i2s_port_t)m_i2s_num, (int16_t*)m_outBuff + count, validSamples * sampleSize, &i2s_bytesConsumed, 10);
2375-
#endif
2376-
23772309
if( ! (err == ESP_OK || err == ESP_ERR_TIMEOUT)) goto exit;
23782310
m_validSamples -= i2s_bytesConsumed / sampleSize;
23792311
count += i2s_bytesConsumed / 2;
@@ -4774,21 +4706,10 @@ bool Audio::setPinout(uint8_t BCLK, uint8_t LRC, uint8_t DOUT, int8_t MCLK) {
47744706
}
47754707
esp_err_t result = ESP_OK;
47764708

4777-
if(m_f_internalDAC) {
4778-
#if(ESP_IDF_VERSION_MAJOR != 5)
4779-
i2s_set_pin((i2s_port_t)m_i2s_num, NULL);
4780-
#endif
4781-
return true;
4782-
}
4783-
4784-
#if(ESP_ARDUINO_VERSION_MAJOR < 2)
4785-
log_e("Arduino Version too old!");
4786-
#endif
4787-
#if(ESP_ARDUINO_VERSION_MAJOR == 2 && ESP_ARDUINO_VERSION_PATCH < 8)
4788-
log_e("Arduino Version must be 2.0.8 or higher!");
4709+
#if(ESP_ARDUINO_VERSION_MAJOR < 3)
4710+
log_e("Arduino Version must be 3.0.0 or higher!");
47894711
#endif
47904712

4791-
#if(ESP_IDF_VERSION_MAJOR == 5)
47924713
i2s_std_gpio_config_t gpio_cfg = {};
47934714
gpio_cfg.bclk = (gpio_num_t)BCLK;
47944715
gpio_cfg.din = (gpio_num_t)I2S_GPIO_UNUSED;
@@ -4798,14 +4719,7 @@ bool Audio::setPinout(uint8_t BCLK, uint8_t LRC, uint8_t DOUT, int8_t MCLK) {
47984719
I2Sstop(m_i2s_num);
47994720
result = i2s_channel_reconfig_std_gpio(m_i2s_tx_handle, &gpio_cfg);
48004721
I2Sstart(m_i2s_num);
4801-
#else
4802-
m_pin_config.bck_io_num = BCLK;
4803-
m_pin_config.ws_io_num = LRC; // wclk = lrc
4804-
m_pin_config.data_out_num = DOUT;
4805-
m_pin_config.data_in_num = I2S_GPIO_UNUSED;
4806-
m_pin_config.mck_io_num = MCLK;
4807-
result = i2s_set_pin((i2s_port_t)m_i2s_num, &m_pin_config);
4808-
#endif
4722+
48094723
return (result == ESP_OK);
48104724
}
48114725
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -4912,24 +4826,6 @@ bool Audio::setFilePos(uint32_t pos) {
49124826
return false;
49134827
}
49144828
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4915-
bool Audio::audioFileSeek(const float speed) {
4916-
// 0.5 is half speed
4917-
// 1.0 is normal speed
4918-
// 1.5 is one and half speed
4919-
// if((speed > 1.5f) || (speed < 0.25f)) return false;
4920-
4921-
// uint32_t srate = getSampleRate() * speed;
4922-
// #if ESP_IDF_VERSION_MAJOR == 5
4923-
// I2Sstop(m_i2s_num);
4924-
// m_i2s_std_cfg.clk_cfg.sample_rate_hz = srate;
4925-
// i2s_channel_reconfig_std_clock(m_i2s_tx_handle, &m_i2s_std_cfg.clk_cfg);
4926-
// I2Sstart(m_i2s_num);
4927-
// #else
4928-
// i2s_set_sample_rates((i2s_port_t)m_i2s_num, srate);
4929-
// #endif
4930-
return true;
4931-
}
4932-
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49334829
bool Audio::setSampleRate(uint32_t sampRate) {
49344830
if(!sampRate) sampRate = 44100; // fuse, if there is no value -> set default #209
49354831
m_sampleRate = sampRate;
@@ -4958,7 +4854,6 @@ uint8_t Audio::getChannels() {
49584854
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49594855
void Audio::reconfigI2S(){
49604856

4961-
#if ESP_IDF_VERSION_MAJOR == 5
49624857
I2Sstop(0);
49634858

49644859
if(getBitsPerSample() == 8 && getChannels() == 2) m_i2s_std_cfg.clk_cfg.sample_rate_hz = getSampleRate() * 2;
@@ -4973,10 +4868,7 @@ void Audio::reconfigI2S(){
49734868
i2s_channel_reconfig_std_slot(m_i2s_tx_handle, &m_i2s_std_cfg.slot_cfg);
49744869

49754870
I2Sstart(m_i2s_num);
4976-
#else
4977-
m_i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
4978-
i2s_set_clk((i2s_port_t)m_i2s_num, m_sampleRate, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
4979-
#endif
4871+
49804872
memset(m_filterBuff, 0, sizeof(m_filterBuff)); // Clear FilterBuffer
49814873
IIR_calculateCoefficients(m_gain0, m_gain1, m_gain2); // must be recalculated after each samplerate change
49824874
return;
@@ -4999,18 +4891,6 @@ void Audio::setI2SCommFMT_LSB(bool commFMT) {
49994891

50004892
m_f_commFMT = commFMT;
50014893

5002-
#if ESP_IDF_VERSION_MAJOR < 5
5003-
if(commFMT) {
5004-
AUDIO_INFO("commFMT = LSBJ (Least Significant Bit Justified)");
5005-
m_i2s_config.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_MSB);
5006-
}
5007-
else {
5008-
AUDIO_INFO("commFMT = Philips");
5009-
m_i2s_config.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S);
5010-
}
5011-
i2s_driver_uninstall((i2s_port_t)m_i2s_num);
5012-
i2s_driver_install((i2s_port_t)m_i2s_num, &m_i2s_config, 0, NULL);
5013-
#else
50144894
i2s_channel_disable(m_i2s_tx_handle);
50154895
if(commFMT) {
50164896
AUDIO_INFO("commFMT = LSBJ (Least Significant Bit Justified)");
@@ -5022,7 +4902,6 @@ void Audio::setI2SCommFMT_LSB(bool commFMT) {
50224902
}
50234903
i2s_channel_reconfig_std_slot(m_i2s_tx_handle, &m_i2s_std_cfg.slot_cfg);
50244904
i2s_channel_enable(m_i2s_tx_handle);
5025-
#endif
50264905
}
50274906
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
50284907
void Audio::computeVUlevel(int16_t sample[2]) {
@@ -5134,9 +5013,6 @@ void Audio::setBalance(int8_t bal) { // bal -16...16
51345013
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
51355014
void Audio::setVolume(uint8_t vol, uint8_t curve) { // curve 0: default, curve 1: flat at the beginning
51365015

5137-
uint16_t v = ESP_ARDUINO_VERSION_MAJOR * 100 + ESP_ARDUINO_VERSION_MINOR * 10 + ESP_ARDUINO_VERSION_PATCH;
5138-
if(v < 207) AUDIO_INFO("Do not use this ancient Adruino version V%d.%d.%d", ESP_ARDUINO_VERSION_MAJOR, ESP_ARDUINO_VERSION_MINOR, ESP_ARDUINO_VERSION_PATCH);
5139-
51405016
if(vol > m_vol_steps) m_vol = m_vol_steps;
51415017
else m_vol = vol;
51425018

0 commit comments

Comments
 (0)