bind sound functions and add sample startup music

This commit is contained in:
Eduardo Bart 2012-04-14 11:19:58 -03:00
parent 8e679f2da7
commit c3eae3ba06
12 changed files with 110 additions and 50 deletions

View File

@ -34,10 +34,16 @@ function Client.init()
g_window.setTitle('OTClient') g_window.setTitle('OTClient')
g_window.setIcon(resolvepath('clienticon.png')) g_window.setIcon(resolvepath('clienticon.png'))
-- show the only window after the first frame is rendered -- show the only window after the first frame is rendered
scheduleEvent(function() scheduleEvent(function()
scheduleEvent(function() scheduleEvent(function()
g_window.show() g_window.show()
-- play startup music
g_sounds.playMusic("startup.ogg", 3)
connect(g_game, { onGameStart = function() g_sounds.stopMusic(3) end })
connect(g_game, { onGameEnd= function() g_sounds.playMusic("startup.ogg", 3) end })
end, 0) end, 0)
end, 0) end, 0)
end end

BIN
modules/client/startup.ogg Normal file

Binary file not shown.

View File

@ -32,6 +32,7 @@
#include <framework/platform/platformwindow.h> #include <framework/platform/platformwindow.h>
#include <framework/core/modulemanager.h> #include <framework/core/modulemanager.h>
#include <framework/core/module.h> #include <framework/core/module.h>
#include <framework/sound/soundmanager.h>
void Application::registerLuaFunctions() void Application::registerLuaFunctions()
{ {
@ -531,6 +532,19 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction("g_fonts", "fontExists", std::bind(&FontManager::fontExists, &g_fonts, std::placeholders::_1)); g_lua.bindClassStaticFunction("g_fonts", "fontExists", std::bind(&FontManager::fontExists, &g_fonts, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_fonts", "setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, std::placeholders::_1)); g_lua.bindClassStaticFunction("g_fonts", "setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, std::placeholders::_1));
// SoundManager
g_lua.registerStaticClass("g_sounds");
g_lua.bindClassStaticFunction("g_sounds", "preload", std::bind(&SoundManager::preload, &g_sounds, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sounds", "enableSound", std::bind(&SoundManager::enableSound, &g_sounds, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sounds", "play", std::bind(&SoundManager::play, &g_sounds, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sounds", "enableMusic", std::bind(&SoundManager::enableMusic, &g_sounds, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sounds", "playMusic", std::bind(&SoundManager::playMusic, &g_sounds, std::placeholders::_1, std::placeholders::_2));
g_lua.bindClassStaticFunction("g_sounds", "stopMusic", std::bind(&SoundManager::stopMusic, &g_sounds, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sounds", "isMusicEnabled", std::bind(&SoundManager::isMusicEnabled, &g_sounds));
g_lua.bindClassStaticFunction("g_sounds", "isSoundEnabled", std::bind(&SoundManager::isSoundEnabled, &g_sounds));
g_lua.bindClassStaticFunction("g_sounds", "isAudioEnabled", std::bind(&SoundManager::isAudioEnabled, &g_sounds));
g_lua.bindClassStaticFunction("g_sounds", "getCurrentMusic", std::bind(&SoundManager::getCurrentMusic, &g_sounds));
// EventDispatcher // EventDispatcher
g_lua.registerStaticClass("g_eventDispatcher"); g_lua.registerStaticClass("g_eventDispatcher");
g_lua.bindClassStaticFunction("g_eventDispatcher", "addEvent", std::bind(&EventDispatcher::addEvent, &g_eventDispatcher, std::placeholders::_1, std::placeholders::_2)); g_lua.bindClassStaticFunction("g_eventDispatcher", "addEvent", std::bind(&EventDispatcher::addEvent, &g_eventDispatcher, std::placeholders::_1, std::placeholders::_2));

View File

@ -94,6 +94,12 @@ void CombinedSoundSource::setVelocity(const Point& velocity)
source->setVelocity(velocity); source->setVelocity(velocity);
} }
void CombinedSoundSource::setFading(SoundSource::FadeState state, float fadetime)
{
for(const SoundSourcePtr& source : m_sources)
source->setFading(state, fadetime);
}
void CombinedSoundSource::update() void CombinedSoundSource::update()
{ {
for(const SoundSourcePtr& source : m_sources) for(const SoundSourcePtr& source : m_sources)

View File

@ -44,6 +44,7 @@ public:
void setPitch(float pitch); void setPitch(float pitch);
void setPosition(const Point& pos); void setPosition(const Point& pos);
void setVelocity(const Point& velocity); void setVelocity(const Point& velocity);
void setFading(FadeState state, float fadetime);
protected: protected:
virtual void update(); virtual void update();

View File

@ -49,12 +49,6 @@ void SoundManager::init()
m_musicEnabled = true; m_musicEnabled = true;
m_soundEnabled = true; m_soundEnabled = true;
/*
g_eventDispatcher.scheduleEvent([this] {
play("/1.ogg");
}, 10);
*/
} }
void SoundManager::terminate() void SoundManager::terminate()
@ -160,12 +154,12 @@ void SoundManager::enableMusic(bool enable)
m_musicEnabled = enable; m_musicEnabled = enable;
if(enable) if(enable)
playMusic(m_currentMusic); playMusic(m_currentMusic, 3.0f);
else else
m_musicSource = nullptr; m_musicSource = nullptr;
} }
void SoundManager::playMusic(const std::string& filename, bool fade) void SoundManager::playMusic(const std::string& filename, float fadetime)
{ {
if(m_currentMusic == filename && m_musicSource) if(m_currentMusic == filename && m_musicSource)
return; return;
@ -178,11 +172,31 @@ void SoundManager::playMusic(const std::string& filename, bool fade)
m_musicSource = nullptr; m_musicSource = nullptr;
return; return;
} }
m_musicSource = createSoundSource(filename);
if(!m_musicSource) {
logError("unable to play '", filename, "'");
return;
}
m_musicSource->setRelative(true);
if(fadetime > 0) {
m_musicSource->setGain(0);
m_musicSource->setFading(StreamSoundSource::FadingOn, fadetime);
}
m_musicSource->play();
} }
void SoundManager::stopMusic(float fadetime) void SoundManager::stopMusic(float fadetime)
{ {
if(m_musicSource) {
if(fadetime > 0)
m_musicSource->setFading(StreamSoundSource::FadingOff, 3.0f);
else
m_musicSource->stop();
}
} }
SoundSourcePtr SoundManager::createSoundSource(const std::string& filename) SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)

