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 size_t convert(uint8_t(*src),
size_t byte_count)
override {
114 size_t size = byte_count / channels /
sizeof(T);
115 T *sample = (T *)src;
116 setup((T *)src, size);
120 for (
size_t j = 0; j < size; j++) {
121 for (
int ch = 0; ch < channels; ch++) {
122 sample[(j * channels) + ch] =
123 sample[(j * channels) + ch] - offset_to[ch];
127 for (
size_t j = 0; j < size; j++) {
128 for (
int ch = 0; ch < channels; ch++) {
129 sample[(j * channels) + ch] = sample[(j * channels) + ch] -
131 (offset_step[ch] * size);
146 bool is_setup =
false;
150 void setup(T *src,
size_t size) {
151 if (size == 0)
return;
152 if (!is_setup || is_dynamic) {
153 if (offset_from.size() == 0) {
154 offset_from.resize(channels);
155 offset_to.resize(channels);
156 offset_step.resize(channels);
157 total.resize(channels);
161 for (
int ch = 0; ch < channels; ch++) {
162 offset_from[ch] = offset_to[ch];
167 for (
size_t j = 0; j < size; j++) {
168 for (
int ch = 0; ch < channels; ch++) {
169 total[ch] += src[(j * channels) + ch];
172 for (
int ch = 0; ch < channels; ch++) {
173 offset_to[ch] = total[ch] / size;
174 offset_step[ch] = (offset_to[ch] - offset_from[ch]) / size;
194 begin(channels, bitsPerSample);
199 if (p_converter !=
nullptr) {
201 p_converter =
nullptr;
205 bool begin(
AudioInfo info,
bool isDynamic =
false) {
209 bool begin(
int channels,
int bitsPerSample,
bool isDynamic =
false) {
211 if (p_converter !=
nullptr && channels == this->channels &&
212 bitsPerSample == this->bits_per_sample) {
215 this->channels = channels;
216 this->bits_per_sample = bitsPerSample;
218 assert(p_converter ==
nullptr);
219 switch (bits_per_sample) {
237 return p_converter !=
nullptr;
240 size_t convert(uint8_t *src,
size_t size)
override {
241 if (p_converter ==
nullptr)
return 0;
242 return p_converter->convert(src, size);
247 int bits_per_sample = 0;
259template <
typename T =
int16_t>
264 size_t convert(uint8_t *src,
size_t byte_count)
override {
266 int size = byte_count / channels /
sizeof(T);
267 T *sample = (T *)src;
268 for (
size_t j = 0; j < size; j++) {
270 *sample = *(sample + 1);
271 *(sample + 1) = temp;
298template <
typename T =
int16_t>
303 this->channels = channels;
321 size_t convert(uint8_t *src,
size_t byte_count) {
323 int size = byte_count / channels /
sizeof(T);
324 setup((T *)src, size);
325 if (left_empty && !right_empty) {
326 T *sample = (T *)src;
327 for (
size_t j = 0; j < size; j++) {
328 *sample = *(sample + 1);
331 }
else if (!left_empty && right_empty) {
332 T *sample = (T *)src;
333 for (
size_t j = 0; j < size; j++) {
334 *(sample + 1) = *sample;
343 bool is_setup =
false;
344 bool left_empty =
true;
345 bool right_empty =
true;
348 void setup(T *src,
size_t size) {
350 for (
int j = 0; j < size; j++) {
357 for (
int j = 0; j < size - 1; j++) {
365 if (!right_empty || !left_empty) {
381template <
typename T =
int16_t>
386 size_t convert(uint8_t *src,
size_t byte_count)
override {
387 int size = byte_count / channels /
sizeof(T);
388 T *sample = (T *)src;
389 for (
int i = 0; i < size; i++) {
390 for (
int j = 0; j < channels; j++) {
391 *sample = *sample + 0x8000;
409template <
typename T =
int16_t>
415 from_channels = channelCountOfSource;
416 to_channels = channelCountOfTarget;
419 void setSourceChannels(
int channelCountOfSource) {
420 from_channels = channelCountOfSource;
423 void setTargetChannels(
int channelCountOfTarget) {
424 to_channels = channelCountOfTarget;
427 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
429 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
430 LOGD(
"convert %d -> %d", from_channels, to_channels);
431 assert(to_channels <= from_channels);
432 int frame_count = size / (
sizeof(T) * from_channels);
433 size_t result_size = 0;
434 T *result = (T *)target;
435 T *source = (T *)src;
436 int reduceDiv = from_channels - to_channels + 1;
438 for (
int i = 0; i < frame_count; i++) {
440 for (
int j = 0; j < to_channels - 1; j++) {
441 *result++ = *source++;
442 result_size +=
sizeof(T);
445 T total = (int16_t)0;
446 for (
int j = to_channels - 1; j < from_channels; j++) {
447 total += *source++ / reduceDiv;
450 result_size +=
sizeof(T);
468 ChannelReducer(
int channelCountOfTarget,
int channelCountOfSource,
470 from_channels = channelCountOfSource;
471 to_channels = channelCountOfTarget;
472 bits = bitsPerSample;
475 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
477 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
481 return cr8.convert(target, src, size);
485 return cr16.convert(target, src, size);
489 return cr24.convert(target, src, size);
493 return cr32.convert(target, src, size);
509template <
typename T =
int16_t>
524 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
526 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
527 if (size % (
sizeof(T) * channels) > 0) {
528 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
529 (
int)size, channels);
533 int frame_count = size / (
sizeof(T) * channels);
534 T *p_target = (T *)target;
535 T *p_source = (T *)src;
536 size_t result_size = 0;
538 for (
int i = 0; i < frame_count; i++) {
539 if (++count == factor) {
542 for (
int ch = 0; ch < channels; ch++) {
543 *p_target++ = p_source[i * channels + ch];
544 result_size +=
sizeof(T);
549 LOGD(
"decimate %d: %d -> %d bytes", factor, (
int)size, (
int)result_size);
553 operator bool() {
return factor > 1; }
570 Decimate(
int factor,
int channels,
int bits_per_sample) {
573 setBits(bits_per_sample);
577 void setBits(
int bits) { this->bits = bits; }
581 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
582 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
585 DecimateT<int8_t> dec8(factor, channels);
586 return dec8.convert(target, src, size);
589 DecimateT<int16_t> dec16(factor, channels);
590 return dec16.convert(target, src, size);
593 DecimateT<int24_t> dec24(factor, channels);
594 return dec24.convert(target, src, size);
597 DecimateT<int32_t> dec32(factor, channels);
598 return dec32.convert(target, src, size);
604 operator bool() {
return factor > 1; };
623template <
typename T =
int16_t>
630 using type = int16_t;
635 using type = int32_t;
640 using type = int32_t;
645 using type = int64_t;
648template <
typename T =
int16_t>
652 BinT(
int binSize,
int channels,
bool average) {
653 setChannels(channels);
656 this->partialBinSize = 0;
657 this->partialBin =
new T[channels]();
660 ~BinT() {
delete[] this->partialBin; }
662 void setChannels(
int channels) { this->channels = channels; }
663 void setBinSize(
int binSize) { this->binSize = binSize; }
664 void setAverage(
bool average) { this->average = average; }
666 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
668 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
677 if (size % (
sizeof(T) * channels) > 0) {
678 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
679 (
int)size, channels);
684 size / (
sizeof(T) * channels);
689 int bin_count = total_samples / binSize;
690 int remaining_samples =
691 total_samples % binSize;
692 T *p_target = (T *)target;
693 T *p_source = (T *)src;
694 size_t result_size = 0;
697 typename AppropriateSumType<T>::type sums[channels];
698 int current_sample = 0;
702 if (partialBinSize > 0) {
703 int samples_needed = binSize - partialBinSize;
704 bool have_enough_samples = (samples_needed < sample_count);
705 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
707 for (
int ch = 0; ch < channels; ch++) {
708 sums[ch] = partialBin[ch];
711 for (
int j = 0; j < samples_to_bin; j++) {
712 for (
int ch = 0; ch < channels; ch++) {
713 sums[ch] += p_source[current_sample * channels + ch];
718 if (have_enough_samples) {
721 for (
int ch = 0; ch < channels; ch++) {
722 p_target[result_size /
sizeof(T)] =
723 static_cast<T
>(sums[ch] / binSize);
724 result_size +=
sizeof(T);
727 for (
int ch = 0; ch < channels; ch++) {
728 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
729 result_size +=
sizeof(T);
735 for (
int ch = 0; ch < channels; ch++) {
736 partialBin[ch] = sums[ch];
738 partialBinSize += current_sample;
740 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize,
741 current_sample, total_samples, partialBinSize, (
int)size,
749 for (
int i = 0; i < bin_count; i++) {
750 for (
int ch = 0; ch < channels; ch++) {
751 sums[ch] = p_source[current_sample * channels +
756 for (
int j = 1; j < binSize; j++) {
757 for (
int ch = 0; ch < channels; ch++) {
758 sums[ch] += p_source[(current_sample + j) * channels + ch];
761 current_sample += binSize;
765 for (
int ch = 0; ch < channels; ch++) {
766 p_target[result_size /
sizeof(T)] =
767 static_cast<T
>(sums[ch] / binSize);
768 result_size +=
sizeof(T);
771 for (
int ch = 0; ch < channels; ch++) {
772 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
773 result_size +=
sizeof(T);
780 for (
int i = 0; i < remaining_samples; i++) {
781 for (
int ch = 0; ch < channels; ch++) {
782 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
785 partialBinSize = remaining_samples;
787 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize,
788 current_sample, total_samples, partialBinSize, (
int)size,
810 Bin(
int binSize,
int channels,
bool average,
int bits_per_sample) {
811 setChannels(channels);
814 setBits(bits_per_sample);
817 void setChannels(
int channels) { this->channels = channels; }
818 void setBits(
int bits) { this->bits = bits; }
819 void setBinSize(
int binSize) { this->binSize = binSize; }
820 void setAverage(
bool average) { this->average = average; }
822 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
823 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
827 return bin8.convert(target, src, size);
831 return bin16.convert(target, src, size);
835 return bin24.convert(target, src, size);
839 return bin32.convert(target, src, size);
842 LOGE(
"Number of bits %d not supported.", bits);
852 bool average =
false;
869template <
typename T =
int16_t>
874 size_t convert(uint8_t *src,
size_t size)
override {
875 return convert(src, src, size);
878 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
879 LOGD(
"channel subtract %d samples, %d bytes", (
int)(size /
sizeof(T)),
883 if (size % (
sizeof(T) * 2) > 0) {
884 LOGE(
"Buffer size is not even");
891 T *p_result = (T *)target;
892 T *p_source = (T *)src;
894 for (
int i = 0; i < sample_count; i++) {
896 auto tmp = *p_source++;
901 return sizeof(T) * sample_count;
908 ChannelDiff(
int bitsPerSample) { setBits(bitsPerSample); }
909 void setBits(
int bits) { this->bits = bits; }
911 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
912 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
916 return cd8.convert(target, src, size);
920 return cd16.convert(target, src, size);
924 return cd24.convert(target, src, size);
928 return cd32.convert(target, src, size);
931 LOGE(
"Number of bits %d not supported.", bits);
954template <
typename T =
int16_t>
959 size_t convert(uint8_t *src,
size_t size)
override {
960 return convert(src, src, size);
963 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
964 if (size % (
sizeof(T) * 2) > 0) {
965 LOGE(
"Buffer size is not even");
972 T *p_result = (T *)target;
973 T *p_source = (T *)src;
975 for (
int i = 0; i < sample_count; i++) {
978 auto tmp = *p_source++;
980 *p_result++ = tmp / 2;
983 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
985 return sizeof(T) * sample_count;
996template <
typename T =
int16_t>
999 ChannelMixer(
int channels = 2) { this->channels = channels; }
1000 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1002 T *targetT = (T *)target;
1003 int samples = size /
sizeof(T);
1004 assert(samples % channels == 0);
1005 for (
int j = 0; j < samples; j += channels) {
1007 for (
int ch = 0; ch < channels; ch++) {
1008 sum += srcT[j + ch];
1010 T avg = sum / channels;
1011 for (
int ch = 0; ch < channels; ch++) {
1012 targetT[j + ch] = avg;
1038 ChannelAvg(
int bitsPerSample) { setBits(bitsPerSample); }
1039 void setBits(
int bits) { this->bits = bits; }
1041 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1042 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1046 return ca8.convert(target, src, size);
1050 return ca16.convert(target, src, size);
1054 return ca24.convert(target, src, size);
1058 return ca32.convert(target, src, size);
1061 LOGE(
"Number of bits %d not supported.", bits);
1084template <
typename T =
int16_t>
1089 setChannels(channels);
1090 setBinSize(binSize);
1091 setAverage(average);
1092 this->partialBinSize = 0;
1096 this->partialBin =
new T[channels]();
1101 void setChannels(
int channels) {
1102 if ((channels % 2) > 0) {
1103 LOGE(
"Number of channels needs to be even");
1104 this->channels = channels + 1;
1106 this->channels = channels;
1108 void setBinSize(
int binSize) { this->binSize = binSize; }
1109 void setAverage(
bool average) { this->average = average; }
1111 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1113 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1117 if (size % (
sizeof(T) * channels) > 0) {
1118 LOGE(
"Buffer size needs to be multiple of channels")
1123 size / (
sizeof(T) * channels);
1128 int bin_count = total_samples / binSize;
1129 int remaining_samples =
1130 total_samples % binSize;
1131 T *p_target = (T *)target;
1132 T *p_source = (T *)src;
1133 size_t result_size = 0;
1136 typename AppropriateSumType<T>::type sums[channels];
1137 int current_sample = 0;
1141 if (partialBinSize > 0) {
1144 int samples_needed = binSize - partialBinSize;
1145 bool have_enough_samples = (samples_needed < sample_count);
1146 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
1149 for (
int ch = 0; ch < channels; ch++) {
1150 sums[ch] = partialBin[ch];
1154 for (
int j = 0; j < samples_to_bin; j++) {
1155 for (
int ch = 0; ch < channels; ch++) {
1156 sums[ch] += p_source[current_sample * channels + ch];
1162 if (have_enough_samples) {
1165 for (
int ch = 0; ch < channels; ch += 2) {
1166 p_target[result_size /
sizeof(T)] =
1167 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1168 result_size +=
sizeof(T);
1171 for (
int ch = 0; ch < channels; ch += 2) {
1172 p_target[result_size /
sizeof(T)] =
1173 static_cast<T
>((sums[ch] - sums[ch + 1]));
1174 result_size +=
sizeof(T);
1182 for (
int ch = 0; ch < channels; ch++) {
1183 partialBin[ch] = sums[ch];
1185 partialBinSize += current_sample;
1187 "bin & channel subtract %d: %d of %d remaining %d samples, %d "
1190 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1200 for (
int i = 0; i < bin_count; i++) {
1203 for (
int ch = 0; ch < channels; ch++) {
1204 sums[ch] = p_source[current_sample * channels +
1209 for (
int j = 1; j < binSize; j++) {
1210 for (
int ch = 0; ch < channels; ch++) {
1211 sums[ch] += p_source[(current_sample + j) * channels + ch];
1214 current_sample += binSize;
1218 for (
int ch = 0; ch < channels; ch += 2) {
1219 p_target[result_size /
sizeof(T)] =
1220 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1221 result_size +=
sizeof(T);
1224 for (
int ch = 0; ch < channels; ch += 2) {
1225 p_target[result_size /
sizeof(T)] =
1226 static_cast<T
>((sums[ch] - sums[ch + 1]));
1227 result_size +=
sizeof(T);
1235 for (
int i = 0; i < remaining_samples; i++) {
1236 for (
int ch = 0; ch < channels; ch++) {
1237 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
1240 partialBinSize = remaining_samples;
1243 "bin & channel subtract %d: %d of %d remaining %d samples, %d > %d "
1245 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1254 bool average =
true;
1269 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1270 setChannels(channels);
1271 setBinSize(binSize);
1272 setAverage(average);
1273 setBits(bits_per_sample);
1276 void setChannels(
int channels) {
1277 if ((channels % 2) == 0) {
1278 this->channels = channels;
1280 LOGE(
"Number of channels needs to be even");
1281 this->channels = channels + 1;
1285 void setBits(
int bits) { this->bits = bits; }
1286 void setBinSize(
int binSize) { this->binSize = binSize; }
1287 void setAverage(
bool average) { this->average = average; }
1289 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1290 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1294 return bd8.convert(target, src, size);
1298 return bd16.convert(target, src, size);
1302 return bd24.convert(target, src, size);
1306 return bd32.convert(target, src, size);
1309 LOGE(
"Number of bits %d not supported.", bits);
1319 bool average =
true;
1327template <
typename T =
int16_t>
1333 from_channels = channelCountOfSource;
1334 to_channels = channelCountOfTarget;
1337 void setSourceChannels(
int channelCountOfSource) {
1338 from_channels = channelCountOfSource;
1341 void setTargetChannels(
int channelCountOfTarget) {
1342 to_channels = channelCountOfTarget;
1345 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1346 if (from_channels == 0)
return size;
1347 int frame_count = size / (
sizeof(T) * from_channels);
1348 size_t result_size = 0;
1349 T *result = (T *)target;
1350 T *source = (T *)src;
1351 T value = (int16_t)0;
1352 for (
int i = 0; i < frame_count; i++) {
1354 for (
int j = 0; j < from_channels; j++) {
1357 result_size +=
sizeof(T);
1360 for (
int j = from_channels; j < to_channels; j++) {
1362 result_size +=
sizeof(T);
1370 return inSize * to_channels / from_channels;
1374 int from_channels = 0;
1375 int to_channels = 0;
1383template <
typename T =
int16_t>
1389 from_channels = channelCountOfSource;
1390 to_channels = channelCountOfTarget;
1393 void setSourceChannels(
int channelCountOfSource) {
1394 from_channels = channelCountOfSource;
1397 void setTargetChannels(
int channelCountOfTarget) {
1398 to_channels = channelCountOfTarget;
1401 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1402 if (from_channels == to_channels) {
1403 memcpy(target, src, size);
1407 if (from_channels > to_channels) {
1408 reducer.setSourceChannels(from_channels);
1409 reducer.setTargetChannels(to_channels);
1411 enhancer.setSourceChannels(from_channels);
1412 enhancer.setTargetChannels(to_channels);
1416 if (from_channels > to_channels) {
1417 return reducer.convert(target, src, size);
1419 return enhancer.convert(target, src, size);
1435template <
typename T =
int16_t>
1454 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1457 size_t convert(uint8_t *src,
size_t size) {
1458 for (
int i = 0; i < converters.size(); i++) {
1459 converters[i]->convert(src, size);
1478 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1479 bool result_bool =
false;
1480 int len = inBits / 8 * n;
1481 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1482 uint8_t buffer[len];
1483 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1485 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1491 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1493 bool result_bool =
false;
1496 int8_t *buffer = (int8_t *)bufferIn;
1497 for (
int j = 0; j < n; j++) {
1498 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1503 int16_t *buffer = (int16_t *)bufferIn;
1504 for (
int j = 0; j < n; j++) {
1505 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1510 int32_t *buffer = (int32_t *)bufferIn;
1511 for (
int j = 0; j < n; j++) {
1512 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1521 Stream *stream_ptr =
nullptr;
1524 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1525 int32_t result =
static_cast<float>(value) /
1541template <
typename T =
int16_t>
1546 size_t convert(uint8_t *src,
size_t size)
override {
1548 for (
size_t j = 0; j < size; j++) {
1549 data[j] = p_filter->process(data[j]);
1564template <
typename T,
typename FT>
1569 this->channels = channels;
1572 for (
int j = 0; j < channels; j++) {
1573 filters[j] =
nullptr;
1579 for (
int j = 0; j < channels; j++) {
1580 if (filters[j] !=
nullptr) {
1590 if (channel < channels) {
1591 if (filters[channel] !=
nullptr) {
1592 delete filters[channel];
1594 filters[channel] = filter;
1596 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1602 size_t convert(uint8_t *src,
size_t size) {
1603 int count = size / channels /
sizeof(T);
1604 T *sample = (T *)src;
1605 for (
size_t j = 0; j < count; j++) {
1606 for (
int channel = 0; channel < channels; channel++) {
1607 if (filters[channel] !=
nullptr) {
1608 *sample = filters[channel]->process(*sample);
1616 int getChannels() {
return channels; }
1619 Filter<FT> **filters =
nullptr;
1632template <
typename T =
int16_t>
1636 set(n, aplidudeLimit);
1639 virtual size_t convert(uint8_t *data,
size_t size)
override {
1644 size_t sample_count = size /
sizeof(T);
1645 size_t write_count = 0;
1646 T *audio = (T *)data;
1649 T *p_buffer = (T *)data;
1650 for (
int j = 0; j < sample_count; j++) {
1651 int pos = findLastAudioPos(audio, j);
1654 *p_buffer++ = audio[j];
1659 size_t write_size = write_count *
sizeof(T);
1660 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1663 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1670 bool active =
false;
1671 const uint8_t *buffer =
nullptr;
1673 int priorLastAudioPos = 0;
1674 int amplidude_limit = 0;
1676 void set(
int n = 5,
int aplidudeLimit = 2) {
1677 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1679 this->amplidude_limit = aplidudeLimit;
1680 this->priorLastAudioPos = n + 1;
1681 this->active = n > 0;
1685 int findLastAudioPos(T *audio,
int pos) {
1686 for (
int j = 0; j < n; j++) {
1689 return priorLastAudioPos;
1692 if (abs(audio[pos - j]) > amplidude_limit) {
1707template <
typename T =
int16_t>
1711 this->channels = channels;
1712 from_beginning = fromBeginning;
1715 virtual size_t convert(uint8_t *src,
size_t size) {
1716 for (
int ch = 0; ch < channels; ch++) {
1718 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1720 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1726 bool from_beginning;
1730 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1732 T first = values[channel];
1733 for (
int j = 0; j < sampleCount; j += channels) {
1735 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1744 void clearAfterLastTransition(
int channels,
int channel, T *values,
1746 int lastPos = sampleCount - channels + channel;
1747 T last = values[lastPos];
1748 for (
int j = lastPos; j >= 0; j -= channels) {
1750 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1766template <
typename T =
int16_t>
1771 this->channels = channels;
1773 from_beginning = fromBeginning;
1776 virtual size_t convert(uint8_t *src,
size_t size) {
1777 for (
int ch = 0; ch < channels; ch++) {
1779 processStart(channels, ch, (T *)src, size /
sizeof(T));
1780 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1786 bool from_beginning;
1792 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1793 for (
int j = 0; j < sampleCount; j += channels) {
1794 if (factor >= 0.8) {
1797 values[j] = factor * values[j];
1803 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1804 int lastPos = sampleCount - channels + channel;
1805 for (
int j = lastPos; j >= 0; j -= channels) {
1806 if (factor >= 0.8) {
1809 values[j] = factor * values[j];
1821template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1824 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1826 size_t convert(uint8_t *src,
size_t size) {
1828 size_t samples = (size / Cn) /
sizeof(T);
1829 for (
size_t s = 0; s < samples; s++) {
1830 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1832 for (
size_t c = 0; c < Cn; c++) {
1834 chan[s * Cn + c] = chan[s * Cn + Cx];
1838 if (_max_val < chan[s * Cn]) {
1839 _max_val = chan[s * Cn];
1844 if (now - _prev_ms > 1000) {
1846 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1850 return samples * Cn *
sizeof(T);
1864template <
typename T =
int16_t>
1868 this->callback = callback;
1869 this->channels = channels;
1872 size_t convert(uint8_t *src,
size_t size) {
1873 int samples = size /
sizeof(T);
1875 for (
int j = 0; j < samples; j++) {
1876 srcT[j] = callback(srcT[j], j % channels);
1882 T (*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:286