#include #include "AudioResampler.h" #include "../logger.h" using namespace std; using namespace tc::audio; AudioResampler::AudioResampler(size_t irate, size_t orate, size_t channels) : input_rate_{irate}, output_rate_{orate}, channels_{channels} { if(this->input_rate() != this->output_rate()) { soxr_error_t error; this->soxr_handle = soxr_create((double) this->input_rate_, (double) this->output_rate_, (unsigned) this->channels_, &error, nullptr, nullptr, nullptr); if(!this->soxr_handle) { log_error(category::audio, tr("Failed to create soxr resampler: {}. Input: {}; Output: {}; Channels: {}"), error, this->input_rate_, this->output_rate_, this->channels_); } } } AudioResampler::~AudioResampler() { if(this->soxr_handle) soxr_delete(this->soxr_handle); } ssize_t AudioResampler::process(void *output, const void *input, size_t input_length) { if(this->io_ratio() == 1) { if(input != output) { memcpy(output, input, input_length * this->channels_ * 4); } return input_length; } if(!this->soxr_handle) { return -2; } size_t output_length = 0; auto error = soxr_process(this->soxr_handle, input, input_length, nullptr, output, this->estimated_output_size(input_length), &output_length); if(error) { log_error(category::audio, tr("Failed to process resample: {}"), error); return -1; } return output_length; }