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; };
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; }
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 && bitsPerSample == this->bits_per_sample){
214 this->channels = channels;
215 this->bits_per_sample = bitsPerSample;
217 assert(p_converter ==
nullptr);
218 switch (bits_per_sample) {
236 return p_converter !=
nullptr;
239 size_t convert(uint8_t *src,
size_t size)
override {
240 if (p_converter ==
nullptr)
return 0;
241 return p_converter->convert(src, size);
246 int bits_per_sample = 0;
263 size_t convert(uint8_t *src,
size_t byte_count)
override {
265 int size = byte_count / channels /
sizeof(T);
266 T *sample = (T *)src;
267 for (
size_t j = 0; j < size; j++) {
269 *sample = *(sample + 1);
270 *(sample + 1) = temp;
301 this->channels = channels;
319 size_t convert(uint8_t *src,
size_t byte_count) {
321 int size = byte_count / channels /
sizeof(T);
322 setup((T *)src, size);
323 if (left_empty && !right_empty) {
324 T *sample = (T *)src;
325 for (
size_t j = 0; j < size; j++) {
326 *sample = *(sample + 1);
329 }
else if (!left_empty && right_empty) {
330 T *sample = (T *)src;
331 for (
size_t j = 0; j < size; j++) {
332 *(sample + 1) = *sample;
341 bool is_setup =
false;
342 bool left_empty =
true;
343 bool right_empty =
true;
346 void setup(T *src,
size_t size) {
348 for (
int j = 0; j < size; j++) {
355 for (
int j = 0; j < size - 1; j++) {
363 if (!right_empty || !left_empty) {
384 size_t convert(uint8_t *src,
size_t byte_count)
override {
385 int size = byte_count / channels /
sizeof(T);
386 T *sample = (T *)src;
387 for (
int i = 0; i < size; i++) {
388 for (
int j = 0; j < channels; j++) {
389 *sample = *sample + 0x8000;
413 from_channels = channelCountOfSource;
414 to_channels = channelCountOfTarget;
417 void setSourceChannels(
int channelCountOfSource) {
418 from_channels = channelCountOfSource;
421 void setTargetChannels(
int channelCountOfTarget) {
422 to_channels = channelCountOfTarget;
425 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
427 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
428 LOGD(
"convert %d -> %d", from_channels, to_channels);
429 assert(to_channels <= from_channels);
430 int frame_count = size / (
sizeof(T) * from_channels);
431 size_t result_size = 0;
432 T *result = (T *)target;
433 T *source = (T *)src;
434 int reduceDiv = from_channels - to_channels + 1;
436 for (
int i = 0; i < frame_count; i++) {
438 for (
int j = 0; j < to_channels - 1; j++) {
439 *result++ = *source++;
440 result_size +=
sizeof(T);
443 T total = (int16_t)0;
444 for (
int j = to_channels - 1; j < from_channels; j++) {
445 total += *source++ / reduceDiv;
448 result_size +=
sizeof(T);
466 ChannelReducer(
int channelCountOfTarget,
int channelCountOfSource,
468 from_channels = channelCountOfSource;
469 to_channels = channelCountOfTarget;
470 bits = bitsPerSample;
473 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
475 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
479 return cr8.convert(target, src, size);
483 return cr16.convert(target, src, size);
487 return cr24.convert(target, src, size);
491 return cr32.convert(target, src, size);
522 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
524 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
525 if (size % (
sizeof(T) * channels) > 0) {
526 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
527 (
int)size, channels);
531 int frame_count = size / (
sizeof(T) * channels);
532 T *p_target = (T *)target;
533 T *p_source = (T *)src;
534 size_t result_size = 0;
536 for (
int i = 0; i < frame_count; i++) {
537 if (++count == factor) {
540 for (
int ch = 0; ch < channels; ch++) {
541 *p_target++ = p_source[i * channels + ch];
542 result_size +=
sizeof(T);
547 LOGD(
"decimate %d: %d -> %d bytes", factor, (
int)size, (
int)result_size);
551 operator bool() {
return factor > 1; }
568 Decimate(
int factor,
int channels,
int bits_per_sample) {
571 setBits(bits_per_sample);
575 void setBits(
int bits) { this->bits = bits; }
579 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
580 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
583 DecimateT<int8_t> dec8(factor, channels);
584 return dec8.convert(target, src, size);
587 DecimateT<int16_t> dec16(factor, channels);
588 return dec16.convert(target, src, size);
591 DecimateT<int24_t> dec24(factor, channels);
592 return dec24.convert(target, src, size);
595 DecimateT<int32_t> dec32(factor, channels);
596 return dec32.convert(target, src, size);
602 operator bool() {
return factor > 1; };
628 using type = int16_t;
633 using type = int32_t;
638 using type = int32_t;
643 using type = int64_t;
650 BinT(
int binSize,
int channels,
bool average) {
651 setChannels(channels);
654 this->partialBinSize = 0;
655 this->partialBin =
new T[channels]();
658 ~BinT() {
delete[] this->partialBin; }
660 void setChannels(
int channels) { this->channels = channels; }
661 void setBinSize(
int binSize) { this->binSize = binSize; }
662 void setAverage(
bool average) { this->average = average; }
664 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
666 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
675 if (size % (
sizeof(T) * channels) > 0) {
676 LOGE(
"Buffer size %d is not a multiple of the number of channels %d",
677 (
int)size, channels);
682 size / (
sizeof(T) * channels);
687 int bin_count = total_samples / binSize;
688 int remaining_samples =
689 total_samples % binSize;
690 T *p_target = (T *)target;
691 T *p_source = (T *)src;
692 size_t result_size = 0;
695 typename AppropriateSumType<T>::type sums[channels];
696 int current_sample = 0;
700 if (partialBinSize > 0) {
701 int samples_needed = binSize - partialBinSize;
702 bool have_enough_samples = (samples_needed < sample_count);
703 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
705 for (
int ch = 0; ch < channels; ch++) {
706 sums[ch] = partialBin[ch];
709 for (
int j = 0; j < samples_to_bin; j++) {
710 for (
int ch = 0; ch < channels; ch++) {
711 sums[ch] += p_source[current_sample * channels + ch];
716 if (have_enough_samples) {
719 for (
int ch = 0; ch < channels; ch++) {
720 p_target[result_size /
sizeof(T)] =
721 static_cast<T
>(sums[ch] / binSize);
722 result_size +=
sizeof(T);
725 for (
int ch = 0; ch < channels; ch++) {
726 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
727 result_size +=
sizeof(T);
733 for (
int ch = 0; ch < channels; ch++) {
734 partialBin[ch] = sums[ch];
736 partialBinSize += current_sample;
738 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize,
739 current_sample, total_samples, partialBinSize, (
int)size,
747 for (
int i = 0; i < bin_count; i++) {
748 for (
int ch = 0; ch < channels; ch++) {
749 sums[ch] = p_source[current_sample * channels +
754 for (
int j = 1; j < binSize; j++) {
755 for (
int ch = 0; ch < channels; ch++) {
756 sums[ch] += p_source[(current_sample + j) * channels + ch];
759 current_sample += binSize;
763 for (
int ch = 0; ch < channels; ch++) {
764 p_target[result_size /
sizeof(T)] =
765 static_cast<T
>(sums[ch] / binSize);
766 result_size +=
sizeof(T);
769 for (
int ch = 0; ch < channels; ch++) {
770 p_target[result_size /
sizeof(T)] =
static_cast<T
>(sums[ch]);
771 result_size +=
sizeof(T);
778 for (
int i = 0; i < remaining_samples; i++) {
779 for (
int ch = 0; ch < channels; ch++) {
780 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
783 partialBinSize = remaining_samples;
785 LOGD(
"bin %d: %d of %d remaining %d samples, %d > %d bytes", binSize,
786 current_sample, total_samples, partialBinSize, (
int)size,
808 Bin(
int binSize,
int channels,
bool average,
int bits_per_sample) {
809 setChannels(channels);
812 setBits(bits_per_sample);
815 void setChannels(
int channels) { this->channels = channels; }
816 void setBits(
int bits) { this->bits = bits; }
817 void setBinSize(
int binSize) { this->binSize = binSize; }
818 void setAverage(
bool average) { this->average = average; }
820 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
821 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
825 return bin8.convert(target, src, size);
829 return bin16.convert(target, src, size);
833 return bin24.convert(target, src, size);
837 return bin32.convert(target, src, size);
840 LOGE(
"Number of bits %d not supported.", bits);
850 bool average =
false;
872 size_t convert(uint8_t *src,
size_t size)
override {
873 return convert(src, src, size);
876 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
877 LOGD(
"channel subtract %d samples, %d bytes", (
int)(size /
sizeof(T)),
881 if (size % (
sizeof(T) * 2) > 0) {
882 LOGE(
"Buffer size is not even");
889 T *p_result = (T *)target;
890 T *p_source = (T *)src;
892 for (
int i = 0; i < sample_count; i++) {
894 auto tmp = *p_source++;
899 return sizeof(T) * sample_count;
906 ChannelDiff(
int bitsPerSample) { setBits(bitsPerSample); }
907 void setBits(
int bits) { this->bits = bits; }
909 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
910 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
914 return cd8.convert(target, src, size);
918 return cd16.convert(target, src, size);
922 return cd24.convert(target, src, size);
926 return cd32.convert(target, src, size);
929 LOGE(
"Number of bits %d not supported.", bits);
957 size_t convert(uint8_t *src,
size_t size)
override {
958 return convert(src, src, size);
961 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
962 if (size % (
sizeof(T) * 2) > 0) {
963 LOGE(
"Buffer size is not even");
970 T *p_result = (T *)target;
971 T *p_source = (T *)src;
973 for (
int i = 0; i < sample_count; i++) {
976 auto tmp = *p_source++;
978 *p_result++ = tmp / 2;
981 LOGD(
"channel average %d samples, %d bytes", sample_count, (
int)size);
983 return sizeof(T) * sample_count;
990 ChannelAvg(
int bitsPerSample) { setBits(bitsPerSample); }
991 void setBits(
int bits) { this->bits = bits; }
993 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
994 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
998 return ca8.convert(target, src, size);
1002 return ca16.convert(target, src, size);
1006 return ca24.convert(target, src, size);
1010 return ca32.convert(target, src, size);
1013 LOGE(
"Number of bits %d not supported.", bits);
1036template <
typename T>
1041 setChannels(channels);
1042 setBinSize(binSize);
1043 setAverage(average);
1044 this->partialBinSize = 0;
1048 this->partialBin =
new T[channels]();
1053 void setChannels(
int channels) {
1054 if ((channels % 2) > 0) {
1055 LOGE(
"Number of channels needs to be even");
1056 this->channels = channels + 1;
1058 this->channels = channels;
1060 void setBinSize(
int binSize) { this->binSize = binSize; }
1061 void setAverage(
bool average) { this->average = average; }
1063 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1065 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1069 if (size % (
sizeof(T) * channels) > 0) {
1070 LOGE(
"Buffer size needs to be multiple of channels")
1075 size / (
sizeof(T) * channels);
1080 int bin_count = total_samples / binSize;
1081 int remaining_samples =
1082 total_samples % binSize;
1083 T *p_target = (T *)target;
1084 T *p_source = (T *)src;
1085 size_t result_size = 0;
1088 typename AppropriateSumType<T>::type sums[channels];
1089 int current_sample = 0;
1093 if (partialBinSize > 0) {
1096 int samples_needed = binSize - partialBinSize;
1097 bool have_enough_samples = (samples_needed < sample_count);
1098 int samples_to_bin = have_enough_samples ? samples_needed : sample_count;
1101 for (
int ch = 0; ch < channels; ch++) {
1102 sums[ch] = partialBin[ch];
1106 for (
int j = 0; j < samples_to_bin; j++) {
1107 for (
int ch = 0; ch < channels; ch++) {
1108 sums[ch] += p_source[current_sample * channels + ch];
1114 if (have_enough_samples) {
1117 for (
int ch = 0; ch < channels; ch += 2) {
1118 p_target[result_size /
sizeof(T)] =
1119 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1120 result_size +=
sizeof(T);
1123 for (
int ch = 0; ch < channels; ch += 2) {
1124 p_target[result_size /
sizeof(T)] =
1125 static_cast<T
>((sums[ch] - sums[ch + 1]));
1126 result_size +=
sizeof(T);
1134 for (
int ch = 0; ch < channels; ch++) {
1135 partialBin[ch] = sums[ch];
1137 partialBinSize += current_sample;
1139 "bin & channel subtract %d: %d of %d remaining %d samples, %d > %d "
1141 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1151 for (
int i = 0; i < bin_count; i++) {
1154 for (
int ch = 0; ch < channels; ch++) {
1155 sums[ch] = p_source[current_sample * channels +
1160 for (
int j = 1; j < binSize; j++) {
1161 for (
int ch = 0; ch < channels; ch++) {
1162 sums[ch] += p_source[(current_sample + j) * channels + ch];
1165 current_sample += binSize;
1169 for (
int ch = 0; ch < channels; ch += 2) {
1170 p_target[result_size /
sizeof(T)] =
1171 static_cast<T
>((sums[ch] - sums[ch + 1]) / binSize);
1172 result_size +=
sizeof(T);
1175 for (
int ch = 0; ch < channels; ch += 2) {
1176 p_target[result_size /
sizeof(T)] =
1177 static_cast<T
>((sums[ch] - sums[ch + 1]));
1178 result_size +=
sizeof(T);
1186 for (
int i = 0; i < remaining_samples; i++) {
1187 for (
int ch = 0; ch < channels; ch++) {
1188 partialBin[ch] += p_source[(current_sample + i) * channels + ch];
1191 partialBinSize = remaining_samples;
1194 "bin & channel subtract %d: %d of %d remaining %d samples, %d > %d "
1196 binSize, current_sample, total_samples, partialBinSize, (
int)size,
1205 bool average =
true;
1220 ChannelBinDiff(
int binSize,
int channels,
bool average,
int bits_per_sample) {
1221 setChannels(channels);
1222 setBinSize(binSize);
1223 setAverage(average);
1224 setBits(bits_per_sample);
1227 void setChannels(
int channels) {
1228 if ((channels % 2) == 0) {
1229 this->channels = channels;
1231 LOGE(
"Number of channels needs to be even");
1232 this->channels = channels + 1;
1236 void setBits(
int bits) { this->bits = bits; }
1237 void setBinSize(
int binSize) { this->binSize = binSize; }
1238 void setAverage(
bool average) { this->average = average; }
1240 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
1241 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1245 return bd8.convert(target, src, size);
1249 return bd16.convert(target, src, size);
1253 return bd24.convert(target, src, size);
1257 return bd32.convert(target, src, size);
1260 LOGE(
"Number of bits %d not supported.", bits);
1270 bool average =
true;
1278template <
typename T>
1284 from_channels = channelCountOfSource;
1285 to_channels = channelCountOfTarget;
1288 void setSourceChannels(
int channelCountOfSource) {
1289 from_channels = channelCountOfSource;
1292 void setTargetChannels(
int channelCountOfTarget) {
1293 to_channels = channelCountOfTarget;
1296 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1297 int frame_count = size / (
sizeof(T) * from_channels);
1298 size_t result_size = 0;
1299 T *result = (T *)target;
1300 T *source = (T *)src;
1301 T value = (int16_t)0;
1302 for (
int i = 0; i < frame_count; i++) {
1304 for (
int j = 0; j < from_channels; j++) {
1307 result_size +=
sizeof(T);
1310 for (
int j = from_channels; j < to_channels; j++) {
1312 result_size +=
sizeof(T);
1320 return inSize * to_channels / from_channels;
1333template <
typename T>
1339 from_channels = channelCountOfSource;
1340 to_channels = channelCountOfTarget;
1343 void setSourceChannels(
int channelCountOfSource) {
1344 from_channels = channelCountOfSource;
1347 void setTargetChannels(
int channelCountOfTarget) {
1348 to_channels = channelCountOfTarget;
1351 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
1352 if (from_channels == to_channels) {
1353 memcpy(target, src, size);
1357 if (from_channels > to_channels) {
1358 reducer.setSourceChannels(from_channels);
1359 reducer.setTargetChannels(to_channels);
1361 enhancer.setSourceChannels(from_channels);
1362 enhancer.setTargetChannels(to_channels);
1366 if (from_channels > to_channels) {
1367 return reducer.convert(target, src, size);
1369 return enhancer.convert(target, src, size);
1385template <
typename T>
1404 void add(
BaseConverter &converter) { converters.push_back(&converter); }
1407 size_t convert(uint8_t *src,
size_t size) {
1408 for (
int i = 0; i < converters.size(); i++) {
1409 converters[i]->convert(src, size);
1493 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
1494 bool result_bool =
false;
1495 int len = inBits / 8 * n;
1496 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
1497 uint8_t buffer[len];
1498 stream_ptr->readBytes((uint8_t *)buffer, n * len);
1500 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
1506 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
1508 bool result_bool =
false;
1511 int8_t *buffer = (int8_t *)bufferIn;
1512 for (
int j = 0; j < n; j++) {
1513 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1518 int16_t *buffer = (int16_t *)bufferIn;
1519 for (
int j = 0; j < n; j++) {
1520 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1525 int32_t *buffer = (int32_t *)bufferIn;
1526 for (
int j = 0; j < n; j++) {
1527 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
1506 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n, {
…}
1536 Stream *stream_ptr =
nullptr;
1539 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1540 int32_t result =
static_cast<float>(value) /
1539 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
…}
1556template <
typename T>
1561 size_t convert(uint8_t *src,
size_t size)
override {
1563 for (
size_t j = 0; j < size; j++) {
1564 data[j] = p_filter->process(data[j]);
1579template <
typename T,
typename FT>
1584 this->channels = channels;
1587 for (
int j = 0; j < channels; j++) {
1588 filters[j] =
nullptr;
1594 for (
int j = 0; j < channels; j++) {
1595 if (filters[j] !=
nullptr) {
1605 if (channel < channels) {
1606 if (filters[channel] !=
nullptr) {
1607 delete filters[channel];
1609 filters[channel] = filter;
1611 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1617 size_t convert(uint8_t *src,
size_t size) {
1618 int count = size / channels /
sizeof(T);
1619 T *sample = (T *)src;
1620 for (
size_t j = 0; j < count; j++) {
1621 for (
int channel = 0; channel < channels; channel++) {
1622 if (filters[channel] !=
nullptr) {
1623 *sample = filters[channel]->process(*sample);
1631 int getChannels() {
return channels; }
1634 Filter<FT> **filters =
nullptr;
1646template <
typename T>
1650 set(n, aplidudeLimit);
1653 virtual size_t convert(uint8_t *data,
size_t size)
override {
1658 size_t sample_count = size /
sizeof(T);
1659 size_t write_count = 0;
1660 T *audio = (T *)data;
1663 T *p_buffer = (T *)data;
1664 for (
int j = 0; j < sample_count; j++) {
1665 int pos = findLastAudioPos(audio, j);
1668 *p_buffer++ = audio[j];
1673 size_t write_size = write_count *
sizeof(T);
1674 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1677 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1684 bool active =
false;
1685 const uint8_t *buffer =
nullptr;
1687 int priorLastAudioPos = 0;
1688 int amplidude_limit = 0;
1690 void set(
int n = 5,
int aplidudeLimit = 2) {
1691 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1693 this->amplidude_limit = aplidudeLimit;
1694 this->priorLastAudioPos = n + 1;
1695 this->active = n > 0;
1699 int findLastAudioPos(T *audio,
int pos) {
1700 for (
int j = 0; j < n; j++) {
1703 return priorLastAudioPos;
1706 if (abs(audio[pos - j]) > amplidude_limit) {
1721template <
typename T>
1725 this->channels = channels;
1726 from_beginning = fromBeginning;
1729 virtual size_t convert(uint8_t *src,
size_t size) {
1730 for (
int ch = 0; ch < channels; ch++) {
1732 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1734 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1740 bool from_beginning;
1744 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1746 T first = values[channel];
1747 for (
int j = 0; j < sampleCount; j += channels) {
1749 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1758 void clearAfterLastTransition(
int channels,
int channel, T *values,
1760 int lastPos = sampleCount - channels + channel;
1761 T last = values[lastPos];
1762 for (
int j = lastPos; j >= 0; j -= channels) {
1764 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1780template <
typename T>
1785 this->channels = channels;
1787 from_beginning = fromBeginning;
1790 virtual size_t convert(uint8_t *src,
size_t size) {
1791 for (
int ch = 0; ch < channels; ch++) {
1793 processStart(channels, ch, (T *)src, size /
sizeof(T));
1794 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1800 bool from_beginning;
1806 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1807 for (
int j = 0; j < sampleCount; j += channels) {
1808 if (factor >= 0.8) {
1811 values[j] = factor * values[j];
1817 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1818 int lastPos = sampleCount - channels + channel;
1819 for (
int j = lastPos; j >= 0; j -= channels) {
1820 if (factor >= 0.8) {
1823 values[j] = factor * values[j];
1834template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1837 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1839 size_t convert(uint8_t *src,
size_t size) {
1841 size_t samples = (size / Cn) /
sizeof(T);
1842 for (
size_t s = 0; s < samples; s++) {
1843 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1845 for (
size_t c = 0; c < Cn; c++) {
1847 chan[s * Cn + c] = chan[s * Cn + Cx];
1851 if (_max_val < chan[s * Cn]) {
1852 _max_val = chan[s * Cn];
1857 if (now - _prev_ms > 1000) {
1859 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1863 return samples * Cn *
sizeof(T);
1877template <
typename T>
1881 this->callback = callback;
1882 this->channels = channels;
1885 size_t convert(uint8_t *src,
size_t size) {
1886 int samples = size /
sizeof(T);
1887 for (
int j = 0; j < samples; j++) {
1888 src[j] = callback(src[j], j % channels);
1894 T (*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition BaseConverter.h:285