mirror of
https://github.com/TomHarte/CLK.git
synced 2024-11-23 03:32:32 +00:00
Actually, QIODevice is listed as reentrant. So no need to forward audio.
That said, latency is still absurd for some reason.
This commit is contained in:
parent
7c7cb61d2f
commit
63ad1290f4
@ -7,15 +7,6 @@
|
|||||||
|
|
||||||
#include "../../Numeric/CRC.hpp"
|
#include "../../Numeric/CRC.hpp"
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
struct AudioEvent: public QEvent {
|
|
||||||
AudioEvent() : QEvent(QEvent::Type::User) {}
|
|
||||||
std::vector<int16_t> audio;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
General Qt implementation notes:
|
General Qt implementation notes:
|
||||||
|
|
||||||
@ -177,17 +168,17 @@ void MainWindow::launchMachine() {
|
|||||||
QAudioFormat idealFormat = defaultDeviceInfo.preferredFormat();
|
QAudioFormat idealFormat = defaultDeviceInfo.preferredFormat();
|
||||||
|
|
||||||
// Use the ideal format's sample rate, provide stereo as long as at least two channels
|
// Use the ideal format's sample rate, provide stereo as long as at least two channels
|
||||||
// are available, and — at least for now — assume 512 samples/buffer is a good size.
|
// are available, and — at least for now — assume a good buffer size.
|
||||||
audioIsStereo = (idealFormat.channelCount() > 1) && speaker->get_is_stereo();
|
audioIsStereo = (idealFormat.channelCount() > 1) && speaker->get_is_stereo();
|
||||||
audioIs8bit = idealFormat.sampleSize() < 16;
|
audioIs8bit = idealFormat.sampleSize() < 16;
|
||||||
const int samplesPerBuffer = 512;
|
const int samplesPerBuffer = 256;
|
||||||
speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo);
|
speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo);
|
||||||
|
|
||||||
// Adjust format appropriately, and create an audio output.
|
// Adjust format appropriately, and create an audio output.
|
||||||
idealFormat.setChannelCount(1 + int(audioIsStereo));
|
idealFormat.setChannelCount(1 + int(audioIsStereo));
|
||||||
idealFormat.setSampleSize(audioIs8bit ? 8 : 16);
|
idealFormat.setSampleSize(audioIs8bit ? 8 : 16);
|
||||||
audioOutput = std::make_unique<QAudioOutput>(idealFormat, this);
|
audioOutput = std::make_unique<QAudioOutput>(idealFormat, this);
|
||||||
audioOutput->setBufferSize(samplesPerBuffer * (audioIsStereo ? 2 : 1) * (audioIs8bit ? 1 : 2));
|
// audioOutput->setBufferSize(samplesPerBuffer * (audioIsStereo ? 2 : 1) * (audioIs8bit ? 1 : 2));
|
||||||
|
|
||||||
qDebug() << idealFormat;
|
qDebug() << idealFormat;
|
||||||
|
|
||||||
@ -235,13 +226,13 @@ void MainWindow::launchMachine() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::speaker_did_complete_samples(Outputs::Speaker::Speaker *, const std::vector<int16_t> &buffer) {
|
void MainWindow::speaker_did_complete_samples(Outputs::Speaker::Speaker *, const std::vector<int16_t> &buffer) {
|
||||||
// Forward this buffrer to the QThread that QAudioOutput lives on.
|
const char *data = reinterpret_cast<const char *>(buffer.data());
|
||||||
AudioEvent *event = new AudioEvent;
|
size_t sizeLeft = buffer.size() * sizeof(int16_t);
|
||||||
event->audio = buffer;
|
while(sizeLeft) {
|
||||||
QApplication::instance()->postEvent(this, event);
|
const auto bytesWritten = audioIODevice->write(data, qint64(sizeLeft));
|
||||||
// const auto bytesWritten = audioIODevice->write(reinterpret_cast<const char *>(buffer.data()), qint64(buffer.size()));
|
sizeLeft -= bytesWritten;
|
||||||
// qDebug() << bytesWritten << "; " << audioOutput->state();
|
data += bytesWritten;
|
||||||
// (void)bytesWritten;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
|
void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
|
||||||
@ -319,19 +310,6 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case QEvent::User: {
|
|
||||||
const auto audioEvent = dynamic_cast<AudioEvent *>(event);
|
|
||||||
if(audioEvent) {
|
|
||||||
const char *buffer = reinterpret_cast<const char *>(audioEvent->audio.data());
|
|
||||||
size_t sizeLeft = audioEvent->audio.size() * sizeof(int16_t);
|
|
||||||
while(sizeLeft) {
|
|
||||||
const auto bytesWritten = audioIODevice->write(buffer, qint64(sizeLeft));
|
|
||||||
sizeLeft -= bytesWritten;
|
|
||||||
buffer += bytesWritten;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user