33 virtual size_t convert(uint8_t *src,
size_t size) = 0;
43 size_t convert(uint8_t(*src),
size_t size)
override {
return size; };
55template <
typename T =
int16_t>
65 size_t convert(uint8_t *src,
size_t byte_count) {
67 int size = byte_count /
channels /
sizeof(T);
68 for (
size_t j = 0; j < size; j++) {
106template <
typename T =
int16_t>
117 size_t convert(uint8_t(*src),
size_t byte_count)
override {
118 size_t size = byte_count /
channels /
sizeof(T);
119 T *sample = (T *)src;
120 setup((T *)src, size);
124 for (
size_t j = 0; j < size; j++) {
125 for (
int ch = 0; ch <
channels; ch++) {
131 for (
size_t j = 0; j < size; j++) {
132 for (
int ch = 0; ch <
channels; ch++) {
165 if (size == 0)
return;
175 for (
int ch = 0; ch <
channels; ch++) {
181 for (
size_t j = 0; j < size; j++) {
182 for (
int ch = 0; ch <
channels; ch++) {
186 for (
int ch = 0; ch <
channels; ch++) {
255 size_t convert(uint8_t *src,
size_t size)
override {
283template <
typename T =
int16_t>
288 size_t convert(uint8_t *src,
size_t byte_count)
override {
290 int size = byte_count /
channels /
sizeof(T);
291 T *sample = (T *)src;
292 for (
size_t j = 0; j < size; j++) {
294 *sample = *(sample + 1);
295 *(sample + 1) = temp;
322template <
typename T =
int16_t>
327 this->channels = channels;
345 size_t convert(uint8_t *src,
size_t byte_count) {
347 int size = byte_count / channels /
sizeof(T);
348 setup((T *)src, size);
349 if (left_empty && !right_empty) {
350 T *sample = (T *)src;
351 for (
size_t j = 0; j < size; j++) {
352 *sample = *(sample + 1);
355 }
else if (!left_empty && right_empty) {
356 T *sample = (T *)src;
357 for (
size_t j = 0; j < size; j++) {
358 *(sample + 1) = *sample;
367 bool is_setup =
false;
368 bool left_empty =
true;
369 bool right_empty =
true;
372 void setup(T *src,
size_t size) {
374 for (
int j = 0; j < size; j++) {
381 for (
int j = 0; j < size - 1; j++) {
389 if (!right_empty || !left_empty) {
405template <
typename T =
int16_t>
410 size_t convert(uint8_t *src,
size_t byte_count)
override {
411 int size = byte_count /
channels /
sizeof(T);
412 T *sample = (T *)src;
413 for (
int i = 0; i < size; i++) {
414 for (
int j = 0; j <
channels; j++) {
415 *sample = *sample + 0x8000;
433template <
typename T =
int16_t>
453 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
457 size_t result_size = 0;
458 T *result = (T *)target;
459 T *source = (T *)src;
462 for (
int i = 0; i < frame_count; i++) {
465 *result++ = *source++;
466 result_size +=
sizeof(T);
469 T total = (int16_t)0;
471 total += *source++ / reduceDiv;
474 result_size +=
sizeof(T);
496 bits = bitsPerSample;
501 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
505 return cr8.
convert(target, src, size);
509 return cr16.
convert(target, src, size);
513 return cr24.
convert(target, src, size);
517 return cr32.
convert(target, src, size);
533template <
typename T =
int16_t>
558 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
560 if (size % (
sizeof(T) *
channels) > 0) {
561 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
566 int frame_count = size / (
sizeof(T) *
channels);
567 T *p_target = (T *)target;
568 T *p_source = (T *)src;
569 size_t result_size = 0;
571 for (
int i = 0; i < frame_count; i++) {
575 for (
int ch = 0; ch <
channels; ch++) {
576 *p_target++ = p_source[i *
channels + ch];
577 result_size +=
sizeof(T);
582 LOGD(
"decimate %d: %d -> %d bytes",
factor, (
int)size, (
int)result_size);
591 LOGE(
"Number of channels must be > 0");
595 LOGE(
"Decimation factor must be > 0");
638 if (
state !=
nullptr) {
645 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
647 if (
state ==
nullptr) {
662 LOGE(
"Number of bits %d not supported.",
bits);
674 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
678 template <
typename T>
682 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
693 LOGE(
"Number of channels must be > 0");
697 LOGE(
"Decimation factor must be > 0");
725template <
typename T =
int16_t>
750template <
typename T =
int16_t>
777 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
788 if (size % (
sizeof(T) *
channels) > 0) {
789 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
794 T *p_target = (T *)target;
795 T *p_source = (T *)src;
796 size_t result_size = 0;
797 int sample_count = size / (
sizeof(T) *
channels);
799 for (
int sample = 0; sample < sample_count; sample++) {
800 for (
int ch = 0; ch <
channels; ch++) {
806 for (
int ch = 0; ch <
channels; ch++) {
807 p_target[result_size /
sizeof(T)] =
810 result_size +=
sizeof(T);
816 LOGD(
"bin %d: processed %d samples, %d remaining, %d > %d bytes",
binSize,
827 LOGE(
"Number of channels must be > 0");
831 LOGE(
"Bin size must be > 0");
847 for (
int ch = 0; ch <
channels; ch++) {
894 if (
state !=
nullptr) {
901 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
903 if (
state ==
nullptr) {
918 LOGE(
"Number of bits %d not supported.",
bits);
928 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
932 template <
typename T>
937 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
948 LOGE(
"Number of channels must be > 0");
952 LOGE(
"Bin size must be > 0");
984template <
typename T =
int16_t>
989 size_t convert(uint8_t *src,
size_t size)
override {
990 return convert(src, src, size);
993 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
994 LOGD(
"channel subtract %d samples, %d bytes", (
int)(size /
sizeof(T)),
998 if (size % (
sizeof(T) * 2) > 0) {
999 LOGE(
"Buffer size is not even");
1006 T *p_result = (T *)target;
1007 T *p_source = (T *)src;
1009 for (
int i = 0; i < sample_count; i++) {
1011 auto tmp = *p_source++;
1016 return sizeof(T) * sample_count;
1027 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1031 return cd8.
convert(target, src, size);
1035 return cd16.
convert(target, src, size);
1039 return cd24.
convert(target, src, size);
1043 return cd32.
convert(target, src, size);
1046 LOGE(
"Number of bits %d not supported.",
bits);
1064template <
typename T =
int16_t,
typename SumT =
float>
1070 T *srcT = (T *)data;
1071 T *targetT = (T *)data;
1072 int samples = size /
sizeof(T);
1074 for (
int j = 0; j < samples; j +=
channels) {
1076 for (
int ch = 0; ch <
channels; ch++) {
1077 sum += srcT[j + ch];
1080 for (
int ch = 0; ch <
channels; ch++) {
1081 targetT[j + ch] = avg;
1104template <
typename T =
int16_t,
typename AvgT =
float>
1109 size_t convert(uint8_t *src,
size_t size)
override {
1110 return convert(src, src, size);
1113 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1114 if (size % (
sizeof(T) * 2) > 0) {
1115 LOGE(
"Buffer size is not even");
1122 T *p_result = (T *)target;
1123 T *p_source = (T *)src;
1125 for (
int i = 0; i < sample_count; i++) {
1128 AvgT tmp = *p_source++;
1130 *p_result++ = tmp / 2;
1133 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
1135 return sizeof(T) * sample_count;
1160 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1162#ifdef PREFER_FIXEDPOINT
1165 return ca8.
convert(target, src, size);
1169 return ca16.
convert(target, src, size);
1173 return ca24.
convert(target, src, size);
1178 return ca8.
convert(target, src, size);
1182 return ca16.
convert(target, src, size);
1186 return ca24.
convert(target, src, size);
1191 return ca32.
convert(target, src, size);
1194 LOGE(
"Number of bits %d not supported.",
bits);
1217template <
typename T =
int16_t>
1232 LOGE(
"Number of channels needs to be even");
1249 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1255 if (size % (
sizeof(T) *
channels) > 0) {
1256 LOGE(
"Buffer size needs to be multiple of channels");
1260 T *p_target = (T *)target;
1261 T *p_source = (T *)src;
1262 size_t result_size = 0;
1263 int sample_count = size / (
sizeof(T) *
channels);
1265 for (
int sample = 0; sample < sample_count; sample++) {
1266 for (
int ch = 0; ch <
channels; ch++) {
1272 for (
int ch = 0; ch <
channels; ch += 2) {
1274 p_target[result_size /
sizeof(T)] =
1276 :
static_cast<T
>(diff);
1277 result_size +=
sizeof(T);
1284 "bin & channel subtract %d: processed %d samples, %d remaining, %d > "
1297 LOGE(
"Number of channels must be > 0");
1301 LOGE(
"Bin size must be > 0");
1317 for (
int ch = 0; ch <
channels; ch++) {
1353 LOGE(
"Number of channels needs to be even");
1372 if (
state !=
nullptr) {
1379 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1381 if (
state ==
nullptr) {
1396 LOGE(
"Number of bits %d not supported.",
bits);
1406 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
1410 template <
typename T>
1415 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
1426 LOGE(
"Number of channels must be > 0");
1430 LOGE(
"Bin size must be > 0");
1453template <
typename T =
int16_t>
1471 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1474 size_t result_size = 0;
1475 T *result = (T *)target;
1476 T *source = (T *)src;
1477 T value = (int16_t)0;
1478 for (
int i = 0; i < frame_count; i++) {
1483 result_size +=
sizeof(T);
1488 result_size +=
sizeof(T);
1509template <
typename T =
int16_t>
1527 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1529 memcpy(target, src, size);
1561template <
typename T =
int16_t>
1584 for (
int i = 0; i < converters.size(); i++) {
1585 converters[i]->convert(src, size);
1604 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1605 bool result_bool =
false;
1606 int len = inBits / 8 * n;
1608 uint8_t buffer[len];
1611 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1617 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1619 bool result_bool =
false;
1622 int8_t *buffer = (int8_t *)bufferIn;
1623 for (
int j = 0; j < n; j++) {
1624 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1629 int16_t *buffer = (int16_t *)bufferIn;
1630 for (
int j = 0; j < n; j++) {
1631 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1636 int32_t *buffer = (int32_t *)bufferIn;
1637 for (
int j = 0; j < n; j++) {
1638 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1650 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1651 int32_t result =
static_cast<float>(value) /
1667template <
typename T =
int16_t>
1672 size_t convert(uint8_t *src,
size_t size)
override {
1674 for (
size_t j = 0; j < size; j++) {
1675 data[j] =
p_filter->process(data[j]);
1690template <
typename T,
typename FT>
1697 for (
int j = 0; j <
channels; j++) {
1707 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1721 int count = size /
channels /
sizeof(T);
1722 T *sample = (T *)src;
1723 for (
size_t j = 0; j < count; j++) {
1724 for (
int channel = 0; channel <
channels; channel++) {
1725 if (
filters[channel] !=
nullptr) {
1726 *sample =
filters[channel]->process(*sample);
1739 if (newChannels ==
channels)
return;
1744 for (
int j = oldChannels; j < newChannels; j++) {
1763template <
typename T =
int16_t>
1767 set(
n, aplidudeLimit);
1773 virtual size_t convert(uint8_t *data,
size_t size)
override {
1778 size_t sample_count = size /
sizeof(T);
1779 size_t write_count = 0;
1780 T *audio = (T *)data;
1783 T *p_buffer = (T *)data;
1784 for (
int j = 0; j < sample_count; j++) {
1788 *p_buffer++ = audio[j];
1793 size_t write_size = write_count *
sizeof(T);
1794 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1810 void set(
int n = 5,
int aplidudeLimit = 2) {
1811 LOGI(
"begin(n=%d, aplidudeLimit=%d",
n, aplidudeLimit);
1813 this->amplidude_limit = aplidudeLimit;
1814 this->priorLastAudioPos =
n + 1;
1815 this->active =
n > 0;
1820 for (
int j = 0; j <
n; j++) {
1841template <
typename T =
int16_t>
1849 virtual size_t convert(uint8_t *src,
size_t size) {
1850 for (
int ch = 0; ch <
channels; ch++) {
1866 T first = values[channel];
1867 for (
int j = 0; j < sampleCount; j +=
channels) {
1869 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1880 int lastPos = sampleCount -
channels + channel;
1881 T last = values[lastPos];
1882 for (
int j = lastPos; j >= 0; j -=
channels) {
1884 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1900template <
typename T =
int16_t>
1915 virtual size_t convert(uint8_t *src,
size_t size) {
1916 int sample_count = size /
sizeof(T);
1933 for (
int frame = 0; frame < frameCount; ++frame) {
1934 if (factor >= 0.8) {
1938 for (
int ch = 0; ch <
channels; ++ch) {
1939 values[pos + ch] = factor * values[pos + ch];
1948 for (
int frame = frameCount - 1; frame >= 0; --frame) {
1949 if (factor >= 0.8) {
1953 for (
int ch = 0; ch <
channels; ++ch) {
1954 values[pos + ch] = factor * values[pos + ch];
1968template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1975 size_t samples = (size / Cn) /
sizeof(T);
1976 for (
size_t s = 0; s < samples; s++) {
1977 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1979 for (
size_t c = 0; c < Cn; c++) {
1981 chan[s * Cn + c] = chan[s * Cn + Cx];
1985 if (_max_val < chan[s * Cn]) {
1986 _max_val = chan[s * Cn];
1991 if (now - _prev_ms > 1000) {
1993 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1997 return samples * Cn *
sizeof(T);
2011template <
typename T =
int16_t>
2014 size_t convert(uint8_t *src,
size_t size)
override {
2016 int samples = size /
sizeof(T);
2017 for (
int j = 0; j < samples; j++) {
2029template <
typename T =
int16_t>
2047 int samples = size /
sizeof(T);
2049 for (
int j = 0; j < samples; j++) {
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
virtual int available()
Definition Arduino.h:139
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:310
@ LeftIsEmpty
Definition BaseConverter.h:310
@ RightIsEmpty
Definition BaseConverter.h:310
@ Auto
Definition BaseConverter.h:310