View File

@ -29,22 +29,20 @@ class SoundManager
{ {
enum { enum {
MAX_CACHE_SIZE = 100000, MAX_CACHE_SIZE = 100000,
POLL_DELAY = 300 POLL_DELAY = 100
}; };
public: public:
void init(); void init();
void terminate(); void terminate();
void poll(); void poll();
void preload(const std::string& filename); void preload(const std::string& filename);
void enableSound(bool enable); void enableSound(bool enable);
void play(const std::string& filename); void play(const std::string& filename);
void enableMusic(bool enable); void enableMusic(bool enable);
void playMusic(const std::string& filename, bool fade = false); void playMusic(const std::string& filename, float fadetime);
void stopMusic(float fadetime = 0); void stopMusic(float fadetime = 0);
bool isMusicEnabled() { return m_musicEnabled; } bool isMusicEnabled() { return m_musicEnabled; }
@ -53,12 +51,13 @@ public:
std::string getCurrentMusic() { return m_currentMusic; } std::string getCurrentMusic() { return m_currentMusic; }
private: private:
StreamSoundSourcePtr createStreamSoundSource(const std::string& filename);
SoundSourcePtr createSoundSource(const std::string& filename); SoundSourcePtr createSoundSource(const std::string& filename);
ALuint loadFileIntoBuffer(const SoundFilePtr& soundFile); ALuint loadFileIntoBuffer(const SoundFilePtr& soundFile);
std::map<std::string, SoundBufferPtr> m_buffers; std::map<std::string, SoundBufferPtr> m_buffers;
std::vector<SoundSourcePtr> m_sources; std::vector<SoundSourcePtr> m_sources;
StreamSoundSourcePtr m_musicSource; SoundSourcePtr m_musicSource;
ALCdevice *m_device; ALCdevice *m_device;
ALCcontext *m_context; ALCcontext *m_context;
Boolean<false> m_musicEnabled; Boolean<false> m_musicEnabled;

View File

@ -23,9 +23,15 @@
#include "soundsource.h" #include "soundsource.h"
#include "soundbuffer.h" #include "soundbuffer.h"
#include <framework/core/clock.h>
SoundSource::SoundSource() SoundSource::SoundSource()
{ {
m_sourceId = 0; m_sourceId = 0;
m_fadeState = NoFading;
m_fadeTime = 0;
m_fadeStartTime = 0;
alGenSources(1, &m_sourceId); alGenSources(1, &m_sourceId);
assert(alGetError() == AL_NO_ERROR); assert(alGetError() == AL_NO_ERROR);
setReferenceDistance(128); setReferenceDistance(128);
@ -105,3 +111,43 @@ void SoundSource::setVelocity(const Point& velocity)
{ {
alSource3f(m_sourceId, AL_VELOCITY, velocity.x, velocity.y, 0); alSource3f(m_sourceId, AL_VELOCITY, velocity.x, velocity.y, 0);
} }
void SoundSource::setFading(FadeState state, float fadeTime)
{
float now = g_clock.time();
if(m_fadeState != NoFading) {
float elapsed = now - m_fadeStartTime;
float add;
if(m_fadeState == FadingOn)
add = -(1-(elapsed / m_fadeTime))*fadeTime;
else
add = -(elapsed / m_fadeTime)*fadeTime;
m_fadeStartTime = now + add;
} else
m_fadeStartTime = now;
m_fadeState = state;
m_fadeTime = fadeTime;
}
void SoundSource::update()
{
float now = g_clock.time();
if(m_fadeState == FadingOn) {
float elapsed = now - m_fadeStartTime;
if(elapsed >= m_fadeTime) {
setGain(1.0);
m_fadeState = NoFading;
} else {
setGain(elapsed / m_fadeTime);
}
} else if(m_fadeState == FadingOff) {
float time = now - m_fadeStartTime;
if(time >= m_fadeTime) {
stop();
m_fadeState = NoFading;
} else {
setGain((m_fadeTime - time) / m_fadeTime);
}
}
}

View File

@ -31,6 +31,8 @@ protected:
SoundSource(ALuint sourceId) : m_sourceId(sourceId) { } SoundSource(ALuint sourceId) : m_sourceId(sourceId) { }
public: public:
enum FadeState { NoFading, FadingOn, FadingOff };
SoundSource(); SoundSource();
virtual ~SoundSource(); virtual ~SoundSource();
@ -45,16 +47,20 @@ public:
virtual void setPitch(float pitch); virtual void setPitch(float pitch);
virtual void setPosition(const Point& pos); virtual void setPosition(const Point& pos);
virtual void setVelocity(const Point& velocity); virtual void setVelocity(const Point& velocity);
virtual void setFading(FadeState state, float fadetime);
protected: protected:
void setBuffer(const SoundBufferPtr& buffer); void setBuffer(const SoundBufferPtr& buffer);
virtual void update() { } virtual void update();
friend class SoundManager; friend class SoundManager;
friend class CombinedSoundSource; friend class CombinedSoundSource;
ALuint m_sourceId; ALuint m_sourceId;
SoundBufferPtr m_buffer; SoundBufferPtr m_buffer;
FadeState m_fadeState;
float m_fadeStartTime;
float m_fadeTime;
}; };
#endif #endif

View File

@ -25,13 +25,11 @@
#include "soundfile.h" #include "soundfile.h"
#include <framework/util/databuffer.h> #include <framework/util/databuffer.h>
#include <framework/core/clock.h>
StreamSoundSource::StreamSoundSource() StreamSoundSource::StreamSoundSource()
{ {
for(auto& buffer : m_buffers) for(auto& buffer : m_buffers)
buffer = SoundBufferPtr(new SoundBuffer); buffer = SoundBufferPtr(new SoundBuffer);
m_fadeState = NoFading;
m_downMix = NoDownMix; m_downMix = NoDownMix;
} }
@ -85,6 +83,8 @@ void StreamSoundSource::unqueueBuffers()
void StreamSoundSource::update() void StreamSoundSource::update()
{ {
SoundSource::update();
ALint processed = 0; ALint processed = 0;
alGetSourcei(m_sourceId, AL_BUFFERS_PROCESSED, &processed); alGetSourcei(m_sourceId, AL_BUFFERS_PROCESSED, &processed);
for(ALint i = 0; i < processed; ++i) { for(ALint i = 0; i < processed; ++i) {
@ -103,25 +103,6 @@ void StreamSoundSource::update()
logTraceError("restarting audio source because of buffer underrun"); logTraceError("restarting audio source because of buffer underrun");
play(); play();
} }
float realTime = g_clock.asyncTime();
if(m_fadeState == FadingOn) {
float time = realTime - m_fadeStartTime;
if(time >= m_fadeTime) {
setGain(1.0);
m_fadeState = NoFading;
} else {
setGain(time / m_fadeTime);
}
} else if(m_fadeState == FadingOff) {
float time = realTime - m_fadeStartTime;
if(time >= m_fadeTime) {
stop();
m_fadeState = NoFading;
} else {
setGain((m_fadeTime - time) / m_fadeTime);
}
}
} }
bool StreamSoundSource::fillBufferAndQueue(ALuint buffer) bool StreamSoundSource::fillBufferAndQueue(ALuint buffer)
@ -187,10 +168,3 @@ void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix)
m_downMix = downMix; m_downMix = downMix;
} }
void StreamSoundSource::setFading(FadeState state, float fadeTime)
{
m_fadeState = state;
m_fadeTime = fadeTime;
m_fadeStartTime = g_clock.asyncTime();
}

