Merge pull request #1498 from ctrlaltca/sound_whatever

Reworked SoundEngine overhaul
This commit is contained in:
Zach 2015-09-12 00:35:20 -04:00
commit 226a8cc017
19 changed files with 100 additions and 129 deletions

View file

@ -137,22 +137,14 @@ 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
@ -281,11 +273,11 @@ if(APPLE)
set(plugin_dest_dir cockatrice.app/Contents/Plugins)
set(qtconf_dest_dir cockatrice.app/Contents/Resources)
# qt4: codecs, iconengines, imageformats, phonon_backend
# qt4: codecs, iconengines, imageformats
# qt5: audio, iconengines, imageformats, platforms, printsupport
install(DIRECTORY "${QT_PLUGINS_DIR}/" DESTINATION ${plugin_dest_dir} COMPONENT Runtime
FILES_MATCHING REGEX "(audio|codecs|iconengines|imageformats|phonon_backend|platforms|printsupport)/.*\\.dylib"
FILES_MATCHING REGEX "(audio|codecs|iconengines|imageformats|platforms|printsupport)/.*\\.dylib"
REGEX ".*_debug\\.dylib" EXCLUDE)
install(CODE "
@ -309,11 +301,11 @@ if(WIN32)
set(plugin_dest_dir Plugins)
set(qtconf_dest_dir .)
# qt4: codecs, iconengines, imageformats, phonon_backend
# qt4: codecs, iconengines, imageformats
# qt5: audio, iconengines, imageformats, platforms, printsupport
install(DIRECTORY "${QT_PLUGINS_DIR}/" DESTINATION ${plugin_dest_dir} COMPONENT Runtime
FILES_MATCHING REGEX "(audio|codecs|iconengines|imageformats|phonon_backend|platforms|printsupport)/.*[^d]\\.dll")
FILES_MATCHING REGEX "(audio|codecs|iconengines|imageformats|platforms|printsupport)/.*[^d]\\.dll")
install(CODE "
file(WRITE \"\${CMAKE_INSTALL_PREFIX}/${qtconf_dest_dir}/qt.conf\" \"[Paths]

View file

@ -11,6 +11,7 @@
#include "pixmapgenerator.h"
#include "settingscache.h"
#include "tab_userlists.h"
#include "soundengine.h"
const QColor DEFAULT_MENTION_COLOR = QColor(194, 31, 47);
const QColor OTHER_USER_COLOR = QColor(0, 65, 255); // dark blue
@ -276,6 +277,7 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send
if (userName.toLower() == fullMentionUpToSpaceOrEnd.toLower()) // Is this user you?
{
// You have received a valid mention!!
soundEngine->playSound("chat_mention");
mentionFormat.setBackground(QBrush(getCustomMentionColor()));
mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black));
cursor.insertText(mention, mentionFormat);
@ -301,6 +303,7 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send
if (isModeratorSendingGlobal(userLevel, fullMentionUpToSpaceOrEnd)) {
// Moderator Sending Global Message
soundEngine->playSound("all_mention");
mentionFormat.setBackground(QBrush(getCustomMentionColor()));
mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black));
cursor.insertText("@" + fullMentionUpToSpaceOrEnd, mentionFormat);

View file

@ -646,7 +646,7 @@ SoundSettingsPage::SoundSettingsPage()
connect(soundPathClearButton, SIGNAL(clicked()), this, SLOT(soundPathClearButtonClicked()));
QPushButton *soundPathButton = new QPushButton("...");
connect(soundPathButton, SIGNAL(clicked()), this, SLOT(soundPathButtonClicked()));
connect(&soundTestButton, SIGNAL(clicked()), soundEngine, SLOT(playerJoined()));
connect(&soundTestButton, SIGNAL(clicked()), soundEngine, SLOT(testSound()));
masterVolumeSlider = new QSlider(Qt::Horizontal);
masterVolumeSlider->setMinimum(0);
@ -654,7 +654,7 @@ SoundSettingsPage::SoundSettingsPage()
masterVolumeSlider->setValue(settingsCache->getMasterVolume());
masterVolumeSlider->setToolTip(QString::number(settingsCache->getMasterVolume()));
connect(settingsCache, SIGNAL(masterVolumeChanged(int)), this, SLOT(masterVolumeChanged(int)));
connect(masterVolumeSlider, SIGNAL(sliderReleased()), soundEngine, SLOT(playerJoined()));
connect(masterVolumeSlider, SIGNAL(sliderReleased()), soundEngine, SLOT(testSound()));
connect(masterVolumeSlider, SIGNAL(valueChanged(int)), settingsCache, SLOT(setMasterVolume(int)));

View file

