2#include "AudioFilter/Filter.h"
3#include "AudioTools/CoreAudio/AudioBasic/Collections.h"
32 virtual size_t convert(uint8_t *src,
size_t size) = 0;
42 size_t convert(uint8_t(*src),
size_t size)
override {
return size; };
54template <
typename T =
int16_t>
58 this->factor_value =
factor;
59 this->maxValue = maxValue;
60 this->offset_value =
offset;
61 this->channels = channels;
64 size_t convert(uint8_t *src,
size_t byte_count) {
66 int size = byte_count / channels /
sizeof(T);
67 for (
size_t j = 0; j < size; j++) {
68 for (
int i = 0; i < channels; i++) {
69 *sample = (*sample + offset_value) * factor_value;
70 if (*sample > maxValue) {
72 }
else if (*sample < -maxValue) {
88 float factor() {
return factor_value; }
105template <
typename T =
int16_t>
109 this->channels = channels;
110 this->is_dynamic = isDynamic;
113 void clear() { resetState(); }
114 void reset() { clear(); }
116 size_t convert(uint8_t(*src),
size_t byte_count)
override {
117 size_t size = byte_count / channels /
sizeof(T);
118 T *sample = (T *)src;
119 setup((T *)src, size);
123 for (
size_t j = 0; j < size; j++) {
124 for (
int ch = 0; ch < channels; ch++) {
125 sample[(j * channels) + ch] =
126 sample[(j * channels) + ch] - offset_to[ch];
130 for (
size_t j = 0; j < size; j++) {
131 for (
int ch = 0; ch < channels; ch++) {
132 sample[(j * channels) + ch] = sample[(j * channels) + ch] -
134 (offset_step[ch] * size);
149 bool is_setup =
false;
163 void setup(T *src,
size_t size) {
164 if (size == 0)
return;
165 if (!is_setup || is_dynamic) {
166 if (offset_from.size() == 0) {
167 offset_from.resize(channels);
168 offset_to.resize(channels);
169 offset_step.resize(channels);
170 total.resize(channels);
174 for (
int ch = 0; ch < channels; ch++) {
175 offset_from[ch] = offset_to[ch];
180 for (
size_t j = 0; j < size; j++) {
181 for (
int ch = 0; ch < channels; ch++) {
182 total[ch] += src[(j * channels) + ch];
185 for (
int ch = 0; ch < channels; ch++) {
186 offset_to[ch] = total[ch] / size;
187 offset_step[ch] = (offset_to[ch] - offset_from[ch]) / size;
207 begin(channels, bitsPerSample);
212 if (p_converter !=
nullptr) {
214 p_converter =
nullptr;
218 bool begin(
AudioInfo info,
bool isDynamic =
false) {
222 bool begin(
int channels,
int bitsPerSample,
bool isDynamic =
false) {
224 if (p_converter !=
nullptr && channels == this->channels &&
225 bitsPerSample == this->bits_per_sample && isDynamic == this->is_dynamic) {
228 this->channels = channels;
229 this->bits_per_sample = bitsPerSample;
230 this->is_dynamic = isDynamic;
232 assert(p_converter ==
nullptr);
233 switch (bits_per_sample) {
251 return p_converter !=
nullptr;
254 size_t convert(uint8_t *src,
size_t size)
override {
255 if (p_converter ==
nullptr)
return 0;
256 return p_converter->convert(src, size);
261 if (channels > 0 && bits_per_sample > 0) {
262 begin(channels, bits_per_sample, is_dynamic);
265 void reset() { clear(); }
269 int bits_per_sample = 0;
270 bool is_dynamic =
false;
282template <
typename T =
int16_t>
287 size_t convert(uint8_t *src,
size_t byte_count)
override {
289 int size = byte_count / channels /
sizeof(T);
290 T *sample = (T *)src;
291 for (
size_t j = 0; j < size; j++) {
293 *sample = *(sample + 1);
294 *(sample + 1) = temp;
321template <
typename T =
int16_t>
326 this->channels = channels;
344 size_t convert(uint8_t *src,
size_t byte_count) {
346 int size = byte_count / channels /
sizeof(T);
347 setup((T *)src, size);
348 if (left_empty && !right_empty) {
349 T *sample = (T *)src;
350 for (
size_t j = 0; j < size; j++) {
351 *sample = *(sample + 1);
354 }
else if (!left_empty && right_empty) {
355 T *sample = (T *)src;
356 for (
size_t j = 0; j < size; j++) {
357 *(sample + 1) = *sample;
366 bool is_setup =
false;
367 bool left_empty =
true;
368 bool right_empty =
true;
371 void setup(T *src,
size_t size) {
373 for (
int j = 0; j < size; j++) {
380 for (
int j = 0; j < size - 1; j++) {
388 if (!right_empty || !left_empty) {
404template <
typename T =
int16_t>
409 size_t convert(uint8_t *src,
size_t byte_count)
override {
410 int size = byte_count / channels /
sizeof(T);
411 T *sample = (T *)src;
412 for (
int i = 0; i < size; i++) {
413 for (
int j = 0; j < channels; j++) {
414 *sample = *sample + 0x8000;
432template <
typename T =
int16_t>
438 from_channels = channelCountOfSource;
439 to_channels = channelCountOfTarget;
442 void setSourceChannels(
int channelCountOfSource) {
443 from_channels = channelCountOfSource;
446 void setTargetChannels(
int channelCountOfTarget) {
447 to_channels = channelCountOfTarget;
450 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
452 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
453 LOGD(
"convert %d -> %d", from_channels, to_channels);
454 assert(to_channels <= from_channels);
455 int frame_count = size / (
sizeof(T) * from_channels);
456 size_t result_size = 0;
457 T *result = (T *)target;
458 T *source = (T *)src;
459 int reduceDiv = from_channels - to_channels + 1;
461 for (
int i = 0; i < frame_count; i++) {
463 for (
int j = 0; j < to_channels - 1; j++) {
464 *result++ = *source++;
465 result_size +=
sizeof(T);
468 T total = (int16_t)0;
469 for (
int j = to_channels - 1; j < from_channels; j++) {
470 total += *source++ / reduceDiv;
473 result_size +=
sizeof(T);
491 ChannelReducer(
int channelCountOfTarget,
int channelCountOfSource,
493 from_channels = channelCountOfSource;
494 to_channels = channelCountOfTarget;
495 bits = bitsPerSample;
498 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
500 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
504 return cr8.convert(target, src, size);
508 return cr16.convert(target, src, size);
512 return cr24.convert(target, src, size);
516 return cr32.convert(target, src, size);
532template <
typename T =
int16_t>
543 this->channels = channels;
549 this->factor = factor;
552 void clear() { resetState(); }
553 void reset() { clear(); }
555 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
557 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
558 if (!isConfigValid())
return 0;
559 if (size % (
sizeof(T) * channels) > 0) {
560 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
561 (
int)size, channels);
565 int frame_count = size / (
sizeof(T) * channels);
566 T *p_target = (T *)target;
567 T *p_source = (T *)src;
568 size_t result_size = 0;
570 for (
int i = 0; i < frame_count; i++) {
571 if (++count == factor) {
574 for (
int ch = 0; ch < channels; ch++) {
575 *p_target++ = p_source[i * channels + ch];
576 result_size +=
sizeof(T);
581 LOGD(
"decimate %d: %d -> %d bytes", factor, (
int)size, (
int)result_size);
585 operator bool() {
return factor > 1; }
588 bool isConfigValid() {
590 LOGE(
"Number of channels must be > 0");
594 LOGE(
"Decimation factor must be > 0");
600 void resetState() { count = 0; }
616 Decimate(
int factor,
int channels,
int bits_per_sample) {
619 setBits(bits_per_sample);
623 this->channels = channels;
626 void setBits(
int bits) {
632 this->factor = factor;
637 if (state !=
nullptr) {
641 void reset() { clear(); }
643 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
644 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
645 if (!isConfigValid())
return 0;
646 if (state ==
nullptr) {
649 state =
new DecimateStateT<int8_t>(factor, channels);
652 state =
new DecimateStateT<int16_t>(factor, channels);
655 state =
new DecimateStateT<int24_t>(factor, channels);
658 state =
new DecimateStateT<int32_t>(factor, channels);
661 LOGE(
"Number of bits %d not supported.", bits);
665 return state->convert(target, src, size);
668 operator bool() {
return factor > 1; };
673 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
674 virtual void reset() = 0;
677 template <
typename T>
679 DecimateStateT(
int factor,
int channels) : converter(factor, channels) {}
681 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
682 return converter.convert(target, src, size);
685 void reset()
override { converter.reset(); }
690 bool isConfigValid() {
692 LOGE(
"Number of channels must be > 0");
696 LOGE(
"Decimation factor must be > 0");
710 DecimateState *state =
nullptr;
724template <
typename T =
int16_t>
731 using type = int16_t;
736 using type = int32_t;
741 using type = int32_t;
746 using type = int64_t;
749template <
typename T =
int16_t>
753 BinT(
int binSize,
int channels,
bool average) {
754 setChannels(channels);
760 ~BinT() {
delete[] this->partialBin; }
762 void setChannels(
int channels) {
763 this->channels = channels;
766 void setBinSize(
int binSize) {
767 this->binSize = binSize;
770 void setAverage(
bool average) { this->average = average; }
771 void clear() { resetState(); }
772 void reset() { clear(); }
774 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
776 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
777 if (!isConfigValid())
return 0;
787 if (size % (
sizeof(T) * channels) > 0) {
788 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
789 (
int)size, channels);
793 T *p_target = (T *)target;
794 T *p_source = (T *)src;
795 size_t result_size = 0;
796 int sample_count = size / (
sizeof(T) * channels);
798 for (
int sample = 0; sample < sample_count; sample++) {
799 for (
int ch = 0; ch < channels; ch++) {
800 partialBin[ch] += p_source[sample * channels + ch];
804 if (partialBinSize == binSize) {
805 for (
int ch = 0; ch < channels; ch++) {
806 p_target[result_size /
sizeof(T)] =
807 average ?
static_cast<T
>(partialBin[ch] / binSize)
808 :
static_cast<T
>(partialBin[ch]);
809 result_size +=
sizeof(T);
815 LOGD(
"bin %d: processed %d samples, %d remaining, %d > %d bytes", binSize,
816 sample_count, partialBinSize, (
int)size, (
int)result_size);
822 using SumT =
typename AppropriateSumType<T>::type;
824 bool isConfigValid() {
826 LOGE(
"Number of channels must be > 0");
830 LOGE(
"Bin size must be > 0");
838 partialBin = channels > 0 ?
new SumT[channels]() :
nullptr;
843 if (partialBin ==
nullptr && channels > 0) {
844 partialBin =
new SumT[channels]();
845 }
else if (partialBin !=
nullptr) {
846 for (
int ch = 0; ch < channels; ch++) {
856 SumT *partialBin =
nullptr;
857 int partialBinSize = 0;
868 Bin(
int binSize,
int channels,
bool average,
int bits_per_sample) {
869 setChannels(channels);
872 setBits(bits_per_sample);
874 ~Bin()
override {
delete state; }
876 void setChannels(
int channels) {
877 this->channels = channels;
880 void setBits(
int bits) {
884 void setBinSize(
int binSize) {
885 this->binSize = binSize;
888 void setAverage(
bool average) {
889 this->average = average;
893 if (state !=
nullptr) {
897 void reset() { clear(); }
899 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
900 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
901 if (!isConfigValid())
return 0;
902 if (state ==
nullptr) {
917 LOGE(
"Number of bits %d not supported.", bits);
921 return state->convert(target, src, size);
927 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
928 virtual void reset() = 0;
931 template <
typename T>
933 BinStateT(
int binSize,
int channels,
bool average)
934 : converter(binSize, channels, average) {}
936 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
937 return converter.convert(target, src, size);
940 void reset()
override { converter.reset(); }
945 bool isConfigValid() {
947 LOGE(
"Number of channels must be > 0");
951 LOGE(
"Bin size must be > 0");
965 bool average =
false;
966 BinState *state =
nullptr;
983template <
typename T =
int16_t>
988 size_t convert(uint8_t *src,
size_t size)
override {
989 return convert(src, src, size);
992 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
993 LOGD(
"channel subtract %d samples, %d bytes", (
int)(size /
sizeof(T)),
997 if (size % (
sizeof(T) * 2) > 0) {
998 LOGE(
"Buffer size is not even");
1005 T *p_result = (T *)target;
1006 T *p_source = (T *)src;
1008 for (
int i = 0; i < sample_count; i++) {
1010 auto tmp = *p_source++;
1015 return sizeof(T) * sample_count;
1022 ChannelDiff(
int bitsPerSample) { setBits(bitsPerSample); }
1023 void setBits(
int bits) { this->bits = bits; }
1025 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1026 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1030 return cd8.convert(target, src, size);
1034 return cd16.convert(target, src, size);
1038 return cd24.convert(target, src, size);
1042 return cd32.convert(target, src, size);
1045 LOGE(
"Number of bits %d not supported.", bits);
1063template <
typename T =
int16_t,
typename SumT =
float>
1066 ChannelMixer(
int channels = 2) { this->channels = channels; }
1067 size_t convert(uint8_t *data,
size_t size) {
1068 if (channels <= 1)
return size;
1069 T *srcT = (T *)data;
1070 T *targetT = (T *)data;
1071 int samples = size /
sizeof(T);
1072 assert(samples % channels == 0);
1073 for (
int j = 0; j < samples; j += channels) {
1075 for (
int ch = 0; ch < channels; ch++) {
1076 sum += srcT[j + ch];
1078 T avg = sum / channels;
1079 for (
int ch = 0; ch < channels; ch++) {
1080 targetT[j + ch] = avg;
1103template <
typename T =
int16_t,
typename AvgT =
float>
1108 size_t convert(uint8_t *src,
size_t size)
override {
1109 return convert(src, src, size);
1112 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1113 if (size % (
sizeof(T) * 2) > 0) {
1114 LOGE(
"Buffer size is not even");
1121 T *p_result = (T *)target;
1122 T *p_source = (T *)src;
1124 for (
int i = 0; i < sample_count; i++) {
1127 AvgT tmp = *p_source++;
1129 *p_result++ = tmp / 2;
1132 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
1134 return sizeof(T) * sample_count;
1155 ChannelAvg(
int bitsPerSample) { setBits(bitsPerSample); }
1156 void setBits(
int bits) { this->bits = bits; }
1158 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1159 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1161#ifdef PREFER_FIXEDPOINT
1164 return ca8.convert(target, src, size);
1168 return ca16.convert(target, src, size);
1172 return ca24.convert(target, src, size);
1177 return ca8.convert(target, src, size);
1181 return ca16.convert(target, src, size);
1185 return ca24.convert(target, src, size);
1190 return ca32.convert(target, src, size);
1193 LOGE(
"Number of bits %d not supported.", bits);
1216template <
typename T =
int16_t>
1221 setChannels(channels);
1222 setBinSize(binSize);
1223 setAverage(average);
1229 void setChannels(
int channels) {
1230 if ((channels % 2) > 0) {
1231 LOGE(
"Number of channels needs to be even");
1232 this->channels = channels + 1;
1234 this->channels = channels;
1238 void setBinSize(
int binSize) {
1239 this->binSize = binSize;
1242 void setAverage(
bool average) { this->average = average; }
1243 void clear() { resetState(); }
1244 void reset() { clear(); }
1246 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1248 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1249 if (!isConfigValid())
return 0;
1254 if (size % (
sizeof(T) * channels) > 0) {
1255 LOGE(
"Buffer size needs to be multiple of channels");
1259 T *p_target = (T *)target;
1260 T *p_source = (T *)src;
1261 size_t result_size = 0;
1262 int sample_count = size / (
sizeof(T) * channels);
1264 for (
int sample = 0; sample < sample_count; sample++) {
1265 for (
int ch = 0; ch < channels; ch++) {
1266 partialBin[ch] += p_source[sample * channels + ch];
1270 if (partialBinSize == binSize) {
1271 for (
int ch = 0; ch < channels; ch += 2) {
1272 SumT diff = partialBin[ch] - partialBin[ch + 1];
1273 p_target[result_size /
sizeof(T)] =
1274 average ?
static_cast<T
>(diff / binSize)
1275 :
static_cast<T
>(diff);
1276 result_size +=
sizeof(T);
1283 "bin & channel subtract %d: processed %d samples, %d remaining, %d > "
1285 binSize, sample_count, partialBinSize, (
int)size,
1292 using SumT =
typename AppropriateSumType<T>::type;
1294 bool isConfigValid() {
1295 if (channels <= 0) {
1296 LOGE(
"Number of channels must be > 0");
1300 LOGE(
"Bin size must be > 0");
1306 void resizeState() {
1307 delete[] partialBin;
1308 partialBin = channels > 0 ?
new SumT[channels]() :
nullptr;
1313 if (partialBin ==
nullptr && channels > 0) {
1314 partialBin =
new SumT[channels]();
1315 }
else if (partialBin !=
nullptr) {
1316 for (
int ch = 0; ch < channels; ch++) {
1325 bool average =
true;
1326 SumT *partialBin =
nullptr;
1327 int partialBinSize = 0;
1340 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1341 setChannels(channels);
1342 setBinSize(binSize);
1343 setAverage(average);
1344 setBits(bits_per_sample);
1348 void setChannels(
int channels) {
1349 if ((channels % 2) == 0) {
1350 this->channels = channels;
1352 LOGE(
"Number of channels needs to be even");
1353 this->channels = channels + 1;
1358 void setBits(
int bits) {
1362 void setBinSize(
int binSize) {
1363 this->binSize = binSize;
1366 void setAverage(
bool average) {
1367 this->average = average;
1371 if (state !=
nullptr) {
1375 void reset() { clear(); }
1377 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1378 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1379 if (!isConfigValid())
return 0;
1380 if (state ==
nullptr) {
1395 LOGE(
"Number of bits %d not supported.", bits);
1399 return state->convert(target, src, size);
1405 virtual size_t convert(uint8_t *target, uint8_t *src,
size_t size) = 0;
1406 virtual void reset() = 0;
1409 template <
typename T>
1412 : converter(binSize, channels, average) {}
1414 size_t convert(uint8_t *target, uint8_t *src,
size_t size)
override {
1415 return converter.convert(target, src, size);
1418 void reset()
override { converter.reset(); }
1423 bool isConfigValid() {
1424 if (channels <= 0) {
1425 LOGE(
"Number of channels must be > 0");
1429 LOGE(
"Bin size must be > 0");
1443 bool average =
true;
1444 ChannelBinDiffState *state =
nullptr;
1452template <
typename T =
int16_t>
1458 from_channels = channelCountOfSource;
1459 to_channels = channelCountOfTarget;
1462 void setSourceChannels(
int channelCountOfSource) {
1463 from_channels = channelCountOfSource;
1466 void setTargetChannels(
int channelCountOfTarget) {
1467 to_channels = channelCountOfTarget;
1470 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1471 if (from_channels == 0)
return size;
1472 int frame_count = size / (
sizeof(T) * from_channels);
1473 size_t result_size = 0;
1474 T *result = (T *)target;
1475 T *source = (T *)src;
1476 T value = (int16_t)0;
1477 for (
int i = 0; i < frame_count; i++) {
1479 for (
int j = 0; j < from_channels; j++) {
1482 result_size +=
sizeof(T);
1485 for (
int j = from_channels; j < to_channels; j++) {
1487 result_size +=
sizeof(T);
1495 return inSize * to_channels / from_channels;
1499 int from_channels = 0;
1500 int to_channels = 0;
1508template <
typename T =
int16_t>
1514 from_channels = channelCountOfSource;
1515 to_channels = channelCountOfTarget;
1518 void setSourceChannels(
int channelCountOfSource) {
1519 from_channels = channelCountOfSource;
1522 void setTargetChannels(
int channelCountOfTarget) {
1523 to_channels = channelCountOfTarget;
1526 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1527 if (from_channels == to_channels) {
1528 memcpy(target, src, size);
1532 if (from_channels > to_channels) {
1533 reducer.setSourceChannels(from_channels);
1534 reducer.setTargetChannels(to_channels);
1536 enhancer.setSourceChannels(from_channels);
1537 enhancer.setTargetChannels(to_channels);
1541 if (from_channels > to_channels) {
1542 return reducer.convert(target, src, size);
1544 return enhancer.convert(target, src, size);
1560template <
typename T =
int16_t>
1579 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1582 size_t convert(uint8_t *src,
size_t size) {
1583 for (
int i = 0; i < converters.size(); i++) {
1584 converters[i]->convert(src, size);
1603 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1604 bool result_bool =
false;
1605 int len = inBits / 8 * n;
1606 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1607 uint8_t buffer[len];
1608 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1610 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1616 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1618 bool result_bool =
false;
1621 int8_t *buffer = (int8_t *)bufferIn;
1622 for (
int j = 0; j < n; j++) {
1623 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1628 int16_t *buffer = (int16_t *)bufferIn;
1629 for (
int j = 0; j < n; j++) {
1630 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1635 int32_t *buffer = (int32_t *)bufferIn;
1636 for (
int j = 0; j < n; j++) {
1637 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1646 Stream *stream_ptr =
nullptr;
1649 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1650 int32_t result =
static_cast<float>(value) /
1666template <
typename T =
int16_t>
1671 size_t convert(uint8_t *src,
size_t size)
override {
1673 for (
size_t j = 0; j < size; j++) {
1674 data[j] = p_filter->process(data[j]);
1689template <
typename T,
typename FT>
1694 this->channels = channels;
1697 for (
int j = 0; j < channels; j++) {
1698 filters[j] =
nullptr;
1704 for (
int j = 0; j < channels; j++) {
1705 if (filters[j] !=
nullptr) {
1715 if (channel < channels) {
1716 if (filters[channel] !=
nullptr) {
1717 delete filters[channel];
1719 filters[channel] = filter;
1721 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1727 size_t convert(uint8_t *src,
size_t size) {
1728 int count = size / channels /
sizeof(T);
1729 T *sample = (T *)src;
1730 for (
size_t j = 0; j < count; j++) {
1731 for (
int channel = 0; channel < channels; channel++) {
1732 if (filters[channel] !=
nullptr) {
1733 *sample = filters[channel]->process(*sample);
1741 int getChannels() {
return channels; }
1744 Filter<FT> **filters =
nullptr;
1757template <
typename T =
int16_t>
1761 set(n, aplidudeLimit);
1764 void clear() { priorLastAudioPos = n + 1; }
1765 void reset() { clear(); }
1767 virtual size_t convert(uint8_t *data,
size_t size)
override {
1772 size_t sample_count = size /
sizeof(T);
1773 size_t write_count = 0;
1774 T *audio = (T *)data;
1777 T *p_buffer = (T *)data;
1778 for (
int j = 0; j < sample_count; j++) {
1779 int pos = findLastAudioPos(audio, j);
1782 *p_buffer++ = audio[j];
1787 size_t write_size = write_count *
sizeof(T);
1788 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1791 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1798 bool active =
false;
1799 const uint8_t *buffer =
nullptr;
1801 int priorLastAudioPos = 0;
1802 int amplidude_limit = 0;
1804 void set(
int n = 5,
int aplidudeLimit = 2) {
1805 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1807 this->amplidude_limit = aplidudeLimit;
1808 this->priorLastAudioPos = n + 1;
1809 this->active = n > 0;
1813 int findLastAudioPos(T *audio,
int pos) {
1814 for (
int j = 0; j < n; j++) {
1817 return priorLastAudioPos;
1820 if (abs(audio[pos - j]) > amplidude_limit) {
1835template <
typename T =
int16_t>
1839 this->channels = channels;
1840 from_beginning = fromBeginning;
1843 virtual size_t convert(uint8_t *src,
size_t size) {
1844 for (
int ch = 0; ch < channels; ch++) {
1846 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1848 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1854 bool from_beginning;
1858 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1860 T first = values[channel];
1861 for (
int j = 0; j < sampleCount; j += channels) {
1863 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1872 void clearAfterLastTransition(
int channels,
int channel, T *values,
1874 int lastPos = sampleCount - channels + channel;
1875 T last = values[lastPos];
1876 for (
int j = lastPos; j >= 0; j -= channels) {
1878 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1894template <
typename T =
int16_t>
1899 this->channels = channels;
1901 from_beginning = fromBeginning;
1908 void reset() { clear(); }
1909 virtual size_t convert(uint8_t *src,
size_t size) {
1910 int sample_count = size /
sizeof(T);
1911 int frame_count = channels > 0 ? sample_count / channels : 0;
1912 if (from_beginning) processStart((T *)src, frame_count);
1913 if (from_end) processEnd((T *)src, frame_count);
1918 bool from_beginning;
1922 float start_factor = 0;
1923 float end_factor = 0;
1925 void processStart(T *values,
int frameCount) {
1926 float factor = start_factor;
1927 for (
int frame = 0; frame < frameCount; ++frame) {
1928 if (factor >= 0.8) {
1931 int pos = frame * channels;
1932 for (
int ch = 0; ch < channels; ++ch) {
1933 values[pos + ch] = factor * values[pos + ch];
1937 start_factor = factor;
1940 void processEnd(T *values,
int frameCount) {
1941 float factor = end_factor;
1942 for (
int frame = frameCount - 1; frame >= 0; --frame) {
1943 if (factor >= 0.8) {
1946 int pos = frame * channels;
1947 for (
int ch = 0; ch < channels; ++ch) {
1948 values[pos + ch] = factor * values[pos + ch];
1952 end_factor = factor;
1962template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1965 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1967 size_t convert(uint8_t *src,
size_t size) {
1969 size_t samples = (size / Cn) /
sizeof(T);
1970 for (
size_t s = 0; s < samples; s++) {
1971 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1973 for (
size_t c = 0; c < Cn; c++) {
1975 chan[s * Cn + c] = chan[s * Cn + Cx];
1979 if (_max_val < chan[s * Cn]) {
1980 _max_val = chan[s * Cn];
1985 if (now - _prev_ms > 1000) {
1987 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1991 return samples * Cn *
sizeof(T);
2005template <
typename T =
int16_t>
2009 this->callback = callback;
2010 this->channels = channels;
2013 size_t convert(uint8_t *src,
size_t size) {
2014 int samples = size /
sizeof(T);
2016 for (
int j = 0; j < samples; j++) {
2017 srcT[j] = callback(srcT[j], j % channels);
2023 T (*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:309