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);
949template <
typename T =
int16_t,
typename SumT =
float>
952 ChannelMixer(
int channels = 2) { this->channels = channels; }
953 size_t convert(uint8_t *data,
size_t size) {
954 if (channels <= 1)
return size;
956 T *targetT = (T *)data;
957 int samples = size /
sizeof(T);
958 assert(samples % channels == 0);
959 for (
int j = 0; j < samples; j += channels) {
961 for (
int ch = 0; ch < channels; ch++) {
964 T avg = sum / channels;
965 for (
int ch = 0; ch < channels; ch++) {
966 targetT[j + ch] = avg;
989template <
typename T =
int16_t,
typename AvgT =
float>
994 size_t convert(uint8_t *src,
size_t size)
override {
995 return convert(src, src, size);
998 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
999 if (size % (
sizeof(T) * 2) > 0) {
1000 LOGE(
"Buffer size is not even");
1007 T *p_result = (T *)target;
1008 T *p_source = (T *)src;
1010 for (
int i = 0; i < sample_count; i++) {
1013 AvgT tmp = *p_source++;
1015 *p_result++ = tmp / 2;
1018 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
1020 return sizeof(T) * sample_count;
1041 ChannelAvg(
int bitsPerSample) { setBits(bitsPerSample); }
1042 void setBits(
int bits) { this->bits = bits; }
1044 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1045 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1047#ifdef PREFER_FIXEDPOINT
1050 return ca8.convert(target, src, size);
1054 return ca16.convert(target, src, size);
1058 return ca24.convert(target, src, size);
1063 return ca8.convert(target, src, size);
1067 return ca16.convert(target, src, size);
1071 return ca24.convert(target, src, size);
1076 return ca32.convert(target, src, size);
1079 LOGE(
"Number of bits %d not supported.", bits);
1102template <
typename T =
int16_t>
1107 setChannels(channels);
1108 setBinSize(binSize);
1109 setAverage(average);
1110 this->partialBinSize = 0;
1114 this->partialBin =
new T[channels]();
1119 void setChannels(
int channels) {
1120 if ((channels % 2) > 0) {
1121 LOGE(
"Number of channels needs to be even");
1122 this->channels = channels + 1;
1124 this->channels = channels;
1126 void setBinSize(
int binSize) { this->binSize = binSize; }
1127 void setAverage(
bool average) { this->average = average; }
1129 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1131 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1135 if (size % (
sizeof(T) * channels) > 0) {
1136 LOGE(
"Buffer size needs to be multiple of channels")
1141 size / (
sizeof(T) * channels);
1146 int bin_count = total_samples / binSize;
1147 int remaining_samples =
1148 total_samples % binSize;
1149 T *p_target = (T *)target;
1150 T *p_source = (T *)src;
1151 size_t result_size = 0;
1154 typename AppropriateSumType<T>::type sums[channels];
1155 int current_sample = 0;
1159 if (partialBinSize > 0) {
1162 int samples_needed = binSize - partialBinSize;
1163 bool have_enough_samples = (samples_needed < sample_count);
1164 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
1167 for (
int ch = 0; ch < channels; ch++) {
1168 sums[ch] = partialBin[ch];
1172 for (
int j = 0; j < samples_to_bin; j++) {
1173 for (
int ch = 0; ch < channels; ch++) {
1174 sums[ch] += p_source[current_sample * channels + ch];
1180 if (have_enough_samples) {
1183 for (
int ch = 0; ch < channels; ch += 2) {
1184 p_target[result_size /
sizeof(T)] =
1185 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1186 result_size +=
sizeof(T);
1189 for (
int ch = 0; ch < channels; ch += 2) {
1190 p_target[result_size /
sizeof(T)] =
1191 static_cast<T
>((sums[ch] - sums[ch + 1]));
1192 result_size +=
sizeof(T);
1200 for (
int ch = 0; ch < channels; ch++) {
1201 partialBin[ch] = sums[ch];
1203 partialBinSize += current_sample;
1205 "bin & channel subtract %d: %d of %d remaining %d samples, %d "
1208 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1218 for (
int i = 0; i < bin_count; i++) {
1221 for (
int ch = 0; ch < channels; ch++) {
1222 sums[ch] = p_source[current_sample * channels +
1227 for (
int j = 1; j < binSize; j++) {
1228 for (
int ch = 0; ch < channels; ch++) {
1229 sums[ch] += p_source[(current_sample + j) * channels + ch];
1232 current_sample += binSize;
1236 for (
int ch = 0; ch < channels; ch += 2) {
1237 p_target[result_size /
sizeof(T)] =
1238 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1239 result_size +=
sizeof(T);
1242 for (
int ch = 0; ch < channels; ch += 2) {
1243 p_target[result_size /
sizeof(T)] =
1244 static_cast<T
>((sums[ch] - sums[ch + 1]));
1245 result_size +=
sizeof(T);
1253 for (
int i = 0; i < remaining_samples; i++) {
1254 for (
int ch = 0; ch < channels; ch++) {
1255 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
1258 partialBinSize = remaining_samples;
1261 "bin & channel subtract %d: %d of %d remaining %d samples, %d > %d "
1263 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1272 bool average =
true;
1287 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1288 setChannels(channels);
1289 setBinSize(binSize);
1290 setAverage(average);
1291 setBits(bits_per_sample);
1294 void setChannels(
int channels) {
1295 if ((channels % 2) == 0) {
1296 this->channels = channels;
1298 LOGE(
"Number of channels needs to be even");
1299 this->channels = channels + 1;
1303 void setBits(
int bits) { this->bits = bits; }
1304 void setBinSize(
int binSize) { this->binSize = binSize; }
1305 void setAverage(
bool average) { this->average = average; }
1307 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1308 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1312 return bd8.convert(target, src, size);
1316 return bd16.convert(target, src, size);
1320 return bd24.convert(target, src, size);
1324 return bd32.convert(target, src, size);
1327 LOGE(
"Number of bits %d not supported.", bits);
1337 bool average =
true;
1345template <
typename T =
int16_t>
1351 from_channels = channelCountOfSource;
1352 to_channels = channelCountOfTarget;
1355 void setSourceChannels(
int channelCountOfSource) {
1356 from_channels = channelCountOfSource;
1359 void setTargetChannels(
int channelCountOfTarget) {
1360 to_channels = channelCountOfTarget;
1363 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1364 if (from_channels == 0)
return size;
1365 int frame_count = size / (
sizeof(T) * from_channels);
1366 size_t result_size = 0;
1367 T *result = (T *)target;
1368 T *source = (T *)src;
1369 T value = (int16_t)0;
1370 for (
int i = 0; i < frame_count; i++) {
1372 for (
int j = 0; j < from_channels; j++) {
1375 result_size +=
sizeof(T);
1378 for (
int j = from_channels; j < to_channels; j++) {
1380 result_size +=
sizeof(T);
1388 return inSize * to_channels / from_channels;
1392 int from_channels = 0;
1393 int to_channels = 0;
1401template <
typename T =
int16_t>
1407 from_channels = channelCountOfSource;
1408 to_channels = channelCountOfTarget;
1411 void setSourceChannels(
int channelCountOfSource) {
1412 from_channels = channelCountOfSource;
1415 void setTargetChannels(
int channelCountOfTarget) {
1416 to_channels = channelCountOfTarget;
1419 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1420 if (from_channels == to_channels) {
1421 memcpy(target, src, size);
1425 if (from_channels > to_channels) {
1426 reducer.setSourceChannels(from_channels);
1427 reducer.setTargetChannels(to_channels);
1429 enhancer.setSourceChannels(from_channels);
1430 enhancer.setTargetChannels(to_channels);
1434 if (from_channels > to_channels) {
1435 return reducer.convert(target, src, size);
1437 return enhancer.convert(target, src, size);
1453template <
typename T =
int16_t>
1472 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1475 size_t convert(uint8_t *src,
size_t size) {
1476 for (
int i = 0; i < converters.size(); i++) {
1477 converters[i]->convert(src, size);
1496 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1497 bool result_bool =
false;
1498 int len = inBits / 8 * n;
1499 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1500 uint8_t buffer[len];
1501 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1503 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1509 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1511 bool result_bool =
false;
1514 int8_t *buffer = (int8_t *)bufferIn;
1515 for (
int j = 0; j < n; j++) {
1516 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1521 int16_t *buffer = (int16_t *)bufferIn;
1522 for (
int j = 0; j < n; j++) {
1523 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1528 int32_t *buffer = (int32_t *)bufferIn;
1529 for (
int j = 0; j < n; j++) {
1530 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1539 Stream *stream_ptr =
nullptr;
1542 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1543 int32_t result =
static_cast<float>(value) /
1559template <
typename T =
int16_t>
1564 size_t convert(uint8_t *src,
size_t size)
override {
1566 for (
size_t j = 0; j < size; j++) {
1567 data[j] = p_filter->process(data[j]);
1582template <
typename T,
typename FT>
1587 this->channels = channels;
1590 for (
int j = 0; j < channels; j++) {
1591 filters[j] =
nullptr;
1597 for (
int j = 0; j < channels; j++) {
1598 if (filters[j] !=
nullptr) {
1608 if (channel < channels) {
1609 if (filters[channel] !=
nullptr) {
1610 delete filters[channel];
1612 filters[channel] = filter;
1614 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1620 size_t convert(uint8_t *src,
size_t size) {
1621 int count = size / channels /
sizeof(T);
1622 T *sample = (T *)src;
1623 for (
size_t j = 0; j < count; j++) {
1624 for (
int channel = 0; channel < channels; channel++) {
1625 if (filters[channel] !=
nullptr) {
1626 *sample = filters[channel]->process(*sample);
1634 int getChannels() {
return channels; }
1637 Filter<FT> **filters =
nullptr;
1650template <
typename T =
int16_t>
1654 set(n, aplidudeLimit);
1657 virtual size_t convert(uint8_t *data,
size_t size)
override {
1662 size_t sample_count = size /
sizeof(T);
1663 size_t write_count = 0;
1664 T *audio = (T *)data;
1667 T *p_buffer = (T *)data;
1668 for (
int j = 0; j < sample_count; j++) {
1669 int pos = findLastAudioPos(audio, j);
1672 *p_buffer++ = audio[j];
1677 size_t write_size = write_count *
sizeof(T);
1678 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1681 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1688 bool active =
false;
1689 const uint8_t *buffer =
nullptr;
1691 int priorLastAudioPos = 0;
1692 int amplidude_limit = 0;
1694 void set(
int n = 5,
int aplidudeLimit = 2) {
1695 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1697 this->amplidude_limit = aplidudeLimit;
1698 this->priorLastAudioPos = n + 1;
1699 this->active = n > 0;
1703 int findLastAudioPos(T *audio,
int pos) {
1704 for (
int j = 0; j < n; j++) {
1707 return priorLastAudioPos;
1710 if (abs(audio[pos - j]) > amplidude_limit) {
1725template <
typename T =
int16_t>
1729 this->channels = channels;
1730 from_beginning = fromBeginning;
1733 virtual size_t convert(uint8_t *src,
size_t size) {
1734 for (
int ch = 0; ch < channels; ch++) {
1736 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1738 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1744 bool from_beginning;
1748 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1750 T first = values[channel];
1751 for (
int j = 0; j < sampleCount; j += channels) {
1753 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1762 void clearAfterLastTransition(
int channels,
int channel, T *values,
1764 int lastPos = sampleCount - channels + channel;
1765 T last = values[lastPos];
1766 for (
int j = lastPos; j >= 0; j -= channels) {
1768 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1784template <
typename T =
int16_t>
1789 this->channels = channels;
1791 from_beginning = fromBeginning;
1794 virtual size_t convert(uint8_t *src,
size_t size) {
1795 for (
int ch = 0; ch < channels; ch++) {
1797 processStart(channels, ch, (T *)src, size /
sizeof(T));
1798 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1804 bool from_beginning;
1810 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1811 for (
int j = 0; j < sampleCount; j += channels) {
1812 if (factor >= 0.8) {
1815 values[j] = factor * values[j];
1821 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1822 int lastPos = sampleCount - channels + channel;
1823 for (
int j = lastPos; j >= 0; j -= channels) {
1824 if (factor >= 0.8) {
1827 values[j] = factor * values[j];
1839template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1842 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1844 size_t convert(uint8_t *src,
size_t size) {
1846 size_t samples = (size / Cn) /
sizeof(T);
1847 for (
size_t s = 0; s < samples; s++) {
1848 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1850 for (
size_t c = 0; c < Cn; c++) {
1852 chan[s * Cn + c] = chan[s * Cn + Cx];
1856 if (_max_val < chan[s * Cn]) {
1857 _max_val = chan[s * Cn];
1862 if (now - _prev_ms > 1000) {
1864 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1868 return samples * Cn *
sizeof(T);
1882template <
typename T =
int16_t>
1886 this->callback = callback;
1887 this->channels = channels;
1890 size_t convert(uint8_t *src,
size_t size) {
1891 int samples = size /
sizeof(T);
1893 for (
int j = 0; j < samples; j++) {
1894 srcT[j] = callback(srcT[j], j % channels);
1900 T (*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:286