2014-07-14 05:03:36 +08:00
|
|
|
#include "audioinputproxy.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#define RING_SIZE 44100*2*2
|
|
|
|
|
|
|
|
AudioInputProxy::AudioInputProxy(QObject *parent) :
|
|
|
|
QIODevice(parent),
|
|
|
|
callback(nullptr),
|
2014-07-14 21:16:44 +08:00
|
|
|
ring_buffer(new MemRing<int16_t>(RING_SIZE))
|
2014-07-14 05:03:36 +08:00
|
|
|
{
|
2014-07-14 21:16:44 +08:00
|
|
|
open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
|
|
|
qDebug() << "AudioInputProxy::AudioInputProxy";
|
2014-07-14 05:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioInputProxy::~AudioInputProxy()
|
|
|
|
{
|
|
|
|
if (ring_buffer) {
|
|
|
|
delete ring_buffer;
|
|
|
|
ring_buffer = 0;
|
|
|
|
}
|
2014-07-14 21:16:44 +08:00
|
|
|
close();
|
|
|
|
qDebug() << "AudioInputProxy::~AudioInputProxy";
|
2014-07-14 05:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudioInputProxy::readData(char *data, qint64 len)
|
|
|
|
{
|
2014-07-14 21:16:44 +08:00
|
|
|
qDebug() << "AudioInputProxy::read" << len;
|
|
|
|
return ring_buffer->pull((int16_t*)data, len/2)*2;
|
2014-07-14 05:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudioInputProxy::writeData(const char* data, qint64 len)
|
|
|
|
{
|
2014-07-14 21:16:44 +08:00
|
|
|
qDebug() << "AudioInputProxy::write" << len;
|
|
|
|
auto ret = ring_buffer->push((int16_t*)data, len/2)*2;
|
2014-07-14 05:03:36 +08:00
|
|
|
if (callback != nullptr) {
|
|
|
|
callback();
|
|
|
|
}
|
2014-07-14 21:16:44 +08:00
|
|
|
return ret;
|
2014-07-14 05:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AudioInputProxy::bytesAvailable() const
|
|
|
|
{
|
|
|
|
return ring_buffer->readSpace() + QIODevice::bytesAvailable();
|
|
|
|
}
|
2014-07-14 21:16:44 +08:00
|
|
|
|
|
|
|
bool AudioInputProxy::isSequential() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|