#include "gtl_hw_audio_input_device.h" #include namespace gtl { namespace hw { audio_input_device::audio_input_device(QObject *parent) : QIODevice{parent} , _buffer(1000) , _ptr_recv(0) , _ptr_send(0) , _frame_bytes(1) { } void audio_input_device::set_buffer(qreal rate, int frame_bytes) { _buffer.resize(rate * frame_bytes); _ptr_recv = 0; _ptr_send = 0; _frame_bytes = frame_bytes; } void audio_input_device::get_data(std::back_insert_iterator> buffer) { int ptr_send = _ptr_send; int cnt = 0; while(ptr_send != _ptr_recv) { ptr_send = (ptr_send + 1) % _buffer.size(); cnt++; if(cnt % _frame_bytes == 0) { auto begin = _buffer.begin() + _ptr_send; auto end = ptr_send == 0 ? _buffer.end() : _buffer.begin() + ptr_send; std::copy(begin, end, buffer); _ptr_send = ptr_send; } } // qDebug() << "prt_send: " << _ptr_send; } qint64 audio_input_device::readData(char *data, qint64 maxSize) { Q_UNUSED(data); Q_UNUSED(maxSize); return -1; } qint64 audio_input_device::writeData(const char *data, qint64 maxSize) { int cnt = 0; while(cnt != maxSize) { int size = std::min(_buffer.size() - _ptr_recv, maxSize - cnt); std::copy(&data[cnt], &data[cnt + size], &_buffer[_ptr_recv]); _ptr_recv = (_ptr_recv + size) % _buffer.size(); cnt += size; } // qDebug() << "prt_recv: " << _ptr_recv; return maxSize; } } }