35 virtual size_t convert(uint8_t *src,
size_t size) = 0;
45 size_t convert(uint8_t(*src),
size_t size)
override {
return size; };
57template <
typename T =
int16_t>
67 size_t convert(uint8_t *src,
size_t byte_count) {
69 int size = byte_count /
channels /
sizeof(T);
76 for (
size_t j = 0; j < size; j++) {
78 int in = (int)(*sample) + off;
87 }
else if (scaled < -mv) {
90 *sample = (T)(
int)scaled;
131template <
typename T =
int16_t>
142 size_t convert(uint8_t(*src),
size_t byte_count)
override {
143 size_t size = byte_count /
channels /
sizeof(T);
144 T *sample = (T *)src;
145 setup((T *)src, size);
149 for (
size_t j = 0; j < size; j++) {
150 for (
int ch = 0; ch <
channels; ch++) {
151 sample[(j *
channels) + ch] = (T)(int)(
156 for (
size_t j = 0; j < size; j++) {
157 for (
int ch = 0; ch <
channels; ch++) {
196 if (size == 0)
return;
207 for (
int ch = 0; ch <
channels; ch++) {
215 for (
int ch = 0; ch <
channels; ch++) {
221 for (
size_t j = 0; j < size; j++) {
222 for (
int ch = 0; ch <
channels; ch++) {
226 for (
int ch = 0; ch <
channels; ch++) {
295 size_t convert(uint8_t *src,
size_t size)
override {
323template <
typename T =
int16_t>
328 size_t convert(uint8_t *src,
size_t byte_count)
override {
330 int size = byte_count /
channels /
sizeof(T);
331 T *sample = (T *)src;
332 for (
size_t j = 0; j < size; j++) {
334 *sample = *(sample + 1);
335 *(sample + 1) = temp;
362template <
typename T =
int16_t>
367 this->channels = channels;
385 size_t convert(uint8_t *src,
size_t byte_count) {
387 int size = byte_count / channels /
sizeof(T);
388 setup((T *)src, size);
389 if (left_empty && !right_empty) {
390 T *sample = (T *)src;
391 for (
size_t j = 0; j < size; j++) {
392 *sample = *(sample + 1);
395 }
else if (!left_empty && right_empty) {
396 T *sample = (T *)src;
397 for (
size_t j = 0; j < size; j++) {
398 *(sample + 1) = *sample;
407 bool is_setup =
false;
408 bool left_empty =
true;
409 bool right_empty =
true;
412 void setup(T *src,
size_t size) {
414 for (
int j = 0; j < size; j++) {
421 for (
int j = 0; j < size - 1; j++) {
429 if (!right_empty || !left_empty) {
445template <
typename T =
int16_t>
450 size_t convert(uint8_t *src,
size_t byte_count)
override {
451 int size = byte_count /
channels /
sizeof(T);
452 T *sample = (T *)src;
453 for (
int i = 0; i < size; i++) {
454 for (
int j = 0; j <
channels; j++) {
455 *sample = *sample + 0x8000;
473template <
typename T =
int16_t>
493 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
497 size_t result_size = 0;
498 T *result = (T *)target;
499 T *source = (T *)src;
502 for (
int i = 0; i < frame_count; i++) {
505 *result++ = *source++;
506 result_size +=
sizeof(T);
509 T total = (int16_t)0;
511 total += *source++ / reduceDiv;
514 result_size +=
sizeof(T);
536 bits = bitsPerSample;
541 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
545 return cr8.
convert(target, src, size);
549 return cr16.
convert(target, src, size);
553 return cr24.
convert(target, src, size);
557 return cr32.
convert(target, src, size);
573template <
typename T =
int16_t>
598 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
600 if (size % (
sizeof(T) *
channels) > 0) {
601 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
606 int frame_count = size / (
sizeof(T) *
channels);
607 T *p_target = (T *)target;
608 T *p_source = (T *)src;
609 size_t result_size = 0;
611 for (
int i = 0; i < frame_count; i++) {
615 for (
int ch = 0; ch <
channels; ch++) {
616 *p_target++ = p_source[i *
channels + ch];
617 result_size +=
sizeof(T);
622 LOGD(
"decimate %d: %d -> %d bytes",
factor, (
int)size, (
int)result_size);
631 LOGE(
"Number of channels must be > 0");
635 LOGE(
"Decimation factor must be > 0");
678 if (
state !=
nullptr) {
685 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
687 if (
state ==
nullptr) {
702 LOGE(
"Number of bits %d not supported.",
bits);
714 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
718 template <
typename T>
722 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
733 LOGE(
"Number of channels must be > 0");
737 LOGE(
"Decimation factor must be > 0");
765template <
typename T =
int16_t>
790template <
typename T =
int16_t>
817 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
828 if (size % (
sizeof(T) *
channels) > 0) {
829 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
834 T *p_target = (T *)target;
835 T *p_source = (T *)src;
836 size_t result_size = 0;
837 int sample_count = size / (
sizeof(T) *
channels);
839 for (
int sample = 0; sample < sample_count; sample++) {
840 for (
int ch = 0; ch <
channels; ch++) {
846 for (
int ch = 0; ch <
channels; ch++) {
847 p_target[result_size /
sizeof(T)] =
850 result_size +=
sizeof(T);
856 LOGD(
"bin %d: processed %d samples, %d remaining, %d > %d bytes",
binSize,
867 LOGE(
"Number of channels must be > 0");
871 LOGE(
"Bin size must be > 0");
887 for (
int ch = 0; ch <
channels; ch++) {
934 if (
state !=
nullptr) {
941 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
943 if (
state ==
nullptr) {
958 LOGE(
"Number of bits %d not supported.",
bits);
968 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
972 template <
typename T>
977 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
988 LOGE(
"Number of channels must be > 0");
992 LOGE(
"Bin size must be > 0");
1024template <
typename T =
int16_t>
1029 size_t convert(uint8_t *src,
size_t size)
override {
1030 return convert(src, src, size);
1033 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1034 LOGD(
"channel subtract %d samples, %d bytes", (
int)(size /
sizeof(T)),
1038 if (size % (
sizeof(T) * 2) > 0) {
1039 LOGE(
"Buffer size is not even");
1046 T *p_result = (T *)target;
1047 T *p_source = (T *)src;
1049 for (
int i = 0; i < sample_count; i++) {
1051 auto tmp = *p_source++;
1056 return sizeof(T) * sample_count;
1067 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1071 return cd8.
convert(target, src, size);
1075 return cd16.
convert(target, src, size);
1079 return cd24.
convert(target, src, size);
1083 return cd32.
convert(target, src, size);
1086 LOGE(
"Number of bits %d not supported.",
bits);
1104#if PREFER_FIXEDPOINT
1108template <
typename T =
int16_t,
typename SumT = soft_
float_t>
1110template <
typename T =
int16_t,
typename SumT =
float>
1117 T *srcT = (T *)data;
1118 T *targetT = (T *)data;
1119 int samples = size /
sizeof(T);
1121 for (
int j = 0; j < samples; j +=
channels) {
1123 for (
int ch = 0; ch <
channels; ch++) {
1124 sum += (SumT)srcT[j + ch];
1127 for (
int ch = 0; ch <
channels; ch++) {
1128 targetT[j + ch] = avg;
1151template <
typename T =
int16_t,
typename AvgT =
float>
1156 size_t convert(uint8_t *src,
size_t size)
override {
1157 return convert(src, src, size);
1160 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1161 if (size % (
sizeof(T) * 2) > 0) {
1162 LOGE(
"Buffer size is not even");
1169 T *p_result = (T *)target;
1170 T *p_source = (T *)src;
1172 for (
int i = 0; i < sample_count; i++) {
1175 AvgT tmp = *p_source++;
1177 *p_result++ = tmp / 2;
1180 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
1182 return sizeof(T) * sample_count;
1207 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1209#if PREFER_FIXEDPOINT
1212 return ca8.
convert(target, src, size);
1216 return ca16.
convert(target, src, size);
1220 return ca24.
convert(target, src, size);
1224 return ca32.
convert(target, src, size);
1229 return ca8.
convert(target, src, size);
1233 return ca16.
convert(target, src, size);
1237 return ca24.
convert(target, src, size);
1241 return ca32.
convert(target, src, size);
1245 LOGE(
"Number of bits %d not supported.",
bits);
1268template <
typename T =
int16_t>
1283 LOGE(
"Number of channels needs to be even");
1300 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1306 if (size % (
sizeof(T) *
channels) > 0) {
1307 LOGE(
"Buffer size needs to be multiple of channels");
1311 T *p_target = (T *)target;
1312 T *p_source = (T *)src;
1313 size_t result_size = 0;
1314 int sample_count = size / (
sizeof(T) *
channels);
1316 for (
int sample = 0; sample < sample_count; sample++) {
1317 for (
int ch = 0; ch <
channels; ch++) {
1323 for (
int ch = 0; ch <
channels; ch += 2) {
1325 p_target[result_size /
sizeof(T)] =
1327 :
static_cast<T
>(diff);
1328 result_size +=
sizeof(T);
1335 "bin & channel subtract %d: processed %d samples, %d remaining, %d > "
1348 LOGE(
"Number of channels must be > 0");
1352 LOGE(
"Bin size must be > 0");
1368 for (
int ch = 0; ch <
channels; ch++) {
1404 LOGE(
"Number of channels needs to be even");
1423 if (
state !=
nullptr) {
1430 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1432 if (
state ==
nullptr) {
1447 LOGE(
"Number of bits %d not supported.",
bits);
1457 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
1461 template <
typename T>
1466 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
1477 LOGE(
"Number of channels must be > 0");
1481 LOGE(
"Bin size must be > 0");
1504template <
typename T =
int16_t>
1522 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1525 size_t result_size = 0;
1526 T *result = (T *)target;
1527 T *source = (T *)src;
1528 T value = (int16_t)0;
1529 for (
int i = 0; i < frame_count; i++) {
1534 result_size +=
sizeof(T);
1539 result_size +=
sizeof(T);
1560template <
typename T =
int16_t>
1578 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1580 memcpy(target, src, size);
1612template <
typename T =
int16_t>
1635 for (
int i = 0; i < converters.size(); i++) {
1636 converters[i]->convert(src, size);
1655 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1656 bool result_bool =
false;
1657 int len = inBits / 8 * n;
1659 uint8_t buffer[len];
1662 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1668 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1670 bool result_bool =
false;
1673 int8_t *buffer = (int8_t *)bufferIn;
1674 for (
int j = 0; j < n; j++) {
1675 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1680 int16_t *buffer = (int16_t *)bufferIn;
1681 for (
int j = 0; j < n; j++) {
1682 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1687 int32_t *buffer = (int32_t *)bufferIn;
1688 for (
int j = 0; j < n; j++) {
1689 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1701 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1702 int32_t result =
static_cast<float>(value) /
1718template <
typename T =
int16_t>
1723 size_t convert(uint8_t *src,
size_t size)
override {
1725 for (
size_t j = 0; j < size; j++) {
1748template <
typename T,
typename FT>
1784template <
typename T,
typename FT>
1791 for (
int j = 0; j <
channels; j++) {
1801 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1815 int count = size /
channels /
sizeof(T);
1816 T *sample = (T *)src;
1817 for (
size_t j = 0; j < count; j++) {
1818 for (
int channel = 0; channel <
channels; channel++) {
1819 if (
filters[channel] !=
nullptr) {
1821 FT out =
filters[channel]->process(in);
1835 if (newChannels ==
channels)
return;
1840 for (
int j = oldChannels; j < newChannels; j++) {
1859template <
typename T =
int16_t>
1863 set(
n, aplidudeLimit);
1869 virtual size_t convert(uint8_t *data,
size_t size)
override {
1874 size_t sample_count = size /
sizeof(T);
1875 size_t write_count = 0;
1876 T *audio = (T *)data;
1879 T *p_buffer = (T *)data;
1880 for (
int j = 0; j < sample_count; j++) {
1884 *p_buffer++ = audio[j];
1889 size_t write_size = write_count *
sizeof(T);
1890 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1906 void set(
int n = 5,
int aplidudeLimit = 2) {
1907 LOGI(
"begin(n=%d, aplidudeLimit=%d",
n, aplidudeLimit);
1909 this->amplidude_limit = aplidudeLimit;
1910 this->priorLastAudioPos =
n + 1;
1911 this->active =
n > 0;
1916 for (
int j = 0; j <
n; j++) {
1937template <
typename T =
int16_t>
1945 virtual size_t convert(uint8_t *src,
size_t size) {
1946 for (
int ch = 0; ch <
channels; ch++) {
1962 T first = values[channel];
1963 for (
int j = 0; j < sampleCount; j +=
channels) {
1965 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1976 int lastPos = sampleCount -
channels + channel;
1977 T last = values[lastPos];
1978 for (
int j = lastPos; j >= 0; j -=
channels) {
1980 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1996template <
typename T =
int16_t>
2011 virtual size_t convert(uint8_t *src,
size_t size) {
2012 int sample_count = size /
sizeof(T);
2029 for (
int frame = 0; frame < frameCount; ++frame) {
2030 if (factor >= 0.8) {
2034 for (
int ch = 0; ch <
channels; ++ch) {
2035 values[pos + ch] = factor * values[pos + ch];
2044 for (
int frame = frameCount - 1; frame >= 0; --frame) {
2045 if (factor >= 0.8) {
2049 for (
int ch = 0; ch <
channels; ++ch) {
2050 values[pos + ch] = factor * values[pos + ch];
2064template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
2071 size_t samples = (size / Cn) /
sizeof(T);
2072 for (
size_t s = 0; s < samples; s++) {
2073 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
2075 for (
size_t c = 0; c < Cn; c++) {
2077 chan[s * Cn + c] = chan[s * Cn + Cx];
2081 if (_max_val < chan[s * Cn]) {
2082 _max_val = chan[s * Cn];
2087 if (now - _prev_ms > 1000) {
2089 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
2093 return samples * Cn *
sizeof(T);
2107template <
typename T =
int16_t>
2110 size_t convert(uint8_t *src,
size_t size)
override {
2112 int samples = size /
sizeof(T);
2113 for (
int j = 0; j < samples; j++) {
2125template <
typename T =
int16_t>
2143 int samples = size /
sizeof(T);
2145 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:350
@ LeftIsEmpty
Definition BaseConverter.h:350
@ RightIsEmpty
Definition BaseConverter.h:350
@ Auto
Definition BaseConverter.h:350