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,
typename SumT =
float>
999 ChannelMixer(
int channels = 2) { this->channels = channels; }
1000 size_t convert(uint8_t *data,
size_t size) {
1001 if (channels <= 1)
return size;
1002 T *srcT = (T *)data;
1003 T *targetT = (T *)data;
1004 int samples = size /
sizeof(T);
1005 assert(samples % channels == 0);
1006 for (
int j = 0; j < samples; j += channels) {
1008 for (
int ch = 0; ch < channels; ch++) {
1009 sum += srcT[j + ch];
1011 T avg = sum / channels;
1012 for (
int ch = 0; ch < channels; ch++) {
1013 targetT[j + ch] = avg;
1039 ChannelAvg(
int bitsPerSample) { setBits(bitsPerSample); }
1040 void setBits(
int bits) { this->bits = bits; }
1042 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1043 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1047 return ca8.convert(target, src, size);
1051 return ca16.convert(target, src, size);
1055 return ca24.convert(target, src, size);
1059 return ca32.convert(target, src, size);
1062 LOGE(
"Number of bits %d not supported.", bits);
1085template <
typename T =
int16_t>
1090 setChannels(channels);
1091 setBinSize(binSize);
1092 setAverage(average);
1093 this->partialBinSize = 0;
1097 this->partialBin =
new T[channels]();
1102 void setChannels(
int channels) {
1103 if ((channels % 2) > 0) {
1104 LOGE(
"Number of channels needs to be even");
1105 this->channels = channels + 1;
1107 this->channels = channels;
1109 void setBinSize(
int binSize) { this->binSize = binSize; }
1110 void setAverage(
bool average) { this->average = average; }
1112 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1114 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1118 if (size % (
sizeof(T) * channels) > 0) {
1119 LOGE(
"Buffer size needs to be multiple of channels")
1124 size / (
sizeof(T) * channels);
1129 int bin_count = total_samples / binSize;
1130 int remaining_samples =
1131 total_samples % binSize;
1132 T *p_target = (T *)target;
1133 T *p_source = (T *)src;
1134 size_t result_size = 0;
1137 typename AppropriateSumType<T>::type sums[channels];
1138 int current_sample = 0;
1142 if (partialBinSize > 0) {
1145 int samples_needed = binSize - partialBinSize;
1146 bool have_enough_samples = (samples_needed < sample_count);
1147 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
1150 for (
int ch = 0; ch < channels; ch++) {
1151 sums[ch] = partialBin[ch];
1155 for (
int j = 0; j < samples_to_bin; j++) {
1156 for (
int ch = 0; ch < channels; ch++) {
1157 sums[ch] += p_source[current_sample * channels + ch];
1163 if (have_enough_samples) {
1166 for (
int ch = 0; ch < channels; ch += 2) {
1167 p_target[result_size /
sizeof(T)] =
1168 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1169 result_size +=
sizeof(T);
1172 for (
int ch = 0; ch < channels; ch += 2) {
1173 p_target[result_size /
sizeof(T)] =
1174 static_cast<T
>((sums[ch] - sums[ch + 1]));
1175 result_size +=
sizeof(T);
1183 for (
int ch = 0; ch < channels; ch++) {
1184 partialBin[ch] = sums[ch];
1186 partialBinSize += current_sample;
1188 "bin & channel subtract %d: %d of %d remaining %d samples, %d "
1191 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1201 for (
int i = 0; i < bin_count; i++) {
1204 for (
int ch = 0; ch < channels; ch++) {
1205 sums[ch] = p_source[current_sample * channels +
1210 for (
int j = 1; j < binSize; j++) {
1211 for (
int ch = 0; ch < channels; ch++) {
1212 sums[ch] += p_source[(current_sample + j) * channels + ch];
1215 current_sample += binSize;
1219 for (
int ch = 0; ch < channels; ch += 2) {
1220 p_target[result_size /
sizeof(T)] =
1221 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1222 result_size +=
sizeof(T);
1225 for (
int ch = 0; ch < channels; ch += 2) {
1226 p_target[result_size /
sizeof(T)] =
1227 static_cast<T
>((sums[ch] - sums[ch + 1]));
1228 result_size +=
sizeof(T);
1236 for (
int i = 0; i < remaining_samples; i++) {
1237 for (
int ch = 0; ch < channels; ch++) {
1238 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
1241 partialBinSize = remaining_samples;
1244 "bin & channel subtract %d: %d of %d remaining %d samples, %d > %d "
1246 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1255 bool average =
true;
1270 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1271 setChannels(channels);
1272 setBinSize(binSize);
1273 setAverage(average);
1274 setBits(bits_per_sample);
1277 void setChannels(
int channels) {
1278 if ((channels % 2) == 0) {
1279 this->channels = channels;
1281 LOGE(
"Number of channels needs to be even");
1282 this->channels = channels + 1;
1286 void setBits(
int bits) { this->bits = bits; }
1287 void setBinSize(
int binSize) { this->binSize = binSize; }
1288 void setAverage(
bool average) { this->average = average; }
1290 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1291 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1295 return bd8.convert(target, src, size);
1299 return bd16.convert(target, src, size);
1303 return bd24.convert(target, src, size);
1307 return bd32.convert(target, src, size);
1310 LOGE(
"Number of bits %d not supported.", bits);
1320 bool average =
true;
1328template <
typename T =
int16_t>
1334 from_channels = channelCountOfSource;
1335 to_channels = channelCountOfTarget;
1338 void setSourceChannels(
int channelCountOfSource) {
1339 from_channels = channelCountOfSource;
1342 void setTargetChannels(
int channelCountOfTarget) {
1343 to_channels = channelCountOfTarget;
1346 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1347 if (from_channels == 0)
return size;
1348 int frame_count = size / (
sizeof(T) * from_channels);
1349 size_t result_size = 0;
1350 T *result = (T *)target;
1351 T *source = (T *)src;
1352 T value = (int16_t)0;
1353 for (
int i = 0; i < frame_count; i++) {
1355 for (
int j = 0; j < from_channels; j++) {
1358 result_size +=
sizeof(T);
1361 for (
int j = from_channels; j < to_channels; j++) {
1363 result_size +=
sizeof(T);
1371 return inSize * to_channels / from_channels;
1375 int from_channels = 0;
1376 int to_channels = 0;
1384template <
typename T =
int16_t>
1390 from_channels = channelCountOfSource;
1391 to_channels = channelCountOfTarget;
1394 void setSourceChannels(
int channelCountOfSource) {
1395 from_channels = channelCountOfSource;
1398 void setTargetChannels(
int channelCountOfTarget) {
1399 to_channels = channelCountOfTarget;
1402 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1403 if (from_channels == to_channels) {
1404 memcpy(target, src, size);
1408 if (from_channels > to_channels) {
1409 reducer.setSourceChannels(from_channels);
1410 reducer.setTargetChannels(to_channels);
1412 enhancer.setSourceChannels(from_channels);
1413 enhancer.setTargetChannels(to_channels);
1417 if (from_channels > to_channels) {
1418 return reducer.convert(target, src, size);
1420 return enhancer.convert(target, src, size);
1436template <
typename T =
int16_t>
1455 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1458 size_t convert(uint8_t *src,
size_t size) {
1459 for (
int i = 0; i < converters.size(); i++) {
1460 converters[i]->convert(src, size);
1479 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1480 bool result_bool =
false;
1481 int len = inBits / 8 * n;
1482 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1483 uint8_t buffer[len];
1484 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1486 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1492 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1494 bool result_bool =
false;
1497 int8_t *buffer = (int8_t *)bufferIn;
1498 for (
int j = 0; j < n; j++) {
1499 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1504 int16_t *buffer = (int16_t *)bufferIn;
1505 for (
int j = 0; j < n; j++) {
1506 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1511 int32_t *buffer = (int32_t *)bufferIn;
1512 for (
int j = 0; j < n; j++) {
1513 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1522 Stream *stream_ptr =
nullptr;
1525 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1526 int32_t result =
static_cast<float>(value) /
1542template <
typename T =
int16_t>
1547 size_t convert(uint8_t *src,
size_t size)
override {
1549 for (
size_t j = 0; j < size; j++) {
1550 data[j] = p_filter->process(data[j]);
1565template <
typename T,
typename FT>
1570 this->channels = channels;
1573 for (
int j = 0; j < channels; j++) {
1574 filters[j] =
nullptr;
1580 for (
int j = 0; j < channels; j++) {
1581 if (filters[j] !=
nullptr) {
1591 if (channel < channels) {
1592 if (filters[channel] !=
nullptr) {
1593 delete filters[channel];
1595 filters[channel] = filter;
1597 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1603 size_t convert(uint8_t *src,
size_t size) {
1604 int count = size / channels /
sizeof(T);
1605 T *sample = (T *)src;
1606 for (
size_t j = 0; j < count; j++) {
1607 for (
int channel = 0; channel < channels; channel++) {
1608 if (filters[channel] !=
nullptr) {
1609 *sample = filters[channel]->process(*sample);
1617 int getChannels() {
return channels; }
1620 Filter<FT> **filters =
nullptr;
1633template <
typename T =
int16_t>
1637 set(n, aplidudeLimit);
1640 virtual size_t convert(uint8_t *data,
size_t size)
override {
1645 size_t sample_count = size /
sizeof(T);
1646 size_t write_count = 0;
1647 T *audio = (T *)data;
1650 T *p_buffer = (T *)data;
1651 for (
int j = 0; j < sample_count; j++) {
1652 int pos = findLastAudioPos(audio, j);
1655 *p_buffer++ = audio[j];
1660 size_t write_size = write_count *
sizeof(T);
1661 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1664 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1671 bool active =
false;
1672 const uint8_t *buffer =
nullptr;
1674 int priorLastAudioPos = 0;
1675 int amplidude_limit = 0;
1677 void set(
int n = 5,
int aplidudeLimit = 2) {
1678 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1680 this->amplidude_limit = aplidudeLimit;
1681 this->priorLastAudioPos = n + 1;
1682 this->active = n > 0;
1686 int findLastAudioPos(T *audio,
int pos) {
1687 for (
int j = 0; j < n; j++) {
1690 return priorLastAudioPos;
1693 if (abs(audio[pos - j]) > amplidude_limit) {
1708template <
typename T =
int16_t>
1712 this->channels = channels;
1713 from_beginning = fromBeginning;
1716 virtual size_t convert(uint8_t *src,
size_t size) {
1717 for (
int ch = 0; ch < channels; ch++) {
1719 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1721 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1727 bool from_beginning;
1731 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1733 T first = values[channel];
1734 for (
int j = 0; j < sampleCount; j += channels) {
1736 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1745 void clearAfterLastTransition(
int channels,
int channel, T *values,
1747 int lastPos = sampleCount - channels + channel;
1748 T last = values[lastPos];
1749 for (
int j = lastPos; j >= 0; j -= channels) {
1751 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1767template <
typename T =
int16_t>
1772 this->channels = channels;
1774 from_beginning = fromBeginning;
1777 virtual size_t convert(uint8_t *src,
size_t size) {
1778 for (
int ch = 0; ch < channels; ch++) {
1780 processStart(channels, ch, (T *)src, size /
sizeof(T));
1781 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1787 bool from_beginning;
1793 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1794 for (
int j = 0; j < sampleCount; j += channels) {
1795 if (factor >= 0.8) {
1798 values[j] = factor * values[j];
1804 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1805 int lastPos = sampleCount - channels + channel;
1806 for (
int j = lastPos; j >= 0; j -= channels) {
1807 if (factor >= 0.8) {
1810 values[j] = factor * values[j];
1822template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1825 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1827 size_t convert(uint8_t *src,
size_t size) {
1829 size_t samples = (size / Cn) /
sizeof(T);
1830 for (
size_t s = 0; s < samples; s++) {
1831 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1833 for (
size_t c = 0; c < Cn; c++) {
1835 chan[s * Cn + c] = chan[s * Cn + Cx];
1839 if (_max_val < chan[s * Cn]) {
1840 _max_val = chan[s * Cn];
1845 if (now - _prev_ms > 1000) {
1847 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1851 return samples * Cn *
sizeof(T);
1865template <
typename T =
int16_t>
1869 this->callback = callback;
1870 this->channels = channels;
1873 size_t convert(uint8_t *src,
size_t size) {
1874 int samples = size /
sizeof(T);
1876 for (
int j = 0; j < samples; j++) {
1877 srcT[j] = callback(srcT[j], j % channels);
1883 T (*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:286