@ -37,7 +37,7 @@ bool MessageLogWidget::userIsFemale() const
void MessageLogWidget::logGameJoined(int gameId)
{
soundEngine->playerJoined();
soundEngine->playSound("player_join");
if (userIsFemale())
appendHtmlServerMessage(tr("You have joined game #%1.", "female").arg("<font color=\"blue\">"+ QString::number(gameId) + "</font>"));
else
@ -54,7 +54,7 @@ void MessageLogWidget::logReplayStarted(int gameId)
void MessageLogWidget::logJoin(Player *player)
{
soundEngine->playerJoined();
soundEngine->playSound("player_join");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 has joined the game.", "female").arg(sanitizeHtml(player->getName())));
else
@ -63,6 +63,7 @@ void MessageLogWidget::logJoin(Player *player)
void MessageLogWidget::logLeave(Player *player)
{
soundEngine->playSound("player_leave");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 has left the game.", "female").arg(sanitizeHtml(player->getName())));
else
@ -81,11 +82,13 @@ void MessageLogWidget::logKicked()
void MessageLogWidget::logJoinSpectator(QString name)
{
soundEngine->playSound("spectator_join");
appendHtmlServerMessage(tr("%1 is now watching the game.").arg(sanitizeHtml(name)));
}
void MessageLogWidget::logLeaveSpectator(QString name)
{
soundEngine->playSound("spectator_leave");
appendHtmlServerMessage(tr("%1 is not watching the game any more.").arg(sanitizeHtml(name)));
}
@ -134,6 +137,7 @@ void MessageLogWidget::logSetSideboardLock(Player *player, bool locked)
void MessageLogWidget::logConcede(Player *player)
{
soundEngine->playSound("player_concede");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 has conceded the game.", "female").arg(sanitizeHtml(player->getName())));
else
@ -148,11 +152,13 @@ void MessageLogWidget::logGameStart()
void MessageLogWidget::logConnectionStateChanged(Player *player, bool connectionState)
{
if (connectionState) {
soundEngine->playSound("player_reconnect");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 has restored connection to the game.", "female").arg(sanitizeHtml(player->getName())));
else
appendHtmlServerMessage(tr("%1 has restored connection to the game.", "male").arg(sanitizeHtml(player->getName())));
} else {
soundEngine->playSound("player_disconnect");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 has lost connection to the game.", "female").arg(sanitizeHtml(player->getName())));
else
@ -172,6 +178,7 @@ void MessageLogWidget::logSpectatorSay(QString spectatorName, UserLevelFlags spe
void MessageLogWidget::logShuffle(Player *player, CardZone *zone)
{
soundEngine->playSound("shuffle");
if (currentContext != MessageContext_Mulligan) {
appendHtmlServerMessage((isFemale(player)
? tr("%1 shuffles %2.", "female")
@ -183,6 +190,7 @@ void MessageLogWidget::logShuffle(Player *player, CardZone *zone)
void MessageLogWidget::logRollDie(Player *player, int sides, int roll)
{
soundEngine->playSound("roll_dice");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 rolls a %2 with a %3-sided die.", "female").arg(sanitizeHtml(player->getName())).arg("<font color=\"blue\">" + QString::number(roll) + "</font>").arg("<font color=\"blue\">" + QString::number(sides) + "</font>"));
else
@ -194,6 +202,7 @@ void MessageLogWidget::logDrawCards(Player *player, int number)
if (currentContext == MessageContext_Mulligan)
mulliganPlayer = player;
else {
soundEngine->playSound("draw_card");
if (isFemale(player))
appendHtmlServerMessage(tr("%1 draws %2 card(s).", "female").arg(sanitizeHtml(player->getName())).arg("<font color=\"blue\">" + QString::number(number) + "</font>"));
else
@ -296,6 +305,7 @@ void MessageLogWidget::doMoveCard(LogMoveCard &attributes)
QString finalStr;
if (targetName == "table") {
soundEngine->playSound("play_card");
if (moveCardTapped.value(attributes.card))
finalStr = tr("%1 puts %2 into play tapped%3.");
else
@ -318,6 +328,7 @@ void MessageLogWidget::doMoveCard(LogMoveCard &attributes)
} else if (targetName == "sb")
finalStr = tr("%1 moves %2%3 to sideboard.");
else if (targetName == "stack") {
soundEngine->playSound("play_card");
finalStr = tr("%1 plays %2%3.");
}
@ -565,7 +576,10 @@ void MessageLogWidget::logSetCardCounter(Player *player, QString cardName, int c
void MessageLogWidget::logSetTapped(Player *player, CardItem *card, bool tapped)
{
soundEngine->tap();
if (tapped)
soundEngine->playSound("tap_card");
else
soundEngine->playSound("untap_card");
if (currentContext == MessageContext_MoveCard)
moveCardTapped.insert(card, tapped);
@ -603,6 +617,9 @@ void MessageLogWidget::logSetTapped(Player *player, CardItem *card, bool tapped)
void MessageLogWidget::logSetCounter(Player *player, QString counterName, int value, int oldValue)
{
if (counterName == "life")
soundEngine->playSound("life_change");
QString str;
if (isFemale(player))
str = tr("%1 sets counter %2 to %3 (%4%5).", "female");
@ -799,17 +816,17 @@ void MessageLogWidget::logSetActivePhase(int phase)
{
QString phaseName;
switch (phase) {
case 0: phaseName = tr("untap step"); break;
case 1: phaseName = tr("upkeep step"); break;
case 2: phaseName = tr("draw step"); break;
case 3: phaseName = tr("first main phase"); break;
case 4: phaseName = tr("beginning of combat step"); break;
case 5: phaseName = tr("declare attackers step"); soundEngine->attack(); break;
case 6: phaseName = tr("declare blockers step"); break;
case 7: phaseName = tr("combat damage step"); break;
case 8: phaseName = tr("end of combat step"); break;
case 9: phaseName = tr("second main phase"); break;
case 10: phaseName = tr("ending phase"); soundEngine->endStep(); break;
case 0: phaseName = tr("untap step"); soundEngine->playSound("untap_step"); break;
case 1: phaseName = tr("upkeep step"); soundEngine->playSound("upkeep_step"); break;
case 2: phaseName = tr("draw step"); soundEngine->playSound("draw_step"); break;
case 3: phaseName = tr("first main phase"); soundEngine->playSound("main_1"); break;
case 4: phaseName = tr("beginning of combat step"); soundEngine->playSound("start_combat"); break;
case 5: phaseName = tr("declare attackers step"); soundEngine->playSound("attack_step"); break;
case 6: phaseName = tr("declare blockers step"); soundEngine->playSound("block_step"); break;
case 7: phaseName = tr("combat damage step"); soundEngine->playSound("damage_step"); break;
case 8: phaseName = tr("end of combat step"); soundEngine->playSound("end_combat"); break;
case 9: phaseName = tr("second main phase"); soundEngine->playSound("main_2"); break;
case 10: phaseName = tr("ending phase"); soundEngine->playSound("end_step"); break;
}
appendHtml("<font color=\"green\"><b>" + QDateTime::currentDateTime().toString("[hh:mm:ss] ") + tr("It is now the %1.").arg(phaseName) + "</b></font>");
}

View file

@ -1,102 +1,73 @@
#include "soundengine.h"
#include "settingscache.h"
#include <QAudioOutput>
#include <QAudioFormat>
#include <QFile>
#include <QBuffer>
#include <QFileInfo>
#include <QSound>
/*
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), audio(0)
: QObject(parent), enabled(false)
{
inputBuffer = new QBuffer(this);
connect(settingsCache, SIGNAL(soundPathChanged()), this, SLOT(cacheData()));
connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged()));
cacheData();
soundEnabledChanged();
lastTapPlayed = QDateTime::currentDateTime();
lastEndStepPlayed = QDateTime::currentDateTime();
lastAttackStepPlayed = QDateTime::currentDateTime();
}
void SoundEngine::cacheData()
{
static const QStringList fileNames = QStringList()
<< "end_step" << "tap" << "player_joined" << "attack";
for (int i = 0; i < fileNames.size(); ++i) {
QFile file(settingsCache->getSoundPath() + "/" + fileNames[i] + ".raw");
if(!file.exists())
continue;
file.open(QIODevice::ReadOnly);
audioData.insert(fileNames[i], file.readAll());
file.close();
}
}
void SoundEngine::soundEnabledChanged()
{
if (settingsCache->getSoundEnabled()) {
qDebug("SoundEngine: enabling sound");
QAudioFormat format;
#if QT_VERSION < 0x050000
format.setFrequency(44100);
format.setChannels(1);
#if QT_VERSION < 0x050000 //QT4
if(QSound::isAvailable())
{
qDebug("SoundEngine: enabling sound");
enabled = true;
} else {
qDebug("SoundEngine: sound not available");
enabled = false;
}
#else
format.setSampleRate(44100);
format.setChannelCount(1);
qDebug("SoundEngine: enabling sound");
enabled = true;
#endif
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
audio = new QAudioOutput(format, this);
} else if (audio) {
} else {
qDebug("SoundEngine: disabling sound");
audio->stop();
audio->deleteLater();
audio = 0;
enabled = false;
}
}
void SoundEngine::playSound(const QString &fileName)
#include <QDebug>
void SoundEngine::playSound(QString fileName)
{
if (!audio)
if(!enabled)
return;
audio->stop();
inputBuffer->close();
inputBuffer->setData(audioData[fileName]);
inputBuffer->open(QIODevice::ReadOnly);
#if QT_VERSION >= 0x050000
audio->setVolume(settingsCache->getMasterVolume() / 100.0);
#endif
audio->start(inputBuffer);
QFileInfo fi(settingsCache->getSoundPath() + "/" + fileName + ".wav");
qDebug() << "playing" << fi.absoluteFilePath();
if(!fi.exists())
return;
QSound::play(fi.absoluteFilePath());
}
void SoundEngine::endStep()
void SoundEngine::testSound()
{
if (lastEndStepPlayed.secsTo(QDateTime::currentDateTime()) >= 1)
playSound("end_step");
lastEndStepPlayed = QDateTime::currentDateTime();
}
void SoundEngine::tap()
{
if (lastTapPlayed.secsTo(QDateTime::currentDateTime()) >= 1)
playSound("tap");
lastTapPlayed = QDateTime::currentDateTime();
}
void SoundEngine::playerJoined()
{
playSound("player_joined");
}
void SoundEngine::attack() {
if (lastAttackStepPlayed.secsTo(QDateTime::currentDateTime()) >= 1)
playSound("attack");
lastAttackStepPlayed = QDateTime::currentDateTime();
}
playSound(TEST_SOUND_FILENAME);
}

View file

@ -2,34 +2,19 @@
#define SOUNDENGINE_H
#include <QObject>
#include <QMap>
#include <QDateTime>
class QAudioOutput;
class QBuffer;
class SoundEngine : public QObject {
Q_OBJECT
private:
void playSound(const QString &fileName);
QMap<QString, QByteArray> audioData;
QBuffer *inputBuffer;
QAudioOutput *audio;
QDateTime lastTapPlayed;
QDateTime lastEndStepPlayed;
QDateTime lastAttackStepPlayed;
bool enabled;
private slots:
void cacheData();
void soundEnabledChanged();
public:
SoundEngine(QObject *parent = 0);
void playSound(QString fileName);
public slots:
void endStep();
void tap();
void playerJoined();
void attack();
void testSound();
};
extern SoundEngine *soundEngine;
#endif

View file

@ -3,13 +3,14 @@
#include <QHBoxLayout>
#include <QMenu>
#include <QAction>
#include <QSystemTrayIcon>
#include <QApplication>
#include "tab_message.h"
#include "abstractclient.h"
#include "chatview.h"
#include "main.h"
#include "settingscache.h"
#include <QSystemTrayIcon>
#include <QApplication>
#include "soundengine.h"
#include "pending_command.h"
#include "pb/session_commands.pb.h"
@ -111,6 +112,8 @@ void TabMessage::processUserMessageEvent(const Event_UserMessage &event)
{
const UserLevelFlags userLevel(event.sender_name() == otherUserInfo->name() ? otherUserInfo->user_level() : ownUserInfo->user_level());
chatView->appendMessage(QString::fromStdString(event.message()), QString::fromStdString(event.sender_name()), userLevel, true);
if (tabSupervisor->currentIndex() != tabSupervisor->indexOf(this))
soundEngine->playSound("private_message");
if (settingsCache->getShowMessagePopup() && shouldShowSystemPopup(event))
showSystemPopup(event);

View file

@ -2,7 +2,7 @@
#
# Installs default sound files
FILE(GLOB sounds "${CMAKE_CURRENT_SOURCE_DIR}/*.raw")
FILE(GLOB sounds "${CMAKE_CURRENT_SOURCE_DIR}/*.wav")
if(UNIX)
if(APPLE)

Binary file not shown.

BIN
sounds/attack_step.wav Normal file

Binary file not shown.

Binary file not shown.

BIN
sounds/end_step.wav Normal file

Binary file not shown.

BIN
sounds/player_join.wav Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
sounds/start_combat.wav Normal file

Binary file not shown.

Binary file not shown.

BIN
sounds/tap_card.wav Normal file

Binary file not shown.

View file

@ -10,7 +10,7 @@ if [[ $TRAVIS_OS_NAME == "osx" ]] ; then
else
if (( QT4 )); then
sudo apt-get update -qq
sudo apt-get install -y qtmobility-dev libprotobuf-dev protobuf-compiler libqt4-dev
sudo apt-get install -y libprotobuf-dev protobuf-compiler libqt4-dev
else
sudo add-apt-repository -y ppa:beineri/opt-qt521
sudo add-apt-repository -y ppa:kalakris/cmake