From c1778a8fee75a478c7532430d0a4bb0b56e59c68 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 30 Dec 2023 23:03:15 -0500 Subject: [PATCH] Reintroduce Qt5 support. --- OSBindings/Qt/clksignal.pro | 5 +++-- OSBindings/Qt/mainwindow.cpp | 25 ++++++++++++++++++++++++- OSBindings/Qt/mainwindow.h | 13 ++++++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/OSBindings/Qt/clksignal.pro b/OSBindings/Qt/clksignal.pro index 3353d979e..8b3c3b8e2 100644 --- a/OSBindings/Qt/clksignal.pro +++ b/OSBindings/Qt/clksignal.pro @@ -1,4 +1,5 @@ -QT += core gui multimedia widgets openglwidgets +QT += core gui multimedia widgets +greaterThan(5, QT_MAJOR_VERSION) QT += openglwidgets # Be specific about C++17 but also try the vaguer C++1z for older # versions of Qt. @@ -25,7 +26,7 @@ DEFINES += IGNORE_APPLE QMAKE_CXXFLAGS_RELEASE += -DNDEBUG # Generate warnings for any use of APIs deprecated prior to Qt 7.0.0. -# Development was performed against Qt 6.6.1. +# Development was performed against Qt 6.6.1 and Qt 5.15.2 DEFINES += QT_DEPRECATED_WARNINGS DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x070000 diff --git a/OSBindings/Qt/mainwindow.cpp b/OSBindings/Qt/mainwindow.cpp index d0b9a53e9..7e4df42f6 100644 --- a/OSBindings/Qt/mainwindow.cpp +++ b/OSBindings/Qt/mainwindow.cpp @@ -2,14 +2,17 @@ #include "settings.h" #include "timer.h" +#include + #include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #include #include +#endif #include -#include #include @@ -317,16 +320,32 @@ void MainWindow::launchMachine() { static constexpr size_t samplesPerBuffer = 256; // TODO: select this dynamically. const auto speaker = audio_producer->get_speaker(); if(speaker) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) QAudioDevice device(QMediaDevices::defaultAudioOutput()); if(true) { // TODO: how to check that audio output is available in Qt6? QAudioFormat idealFormat = device.preferredFormat(); +#else + const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultOutputDevice(); + if(!defaultDeviceInfo.isNull()) { + QAudioFormat idealFormat = defaultDeviceInfo.preferredFormat(); +#endif // 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. audioIsStereo = (idealFormat.channelCount() > 1) && speaker->get_is_stereo(); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) audioIs8bit = idealFormat.sampleFormat() == QAudioFormat::UInt8; +#else + audioIs8bit = idealFormat.sampleSize() < 16; +#endif + idealFormat.setChannelCount(1 + int(audioIsStereo)); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) idealFormat.setSampleFormat(audioIs8bit ? QAudioFormat::UInt8 : QAudioFormat::Int16); +#else + idealFormat.setSampleSize(audioIs8bit ? 8 : 16); +#endif speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo); speaker->set_delegate(this); @@ -334,7 +353,11 @@ void MainWindow::launchMachine() { audioThread.start(); audioThread.performAsync([&] { // Create an audio output. +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) audioOutput = std::make_unique(device, idealFormat); +#else + audioOutput = std::make_unique(idealFormat); +#endif // Start the output. The additional `audioBuffer` is meant to minimise latency, // believe it or not, given Qt's semantics. diff --git a/OSBindings/Qt/mainwindow.h b/OSBindings/Qt/mainwindow.h index a1aa5e80b..f2dd530fa 100644 --- a/OSBindings/Qt/mainwindow.h +++ b/OSBindings/Qt/mainwindow.h @@ -1,7 +1,14 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include +#include + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + #include +#else + #include +#endif + #include #include @@ -70,7 +77,11 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat std::unique_ptr machine; std::mutex machineMutex; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) std::unique_ptr audioOutput; +#else + std::unique_ptr audioOutput; +#endif bool audioIs8bit = false, audioIsStereo = false; void speaker_did_complete_samples(Outputs::Speaker::Speaker *speaker, const std::vector &buffer) override; AudioBuffer audioBuffer;