1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-28 07:29:45 +00:00

Declines to set up audio output if none is available.

This commit is contained in:
Thomas Harte 2020-07-02 22:58:15 -04:00
parent 402f2ddbd9
commit 2d5e9bf1bb

View File

@ -307,29 +307,31 @@ void MainWindow::launchMachine() {
const auto speaker = audio_producer->get_speaker(); const auto speaker = audio_producer->get_speaker();
if(speaker) { if(speaker) {
const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultOutputDevice(); const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultOutputDevice();
QAudioFormat idealFormat = defaultDeviceInfo.preferredFormat(); if(!defaultDeviceInfo.isNull()) {
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 a good buffer 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;
idealFormat.setChannelCount(1 + int(audioIsStereo)); idealFormat.setChannelCount(1 + int(audioIsStereo));
idealFormat.setSampleSize(audioIs8bit ? 8 : 16); idealFormat.setSampleSize(audioIs8bit ? 8 : 16);
speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo); speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo);
speaker->set_delegate(this); speaker->set_delegate(this);
audioThread.start(); audioThread.start();
audioThread.performAsync([this, idealFormat] { audioThread.performAsync([this, idealFormat] {
// Create an audio output. // Create an audio output.
audioOutput = std::make_unique<QAudioOutput>(idealFormat); audioOutput = std::make_unique<QAudioOutput>(idealFormat);
// Start the output. The additional `audioBuffer` is meant to minimise latency, // Start the output. The additional `audioBuffer` is meant to minimise latency,
// believe it or not, given Qt's semantics. // believe it or not, given Qt's semantics.
audioOutput->setBufferSize(samplesPerBuffer * sizeof(int16_t)); audioOutput->setBufferSize(samplesPerBuffer * sizeof(int16_t));
audioOutput->start(&audioBuffer); audioOutput->start(&audioBuffer);
audioBuffer.setDepth(audioOutput->bufferSize()); audioBuffer.setDepth(audioOutput->bufferSize());
}); });
}
} }
} }