2012-04-13 21:54:08 +02:00
|
|
|
/*
|
2017-01-13 11:47:07 +01:00
|
|
|
* Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
|
2012-04-13 21:54:08 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "soundmanager.h"
|
|
|
|
#include "soundsource.h"
|
|
|
|
#include "soundbuffer.h"
|
|
|
|
#include "soundfile.h"
|
|
|
|
#include "streamsoundsource.h"
|
2012-04-14 15:15:51 +02:00
|
|
|
#include "combinedsoundsource.h"
|
2012-04-13 21:54:08 +02:00
|
|
|
|
|
|
|
#include <framework/core/clock.h>
|
|
|
|
#include <framework/core/eventdispatcher.h>
|
2012-07-18 03:22:21 +02:00
|
|
|
#include <framework/core/resourcemanager.h>
|
2013-03-04 22:56:22 +01:00
|
|
|
#include <framework/core/asyncdispatcher.h>
|
|
|
|
#include <thread>
|
2012-04-13 21:54:08 +02:00
|
|
|
|
|
|
|
SoundManager g_sounds;
|
|
|
|
|
|
|
|
void SoundManager::init()
|
|
|
|
{
|
|
|
|
m_device = alcOpenDevice(NULL);
|
|
|
|
if(!m_device) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error("unable to open audio device");
|
2012-04-13 21:54:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_context = alcCreateContext(m_device, NULL);
|
|
|
|
if(!m_context) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error(stdext::format("unable to create audio context: %s", alcGetString(m_device, alcGetError(m_device))));
|
2012-04-13 21:54:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
if(alcMakeContextCurrent(m_context) != ALC_TRUE) {
|
|
|
|
g_logger.error(stdext::format("unable to make context current: %s", alcGetString(m_device, alcGetError(m_device))));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::terminate()
|
|
|
|
{
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
|
|
|
|
2013-03-06 17:05:46 +01:00
|
|
|
for(auto it = m_streamFiles.begin(); it != m_streamFiles.end();++it) {
|
|
|
|
auto& future = it->second;
|
|
|
|
future.wait();
|
|
|
|
}
|
2013-03-04 22:56:22 +01:00
|
|
|
m_streamFiles.clear();
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
m_sources.clear();
|
|
|
|
m_buffers.clear();
|
2013-01-16 19:46:42 +01:00
|
|
|
m_channels.clear();
|
2013-03-04 22:56:22 +01:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
m_audioEnabled = false;
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2012-04-14 21:52:00 +02:00
|
|
|
alcMakeContextCurrent(nullptr);
|
2012-04-13 21:54:08 +02:00
|
|
|
|
|
|
|
if(m_context) {
|
|
|
|
alcDestroyContext(m_context);
|
|
|
|
m_context = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_device) {
|
|
|
|
alcCloseDevice(m_device);
|
|
|
|
m_device = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-14 11:53:32 +02:00
|
|
|
void SoundManager::poll()
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
|
|
|
static ticks_t lastUpdate = 0;
|
2012-06-02 16:43:27 +02:00
|
|
|
ticks_t now = g_clock.millis();
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2012-04-14 11:53:32 +02:00
|
|
|
if(now - lastUpdate < POLL_DELAY)
|
2012-04-13 21:54:08 +02:00
|
|
|
return;
|
2012-04-14 11:53:32 +02:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
lastUpdate = now;
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
2013-03-04 22:56:22 +01:00
|
|
|
|
|
|
|
for(auto it = m_streamFiles.begin(); it != m_streamFiles.end();) {
|
|
|
|
StreamSoundSourcePtr source = it->first;
|
2013-03-05 06:54:08 +01:00
|
|
|
auto& future = it->second;
|
2013-03-04 22:56:22 +01:00
|
|
|
|
2013-03-05 06:54:08 +01:00
|
|
|
if(future.is_ready()) {
|
2013-03-04 22:56:22 +01:00
|
|
|
SoundFilePtr sound = future.get();
|
|
|
|
if(sound)
|
|
|
|
source->setSoundFile(sound);
|
|
|
|
else
|
|
|
|
source->stop();
|
|
|
|
it = m_streamFiles.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
for(auto it = m_sources.begin(); it != m_sources.end();) {
|
|
|
|
SoundSourcePtr source = *it;
|
|
|
|
|
|
|
|
source->update();
|
|
|
|
|
|
|
|
if(!source->isPlaying())
|
|
|
|
it = m_sources.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
for(auto it : m_channels) {
|
|
|
|
it.second->update();
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(m_context) {
|
|
|
|
alcProcessContext(m_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
void SoundManager::setAudioEnabled(bool enable)
|
|
|
|
{
|
|
|
|
if(m_audioEnabled == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_audioEnabled = enable;
|
|
|
|
if(!enable) {
|
|
|
|
ensureContext();
|
|
|
|
for(const SoundSourcePtr& source : m_sources) {
|
|
|
|
source->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:34:17 +02:00
|
|
|
void SoundManager::preload(std::string filename)
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
2013-01-16 19:46:42 +01:00
|
|
|
filename = resolveSoundFile(filename);
|
2012-07-18 10:34:17 +02:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
auto it = m_buffers.find(filename);
|
|
|
|
if(it != m_buffers.end())
|
|
|
|
return;
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
2012-04-13 21:54:08 +02:00
|
|
|
SoundFilePtr soundFile = SoundFile::loadSoundFile(filename);
|
|
|
|
|
|
|
|
// only keep small files
|
2013-01-16 19:46:42 +01:00
|
|
|
if(!soundFile || soundFile->getSize() > MAX_CACHE_SIZE)
|
2012-04-13 21:54:08 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
SoundBufferPtr buffer = SoundBufferPtr(new SoundBuffer);
|
2012-04-14 15:15:51 +02:00
|
|
|
if(buffer->fillBuffer(soundFile))
|
2012-04-13 21:54:08 +02:00
|
|
|
m_buffers[filename] = buffer;
|
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
SoundSourcePtr SoundManager::play(std::string filename, float fadetime, float gain)
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
2013-01-16 19:46:42 +01:00
|
|
|
if(!m_audioEnabled)
|
|
|
|
return nullptr;
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
if(gain == 0)
|
|
|
|
gain = 1.0f;
|
2012-07-18 10:34:17 +02:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
filename = resolveSoundFile(filename);
|
2012-04-13 21:54:08 +02:00
|
|
|
SoundSourcePtr soundSource = createSoundSource(filename);
|
|
|
|
if(!soundSource) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error(stdext::format("unable to play '%s'", filename));
|
2013-01-16 19:46:42 +01:00
|
|
|
return nullptr;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
soundSource->setName(filename);
|
2012-04-13 21:54:08 +02:00
|
|
|
soundSource->setRelative(true);
|
2013-01-16 19:46:42 +01:00
|
|
|
soundSource->setGain(gain);
|
|
|
|
|
2013-03-04 22:56:22 +01:00
|
|
|
if(fadetime > 0)
|
2013-01-16 19:46:42 +01:00
|
|
|
soundSource->setFading(StreamSoundSource::FadingOn, fadetime);
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
soundSource->play();
|
|
|
|
|
|
|
|
m_sources.push_back(soundSource);
|
2013-01-16 19:46:42 +01:00
|
|
|
|
|
|
|
return soundSource;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
SoundChannelPtr SoundManager::getChannel(int channel)
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
|
|
|
if(!m_channels[channel])
|
|
|
|
m_channels[channel] = SoundChannelPtr(new SoundChannel(channel));
|
|
|
|
return m_channels[channel];
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
void SoundManager::stopAll()
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
2013-01-16 19:46:42 +01:00
|
|
|
ensureContext();
|
|
|
|
for(const SoundSourcePtr& source : m_sources) {
|
|
|
|
source->stop();
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2012-04-14 16:19:58 +02:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
for(auto it : m_channels) {
|
|
|
|
it.second->stop();
|
2012-04-14 16:19:58 +02:00
|
|
|
}
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)
|
|
|
|
{
|
2012-04-14 15:15:51 +02:00
|
|
|
SoundSourcePtr source;
|
2012-04-13 21:54:08 +02:00
|
|
|
|
2013-01-16 19:46:42 +01:00
|
|
|
try {
|
|
|
|
auto it = m_buffers.find(filename);
|
|
|
|
if(it != m_buffers.end()) {
|
2012-04-14 15:15:51 +02:00
|
|
|
source = SoundSourcePtr(new SoundSource);
|
2013-01-16 19:46:42 +01:00
|
|
|
source->setBuffer(it->second);
|
2012-04-13 21:54:08 +02:00
|
|
|
} else {
|
2013-03-04 22:56:22 +01:00
|
|
|
#if defined __linux && !defined OPENGL_ES
|
|
|
|
// due to OpenAL implementation bug, stereo buffers are always downmixed to mono on linux systems
|
|
|
|
// this is hack to work around the issue
|
|
|
|
// solution taken from http://opensource.creative.com/pipermail/openal/2007-April/010355.html
|
|
|
|
CombinedSoundSourcePtr combinedSource(new CombinedSoundSource);
|
|
|
|
StreamSoundSourcePtr streamSource;
|
|
|
|
|
|
|
|
streamSource = StreamSoundSourcePtr(new StreamSoundSource);
|
|
|
|
streamSource->downMix(StreamSoundSource::DownMixLeft);
|
|
|
|
streamSource->setRelative(true);
|
|
|
|
streamSource->setPosition(Point(-128, 0));
|
|
|
|
combinedSource->addSource(streamSource);
|
|
|
|
m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
|
|
|
|
stdext::timer a;
|
|
|
|
try {
|
2014-01-18 15:09:26 +01:00
|
|
|
return SoundFile::loadSoundFile(filename);
|
2013-03-04 22:56:22 +01:00
|
|
|
} catch(std::exception& e) {
|
|
|
|
g_logger.error(e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
streamSource = StreamSoundSourcePtr(new StreamSoundSource);
|
|
|
|
streamSource->downMix(StreamSoundSource::DownMixRight);
|
|
|
|
streamSource->setRelative(true);
|
|
|
|
streamSource->setPosition(Point(128,0));
|
|
|
|
combinedSource->addSource(streamSource);
|
|
|
|
m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
|
|
|
|
try {
|
2014-01-18 15:09:26 +01:00
|
|
|
return SoundFile::loadSoundFile(filename);
|
2013-03-04 22:56:22 +01:00
|
|
|
} catch(std::exception& e) {
|
|
|
|
g_logger.error(e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
source = combinedSource;
|
|
|
|
#else
|
|
|
|
StreamSoundSourcePtr streamSource(new StreamSoundSource);
|
2013-03-05 06:54:08 +01:00
|
|
|
m_streamFiles[streamSource] = g_asyncDispatcher.schedule([=]() -> SoundFilePtr {
|
2013-03-04 22:56:22 +01:00
|
|
|
try {
|
2014-01-18 15:09:26 +01:00
|
|
|
return SoundFile::loadSoundFile(filename);
|
2013-03-04 22:56:22 +01:00
|
|
|
} catch(std::exception& e) {
|
|
|
|
g_logger.error(e.what());
|
|
|
|
return nullptr;
|
2013-01-16 19:46:42 +01:00
|
|
|
}
|
2013-03-04 22:56:22 +01:00
|
|
|
});
|
|
|
|
source = streamSource;
|
|
|
|
#endif
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2013-01-16 19:46:42 +01:00
|
|
|
} catch(std::exception& e) {
|
|
|
|
g_logger.error(stdext::format("failed to load sound source: '%s'", e.what()));
|
|
|
|
return nullptr;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 15:15:51 +02:00
|
|
|
return source;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2013-01-16 19:46:42 +01:00
|
|
|
|
|
|
|
std::string SoundManager::resolveSoundFile(std::string file)
|
|
|
|
{
|
2013-01-28 02:23:53 +01:00
|
|
|
file = g_resources.guessFilePath(file, "ogg");
|
2013-01-16 19:46:42 +01:00
|
|
|
file = g_resources.resolvePath(file);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::ensureContext()
|
|
|
|
{
|
|
|
|
if(m_context)
|
|
|
|
alcMakeContextCurrent(m_context);
|
|
|
|
}
|