Rework the sound engine to use QSound
plus misc fixes
This commit is contained in:
parent
6d56e0dfd6
commit
a06c7b2f9d
3 changed files with 40 additions and 115 deletions
|
@ -774,7 +774,7 @@ SoundSettingsPage::SoundSettingsPage()
|
||||||
masterVolumeSlider->setValue(settingsCache->getMasterVolume());
|
masterVolumeSlider->setValue(settingsCache->getMasterVolume());
|
||||||
masterVolumeSlider->setToolTip(QString::number(settingsCache->getMasterVolume()));
|
masterVolumeSlider->setToolTip(QString::number(settingsCache->getMasterVolume()));
|
||||||
connect(settingsCache, SIGNAL(masterVolumeChanged(int)), this, SLOT(masterVolumeChanged(int)));
|
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)));
|
connect(masterVolumeSlider, SIGNAL(valueChanged(int)), settingsCache, SLOT(setMasterVolume(int)));
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,130 +1,73 @@
|
||||||
#include "soundengine.h"
|
#include "soundengine.h"
|
||||||
#include "settingscache.h"
|
#include "settingscache.h"
|
||||||
#include <QAudioOutput>
|
|
||||||
#include <QAudioFormat>
|
|
||||||
#include <QFile>
|
|
||||||
#include <QBuffer>
|
|
||||||
#include <QStringList>
|
|
||||||
#if QT_VERSION > 0x050000
|
|
||||||
#include <QMediaPlayer>
|
|
||||||
#include <QUrl>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SoundEngine::SoundEngine(QObject *parent)
|
#include <QFileInfo>
|
||||||
: QObject(parent), audio(0)
|
#include <QSound>
|
||||||
{
|
|
||||||
enabled = false;
|
|
||||||
inputBuffer = new QBuffer(this);
|
|
||||||
|
|
||||||
connect(settingsCache, SIGNAL(soundPathChanged()), this, SLOT(cacheData()));
|
/*
|
||||||
connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged()));
|
|
||||||
cacheData();
|
|
||||||
soundEnabledChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoundEngine::cacheData()
|
|
||||||
{
|
|
||||||
fileNames = QStringList()
|
fileNames = QStringList()
|
||||||
/*Phases*/
|
// Phases
|
||||||
<< "untap_step" << "upkeep_step" << "draw_step" << "main_1"
|
<< "untap_step" << "upkeep_step" << "draw_step" << "main_1"
|
||||||
<< "start_combat" << "attack_step" << "block_step" << "damage_step" << "end_combat"
|
<< "start_combat" << "attack_step" << "block_step" << "damage_step" << "end_combat"
|
||||||
<< "main_2" << "end_step"
|
<< "main_2" << "end_step"
|
||||||
/*Game Actions*/
|
// Game Actions
|
||||||
<< "draw_card" << "play_card" << "tap_card" << "untap_card"
|
<< "draw_card" << "play_card" << "tap_card" << "untap_card"
|
||||||
<< "shuffle" << "roll_dice" << "life_change"
|
<< "shuffle" << "roll_dice" << "life_change"
|
||||||
/*Player*/
|
// Player
|
||||||
<< "player_join" << "player_leave" << "player_disconnect" << "player_reconnect" << "player_concede"
|
<< "player_join" << "player_leave" << "player_disconnect" << "player_reconnect" << "player_concede"
|
||||||
/*Spectator*/
|
// Spectator
|
||||||
<< "spectator_join" << "spectator_leave"
|
<< "spectator_join" << "spectator_leave"
|
||||||
/*Chat & UI*/
|
// Chat & UI
|
||||||
<< "chat_mention" << "all_mention" << "private_message";
|
<< "chat_mention" << "all_mention" << "private_message";
|
||||||
|
*/
|
||||||
|
|
||||||
#if QT_VERSION < 0x050000 //QT4
|
#define TEST_SOUND_FILENAME "player_join"
|
||||||
for (int i = 0; i < fileNames.size(); ++i) {
|
|
||||||
QFile file(settingsCache->getSoundPath() + "/" + fileNames[i] + ".wav");
|
SoundEngine::SoundEngine(QObject *parent)
|
||||||
if(!file.exists())
|
: QObject(parent), enabled(false)
|
||||||
continue;
|
{
|
||||||
file.open(QIODevice::ReadOnly);
|
connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged()));
|
||||||
file.seek(44);
|
|
||||||
audioCache.insert(fileNames[i], file.readAll());
|
soundEnabledChanged();
|
||||||
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()
|
void SoundEngine::soundEnabledChanged()
|
||||||
{
|
{
|
||||||
|
if (settingsCache->getSoundEnabled()) {
|
||||||
#if QT_VERSION < 0x050000 //QT4
|
#if QT_VERSION < 0x050000 //QT4
|
||||||
if (settingsCache->getSoundEnabled()) {
|
if(QSound::isAvailable())
|
||||||
qDebug("SoundEngine: enabling sound");
|
{
|
||||||
QAudioFormat format;
|
|
||||||
format.setSampleRate(44100);
|
|
||||||
format.setChannelCount(1);
|
|
||||||
format.setSampleSize(16);
|
|
||||||
format.setCodec("audio/pcm");
|
|
||||||
format.setByteOrder(QAudioFormat::LittleEndian);
|
|
||||||
format.setSampleType(QAudioFormat::SignedInt);
|
|
||||||
audio = new QAudioOutput(format, this);
|
|
||||||
} else if (audio) {
|
|
||||||
qDebug("SoundEngine: disabling sound");
|
|
||||||
audio->stop();
|
|
||||||
audio->deleteLater();
|
|
||||||
audio = 0;
|
|
||||||
}
|
|
||||||
#else //QT5
|
|
||||||
if (settingsCache->getSoundEnabled()) {
|
|
||||||
qDebug("SoundEngine: enabling sound");
|
qDebug("SoundEngine: enabling sound");
|
||||||
enabled = true;
|
enabled = true;
|
||||||
}else{
|
} else {
|
||||||
|
qDebug("SoundEngine: sound not available");
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
qDebug("SoundEngine: enabling sound");
|
||||||
|
enabled = true;
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
qDebug("SoundEngine: disabling sound");
|
qDebug("SoundEngine: disabling sound");
|
||||||
enabled = false;
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
void SoundEngine::playSound(QString fileName)
|
void SoundEngine::playSound(QString fileName)
|
||||||
{
|
{
|
||||||
#if QT_VERSION < 0x050000 //QT4
|
if(!enabled)
|
||||||
if (!fileNames.contains(fileName) || !audio)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
audio->stop();
|
QFileInfo fi(settingsCache->getSoundPath() + "/" + fileName + ".wav");
|
||||||
inputBuffer->close();
|
qDebug() << "playing" << fi.absoluteFilePath();
|
||||||
inputBuffer->setData(audioCache[fileName]);
|
if(!fi.exists())
|
||||||
inputBuffer->open(QIODevice::ReadOnly);
|
|
||||||
audio->start(inputBuffer);
|
|
||||||
#else //QT5
|
|
||||||
if (!audioData.contains(fileName) || !enabled){
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
audioData[fileName]->setVolume(settingsCache->getMasterVolume());
|
QSound::play(fi.absoluteFilePath());
|
||||||
audioData[fileName]->stop();
|
|
||||||
audioData[fileName]->setPosition(0);
|
|
||||||
audioData[fileName]->play();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoundEngine::testSound()
|
void SoundEngine::testSound()
|
||||||
{
|
{
|
||||||
playSound("player_join");
|
playSound(TEST_SOUND_FILENAME);
|
||||||
}
|
}
|
|
@ -2,30 +2,12 @@
|
||||||
#define SOUNDENGINE_H
|
#define SOUNDENGINE_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QMap>
|
|
||||||
#include <QDateTime>
|
|
||||||
#include <QStringList>
|
|
||||||
#if QT_VERSION > 0x050000
|
|
||||||
#include <QMediaPlayer>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class QAudioOutput;
|
|
||||||
class QBuffer;
|
|
||||||
|
|
||||||
class SoundEngine : public QObject {
|
class SoundEngine : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
QMap<QString, QByteArray> audioCache;
|
|
||||||
QBuffer *inputBuffer;
|
|
||||||
QAudioOutput *audio;
|
|
||||||
bool enabled;
|
bool enabled;
|
||||||
QStringList fileNames;
|
|
||||||
|
|
||||||
#if QT_VERSION > 0x050000
|
|
||||||
QMap<QString, QMediaPlayer*> audioData;
|
|
||||||
#endif
|
|
||||||
private slots:
|
private slots:
|
||||||
void cacheData();
|
|
||||||
void soundEnabledChanged();
|
void soundEnabledChanged();
|
||||||
public:
|
public:
|
||||||
SoundEngine(QObject *parent = 0);
|
SoundEngine(QObject *parent = 0);
|
||||||
|
|
Loading…
Reference in a new issue