50 lines
1.4 KiB
C++
50 lines
1.4 KiB
C++
#include <cassert>
|
|
#include <cstring>
|
|
#include "AudioReframer.h"
|
|
|
|
using namespace tc::audio;
|
|
|
|
InputReframer::InputReframer(size_t channels, size_t frame_size) : _frame_size(frame_size), _channels(channels) {
|
|
this->buffer = nullptr;
|
|
this->_buffer_index = 0;
|
|
}
|
|
|
|
InputReframer::~InputReframer() {
|
|
if(this->buffer)
|
|
free(this->buffer);
|
|
}
|
|
|
|
void InputReframer::process(const void *source, size_t samples) {
|
|
if(!this->buffer) {
|
|
this->buffer = (float*) malloc(this->_channels * this->_frame_size * sizeof(float));
|
|
}
|
|
assert(this->on_frame);
|
|
|
|
if(this->_buffer_index > 0) {
|
|
if(this->_buffer_index + samples > this->_frame_size) {
|
|
auto required = this->_frame_size - this->_buffer_index;
|
|
auto length = required * this->_channels * 4;
|
|
|
|
memcpy((char*) this->buffer + this->_buffer_index * 4 * this->_channels, source, length);
|
|
samples -= required;
|
|
source = (char*) source + length;
|
|
|
|
this->on_frame(this->buffer);
|
|
} else {
|
|
memcpy((char*) this->buffer + this->_buffer_index * 4 * this->_channels, source, samples * this->_channels * 4);
|
|
this->_buffer_index += samples;
|
|
return;
|
|
}
|
|
}
|
|
|
|
auto _on_frame = this->on_frame;
|
|
while(samples > this->_frame_size) {
|
|
if(_on_frame)
|
|
_on_frame(source);
|
|
samples -= this->_frame_size;
|
|
source = (char*) source + this->_frame_size * this->_channels * 4;
|
|
}
|
|
if(samples > 0)
|
|
memcpy((char*) this->buffer, source, samples * this->_channels * 4);
|
|
this->_buffer_index = samples;
|
|
} |