2 #include "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 virtual size_t convert(uint8_t(*src),
size_t size) {
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) {
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)
delete p_converter;
200 void begin(
int channels,
int bitsPerSample,
bool isDynamic =
false) {
201 this->channels = channels;
202 this->bits_per_sample = bitsPerSample;
203 if (p_converter !=
nullptr)
delete p_converter;
204 switch (bits_per_sample) {
220 size_t convert(uint8_t *src,
size_t size)
override {
221 if (p_converter ==
nullptr)
return 0;
222 return p_converter->convert(src, size);
239 template <
typename T>
244 size_t convert(uint8_t *src,
size_t byte_count) {
246 int size = byte_count / channels /
sizeof(T);
247 T *sample = (T *)src;
248 for (
size_t j = 0; j < size; j++) {
250 *sample = *(sample + 1) * (sample + 1) = temp;
276 template <
typename T>
281 this->channels = channels;
299 size_t convert(uint8_t *src,
size_t byte_count) {
301 int size = byte_count / channels /
sizeof(T);
302 setup((T *)src, size);
303 if (left_empty && !right_empty) {
304 T *sample = (T *)src;
305 for (
size_t j = 0; j < size; j++) {
306 *sample = *(sample + 1);
309 }
else if (!left_empty && right_empty) {
310 T *sample = (T *)src;
311 for (
size_t j = 0; j < size; j++) {
312 *(sample + 1) = *sample;
321 bool is_setup =
false;
322 bool left_empty =
true;
323 bool right_empty =
true;
326 void setup(T *src,
size_t size) {
328 for (
int j = 0; j < size; j++) {
335 for (
int j = 0; j < size - 1; j++) {
343 if (!right_empty || !left_empty) {
359 template <
typename T>
364 size_t convert(uint8_t *src,
size_t byte_count) {
365 int size = byte_count / channels /
sizeof(T);
366 T *sample = (T *)src;
367 for (
int i = 0; i < size; i++) {
368 for (
int j = 0; j < channels; j++) {
369 *sample = *sample + 0x8000;
387 template <
typename T>
393 from_channels = channelCountOfSource;
394 to_channels = channelCountOfTarget;
397 void setSourceChannels(
int channelCountOfSource) {
398 from_channels = channelCountOfSource;
401 void setTargetChannels(
int channelCountOfTarget) {
402 to_channels = channelCountOfTarget;
405 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
407 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
408 LOGD(
"convert %d -> %d", from_channels, to_channels);
409 assert(to_channels <= from_channels);
410 int frame_count = size / (
sizeof(T) * from_channels);
411 size_t result_size = 0;
412 T *result = (T *)target;
413 T *source = (T *)src;
414 int reduceDiv = from_channels - to_channels + 1;
416 for (
int i = 0; i < frame_count; i++) {
418 for (
int j = 0; j < to_channels - 1; j++) {
419 *result++ = *source++;
420 result_size +=
sizeof(T);
423 T total = (int16_t)0;
424 for (
int j = to_channels - 1; j < from_channels; j++) {
425 total += *source++ / reduceDiv;
428 result_size +=
sizeof(T);
446 ChannelReducer(
int channelCountOfTarget,
int channelCountOfSource,
448 from_channels = channelCountOfSource;
449 to_channels = channelCountOfTarget;
450 bits = bitsPerSample;
453 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
455 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
459 return cr16.convert(target, src, size);
463 return cr24.convert(target, src, size);
467 return cr32.convert(target, src, size);
483 template <
typename T>
496 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
498 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
499 int frame_count = size / (
sizeof(T) * channels);
500 T *p_target = (T *)target;
501 T *p_source = (T *)src;
502 size_t result_size = 0;
504 for (
int i = 0; i < frame_count; i++) {
505 if (++count == factor) {
508 for (
int ch = 0; ch < channels; ch++) {
509 *p_target++ = p_source[i + ch];
510 result_size +=
sizeof(T);
518 operator bool() {
return factor > 1; };
534 Decimate(
int factor,
int channels,
int bits_per_sample) {
537 setBits(bits_per_sample);
541 void setBits(
int bits) { this->bits = bits; }
545 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
546 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
549 DecimateT<int16_t> dec16(factor, channels);
550 return dec16.convert(target, src, size);
553 DecimateT<int24_t> dec24(factor, channels);
554 return dec24.convert(target, src, size);
557 DecimateT<int32_t> dec32(factor, channels);
558 return dec32.convert(target, src, size);
564 operator bool() {
return factor > 1; };
579 template <
typename T>
583 using type = int16_t;
587 using type = int32_t;
591 using type = int32_t;
595 using type = int64_t;
603 template <
typename T>
606 BinT(
int binSize,
int channels,
bool average) {
620 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
622 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
623 int frame_count = size / (
sizeof(T) * channels);
624 T *p_target = (T *)target;
625 T *p_source = (T *)src;
626 size_t result_size = 0;
631 typename AppropriateSumType<T>::type sums[channels];
633 for (
int i = 0; i < frame_count; i += binSize) {
635 for (
int ch = 0; ch < channels; ch++) {
636 sums[ch] = (i * channels + ch < frame_count * channels)
637 ? p_source[i * channels + ch]
643 for (
int j = 1; j < binSize && (i + j) < frame_count; j++) {
644 for (
int ch = 0; ch < channels; ch++) {
645 sums[ch] += p_source[(i + j) * channels + ch];
650 for (
int ch = 0; ch < channels; ch++) {
652 T avg =
static_cast<T
>(sums[ch] / binSize);
655 *p_target++ =
static_cast<T
>(sums[ch]);
658 result_size +=
sizeof(T) * channels;
666 operator bool() {
return binSize > 1; };
683 Bin(
int binSize,
int channels,
bool average,
int bits_per_sample) {
687 setBits(bits_per_sample);
691 void setBits(
int bits) { this->bits = bits; }
697 size_t convert(uint8_t *src,
size_t size) {
return convert(src, src, size); }
698 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
701 BinT<int8_t> bin8(binSize, channels, average);
702 return bin8.convert(target, src, size);
705 BinT<int16_t> bin16(binSize, channels, average);
706 return bin16.convert(target, src, size);
709 BinT<int24_t> bin24(binSize, channels, average);
710 return bin24.convert(target, src, size);
713 BinT<int32_t> bin32(binSize, channels, average);
714 return bin32.convert(target, src, size);
717 LOGE(
"Number of bits %d not supported.", bits);
724 operator bool() {
return binSize > 1; };
730 bool average =
false;
738 template <
typename T>
744 from_channels = channelCountOfSource;
745 to_channels = channelCountOfTarget;
748 void setSourceChannels(
int channelCountOfSource) {
749 from_channels = channelCountOfSource;
752 void setTargetChannels(
int channelCountOfTarget) {
753 to_channels = channelCountOfTarget;
756 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
757 int frame_count = size / (
sizeof(T) * from_channels);
758 size_t result_size = 0;
759 T *result = (T *)target;
760 T *source = (T *)src;
761 T value = (int16_t)0;
762 for (
int i = 0; i < frame_count; i++) {
764 for (
int j = 0; j < from_channels; j++) {
767 result_size +=
sizeof(T);
770 for (
int j = from_channels; j < to_channels; j++) {
772 result_size +=
sizeof(T);
780 return inSize * to_channels / from_channels;
793 template <
typename T>
799 from_channels = channelCountOfSource;
800 to_channels = channelCountOfTarget;
803 void setSourceChannels(
int channelCountOfSource) {
804 from_channels = channelCountOfSource;
807 void setTargetChannels(
int channelCountOfTarget) {
808 to_channels = channelCountOfTarget;
811 size_t convert(uint8_t *target, uint8_t *src,
size_t size) {
812 if (from_channels == to_channels) {
813 memcpy(target, src, size);
817 if (from_channels > to_channels) {
818 reducer.setSourceChannels(from_channels);
819 reducer.setTargetChannels(to_channels);
821 enhancer.setSourceChannels(from_channels);
822 enhancer.setTargetChannels(to_channels);
826 if (from_channels > to_channels) {
827 return reducer.convert(target, src, size);
829 return enhancer.convert(target, src, size);
845 template <
typename T>
864 void add(
BaseConverter &converter) { converters.push_back(&converter); }
867 size_t convert(uint8_t *src,
size_t size) {
868 for (
int i = 0; i < converters.size(); i++) {
869 converters[i]->convert(src, size);
953 bool read(
int inBits,
int outBits,
bool outSigned,
int n, int32_t *result) {
954 bool result_bool =
false;
955 int len = inBits / 8 * n;
956 if (stream_ptr !=
nullptr && stream_ptr->available() > len) {
958 stream_ptr->readBytes((uint8_t *)buffer, n * len);
960 toNumbers((
void *)buffer, inBits, outBits, outSigned, n, result);
966 bool toNumbers(
void *bufferIn,
int inBits,
int outBits,
bool outSigned,
int n,
968 bool result_bool =
false;
971 int8_t *buffer = (int8_t *)bufferIn;
972 for (
int j = 0; j < n; j++) {
973 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
978 int16_t *buffer = (int16_t *)bufferIn;
979 for (
int j = 0; j < n; j++) {
980 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
985 int32_t *buffer = (int32_t *)bufferIn;
986 for (
int j = 0; j < n; j++) {
987 result[j] =
scale(buffer[j], inBits, outBits, outSigned);
996 Stream *stream_ptr =
nullptr;
999 int32_t
scale(int32_t value,
int inBits,
int outBits,
bool outSigned =
true) {
1000 int32_t result =
static_cast<float>(value) /
1016 template <
typename T>
1021 size_t convert(uint8_t *src,
size_t size) {
1023 for (
size_t j = 0; j < size; j++) {
1024 data[j] = p_filter->process(data[j]);
1039 template <
typename T,
typename FT>
1044 this->channels = channels;
1047 for (
int j = 0; j < channels; j++) {
1048 filters[j] =
nullptr;
1054 for (
int j = 0; j < channels; j++) {
1055 if (filters[j] !=
nullptr) {
1065 if (channel < channels) {
1066 if (filters[channel] !=
nullptr) {
1067 delete filters[channel];
1069 filters[channel] = filter;
1071 LOGE(
"Invalid channel nummber %d - max channel is %d", channel,
1077 size_t convert(uint8_t *src,
size_t size) {
1078 int count = size / channels /
sizeof(T);
1079 T *sample = (T *)src;
1080 for (
size_t j = 0; j < count; j++) {
1081 for (
int channel = 0; channel < channels; channel++) {
1082 if (filters[channel] !=
nullptr) {
1083 *sample = filters[channel]->process(*sample);
1091 int getChannels() {
return channels; }
1094 Filter<FT> **filters =
nullptr;
1106 template <
typename T>
1110 set(n, aplidudeLimit);
1113 virtual size_t convert(uint8_t *data,
size_t size)
override {
1118 size_t sample_count = size /
sizeof(T);
1119 size_t write_count = 0;
1120 T *audio = (T *)data;
1123 T *p_buffer = (T *)data;
1124 for (
int j = 0; j < sample_count; j++) {
1125 int pos = findLastAudioPos(audio, j);
1128 *p_buffer++ = audio[j];
1133 size_t write_size = write_count *
sizeof(T);
1134 LOGI(
"filtered silence from %d -> %d", (
int)size, (
int)write_size);
1137 priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
1144 bool active =
false;
1145 const uint8_t *buffer =
nullptr;
1147 int priorLastAudioPos = 0;
1148 int amplidude_limit = 0;
1150 void set(
int n = 5,
int aplidudeLimit = 2) {
1151 LOGI(
"begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
1153 this->amplidude_limit = aplidudeLimit;
1154 this->priorLastAudioPos = n + 1;
1155 this->active = n > 0;
1159 int findLastAudioPos(T *audio,
int pos) {
1160 for (
int j = 0; j < n; j++) {
1163 return priorLastAudioPos;
1166 if (abs(audio[pos - j]) > amplidude_limit) {
1181 template <
typename T>
1185 this->channels = channels;
1186 from_beginning = fromBeginning;
1189 virtual size_t convert(uint8_t *src,
size_t size) {
1190 for (
int ch = 0; ch < channels; ch++) {
1192 clearUpTo1stTransition(channels, ch, (T *)src, size /
sizeof(T));
1194 clearAfterLastTransition(channels, ch, (T *)src, size /
sizeof(T));
1200 bool from_beginning;
1204 void clearUpTo1stTransition(
int channels,
int channel, T *values,
1206 T first = values[channel];
1207 for (
int j = 0; j < sampleCount; j += channels) {
1209 if ((first <= 0.0 && act >= 0.0) || (first >= 0.0 && act <= 0.0)) {
1218 void clearAfterLastTransition(
int channels,
int channel, T *values,
1220 int lastPos = sampleCount - channels + channel;
1221 T last = values[lastPos];
1222 for (
int j = lastPos; j >= 0; j -= channels) {
1224 if ((last <= 0.0 && act >= 0.0) || (last >= 0.0 && act <= 0.0)) {
1240 template <
typename T>
1245 this->channels = channels;
1247 from_beginning = fromBeginning;
1250 virtual size_t convert(uint8_t *src,
size_t size) {
1251 for (
int ch = 0; ch < channels; ch++) {
1253 processStart(channels, ch, (T *)src, size /
sizeof(T));
1254 if (from_end) processEnd(channels, ch, (T *)src, size /
sizeof(T));
1260 bool from_beginning;
1266 void processStart(
int channels,
int channel, T *values,
int sampleCount) {
1267 for (
int j = 0; j < sampleCount; j += channels) {
1268 if (factor >= 0.8) {
1271 values[j] = factor * values[j];
1277 void processEnd(
int channels,
int channel, T *values,
int sampleCount) {
1278 int lastPos = sampleCount - channels + channel;
1279 for (
int j = lastPos; j >= 0; j -= channels) {
1280 if (factor >= 0.8) {
1283 values[j] = factor * values[j];
1294 template <
typename T,
size_t Cn,
size_t Cx,
size_t S>
1297 CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}
1299 size_t convert(uint8_t *src,
size_t size) {
1301 size_t samples = (size / Cn) /
sizeof(T);
1302 for (
size_t s = 0; s < samples; s++) {
1303 chan[s * Cn + Cx] = (Cx < Cn) ? chan[s * Cn + Cx] << S : 0;
1305 for (
size_t c = 0; c < Cn; c++) {
1307 chan[s * Cn + c] = chan[s * Cn + Cx];
1311 if (_max_val < chan[s * Cn]) {
1312 _max_val = chan[s * Cn];
1317 if (now - _prev_ms > 1000) {
1319 LOGI(
"CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
1323 return samples * Cn *
sizeof(T);
1337 template <
typename T>
1341 this->callback = callback;
1342 this->channels = channels;
1345 size_t convert(uint8_t *src,
size_t size) {
1346 int samples = size /
sizeof(T);
1347 for (
int j = 0; j < samples; j++) {
1348 src[j] = callback(src[j], j % channels);
1354 T(*callback)(T in,
int channel);
FillLeftAndRightStatus
Configure ConverterFillLeftAndRight.
Definition: BaseConverter.h:265