SoundEngine overhaul

This commit is contained in:
Jeff 2015-08-26 15:04:53 -04:00
parent 311536d1aa
commit d6398477ae
16 changed files with 128 additions and 81 deletions

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

@ -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

@ -4,48 +4,76 @@
#include <QAudioFormat>
#include <QFile>
#include <QBuffer>
#include <QStringList>
#if QT_VERSION > 0x050000
#include <QMediaPlayer>
#include <QUrl>
#endif
SoundEngine::SoundEngine(QObject *parent)
: QObject(parent), audio(0)
{
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";
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";
#if QT_VERSION < 0x050000 //QT4
for (int i = 0; i < fileNames.size(); ++i) {
QFile file(settingsCache->getSoundPath() + "/" + fileNames[i] + ".raw");
QFile file(settingsCache->getSoundPath() + "/" + fileNames[i] + ".wav");
if(!file.exists())
continue;
file.open(QIODevice::ReadOnly);
audioData.insert(fileNames[i], file.readAll());
file.seek(44);
audioCache.insert(fileNames[i], file.readAll());
file.close();
}
#else //QT5
QStringList ext = QStringList() << ".mp4" << ".mp3" << ".wav";
for (int i = 0; i < fileNames.size(); ++i) {
for (int j = 0; j < ext.size(); ++j) {
QString filepath = settingsCache->getSoundPath() + "/" + fileNames[i] + ext[j];
QFile file(filepath);
if (file.exists()){
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile(filepath));
audioData.insert(fileNames[i], player);
break;
}
}
}
#endif
}
void SoundEngine::soundEnabledChanged()
{
#if QT_VERSION < 0x050000 //QT4
if (settingsCache->getSoundEnabled()) {
qDebug("SoundEngine: enabling sound");
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);
@ -57,46 +85,41 @@ void SoundEngine::soundEnabledChanged()
audio->deleteLater();
audio = 0;
}
#else //QT5
if (settingsCache->getSoundEnabled()) {
qDebug("SoundEngine: enabling sound");
enabled = true;
}else{
qDebug("SoundEngine: disabling sound");
enabled = false;
for (int i = 0; i < fileNames.size(); ++i) {
if (audioData.contains(fileNames[i]) &&
audioData[fileNames[i]]->state() != QMediaPlayer::StoppedState)
audioData[fileNames[i]]->stop();
}
}
#endif
}
void SoundEngine::playSound(const QString &fileName)
void SoundEngine::playSound(QString fileName)
{
if (!audio)
#if QT_VERSION < 0x050000 //QT4
if (!fileNames.contains(fileName) || !audio)
return;
audio->stop();
inputBuffer->close();
inputBuffer->setData(audioData[fileName]);
inputBuffer->setData(audioCache[fileName]);
inputBuffer->open(QIODevice::ReadOnly);
#if QT_VERSION >= 0x050000
audio->setVolume(settingsCache->getMasterVolume() / 100.0);
#endif
audio->start(inputBuffer);
#else //QT5
if (!audioData.contains(fileName) || !enabled){
return;
}
void SoundEngine::endStep()
{
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();
audioData[fileName]->setVolume(settingsCache->getMasterVolume());
audioData[fileName]->stop();
audioData[fileName]->setPosition(0);
audioData[fileName]->play();
#endif
}

View file

@ -4,6 +4,10 @@
#include <QObject>
#include <QMap>
#include <QDateTime>
#include <QStringList>
#if QT_VERSION > 0x050000
#include <QMediaPlayer>
#endif
class QAudioOutput;
class QBuffer;
@ -11,25 +15,22 @@ class QBuffer;
class SoundEngine : public QObject {
Q_OBJECT
private:
void playSound(const QString &fileName);
QMap<QString, QByteArray> audioData;
QMap<QString, QByteArray> audioCache;
QBuffer *inputBuffer;
QAudioOutput *audio;
QDateTime lastTapPlayed;
QDateTime lastEndStepPlayed;
QDateTime lastAttackStepPlayed;
bool enabled;
QStringList fileNames;
#if QT_VERSION > 0x050000
QMap<QString, QMediaPlayer*> audioData;
#endif
private slots:
void cacheData();
void soundEnabledChanged();
public:
SoundEngine(QObject *parent = 0);
public slots:
void endStep();
void tap();
void playerJoined();
void attack();
void playSound(QString fileName);
};
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.wav Normal file

Binary file not shown.

Binary file not shown.

BIN
sounds/end_step.wav Normal file

Binary file not shown.

Binary file not shown.

BIN
sounds/player_joined.wav Normal file

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.wav Normal file

Binary file not shown.