| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2013 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "audioworker.hpp" |
| 17 | #include "audiosource.hpp" |
| 18 | #include "openaloggdata.hpp" |
| 19 | #include "openalwavdata.hpp" |
| 20 | #include "settings.hpp" |
| 21 | |
| 22 | #include <QDir> |
| 23 | #include <QFile> |
| 24 | #include <QString> |
| 25 | |
| 26 | #include <sstream> |
| 27 | |
| 28 | #include <AL/al.h> |
| 29 | |
| 30 | static const int MAX_DIST = 250; |
| 31 | static const int REFERENCE_DIST = 50; |
| 32 | |
| 33 | AudioWorker::AudioWorker(int numCars, bool enabled) |
| 34 | : m_openALDevice(std::make_shared<OpenALDevice>()) |
| 35 | , m_inited(false) |
| 36 | , m_defaultVolume(0.5) |
| 37 | , m_numCars(numCars) |
| 38 | , m_enabled(enabled) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void AudioWorker::init() |
| 43 | { |
| 44 | m_openALDevice->initialize(); // Throws on failure |
| 45 | |
| 46 | alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); |
| 47 | alSpeedOfSound(1000.0); |
| 48 | } |
| 49 | |
| 50 | void AudioWorker::checkFile(QString path) |
| 51 | { |
| 52 | if (!QFile(path).exists()) |
| 53 | { |
| 54 | throw std::runtime_error("File not found: '" + path.toStdString() + "'"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool AudioWorker::enabled() const |
| 59 | { |
| 60 | return m_enabled; |
| 61 | } |
| 62 | |
| 63 | void AudioWorker::connectAudioSource(AudioSource & source) |
| 64 | { |
| 65 | connect(&source, &AudioSource::playRequested, this, &AudioWorker::playSound); |
| 66 | connect(&source, &AudioSource::stopRequested, this, &AudioWorker::stopSound); |
| 67 | connect(&source, &AudioSource::pitchChanged, this, &AudioWorker::setPitch); |
| 68 | connect(&source, &AudioSource::volumeChanged, this, &AudioWorker::setVolume); |
| 69 | connect(&source, &AudioSource::locationChanged, this, &AudioWorker::setLocation); |
| 70 | } |
| 71 | |
| 72 | void AudioWorker::disconnectAudioSource(AudioSource & source) |
| 73 | { |
| 74 | disconnect(&source, &AudioSource::playRequested, this, &AudioWorker::playSound); |
| 75 | disconnect(&source, &AudioSource::stopRequested, this, &AudioWorker::stopSound); |
| 76 | disconnect(&source, &AudioSource::pitchChanged, this, &AudioWorker::setPitch); |
| 77 | disconnect(&source, &AudioSource::volumeChanged, this, &AudioWorker::setVolume); |
| 78 | disconnect(&source, &AudioSource::locationChanged, this, &AudioWorker::setLocation); |
| 79 | } |
| 80 | |
| 81 | void AudioWorker::loadSounds() |
| 82 | { |
| 83 | loadCommonSound("bell", "bell.ogg", m_defaultVolume * 0.5f); |
| 84 | loadCommonSound("cheering", "cheering.ogg", m_defaultVolume * 0.5f); |
| 85 | loadCommonSound("menuBoom", "menuBoom.ogg", m_defaultVolume * 0.5f); |
| 86 | loadCommonSound("menuClick", "menuClick.ogg", m_defaultVolume * 1.0f); |
| 87 | loadCommonSound("pit", "pit.ogg", m_defaultVolume); |
| 88 | |
| 89 | loadMultiInstanceCarSound("carEngine", "carEngine.ogg", m_defaultVolume * 0.33f); |
| 90 | |
| 91 | loadMultiInstanceCarSound("carHit", "carHit.ogg", m_defaultVolume * 0.5f); |
| 92 | loadMultiInstanceCarSound("skid", "skid.ogg", m_defaultVolume * 0.25f); |
| 93 | |
| 94 | loadSingleInstanceCarSound("carHit2", "carHit2.ogg", m_defaultVolume * 0.5f); |
| 95 | loadSingleInstanceCarSound("carHit3", "carHit3.ogg", m_defaultVolume * 0.5f); |
| 96 | } |
| 97 | |
| 98 | void AudioWorker::loadSingleInstanceCarSound(QString handle, QString path, float volume) |
| 99 | { |
| 100 | const auto soundPath = |
| 101 | QString(DATA_PATH) + QDir::separator() + "sounds" + QDir::separator() + path; |
| 102 | checkFile(soundPath); |
| 103 | |
| 104 | const auto source(std::make_shared<OpenALSource>(std::make_shared<OpenALOggData>(soundPath.toStdString()))); |
| 105 | source->setMaxDist(MAX_DIST); |
| 106 | source->setReferenceDist(REFERENCE_DIST); |
| 107 | source->setVolume(volume); |
| 108 | m_soundMap[handle] = source; |
| 109 | } |
| 110 | |
| 111 | void AudioWorker::loadCommonSound(QString handle, QString path, float volume) |
| 112 | { |
| 113 | const QString soundPath = |
| 114 | QString(DATA_PATH) + QDir::separator() + "sounds" + QDir::separator() + path; |
| 115 | checkFile(soundPath); |
| 116 | |
| 117 | m_soundMap[handle] = std::make_shared<OpenALSource>(std::make_shared<OpenALOggData>(soundPath.toStdString())); |
| 118 | m_soundMap[handle]->setVolume(volume); |
| 119 | } |
| 120 | |
| 121 | void AudioWorker::loadMultiInstanceCarSound(QString baseName, QString path, float volume) |
| 122 | { |
| 123 | const QString soundPath = |
| 124 | QString(DATA_PATH) + QDir::separator() + "sounds" + QDir::separator() + path; |
| 125 | checkFile(soundPath); |
| 126 | |
| 127 | const auto sharedData = std::make_shared<OpenALOggData>(soundPath.toStdString()); |
| 128 | |
| 129 | for (int i = 0; i < m_numCars; i++) |
| 130 | { |
| 131 | std::stringstream ss; |
| 132 | ss << baseName.toStdString() << i; |
| 133 | const auto source = std::make_shared<OpenALSource>(sharedData); |
| 134 | m_soundMap[ss.str().c_str()] = source; |
| 135 | source->setMaxDist(MAX_DIST); |
| 136 | source->setReferenceDist(REFERENCE_DIST); |
| 137 | source->setVolume(volume); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void AudioWorker::playSound(const QString & handle, bool loop) |
| 142 | { |
| 143 | if (m_soundMap.count(handle) && m_enabled) |
| 144 | m_soundMap[handle]->play(loop); |
| 145 | } |
| 146 | |
| 147 | void AudioWorker::stopSound(const QString & handle) |
| 148 | { |
| 149 | if (m_soundMap.count(handle)) |
| 150 | m_soundMap[handle]->stop(); |
| 151 | } |
| 152 | |
| 153 | void AudioWorker::setPitch(const QString & handle, float pitch) |
| 154 | { |
| 155 | if (m_soundMap.count(handle)) |
| 156 | m_soundMap[handle]->setPitch(pitch); |
| 157 | } |
| 158 | |
| 159 | void AudioWorker::setVolume(const QString & handle, float volume) |
| 160 | { |
| 161 | if (m_soundMap.count(handle)) |
| 162 | m_soundMap[handle]->setVolume(volume); |
| 163 | } |
| 164 | |
| 165 | void AudioWorker::setDefaultVolume(float volume) |
| 166 | { |
| 167 | m_defaultVolume = volume; |
| 168 | } |
| 169 | |
| 170 | void AudioWorker::setLocation(const QString & handle, float x, float y) |
| 171 | { |
| 172 | if (m_soundMap.count(handle)) |
| 173 | m_soundMap[handle]->setLocation(STFH::Location(x, y)); |
| 174 | } |
| 175 | |
| 176 | void AudioWorker::setListenerLocation(float x, float y) |
| 177 | { |
| 178 | alListener3f(AL_POSITION, x, y, 1); |
| 179 | } |
| 180 | |
| 181 | void AudioWorker::setEnabled(bool enabled) |
| 182 | { |
| 183 | m_enabled = enabled; |
| 184 | } |
| 185 | |
| 186 | AudioWorker::~AudioWorker() = default; |