2020-06-09 04:01:22 +00:00
|
|
|
#ifndef AUDIOSOURCE_H
|
|
|
|
#define AUDIOSOURCE_H
|
|
|
|
|
|
|
|
#include <cstdint>
|
2020-06-20 03:09:20 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
2020-06-09 04:01:22 +00:00
|
|
|
|
|
|
|
#include <QIODevice>
|
|
|
|
|
|
|
|
/*!
|
2021-05-06 16:57:32 +00:00
|
|
|
* \brief An intermediate recepticle for audio data.
|
2020-06-09 04:01:22 +00:00
|
|
|
*
|
|
|
|
* Provides a QIODevice that will attempt to buffer the minimum amount
|
|
|
|
* of data before handing it off to a polling QAudioOutput.
|
|
|
|
*
|
2021-05-06 16:57:32 +00:00
|
|
|
* Adding an extra buffer increases worst-case latency but resolves two
|
|
|
|
* issues uncovered with use of a QAudioOutput by push:
|
|
|
|
*
|
|
|
|
* 1. knowing how much data is currently buffered, to avoid being at a
|
|
|
|
* permanent disadvantage after startup; and
|
|
|
|
* 2. that such QAudioOutputs empirically seem to introduce a minimum
|
|
|
|
* 16384-byte latency.
|
2020-06-09 04:01:22 +00:00
|
|
|
*/
|
|
|
|
struct AudioBuffer: public QIODevice {
|
|
|
|
AudioBuffer() {
|
|
|
|
open(QIODevice::ReadOnly | QIODevice::Unbuffered);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDepth(size_t depth) {
|
2020-06-11 02:14:54 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-06-09 04:01:22 +00:00
|
|
|
buffer.resize(depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
// AudioBuffer-specific behaviour: always provide the latest data,
|
|
|
|
// even if that means skipping some.
|
|
|
|
qint64 readData(char *data, const qint64 maxlen) override {
|
|
|
|
if(!maxlen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard lock(mutex);
|
2020-06-11 02:14:54 +00:00
|
|
|
if(readPointer == writePointer || buffer.empty()) return 0;
|
2020-06-09 04:01:22 +00:00
|
|
|
|
|
|
|
const size_t dataAvailable = std::min(writePointer - readPointer, size_t(maxlen));
|
2020-06-10 03:56:08 +00:00
|
|
|
size_t bytesToCopy = dataAvailable;
|
|
|
|
while(bytesToCopy) {
|
|
|
|
const size_t nextLength = std::min(buffer.size() - (readPointer % buffer.size()), bytesToCopy);
|
2020-06-09 04:01:22 +00:00
|
|
|
memcpy(data, &buffer[readPointer % buffer.size()], nextLength);
|
|
|
|
|
2020-06-10 03:56:08 +00:00
|
|
|
bytesToCopy -= nextLength;
|
2020-06-09 04:01:22 +00:00
|
|
|
data += nextLength;
|
|
|
|
readPointer += nextLength;
|
|
|
|
}
|
|
|
|
|
2020-06-20 03:12:18 +00:00
|
|
|
return qint64(dataAvailable);
|
2020-06-09 04:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 bytesAvailable() const override {
|
|
|
|
std::lock_guard lock(mutex);
|
2020-06-20 03:12:18 +00:00
|
|
|
return qint64(writePointer - readPointer);
|
2020-06-09 04:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Required to make QIODevice concrete; not used.
|
|
|
|
qint64 writeData(const char *, qint64) override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Posts a new set of source data. This buffer permits only the amount of data
|
|
|
|
// specified by @c setDepth to be enqueued into the future. Additional writes
|
|
|
|
// after the buffer is full will overwrite the newest data.
|
|
|
|
void write(const std::vector<int16_t> &source) {
|
|
|
|
std::lock_guard lock(mutex);
|
2020-06-11 02:14:54 +00:00
|
|
|
if(buffer.empty()) return;
|
2020-06-09 04:01:22 +00:00
|
|
|
const size_t sourceSize = source.size() * sizeof(int16_t);
|
|
|
|
|
|
|
|
size_t bytesToCopy = sourceSize;
|
|
|
|
auto data = reinterpret_cast<const uint8_t *>(source.data());
|
|
|
|
while(bytesToCopy) {
|
|
|
|
size_t nextLength = std::min(buffer.size() - (writePointer % buffer.size()), bytesToCopy);
|
|
|
|
memcpy(&buffer[writePointer % buffer.size()], data, nextLength);
|
|
|
|
|
|
|
|
bytesToCopy -= nextLength;
|
|
|
|
data += nextLength;
|
|
|
|
writePointer += nextLength;
|
|
|
|
}
|
2020-06-10 03:56:08 +00:00
|
|
|
|
|
|
|
readPointer = std::max(readPointer, writePointer - buffer.size());
|
2020-06-09 04:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
std::vector<uint8_t> buffer;
|
|
|
|
mutable size_t readPointer = 0;
|
|
|
|
size_t writePointer = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AUDIOSOURCE_H
|