Skip to content

Commit 32324ae

Browse files
committed
channel_format for ESP32 i2s
1 parent f9c58e9 commit 32324ae

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/AudioI2S/I2SConfigESP32V1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace audio_tools {
1010

11+
/// Select left or right channel when number of channels = 1
12+
enum class I2SChannelSelect { Stereo, Left, Right, Default };
13+
1114
/**
1215
* @brief Configuration for ESP32 i2s for IDF > 5.0
1316
* @ingroup platform
@@ -60,6 +63,8 @@ class I2SConfigESP32V1 : public AudioInfo {
6063
int buffer_count = I2S_BUFFER_COUNT;
6164
int buffer_size = I2S_BUFFER_SIZE;
6265
bool use_apll = I2S_USE_APLL;
66+
/// Select left or right channel when channels == 1
67+
I2SChannelSelect channel_format = I2SChannelSelect::Default;
6368

6469
void logInfo(const char* source="") {
6570
AudioInfo::logInfo(source);

src/AudioI2S/I2SESP32V1.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class I2SDriverESP32V1 {
3434
if (info.equals(cfg)) return true;
3535
if (info.equalsExSampleRate(cfg)) {
3636
cfg.sample_rate = info.sample_rate;
37-
LOGI("i2s_set_sample_rates: %d", (int) info.sample_rate);
37+
LOGI("i2s_set_sample_rates: %d", (int)info.sample_rate);
3838
return getDriver(cfg).changeSampleRate(cfg, rx_chan, tx_chan);
3939
}
4040
} else {
@@ -152,29 +152,39 @@ class I2SDriverESP32V1 {
152152
struct DriverI2S : public DriverCommon {
153153
i2s_std_slot_config_t getSlotConfig(I2SConfigESP32V1 &cfg) {
154154
TRACED();
155+
i2s_std_slot_config_t result;
155156
switch (cfg.i2s_format) {
156-
case I2S_RIGHT_JUSTIFIED_FORMAT:
157-
case I2S_LSB_FORMAT:
158-
case I2S_PHILIPS_FORMAT:
159-
case I2S_STD_FORMAT:
160-
return I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
161-
(i2s_data_bit_width_t)cfg.bits_per_sample,
162-
(i2s_slot_mode_t)cfg.channels);
163157
case I2S_LEFT_JUSTIFIED_FORMAT:
164158
case I2S_MSB_FORMAT:
165-
return I2S_STD_MSB_SLOT_DEFAULT_CONFIG(
159+
result = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(
166160
(i2s_data_bit_width_t)cfg.bits_per_sample,
167161
(i2s_slot_mode_t)cfg.channels);
168162
case I2S_PCM:
169-
return I2S_STD_PCM_SLOT_DEFAULT_CONFIG(
163+
result = I2S_STD_PCM_SLOT_DEFAULT_CONFIG(
164+
(i2s_data_bit_width_t)cfg.bits_per_sample,
165+
(i2s_slot_mode_t)cfg.channels);
166+
default:
167+
result = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
170168
(i2s_data_bit_width_t)cfg.bits_per_sample,
171169
(i2s_slot_mode_t)cfg.channels);
172170
}
173-
// use default config
174-
TRACEE();
175-
return I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
176-
(i2s_data_bit_width_t)cfg.bits_per_sample,
177-
(i2s_slot_mode_t)cfg.channels);
171+
172+
// Update slot_mask if only one channel
173+
if (cfg.channels == 1) {
174+
switch (cfg.channel_format) {
175+
case I2SChannelSelect::Left:
176+
result.slot_mask = I2S_STD_SLOT_LEFT;
177+
break;
178+
case I2SChannelSelect::Right:
179+
result.slot_mask = I2S_STD_SLOT_RIGHT;
180+
break;
181+
case I2SChannelSelect::Stereo:
182+
LOGW("Invalid channel_format: %d", cfg.channel_format);
183+
break;
184+
}
185+
}
186+
187+
return result;
178188
}
179189

180190
i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
@@ -445,7 +455,7 @@ class I2SDriverESP32V1 {
445455
return tdm;
446456
#endif
447457
default:
448-
break;
458+
break;
449459
}
450460
LOGE("Unsupported singal_type");
451461
return i2s;

0 commit comments

Comments
 (0)