mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 00:04:48 -07:00
SoundEngine overhaul
This commit is contained in:
parent
311536d1aa
commit
d6398477ae
16 changed files with 128 additions and 81 deletions
|
|
@ -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)
|
||||
: 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";
|
||||
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();
|
||||
}
|
||||
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] + ".wav");
|
||||
if(!file.exists())
|
||||
continue;
|
||||
file.open(QIODevice::ReadOnly);
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue