servatrice/cockatrice/src/soundengine.cpp
Matt Lowe c64eeb4ebe Added sound settings page
+ added sound setting page
+ added sound setting icon
+ moved sound settings from interface settings

Added master volume

+ added master volume slider.
+ volume can be changed by sliding bar or by using the spin box
+ Preview of  volume will be played when dragging and releasing bar.

Added checks for qt4

Qt4 does not have support for setting the volume without some lengthy
work around, I have disabled volume control for qt4 users and have
mentioned that "Master volume requires qt5".

Updated sfx

+ removed all old sfx
+ added new end step and tap sound
+ tap/endstep sound has a timer on it to prevent spamming
+ test sound engine will now use endstep sound

Made end step sfx softer

end step felt too harsh

Added player joined sound

+ added a sound for when a new player joins a room

Updated

Was missing a sound when a player joins

Made end step softer

Made end step even softer

updated sound again
2015-05-06 12:47:48 +02:00

94 lines
2.5 KiB
C++

#include "soundengine.h"
#include "settingscache.h"
#include <QAudioOutput>
#include <QAudioFormat>
#include <QFile>
#include <QBuffer>
SoundEngine::SoundEngine(QObject *parent)
: QObject(parent), audio(0)
{
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();
}
void SoundEngine::cacheData()
{
static const QStringList fileNames = QStringList()
<< "end_step" << "tap" << "player_joined";
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);
#else
format.setSampleRate(44100);
format.setChannelCount(1);
#endif
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;
}
}
void SoundEngine::playSound(const QString &fileName)
{
if (!audio)
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);
}
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");
}