From 5aaae5279c99a028aa9f6084351a41647da312c5 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Sun, 13 Sep 2015 19:37:49 +0200 Subject: [PATCH] revert to using QAudioOutput --- cockatrice/CMakeLists.txt | 8 ++ cockatrice/src/soundengine.cpp | 137 +++++++++++++++++++-------------- cockatrice/src/soundengine.h | 8 +- 3 files changed, 92 insertions(+), 61 deletions(-) diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index eceeb590..d9a4800c 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -137,14 +137,22 @@ set(COCKATRICE_LIBS) # Qt4 stuff if(Qt4_FOUND) + if (NOT QT_QTMULTIMEDIA_FOUND) + FIND_PACKAGE(QtMobility REQUIRED) + endif() + SET(QT_USE_QTNETWORK TRUE) + SET(QT_USE_QTMULTIMEDIA TRUE) SET(QT_USE_QTSVG TRUE) # Include directories INCLUDE(${QT_USE_FILE}) INCLUDE_DIRECTORIES(${QT_INCLUDES}) + INCLUDE_DIRECTORIES(${QT_MOBILITY_INCLUDE_DIR}) + INCLUDE_DIRECTORIES(${QT_MOBILITY_MULTIMEDIAKIT_INCLUDE_DIR}) LIST(APPEND COCKATRICE_LIBS ${QT_LIBRARIES}) LIST(APPEND COCKATRICE_LIBS ${QT_QTMAIN_LIBRARY}) + LIST(APPEND COCKATRICE_LIBS ${QT_MOBILITY_MULTIMEDIAKIT_LIBRARY}) # Let cmake chew Qt4's translations and resource files # Note: header files are MOC-ed automatically by cmake diff --git a/cockatrice/src/soundengine.cpp b/cockatrice/src/soundengine.cpp index 3865fd9b..dcb41b7e 100644 --- a/cockatrice/src/soundengine.cpp +++ b/cockatrice/src/soundengine.cpp @@ -2,8 +2,10 @@ #include "settingscache.h" #include +#include +#include +#include #include -#include #include #if QT_VERSION < 0x050000 #include @@ -12,29 +14,13 @@ #endif #define DEFAULT_THEME_NAME "Default" - -/* - fileNames = QStringList() - // Phases - << "untap_step" << "upkeep_step" << "draw_step" << "main_1" - << "start_combat" << "attack_step" << "block_step" << "damage_step" << "end_combat" - << "main_2" << "end_step" - // Game Actions - << "draw_card" << "play_card" << "tap_card" << "untap_card" - << "shuffle" << "roll_dice" << "life_change" - // Player - << "player_join" << "player_leave" << "player_disconnect" << "player_reconnect" << "player_concede" - // Spectator - << "spectator_join" << "spectator_leave" - // Chat & UI - << "chat_mention" << "all_mention" << "private_message"; -*/ - #define TEST_SOUND_FILENAME "player_join" SoundEngine::SoundEngine(QObject *parent) -: QObject(parent), engine(0) +: QObject(parent), player(0) { + inputBuffer = new QBuffer(this); + ensureThemeDirectoryExists(); connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot())); connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged())); @@ -45,59 +31,68 @@ SoundEngine::SoundEngine(QObject *parent) SoundEngine::~SoundEngine() { - if(engine) + if(player) { - delete engine; - engine = 0; - } + player->deleteLater(); + player = 0; + } + + inputBuffer->deleteLater(); } void SoundEngine::soundEnabledChanged() { if (settingsCache->getSoundEnabled()) { -#if QT_VERSION < 0x050000 //QT4 - if(QSound::isAvailable()) - { - qDebug("SoundEngine: enabling sound"); - enabled = true; - } else { - qDebug("SoundEngine: sound not available"); - enabled = false; - } -#else qDebug("SoundEngine: enabling sound"); - enabled = true; -#endif + if(!player) + { + QAudioFormat format; +#if QT_VERSION < 0x050000 + format.setFrequency(44100); + format.setChannels(1); + #else + format.setSampleRate(44100); + format.setChannelCount(1); + #endif + format.setSampleSize(16); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + player = new QAudioOutput(format, this); + } } else { qDebug("SoundEngine: disabling sound"); - enabled = false; + if(player) + { + player->stop(); + player->deleteLater(); + player = 0; + } } } -#include void SoundEngine::playSound(QString fileName) { - if(!enabled) + if(!player) return; - QFileInfo fi("sounds:/" + fileName + ".wav"); - qDebug() << "playing" << fi.absoluteFilePath(); - if(!fi.exists()) + // still playing the previous sound? + if(player->state() == QAudio::ActiveState) return; - if(engine) - { - if(engine->isFinished()) - { - engine->stop(); - delete engine; - } else { - return; - } - } + if(!audioData.contains(fileName)) + return; - engine = new QSound(fi.absoluteFilePath()); - engine->play(); + qDebug() << "playing" << fileName; + + inputBuffer->close(); + inputBuffer->setData(audioData[fileName]); + inputBuffer->open(QIODevice::ReadOnly); +#if QT_VERSION >= 0x050000 + player->setVolume(settingsCache->getMasterVolume()); +#endif + player->stop(); + player->start(inputBuffer); } void SoundEngine::testSound() @@ -161,8 +156,34 @@ void SoundEngine::themeChangedSlot() QDir dir = getAvailableThemes().value(themeName); - // resources - QStringList resources; - resources << dir.absolutePath(); - QDir::setSearchPaths("sounds", resources); + audioData.clear(); + + static const QStringList fileNames = QStringList() + // Phases + << "untap_step" << "upkeep_step" << "draw_step" << "main_1" + << "start_combat" << "attack_step" << "block_step" << "damage_step" << "end_combat" + << "main_2" << "end_step" + // Game Actions + << "draw_card" << "play_card" << "tap_card" << "untap_card" + << "shuffle" << "roll_dice" << "life_change" + // Player + << "player_join" << "player_leave" << "player_disconnect" << "player_reconnect" << "player_concede" + // Spectator + << "spectator_join" << "spectator_leave" + // Chat & UI + << "chat_mention" << "all_mention" << "private_message" + << "end_step" << "tap" << "player_joined" << "attack"; + + for (int i = 0; i < fileNames.size(); ++i) { + if(!dir.exists(fileNames[i] + ".wav")) + continue; + + QFile file(dir.filePath(fileNames[i] + ".wav")); + file.open(QIODevice::ReadOnly); + // 44 = length of wav header + audioData.insert(fileNames[i], file.readAll().mid(44)); + file.close(); + } + + soundEnabledChanged(); } \ No newline at end of file diff --git a/cockatrice/src/soundengine.h b/cockatrice/src/soundengine.h index ba553c82..ed15cbbf 100644 --- a/cockatrice/src/soundengine.h +++ b/cockatrice/src/soundengine.h @@ -6,7 +6,8 @@ #include #include -class QSound; +class QAudioOutput; +class QBuffer; typedef QMap QStringMap; @@ -18,8 +19,9 @@ public: void playSound(QString fileName); QStringMap &getAvailableThemes(); private: - bool enabled; - QSound * engine; + QMap audioData; + QBuffer *inputBuffer; + QAudioOutput * player; QStringMap availableThemes; protected: void ensureThemeDirectoryExists();