2 #include "AudioTools/CoreAudio/AudioBasic/Collections.h"
3 #include "AudioFilter/Filter.h"
4 #include "AudioTypes.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; };
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; }
105 template <
typename 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);
197 if (p_converter !=
nullptr) {
199 p_converter =
nullptr;
203 void begin(
int channels,
int bitsPerSample,
bool isDynamic =
false) {
204 this->channels = channels;
205 this->bits_per_sample = bitsPerSample;
206 if (p_converter !=
nullptr)
delete p_converter;
207 switch (bits_per_sample) {
227 size_t convert(uint8_t *src,
size_t size)
override {
228 if (p_converter ==
nullptr)
return 0;
229 return p_converter->convert(src, size);
246 template <
typename T>
251 size_t convert(uint8_t *src,
size_t byte_count)
override {
253 int size = byte_count / channels /
sizeof(T);
254 T *sample = (T *)src;
255 for (
size_t j = 0; j < size; j++) {
257 *sample = *(sample + 1);
258 *(sample + 1) = temp;
284 template <
typename T>
289 this->channels = channels;
307 size_t convert(uint8_t *src,
size_t byte_count) {
309 int size = byte_count / channels /
sizeof(T);
310 setup((T *)src, size);
311 if (left_empty && !right_empty) {
312 T *sample = (T *)src;
313 for (
size_t j = 0; j < size; j++) {
314 *sample = *(sample + 1);
317 }
else if (!left_empty && right_empty) {
318 T *sample = (T *)src;
319 for (
size_t j = 0; j < size; j++) {
320 *(sample + 1) = *sample;
329 bool is_setup =
false;
330 bool left_empty =
true;
331 bool right_empty =
true;
334 void setup(T *src,
size_t size) {
336 for (
int j = 0; j < size; j++) {
343 for (
int j = 0; j < size - 1; j++) {
351 if (!right_empty || !left_empty) {
367 template <
typename T>
372 size_t convert(uint8_t *src,
size_t byte_count)
override {
373 int size = byte_count / channels /
sizeof(T);
374 T *sample = (T *)src;
375 for (
int i = 0; i < size; i++) {
376 for (
int j = 0; j < channels; j++) {
377 *sample = *sample + 0x8000;
395 template <
typename T>
401 from_channels = channelCountOfSource;
402 to_channels = channelCountOfTarget;
405 void setSourceChannels(
int channelCountOfSource) {
406 from_channels = channelCountOfSource;
409 void setTargetChannels(
int channelCountOfTarget) {
410 to_channels = channelCountOfTarget;
413 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
415 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
416 LOGD(
"convert %d -> %d", from_channels, to_channels);
417 assert(to_channels <= from_channels);
418 int frame_count = size / (
sizeof(T) * from_channels);
419 size_t result_size = 0;
420 T *result = (T *)target;
421 T *source = (T *)src;
422 int reduceDiv = from_channels - to_channels + 1;
424 for (
int i = 0; i < frame_count; i++) {
426 for (
int j = 0; j < to_channels - 1; j++) {
427 *result++ = *source++;
428 result_size +=
sizeof(T);
431 T total = (int16_t)0;
432 for (
int j = to_channels - 1; j < from_channels; j++) {
433 total += *source++ / reduceDiv;
436 result_size +=
sizeof(T);
454 ChannelReducer(
int channelCountOfTarget,
int channelCountOfSource,
456 from_channels = channelCountOfSource;
457 to_channels = channelCountOfTarget;
458 bits = bitsPerSample;
461 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
463 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
467 return cr8.convert(target, src, size);
471 return cr16.convert(target, src, size);
475 return cr24.convert(target, src, size);
479 return cr32.convert(target, src, size);
495 template <
typename T>
510 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
512 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
514 if (size % (
sizeof(T) * channels) > 0) {
515 LOGE(
"Buffer size %d is not a multiple of the number of channels %d", (
int)size, channels);
519 int frame_count = size / (
sizeof(T) * channels);
520 T *p_target = (T *)target;
521 T *p_source = (T *)src;
522 size_t result_size = 0;
524 for (
int i = 0; i < frame_count; i++) {
525 if (++count == factor) {
528 for (
int ch = 0; ch < channels; ch++) {
529 *p_target++ = p_source[i * channels + ch];
530 result_size +=
sizeof(T);
535 LOGD(
"decimate %d: %d -> %d bytes",factor, (
int)size, (
int)result_size);
539 operator bool() {
return factor > 1; }
555 Decimate(
int factor,
int channels,
int bits_per_sample) {
558 setBits(bits_per_sample);
562 void setBits(
int bits) { this->bits = bits; }
566 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
567 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
570 DecimateT<int8_t> dec8(factor, channels);
571 return dec8.convert(target, src, size);
574 DecimateT<int16_t> dec16(factor, channels);
575 return dec16.convert(target, src, size);
578 DecimateT<int24_t> dec24(factor, channels);
579 return dec24.convert(target, src, size);
582 DecimateT<int32_t> dec32(factor, channels);
583 return dec32.convert(target, src, size);
589 operator bool() {
return factor > 1; };
608 template <
typename T>
615 using type = int16_t;
620 using type = int32_t;
625 using type = int32_t;
630 using type = int64_t;
633 template <
typename T>
637 BinT(
int binSize,
int channels,
bool average) {
638 setChannels(channels);
641 this->partialBinSize = 0;
644 this->partialBin =
new T[channels]();
648 delete[] this->partialBin;
651 void setChannels(
int channels) { this->channels = channels; }
652 void setBinSize(
int binSize) { this->binSize = binSize; }
653 void setAverage(
bool average) { this->average = average; }
655 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
657 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
664 if (size % (
sizeof(T) * channels) > 0) {
665 LOGE(
"Buffer size %d is not a multiple of the number of channels %d", (
int)size, channels);
669 int sample_count = size / (
sizeof(T) * channels);
670 int total_samples = partialBinSize + sample_count;
671 int bin_count = total_samples / binSize;
672 int remaining_samples = total_samples % binSize;
673 T *p_target = (T *)target;
674 T *p_source = (T *)src;
675 size_t result_size = 0;
678 typename AppropriateSumType<T>::type sums[channels];
679 int current_sample = 0;
683 if (partialBinSize > 0) {
685 int samples_needed = binSize - partialBinSize;
686 bool have_enough_samples = (samples_needed < sample_count);
687 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
689 for (
int ch = 0; ch < channels; ch++) {
690 sums[ch] = partialBin[ch];
693 for (
int j = 0; j < samples_to_bin; j++) {
694 for (
int ch = 0; ch < channels; ch++) {
695 sums[ch] += p_source[current_sample * channels + ch];
700 if (have_enough_samples) {
703 for (
int ch = 0; ch < channels; ch++) {
704 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch] / binSize);
705 result_size +=
sizeof(T);
708 for (
int ch = 0; ch < channels; ch++) {
709 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
710 result_size +=
sizeof(T);
716 for (
int ch = 0; ch < channels; ch++) {
717 partialBin[ch] = sums[ch];
719 partialBinSize += current_sample;
721 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize, current_sample, total_samples, partialBinSize, (
int)size, (
int)result_size);
728 for (
int i = 0; i < bin_count; i++) {
729 for (
int ch = 0; ch < channels; ch++) {
730 sums[ch] = p_source[current_sample * channels + ch];
733 for (
int j = 1; j < binSize; j++) {
734 for (
int ch = 0; ch < channels; ch++) {
735 sums[ch] += p_source[(current_sample + j) * channels + ch];
738 current_sample += binSize;
742 for (
int ch = 0; ch < channels; ch++) {
743 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch] / binSize);
744 result_size +=
sizeof(T);
747 for (
int ch = 0; ch < channels; ch++) {
748 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
749 result_size +=
sizeof(T);
756 for (
int i = 0; i < remaining_samples; i++) {
757 for (
int ch = 0; ch < channels; ch++) {
758 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
761 partialBinSize = remaining_samples;
763 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize, current_sample, total_samples, partialBinSize, (
int) size, (
int) result_size);
784 Bin(
int binSize,
int channels,
bool average,
int bits_per_sample) {
785 setChannels(channels);
788 setBits(bits_per_sample);
791 void setChannels(
int channels) { this->channels = channels; }
792 void setBits(
int bits) { this->bits = bits; }
793 void setBinSize(
int binSize) { this->binSize = binSize; }
794 void setAverage(
bool average) { this->average = average; }
796 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
797 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
801 return bin8.convert(target, src, size);
805 return bin16.convert(target, src, size);
809 return bin24.convert(target, src, size);
813 return bin32.convert(target, src, size);
816 LOGE(
"Number of bits %d not supported.", bits);
826 bool average =
false;
843 template <
typename T>
848 size_t convert(uint8_t *src,
size_t size)
override {
return convert(src, src, size); }
850 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
851 LOGD(
"channel subtract %d samples, %d bytes", (
int) (size /
sizeof(T)), (
int) size);
854 if (size % (
sizeof(T) * 2) > 0) {
855 LOGE(
"Buffer size is not even");
859 int sample_count = size / (
sizeof(T) * 2);
860 T *p_result = (T *)target;
861 T *p_source = (T *)src;
863 for (
int i = 0; i < sample_count; i++) {
865 auto tmp = *p_source++;
870 return sizeof(T) * sample_count;
878 setBits(bitsPerSample);
880 void setBits(
int bits) { this->bits = bits; }
882 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
883 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
887 return cd8.convert(target, src, size);
891 return cd16.convert(target, src, size);
895 return cd24.convert(target, src, size);
899 return cd32.convert(target, src, size);
902 LOGE(
"Number of bits %d not supported.", bits);
925 template <
typename T>
930 size_t convert(uint8_t *src,
size_t size)
override {
return convert(src, src, size); }
932 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
934 if (size % (
sizeof(T) * 2) > 0) {
935 LOGE(
"Buffer size is not even");
939 int sample_count = size / (
sizeof(T) * 2);
940 T *p_result = (T *)target;
941 T *p_source = (T *)src;
943 for (
int i = 0; i < sample_count; i++) {
945 auto tmp = *p_source++;
947 *p_result++ = tmp / 2;
950 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
952 return sizeof(T) * sample_count;
960 setBits(bitsPerSample);
962 void setBits(
int bits) { this->bits = bits; }
964 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
965 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
969 return ca8.convert(target, src, size);
973 return ca16.convert(target, src, size);
977 return ca24.convert(target, src, size);
981 return ca32.convert(target, src, size);
984 LOGE(
"Number of bits %d not supported.", bits);
1008 template <
typename T>
1013 setChannels(channels);
1014 setBinSize(binSize);
1015 setAverage(average);
1016 this->partialBinSize = 0;
1019 this->partialBin =
new T[channels]();
1023 delete[] this->partialBin;
1026 void setChannels(
int channels) {
1027 if ((channels % 2) > 0) {
1028 LOGE(
"Number of channels needs to be even");
1029 this->channels = channels+1;
1031 this->channels = channels;
1033 void setBinSize(
int binSize) { this->binSize = binSize; }
1034 void setAverage(
bool average) { this->average = average; }
1036 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1038 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1042 if (size % (
sizeof(T) * channels) > 0) {
1043 LOGE(
"Buffer size needs to be multiple of channels")
1047 int sample_count = size / (
sizeof(T) * channels);
1048 int total_samples = partialBinSize + sample_count;
1049 int bin_count = total_samples / binSize;
1050 int remaining_samples = total_samples % binSize;
1051 T *p_target = (T *)target;
1052 T *p_source = (T *)src;
1053 size_t result_size = 0;
1056 typename AppropriateSumType<T>::type sums[channels];
1057 int current_sample = 0;
1061 if (partialBinSize > 0) {
1065 int samples_needed = binSize - partialBinSize;
1066 bool have_enough_samples = (samples_needed < sample_count);
1067 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
1070 for (
int ch = 0; ch < channels; ch++) {
1071 sums[ch] = partialBin[ch];
1075 for (
int j = 0; j < samples_to_bin; j++) {
1076 for (
int ch = 0; ch < channels; ch++) {
1077 sums[ch] += p_source[current_sample * channels + ch];
1083 if (have_enough_samples) {
1086 for (
int ch = 0; ch < channels; ch+=2) {
1087 p_target[result_size /
sizeof(T)] =
static_cast<T
>((sums[ch] - sums[ch+1]) / binSize);
1088 result_size +=
sizeof(T);
1091 for (
int ch = 0; ch < channels; ch+=2) {
1092 p_target[result_size /
sizeof(T)] =
static_cast<T
>((sums[ch] - sums[ch+1]));
1093 result_size +=
sizeof(T);
1101 for (
int ch = 0; ch < channels; ch++) {
1102 partialBin[ch] = sums[ch];
1104 partialBinSize += current_sample;
1105 LOGD(
"bin & channel subtract %d: %d of %d remaining %d samples, %d > %d bytes", binSize, current_sample, total_samples, partialBinSize,(
int) size,(
int) result_size);
1114 for (
int i = 0; i < bin_count; i++) {
1118 for (
int ch = 0; ch < channels; ch++) {
1119 sums[ch] = p_source[current_sample * channels + ch];
1122 for (
int j = 1; j < binSize; j++) {
1123 for (
int ch = 0; ch < channels; ch++) {
1124 sums[ch] += p_source[(current_sample + j) * channels + ch];
1127 current_sample += binSize;
1131 for (
int ch = 0; ch < channels; ch+=2) {
1132 p_target[result_size /
sizeof(T)] =
static_cast<T
>((sums[ch]-sums[ch+1]) / binSize);
1133 result_size +=
sizeof(T);
1136 for (
int ch = 0; ch < channels; ch+=2) {
1137 p_target[result_size /
sizeof(T)] =
static_cast<T
>((sums[ch]-sums[ch+1]));
1138 result_size +=
sizeof(T);
1146 for (
int i = 0; i < remaining_samples; i++) {
1147 for (
int ch = 0; ch < channels; ch++) {
1148 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
1151 partialBinSize = remaining_samples;
1153 LOGD(
"bin & channel subtract %d: %d of %d remaining %d samples, %d > %d bytes", binSize, current_sample, total_samples, partialBinSize, (
int)size, (
int) result_size);
1161 bool average =
true;
1176 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1177 setChannels(channels);
1178 setBinSize(binSize);
1179 setAverage(average);
1180 setBits(bits_per_sample);
1183 void setChannels(
int channels) {
1184 if ((channels % 2) == 0) {
1185 this->channels = channels;
1187 LOGE(
"Number of channels needs to be even");
1188 this->channels = channels+1;
1192 void setBits(
int bits) { this->bits = bits; }
1193 void setBinSize(
int binSize) { this->binSize = binSize; }
1194 void setAverage(
bool average) { this->average = average; }
1196 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1197 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1201 return bd8.convert(target, src, size);
1205 return bd16.convert(target, src, size);
1209 return bd24.convert(target, src, size);
1213 return bd32.convert(target, src, size);
1216 LOGE(
"Number of bits %d not supported.", bits);
1226 bool average =
true;
1235 template <
typename T>
1241 from_channels = channelCountOfSource;
1242 to_channels = channelCountOfTarget;
1245 void setSourceChannels(
int channelCountOfSource) {
1246 from_channels = channelCountOfSource;
1249 void setTargetChannels(
int channelCountOfTarget) {
1250 to_channels = channelCountOfTarget;
1253 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1254 int frame_count = size / (
sizeof(T) * from_channels);
1255 size_t result_size = 0;
1256 T *result = (T *)target;
1257 T *source = (T *)src;
1258 T value = (int16_t)0;
1259 for (
int i = 0; i < frame_count; i++) {
1261 for (
int j = 0; j < from_channels; j++) {
1264 result_size +=
sizeof(T);
1267 for (
int j = from_channels; j < to_channels; j++) {
1269 result_size +=
sizeof(T);
1277 return inSize * to_channels / from_channels;
1290 template <
typename T>
1296 from_channels = channelCountOfSource;
1297 to_channels = channelCountOfTarget;
1300 void setSourceChannels(
int channelCountOfSource) {
1301 from_channels = channelCountOfSource;
1304 void setTargetChannels(
int channelCountOfTarget) {
1305 to_channels = channelCountOfTarget;
1308 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1309 if (from_channels == to_channels) {
1310 memcpy(target, src, size);
1314 if (from_channels > to_channels) {
1315 reducer.setSourceChannels(from_channels);
1316 reducer.setTargetChannels(to_channels);
1318 enhancer.setSourceChannels(from_channels);
1319 enhancer.setTargetChannels(to_channels);
1323 if (from_channels > to_channels) {
1324 return reducer.convert(target, src, size);
1326 return enhancer.convert(target, src, size);
1342 template <
typename T>
1361 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1364 size_t convert(uint8_t *src,
size_t size) {
1365 for (
int i = 0; i < converters.size(); i++) {
1366 converters[i]->convert(src, size);
1450 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1451 bool result_bool =
false;
1452 int len = inBits / 8 * n;
1453 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1454 uint8_t buffer[len];
1455 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1457 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1463 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1465 bool result_bool =
false;
1468 int8_t *buffer = (int8_t *)bufferIn;
1469 for (
int j = 0; j < n; j++) {
1470 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1475 int16_t *buffer = (int16_t *)bufferIn;
1476 for (
int j = 0; j < n; j++) {
1477 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1482 int32_t *buffer = (int32_t *)bufferIn;
1483 for (
int j = 0; j < n; j++) {
1484 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1493 Stream *stream_ptr =
nullptr;
1496 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1497 int32_t result =
static_cast<float>(value) /
1513 template <
typename T>
1518 size_t convert(uint8_t *src,
size_t size)
override {
1520 for (
size_t j = 0; j < size; j++) {
1521 data[j] = p_filter->process(data[j]);
1536 template <
typename T,
typename FT>
1541 this->channels = channels;
1544 for (
int j = 0; j < channels; j++) {
1545 filters[j] =
nullptr;
1551 for (
int j = 0; j < channels; j++) {
1552 if (filters[j] !=
nullptr) {
1562 if (channel < channels) {
1563 if (filters[channel] !=
nullptr) {
1564 delete filters[channel];
1566 filters[channel] = filter;
1568 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1574 size_t convert(uint8_t *src,
size_t size) {
1575 int count = size / channels /
sizeof(T);
1576 T *sample = (T *)src;
1577 for (
size_t j = 0; j < count; j++) {
1578 for (
int channel = 0; channel < channels; channel++) {
1579 if (filters[channel] !=
nullptr) {
1580 *sample = filters[channel]->process(*sample);
1588 int getChannels() {
return channels; }
1591 Filter<FT> **filters =
nullptr;
1603 template <
typename T>
1607 set(n, aplidudeLimit);
1610 virtual size_t convert(uint8_t *data,
size_t size)
override {
1615 size_t sample_count = size /
sizeof(T);
1616 size_t write_count = 0;
1617 T *audio = (T *)data;
1620 T *p_buffer = (T *)data;
1621 for (
int j = 0; j < sample_count; j++) {
1622 int pos = findLastAudioPos(audio, j);
1625 *p_buffer++ = audio[j];
1630 size_t write_size = write_count *
sizeof(T);
1631 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1634 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1641 bool active =
false;
1642 const uint8_t *buffer =
nullptr;
1644 int priorLastAudioPos = 0;
1645 int amplidude_limit = 0;
1647 void set(
int n = 5,
int aplidudeLimit = 2) {
1648 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1650 this->amplidude_limit = aplidudeLimit;
1651 this->priorLastAudioPos = n + 1;
1652 this->active = n > 0;
1656 int findLastAudioPos(T *audio,
int pos) {
1657 for (
int j = 0; j < n; j++) {
1660 return priorLastAudioPos;
1663 if (abs(audio[pos - j]) > amplidude_limit) {
1678 template <
typename T>
1682 this->channels = channels;
1683 from_beginning = fromBeginning;
1686 virtual size_t convert(uint8_t *src,
size_t size) {
1687 for (
int ch = 0; ch < channels; ch++) {
1689 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1691 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1697 bool from_beginning;
1701 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1703 T first = values[channel];
1704 for (
int j = 0; j < sampleCount; j += channels) {
1706 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1715 void clearAfterLastTransition(
int channels,
int channel, T *values,
1717 int lastPos = sampleCount - channels + channel;
1718 T last = values[lastPos];
1719 for (
int j = lastPos; j >= 0; j -= channels) {
1721 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1737 template <
typename T>
1742 this->channels = channels;
1744 from_beginning = fromBeginning;
1747 virtual size_t convert(uint8_t *src,
size_t size) {
1748 for (
int ch = 0; ch < channels; ch++) {
1750 processStart(channels, ch, (T *)src, size /
sizeof(T));
1751 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1757 bool from_beginning;
1763 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1764 for (
int j = 0; j < sampleCount; j += channels) {
1765 if (factor >= 0.8) {
1768 values[j] = factor * values[j];
1774 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1775 int lastPos = sampleCount - channels + channel;
1776 for (
int j = lastPos; j >= 0; j -= channels) {
1777 if (factor >= 0.8) {
1780 values[j] = factor * values[j];
1791 template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1794 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1796 size_t convert(uint8_t *src,
size_t size) {
1798 size_t samples = (size / Cn) /
sizeof(T);
1799 for (
size_t s = 0; s < samples; s++) {
1800 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1802 for (
size_t c = 0; c < Cn; c++) {
1804 chan[s * Cn + c] = chan[s * Cn + Cx];
1808 if (_max_val < chan[s * Cn]) {
1809 _max_val = chan[s * Cn];
1814 if (now - _prev_ms > 1000) {
1816 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1820 return samples * Cn *
sizeof(T);
1834 template <
typename T>
1838 this->callback = callback;
1839 this->channels = channels;
1842 size_t convert(uint8_t *src,
size_t size) {
1843 int samples = size /
sizeof(T);
1844 for (
int j = 0; j < samples; j++) {
1845 src[j] = callback(src[j], j % channels);
1851 T(*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition: BaseConverter.h:273