revert to using QAudioOutput

This commit is contained in:
Fabio Bas 2015-09-13 19:37:49 +02:00
parent f78c01fa88
commit 5aaae5279c
3 changed files with 92 additions and 61 deletions

View file

@ -137,14 +137,22 @@ set(COCKATRICE_LIBS)
# Qt4 stuff # Qt4 stuff
if(Qt4_FOUND) if(Qt4_FOUND)
if (NOT QT_QTMULTIMEDIA_FOUND)
FIND_PACKAGE(QtMobility REQUIRED)
endif()
SET(QT_USE_QTNETWORK TRUE) SET(QT_USE_QTNETWORK TRUE)
SET(QT_USE_QTMULTIMEDIA TRUE)
SET(QT_USE_QTSVG TRUE) SET(QT_USE_QTSVG TRUE)
# Include directories # Include directories
INCLUDE(${QT_USE_FILE}) INCLUDE(${QT_USE_FILE})
INCLUDE_DIRECTORIES(${QT_INCLUDES}) 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_LIBRARIES})
LIST(APPEND COCKATRICE_LIBS ${QT_QTMAIN_LIBRARY}) LIST(APPEND COCKATRICE_LIBS ${QT_QTMAIN_LIBRARY})
LIST(APPEND COCKATRICE_LIBS ${QT_MOBILITY_MULTIMEDIAKIT_LIBRARY})
# Let cmake chew Qt4's translations and resource files # Let cmake chew Qt4's translations and resource files
# Note: header files are MOC-ed automatically by cmake # Note: header files are MOC-ed automatically by cmake

View file

@ -2,8 +2,10 @@
#include "settingscache.h" #include "settingscache.h"
#include <QApplication> #include <QApplication>
#include <QAudioOutput>
#include <QBuffer>
#include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QSound>
#include <QLibraryInfo> #include <QLibraryInfo>
#if QT_VERSION < 0x050000 #if QT_VERSION < 0x050000
#include <QDesktopServices> #include <QDesktopServices>
@ -12,29 +14,13 @@
#endif #endif
#define DEFAULT_THEME_NAME "Default" #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" #define TEST_SOUND_FILENAME "player_join"
SoundEngine::SoundEngine(QObject *parent) SoundEngine::SoundEngine(QObject *parent)
: QObject(parent), engine(0) : QObject(parent), player(0)
{ {
inputBuffer = new QBuffer(this);
ensureThemeDirectoryExists(); ensureThemeDirectoryExists();
connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot())); connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot()));
connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged())); connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged()));
@ -45,59 +31,68 @@ SoundEngine::SoundEngine(QObject *parent)
SoundEngine::~SoundEngine() SoundEngine::~SoundEngine()
{ {
if(engine) if(player)
{ {
delete engine; player->deleteLater();
engine = 0; player = 0;
} }
inputBuffer->deleteLater();
} }
void SoundEngine::soundEnabledChanged() void SoundEngine::soundEnabledChanged()
{ {
if (settingsCache->getSoundEnabled()) { if (settingsCache->getSoundEnabled()) {
#if QT_VERSION < 0x050000 //QT4 qDebug("SoundEngine: enabling sound");
if(QSound::isAvailable()) if(!player)
{ {
qDebug("SoundEngine: enabling sound"); QAudioFormat format;
enabled = true; #if QT_VERSION < 0x050000
} else { format.setFrequency(44100);
qDebug("SoundEngine: sound not available"); format.setChannels(1);
enabled = false; #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: enabling sound");
enabled = true;
#endif
} else { } else {
qDebug("SoundEngine: disabling sound"); qDebug("SoundEngine: disabling sound");
enabled = false; if(player)
{
player->stop();
player->deleteLater();
player = 0;
}
} }
} }
#include <QDebug>
void SoundEngine::playSound(QString fileName) void SoundEngine::playSound(QString fileName)
{ {
if(!enabled) if(!player)
return; return;
QFileInfo fi("sounds:/" + fileName + ".wav"); // still playing the previous sound?
qDebug() << "playing" << fi.absoluteFilePath(); if(player->state() == QAudio::ActiveState)
if(!fi.exists())
return; return;
if(engine) if(!audioData.contains(fileName))
{
if(engine->isFinished())
{
engine->stop();
delete engine;
} else {
return; return;
}
}
engine = new QSound(fi.absoluteFilePath()); qDebug() << "playing" << fileName;
engine->play();
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() void SoundEngine::testSound()
@ -161,8 +156,34 @@ void SoundEngine::themeChangedSlot()
QDir dir = getAvailableThemes().value(themeName); QDir dir = getAvailableThemes().value(themeName);
// resources audioData.clear();
QStringList resources;
resources << dir.absolutePath(); static const QStringList fileNames = QStringList()
QDir::setSearchPaths("sounds", resources); // 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();
} }

View file

@ -6,7 +6,8 @@
#include <QDir> #include <QDir>
#include <QString> #include <QString>
class QSound; class QAudioOutput;
class QBuffer;
typedef QMap<QString, QString> QStringMap; typedef QMap<QString, QString> QStringMap;
@ -18,8 +19,9 @@ public:
void playSound(QString fileName); void playSound(QString fileName);
QStringMap &getAvailableThemes(); QStringMap &getAvailableThemes();
private: private:
bool enabled; QMap<QString, QByteArray> audioData;
QSound * engine; QBuffer *inputBuffer;
QAudioOutput * player;
QStringMap availableThemes; QStringMap availableThemes;
protected: protected:
void ensureThemeDirectoryExists(); void ensureThemeDirectoryExists();