Skip to content

Commit af283ec

Browse files
committed
I2S Read support for channels = 1
1 parent eeb758e commit af283ec

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/AudioI2S/I2SESP32.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,36 @@ class I2SBase {
8888

8989
size_t readBytes(void *dest, size_t size_bytes){
9090
size_t result = 0;
91-
if (i2s_read(i2s_num, dest, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
92-
LOGE(LOG_METHOD);
91+
if (cfg.channels==2){
92+
if (i2s_read(i2s_num, dest, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
93+
LOGE(LOG_METHOD);
94+
}
95+
} else if (cfg.channels==1){
96+
// I2S has always 2 channels. We support to reduce it to 1
97+
uint8_t temp[size_bytes*2];
98+
if (i2s_read(i2s_num, temp, size_bytes*2, &result, portMAX_DELAY)!=ESP_OK){
99+
LOGE(LOG_METHOD);
100+
}
101+
// convert to 1 channel
102+
switch(cfg.bits_per_sample){
103+
case 16: {
104+
ChannelReducer<int16_t> reducer16(1, 2);
105+
result = reducer16.convert((uint8_t*)dest,temp, result);
106+
} break;
107+
// case 24: {
108+
// ChannelReducer<int24_t> reducer24(1,2);
109+
// result = reducer24.convert((uint8_t*)dest,temp,result);
110+
// } break;
111+
case 32: {
112+
ChannelReducer<int32_t> reducer32(1, 2);
113+
result = reducer32.convert((uint8_t*)dest, temp, result);
114+
} break;
115+
default:
116+
LOGE("invalid bits_per_sample: %d", cfg.bits_per_sample);
117+
break;
118+
}
119+
} else {
120+
LOGE("Invalid channels: %d", cfg.channels);
93121
}
94122
return result;
95123
}

src/AudioTools/Converter.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class ChannelReducer : public BaseConverter<T> {
355355
public:
356356
ChannelReducer() = default;
357357

358-
ChannelReducer(int channelCountOfSource, int channelCountOfTarget=1){
358+
ChannelReducer(int channelCountOfTarget, int channelCountOfSource){
359359
from_channels = channelCountOfSource;
360360
to_channels = channelCountOfTarget;
361361
}
@@ -367,11 +367,15 @@ class ChannelReducer : public BaseConverter<T> {
367367
void setTargetChannels(int channelCountOfTarget) {
368368
to_channels = channelCountOfTarget;
369369
}
370-
370+
371371
size_t convert(uint8_t*src, size_t size) {
372+
return convert(src,src,size);
373+
}
374+
375+
size_t convert(uint8_t*target, uint8_t*src, size_t size) {
372376
int frame_count = size/(sizeof(T)*from_channels);
373377
size_t result_size=0;
374-
T* result = (T*)src;
378+
T* result = (T*)target;
375379
T* source = (T*)src;
376380
int reduceDiv = from_channels-to_channels+1;
377381

@@ -398,6 +402,7 @@ class ChannelReducer : public BaseConverter<T> {
398402
};
399403

400404

405+
401406
/**
402407
* @brief Combines multiple converters
403408
*

0 commit comments

Comments
 (0)