#include #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; }