From 0c1c0fcb56ef937a99235ba826f47908ef09a99d Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sat, 21 May 2011 23:03:51 +0200 Subject: [PATCH] added sound engine files --- cockatrice/src/soundengine.cpp | 77 ++++++++++++++++++++++++++++++++++ cockatrice/src/soundengine.h | 32 ++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 cockatrice/src/soundengine.cpp create mode 100644 cockatrice/src/soundengine.h diff --git a/cockatrice/src/soundengine.cpp b/cockatrice/src/soundengine.cpp new file mode 100644 index 00000000..5daeae9a --- /dev/null +++ b/cockatrice/src/soundengine.cpp @@ -0,0 +1,77 @@ +#include "soundengine.h" +#include "settingscache.h" +#include +#include +#include +#include + +SoundEngine::SoundEngine(QObject *parent) + : QObject(parent) +{ + inputBuffer = new QBuffer; + QAudioFormat format; + format.setFrequency(44100); + format.setChannels(1); + format.setSampleSize(16); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + audio = new QAudioOutput(format, this); + + connect(settingsCache, SIGNAL(soundPathChanged()), this, SLOT(cacheData())); + cacheData(); +} + +void SoundEngine::cacheData() +{ + static const QStringList fileNames = QStringList() + << "notification" << "draw" << "playcard" << "shuffle" << "tap" << "untap"; + for (int i = 0; i < fileNames.size(); ++i) { + QFile file(settingsCache->getSoundPath() + "/" + fileNames[i] + ".raw"); + file.open(QIODevice::ReadOnly); + audioData.insert(fileNames[i], file.readAll()); + file.close(); + } +} + +void SoundEngine::playSound(const QString &fileName) +{ + if (!settingsCache->getSoundEnabled()) + return; + + audio->stop(); + inputBuffer->close(); + inputBuffer->setData(audioData[fileName]); + inputBuffer->open(QIODevice::ReadOnly); + audio->start(inputBuffer); +} + +void SoundEngine::notification() +{ + playSound("notification"); +} + +void SoundEngine::draw() +{ + playSound("draw"); +} + +void SoundEngine::playCard() +{ + playSound("playcard"); +} + +void SoundEngine::shuffle() +{ + playSound("shuffle"); +} + +void SoundEngine::tap() +{ + playSound("tap"); +} + +void SoundEngine::untap() +{ + playSound("untap"); +} diff --git a/cockatrice/src/soundengine.h b/cockatrice/src/soundengine.h new file mode 100644 index 00000000..312a476b --- /dev/null +++ b/cockatrice/src/soundengine.h @@ -0,0 +1,32 @@ +#ifndef SOUNDENGINE_H +#define SOUNDENGINE_H + +#include +#include + +class QAudioOutput; +class QBuffer; + +class SoundEngine : public QObject { + Q_OBJECT +private: + void playSound(const QString &fileName); + QMap audioData; + QBuffer *inputBuffer; + QAudioOutput *audio; +private slots: + void cacheData(); +public: + SoundEngine(QObject *parent = 0); +public slots: + void notification(); + void draw(); + void playCard(); + void shuffle(); + void tap(); + void untap(); +}; + +extern SoundEngine *soundEngine; + +#endif \ No newline at end of file