View File

@ -35,7 +35,6 @@ class StreamSoundSource : public SoundSource
public: public:
enum DownMix { NoDownMix, DownMixLeft, DownMixRight }; enum DownMix { NoDownMix, DownMixLeft, DownMixRight };
enum FadeState { NoFading, FadingOn, FadingOff };
StreamSoundSource(); StreamSoundSource();
virtual ~StreamSoundSource(); virtual ~StreamSoundSource();
@ -46,8 +45,6 @@ public:
void setSoundFile(const SoundFilePtr& soundFile); void setSoundFile(const SoundFilePtr& soundFile);
void downMix(DownMix downMix); void downMix(DownMix downMix);
void setFading(FadeState state, float fadetime);
FadeState getFadeState() { return m_fadeState; }
void update(); void update();
@ -59,9 +56,6 @@ private:
SoundFilePtr m_soundFile; SoundFilePtr m_soundFile;
std::array<SoundBufferPtr,STREAM_FRAGMENTS> m_buffers; std::array<SoundBufferPtr,STREAM_FRAGMENTS> m_buffers;
DownMix m_downMix; DownMix m_downMix;
FadeState m_fadeState;
float m_fadeStartTime;
float m_fadeTime;
Boolean<false> m_looping; Boolean<false> m_looping;
}; };

View File

@ -78,7 +78,7 @@ for line in io.lines(cppclassheader) do
bindline = ' g_lua.bindClassStaticFunction("' .. luaclassname .. '", "' .. funcname .. '", ' .. bindline = ' g_lua.bindClassStaticFunction("' .. luaclassname .. '", "' .. funcname .. '", ' ..
'std::bind(&' .. cppclassname .. "::" .. funcname .. ', &' .. cppclassinstance 'std::bind(&' .. cppclassname .. "::" .. funcname .. ', &' .. cppclassinstance
for i=1,numargs do for i=1,numargs do
bindline = bindline .. ', _' .. i bindline = bindline .. ', std::placeholders::_' .. i
end end
bindline = bindline .. '));' bindline = bindline .. '));'
else else