3 #include "AudioTools/AudioIO.h"
26 p_transform = transform;
27 if (transform ==
nullptr) {
28 LOGE(
"transform is NULL");
31 if (p_stream ==
nullptr) {
32 LOGE(
"p_stream is NULL");
35 byte_factor = getByteFactor();
38 size_t readBytes(uint8_t *data,
size_t byteCount) {
39 LOGD(
"TransformationReader::readBytes: %d", (
int)byteCount);
44 if (p_stream ==
nullptr) {
45 LOGE(
"p_stream is NULL");
48 int read_size = byte_factor * byteCount;
49 LOGD(
"factor %f -> buffer %d bytes", byte_factor, read_size);
50 buffer.resize(read_size);
51 int read_eff = p_stream->readBytes(buffer.data(), read_size);
53 if (read_eff==0)
return 0;
56 p_transform->write(buffer.data(), read_eff);
58 return print_to_array.totalBytesWritten();
61 int availableForWrite() {
return print_to_array.availableForWrite(); }
66 void begin(uint8_t *array,
size_t data_len) {
73 int availableForWrite()
override {
return max_len; }
75 size_t write(
const uint8_t *data,
size_t byteCount)
override {
76 LOGD(
"AdapterPrintToArray::write: %d (%d)", (
int) byteCount, (
int) pos);
77 if (pos + byteCount > max_len)
return 0;
78 memcpy(p_data + pos, data, byteCount);
84 int totalBytesWritten() {
return pos; }
91 float byte_factor = 0.0f;
92 Stream *p_stream =
nullptr;
94 T *p_transform =
nullptr;
102 Print *result = p_transform->getPrint();
103 p_transform->setStream(print_to_array);
104 print_to_array.begin(data, byteCount);
111 if (out) p_transform->setStream(*out);
114 float getByteFactor(){
116 float input_size = 8.0;
118 p_transform->write(buffer.data(), input_size);
120 float output_size = print_to_array.totalBytesWritten();
121 float factor = input_size/output_size;
122 LOGI(
"input_size: %f / output_size: %f / factor (eff): %f", input_size, output_size, factor);
136 assert(getStream() !=
nullptr);
137 reader.begin(
this, getStream());
140 virtual void setStream(
Stream &stream) {
145 virtual void setStream(
Print &print) { p_print = &print; }
147 virtual Print *getPrint() {
return p_print; }
149 virtual Stream *getStream() {
return p_stream; }
151 size_t readBytes(uint8_t *data,
size_t size)
override {
152 LOGD(
"ReformatBaseStream::readBytes: %d", (
int)size);
153 return reader.readBytes(data, size);
156 int available()
override {
157 return DEFAULT_BUFFER_SIZE;
160 int availableForWrite()
override {
161 return DEFAULT_BUFFER_SIZE;
166 Stream *p_stream =
nullptr;
167 Print *p_print =
nullptr;
177 float step_size = 1.0f;
180 int buffer_size = DEFAULT_BUFFER_SIZE;
200 setAudioInfo(out.audioInfo());
210 setAudioInfo(io.audioInfo());
222 LOGI(
"begin step_size: %f", cfg.step_size);
225 out_buffer.resize(cfg.buffer_size);
231 bytes_per_frame = info.bits_per_sample / 8 * info.channels;
235 if (p_stream !=
nullptr) {
242 bool begin(AudioInfo from,
int toRate) {
245 rcfg.to_sample_rate = toRate;
246 rcfg.step_size =
getStepSize(from.sample_rate, toRate);
250 bool begin(AudioInfo info,
float step) {
253 rcfg.step_size = step;
257 void setAudioInfo(AudioInfo info)
override {
258 AudioStream::setAudioInfo(info);
260 if (to_sample_rate != 0) {
267 LOGI(
"setStepSize: %f", step);
274 return sampleRateFrom / sampleRateTo;
282 size_t write(
const uint8_t *buffer,
size_t bytes)
override {
283 LOGD(
"ResampleStream::write: %d", (
int)bytes);
285 switch (info.bits_per_sample) {
287 return write<int16_t>(p_print, buffer, bytes, written);
289 return write<int24_t>(p_print, buffer, bytes, written);
291 return write<int32_t>(p_print, buffer, bytes, written);
300 is_buffer_active = active;
305 if (p_out!=
nullptr && !out_buffer.isEmpty()){
315 bool is_first =
true;
316 float step_size = 1.0;
317 int to_sample_rate = 0;
318 int bytes_per_frame = 0;
319 TransformationReader<ResampleStream> reader;
321 bool is_buffer_active =
false;
322 SingleBuffer<uint8_t> out_buffer{0};
323 Print *p_out=
nullptr;
327 int bytes_per_sample = cfg.bits_per_sample / 8;
328 int last_samples_size = cfg.channels * bytes_per_sample;
329 last_samples.resize(last_samples_size);
330 memset(last_samples.data(), 0, last_samples_size);
334 template <
typename T>
335 size_t write(
Print *p_out,
const uint8_t *buffer,
size_t bytes,
338 if (step_size == 1.0) {
339 return p_print->write(buffer, bytes);
342 if (info.channels == 0) {
343 LOGE(
"channels is 0");
346 T *data = (T *)buffer;
347 int samples = bytes /
sizeof(T);
348 size_t frames = samples / info.channels;
354 setupLastSamples<T>(data, 0);
357 T frame[info.channels];
358 size_t frame_size =
sizeof(frame);
361 while (idx < frames) {
362 for (
int ch = 0; ch < info.channels; ch++) {
363 T result = getValue<T>(data, idx, ch);
367 if (is_buffer_active){
374 written += out_buffer.
writeArray((
const uint8_t *)&frame, frame_size);
376 if (p_out->availableForWrite() < frame_size) {
379 written += p_out->write((
const uint8_t *)&frame, frame_size);
388 setupLastSamples<T>(data, frames - 1);
396 template <
typename T>
397 T
getValue(T *data,
float frame_idx,
int channel) {
399 int frame_idx1 = frame_idx;
400 int frame_idx0 = frame_idx1 - 1;
401 T val0 = lookup<T>(data, frame_idx0, channel);
402 T val1 = lookup<T>(data, frame_idx1, channel);
404 float result =
mapFloat(frame_idx, frame_idx1, frame_idx0, val0, val1);
405 return (
float)round(result);
409 template <
typename T>
410 T lookup(T *data,
int frame,
int channel) {
412 return data[frame * info.channels + channel];
415 T *pt_last_samples = (T *)last_samples.data();
416 return pt_last_samples[channel];
420 template <
typename T>
422 for (
int ch = 0; ch < info.channels; ch++) {
423 T *pt_last_samples = (T *)last_samples.data();
424 pt_last_samples[ch] = data[(frame * info.channels) + ch];