finished portaudio switch
This may have bugs in it, but as far as I can tell it's fully functional.
This commit is contained in:
@@ -22,17 +22,10 @@
|
||||
|
||||
namespace olive {
|
||||
|
||||
PreviewAudioDevice::PreviewAudioDevice(int bytes_per_frame, QObject *parent) :
|
||||
bytes_per_frame_(bytes_per_frame),
|
||||
PreviewAudioDevice::PreviewAudioDevice(QObject *parent) :
|
||||
notify_interval_(0),
|
||||
bytes_read_(0)
|
||||
{
|
||||
// These pointers are always valid
|
||||
using_ = &internal_buffer_[0];
|
||||
pushing_ = &internal_buffer_[1];
|
||||
|
||||
// Default to swap being true because we'll have nothing in the main buffer at first
|
||||
swap_requested_ = true;
|
||||
}
|
||||
|
||||
PreviewAudioDevice::~PreviewAudioDevice()
|
||||
@@ -47,13 +40,9 @@ bool PreviewAudioDevice::isSequential() const
|
||||
|
||||
qint64 PreviewAudioDevice::readData(char *data, qint64 maxSize)
|
||||
{
|
||||
if (swap_requested_) {
|
||||
SwapBuffers(kFullLock);
|
||||
swap_requested_ = false;
|
||||
}
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
// This function should NEVER touch the buffer in `pushing_`
|
||||
qint64 copy_length = qMin(maxSize, qint64(using_->size()));
|
||||
qint64 copy_length = qMin(maxSize, qint64(buffer_.size()));
|
||||
|
||||
if (copy_length) {
|
||||
qint64 new_bytes_read = bytes_read_ + copy_length;
|
||||
@@ -66,13 +55,8 @@ qint64 PreviewAudioDevice::readData(char *data, qint64 maxSize)
|
||||
|
||||
bytes_read_ = new_bytes_read;
|
||||
|
||||
memcpy(data, using_->constData(), copy_length);
|
||||
*using_ = using_->mid(copy_length);
|
||||
}
|
||||
|
||||
if (using_->isEmpty() && !SwapBuffers(kTryLock)) {
|
||||
// Ask push function to swap if it can. If it can't, we'll catch it next read.
|
||||
swap_requested_ = true;
|
||||
memcpy(data, buffer_.constData(), copy_length);
|
||||
buffer_ = buffer_.mid(copy_length);
|
||||
}
|
||||
|
||||
return copy_length;
|
||||
@@ -80,41 +64,19 @@ qint64 PreviewAudioDevice::readData(char *data, qint64 maxSize)
|
||||
|
||||
qint64 PreviewAudioDevice::writeData(const char *data, qint64 length)
|
||||
{
|
||||
// This function should NEVER touch the buffer in `using_`
|
||||
QMutexLocker locker(&lock_);
|
||||
pushing_->append(data, length);
|
||||
|
||||
// If swap requested, do this now
|
||||
if (swap_requested_) {
|
||||
SwapBuffers(kDontLock);
|
||||
swap_requested_ = false;
|
||||
}
|
||||
buffer_.append(data, length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
bool PreviewAudioDevice::SwapBuffers(LockMethod m)
|
||||
void PreviewAudioDevice::clear()
|
||||
{
|
||||
switch (m) {
|
||||
case kDontLock:
|
||||
break;
|
||||
case kTryLock:
|
||||
if (!lock_.tryLock()) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case kFullLock:
|
||||
lock_.lock();
|
||||
break;
|
||||
}
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
std::swap(using_, pushing_);
|
||||
|
||||
if (m != kDontLock) {
|
||||
lock_.unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
buffer_.clear();
|
||||
bytes_read_ = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user