Added events: PlayBackgroundMusic, SetBGMGain, SetSFXGain. Added some editor specific code.

This commit is contained in:
stiffly
2016-01-12 14:03:00 +01:00
parent f80ad1377b
commit 976033c2f6
9 changed files with 139 additions and 43 deletions
@@ -0,0 +1,18 @@
#ifndef Events_PlayBackgroundMusic_h__
#define Events_PlayBackgroundMusic_h__
#include <string>
#include "Core/Entity.h"
#include "Core/Event.h"
namespace Events
{
struct PlayBackgroundMusic : public Event
{
std::string FilePath = "";
};
}
#endif
+1 -1
View File
@@ -2,7 +2,7 @@
#define Events_PlaySoundOnPosition_h__ #define Events_PlaySoundOnPosition_h__
#include <string> #include <string>
#include <glm\common.hpp> #include <glm/common.hpp>
#include "Core/Event.h" #include "Core/Event.h"
namespace Events namespace Events
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_SetBGMGain_h__
#define Events_SetBGMGain_h__
#include "Core/Event.h"
namespace Events
{
struct SetBGMGain : public Event
{
float Gain;
};
}
#endif
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_SetSFXGain_h__
#define Events_SetSFXGain_h__
#include "Core/Event.h"
namespace Events
{
struct SetSFXGain : public Event
{
float Gain;
};
}
#endif
+6 -6
View File
@@ -7,7 +7,7 @@ class Sound : public Resource
{ {
friend class ResourceManager; friend class ResourceManager;
public: public:
Sound(std::string path) { m_Buffer = LoadFile(path); } Sound(std::string path) { m_Buffer = LoadFile(path); m_Path = path; }
~Sound() { ClearBuffer(); } ~Sound() { ClearBuffer(); }
ALuint Buffer() { return m_Buffer; } ALuint Buffer() { return m_Buffer; }
std::string Path() { return m_Path; } std::string Path() { return m_Path; }
@@ -35,14 +35,14 @@ private:
return m_BufferCache[path]; return m_BufferCache[path];
} }
//// Open file // Open file
FILE *fp = fopen(path.c_str(), "rb"); FILE *fp = fopen(path.c_str(), "rb");
if (!fp) { if (!fp) {
printf("Failed to open file %s, no such file exists", path.c_str()); printf("Sound: Failed to open file %s, no such file exists", path.c_str());
return 0; return 0;
} }
//// CHECK FOR VALID WAVE-FILE //CHECK FOR VALID WAVE-FILE
fread(m_Type, sizeof(char), 4, fp); fread(m_Type, sizeof(char), 4, fp);
if (m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') { if (m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') {
printf("ERROR: No RIFF in WAVE-file"); printf("ERROR: No RIFF in WAVE-file");
@@ -62,7 +62,7 @@ private:
return 0; return 0;
} }
//// READ THE DATA FROM WAVE-FILE // READ THE DATA FROM WAVE-FILE
fread(&m_ChunkSize, 4 * sizeof(char), 1, fp); fread(&m_ChunkSize, 4 * sizeof(char), 1, fp);
fread(&m_FormatType, 2 * sizeof(char), 1, fp); fread(&m_FormatType, 2 * sizeof(char), 1, fp);
fread(&m_Channels, 2 * sizeof(char), 1, fp); fread(&m_Channels, 2 * sizeof(char), 1, fp);
@@ -83,7 +83,7 @@ private:
fread(buf, sizeof(char), m_DataSize, fp); fread(buf, sizeof(char), m_DataSize, fp);
fclose(fp); fclose(fp);
//// Create buffer // Create buffer
ALuint format = 0; ALuint format = 0;
if (m_BitsPerSample == 8) { if (m_BitsPerSample == 8) {
if (m_Channels == 1) { if (m_Channels == 1) {
+19 -6
View File
@@ -15,9 +15,12 @@
#include "Sound/EPlaySound.h" #include "Sound/EPlaySound.h"
#include "Sound/EPlaySoundOnEntity.h" #include "Sound/EPlaySoundOnEntity.h"
#include "Sound/EPlaySoundOnPosition.h" #include "Sound/EPlaySoundOnPosition.h"
#include "Sound/EPlayBackgroundMusic.h"
#include "Sound/EPauseSound.h" #include "Sound/EPauseSound.h"
#include "Sound/EStopSound.h"
#include "Sound/EContinueSound.h" #include "Sound/EContinueSound.h"
#include "Sound/EStopSound.h"
#include "Sound/ESetBGMGain.h"
#include "Sound/ESetSFXGain.h"
struct Source struct Source
@@ -31,9 +34,10 @@ class SoundSystem
{ {
public: public:
SoundSystem() { } SoundSystem() { }
SoundSystem(World* world, EventBroker* eventBroker); SoundSystem(World* world, EventBroker* eventBroker, bool editorMode);
~SoundSystem(); ~SoundSystem();
void Update(); // Update emitters // Update emitters / listener
void Update();
private: private:
// Help functions for working with OpenaAL // Help functions for working with OpenaAL
void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); };
@@ -54,9 +58,9 @@ private:
Source* createSource(std::string filePath); Source* createSource(std::string filePath);
void playSound(Source* source); void playSound(Source* source);
void stopSound(Source* source); void stopSound(Source* source);
void stopEmitter(EntityID emitter);
void stopEmitters(); void stopEmitters();
bool isPlaying(ALuint source); bool isPlaying(ALuint source);
void setGain(Source* source, float gain);
void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance); void setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance);
// OpenAL system variables // OpenAL system variables
@@ -64,9 +68,12 @@ private:
ALCcontext* m_ALCcontext = nullptr; ALCcontext* m_ALCcontext = nullptr;
// Logic // Logic
World* m_World; World* m_World = nullptr;
EventBroker* m_EventBroker; EventBroker* m_EventBroker = nullptr;
std::unordered_map<EntityID, Source*> m_Sources; std::unordered_map<EntityID, Source*> m_Sources;
float m_BGMVolumeChannel = 1.f;
float m_SFXVolumeChannel = 1.f;
bool m_EditorEnabled = false;
// Events // Events
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound; EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
@@ -81,6 +88,12 @@ private:
bool OnStopSound(const Events::StopSound &e); bool OnStopSound(const Events::StopSound &e);
EventRelay<SoundSystem, Events::ContinueSound> m_EContinueSound; EventRelay<SoundSystem, Events::ContinueSound> m_EContinueSound;
bool OnContinueSound(const Events::ContinueSound &e); bool OnContinueSound(const Events::ContinueSound &e);
EventRelay<SoundSystem, Events::PlayBackgroundMusic> m_EPlayBackgroundMusic;
bool OnPlayBackgroundMusic(const Events::PlayBackgroundMusic &e);
EventRelay<SoundSystem, Events::SetBGMGain> m_ESetBGMGain;
bool OnSetBGMGain(const Events::SetBGMGain &e); // Not tested
EventRelay<SoundSystem, Events::SetSFXGain> m_ESetSFXGain;
bool OnSetSFXGain(const Events::SetSFXGain &e); // Not tested
}; };
#endif #endif
+1 -1
View File
@@ -1,5 +1,5 @@
<c:SoundEmitter> <c:SoundEmitter>
<FilePath>Audio/crosscounter.wav</FilePath> <FilePath></FilePath>
<Gain>1.0</Gain> <Gain>1.0</Gain>
<Pitch>1.0</Pitch> <Pitch>1.0</Pitch>
<Loop>false</Loop> <Loop>false</Loop>
+60 -16
View File
@@ -1,9 +1,10 @@
#include "Sound/SoundSystem.h" #include "Sound/SoundSystem.h"
SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) SoundSystem::SoundSystem(World* world, EventBroker* eventBroker, bool editorMode)
{ {
m_EventBroker = eventBroker; m_EventBroker = eventBroker;
m_World = world; m_World = world;
m_EditorEnabled = editorMode;
initOpenAL(); initOpenAL();
@@ -17,6 +18,9 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker)
EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound);
EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound); EVENT_SUBSCRIBE_MEMBER(m_EPauseSound, &SoundSystem::OnPauseSound);
EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound); EVENT_SUBSCRIBE_MEMBER(m_EContinueSound, &SoundSystem::OnContinueSound);
EVENT_SUBSCRIBE_MEMBER(m_EPlayBackgroundMusic, &SoundSystem::OnPlayBackgroundMusic);
//EVENT_SUBSCRIBE_MEMBER(m_ESetBGMGain, &SoundSystem::SetBGMGain);
//EVENT_SUBSCRIBE_MEMBER(m_ESetSFXGain, &SoundSystem::OnSetSFXGain);
} }
void SoundSystem::initOpenAL() void SoundSystem::initOpenAL()
@@ -36,6 +40,7 @@ SoundSystem::~SoundSystem()
stopEmitters(); // Stopps emitters stopEmitters(); // Stopps emitters
deleteInactiveEmitters(); // Deletes stopped emitters deleteInactiveEmitters(); // Deletes stopped emitters
// Delete entities
std::unordered_map<EntityID, Source*>::iterator it; std::unordered_map<EntityID, Source*>::iterator it;
for (it = m_Sources.begin(); it != m_Sources.end(); it++) { for (it = m_Sources.begin(); it != m_Sources.end(); it++) {
m_World->DeleteEntity((*it).first); m_World->DeleteEntity((*it).first);
@@ -116,12 +121,15 @@ void SoundSystem::updateEmitters()
std::unordered_map<EntityID, Source*>::iterator i; std::unordered_map<EntityID, Source*>::iterator i;
i = m_Sources.find(emitter); i = m_Sources.find(emitter);
if (i != m_Sources.end()) { if (i != m_Sources.end()) {
// Get previous pos
glm::vec3 previousPos; glm::vec3 previousPos;
alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get previous pos alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z);
glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); // Get next pos // Get next pos
glm::vec3 velocity = nextPos - previousPos; // Calculate velocity glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter);
setSourcePos(m_Sources[emitter]->ALsource, nextPos); // Set next pos // Calculate velocity
setSourceVel(m_Sources[emitter]->ALsource, velocity); // Set velocity glm::vec3 velocity = nextPos - previousPos;
setSourcePos(m_Sources[emitter]->ALsource, nextPos);
setSourceVel(m_Sources[emitter]->ALsource, velocity);
setSoundProperties( setSoundProperties(
m_Sources[emitter]->ALsource, m_Sources[emitter]->ALsource,
(float)(double)(*it)["Gain"], (float)(double)(*it)["Gain"],
@@ -131,8 +139,16 @@ void SoundSystem::updateEmitters()
(float)(double)(*it)["RollOffFactor"], (float)(double)(*it)["RollOffFactor"],
(float)(double)(*it)["ReferenceDistance"] (float)(double)(*it)["ReferenceDistance"]
); );
// To make an emitter play when spawned in editor mode
if (m_EditorEnabled) {
// Path changed
if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) {
m_Sources[emitter]->SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]);
playSound(m_Sources[emitter]);
}
}
} }
// No orientation, emitts in all directions
} }
} }
@@ -156,8 +172,8 @@ Source* SoundSystem::createSource(std::string filePath)
{ {
ALuint alSource; ALuint alSource;
alGenSources((ALuint)1, &alSource); alGenSources((ALuint)1, &alSource);
alSourcei(alSource, AL_REFERENCE_DISTANCE, 1.0); alSourcef(alSource, AL_REFERENCE_DISTANCE, 1.0);
alSourcei(alSource, AL_MAX_DISTANCE, FLT_MAX); alSourcef(alSource, AL_MAX_DISTANCE, FLT_MAX);
Source* source = new Source(); Source* source = new Source();
source->ALsource = alSource; source->ALsource = alSource;
source->SoundResource = ResourceManager::Load<Sound>(filePath); source->SoundResource = ResourceManager::Load<Sound>(filePath);
@@ -175,11 +191,6 @@ void SoundSystem::stopSound(Source* source)
alSourceStop(source->ALsource); alSourceStop(source->ALsource);
} }
void SoundSystem::stopEmitter(EntityID emitter)
{
}
bool SoundSystem::OnPlaySound(const Events::PlaySound & e) bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
{ {
Source* sauce = createSource(e.FilePath); Source* sauce = createSource(e.FilePath);
@@ -190,7 +201,7 @@ bool SoundSystem::OnPlaySound(const Events::PlaySound & e)
bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e) bool SoundSystem::OnPlaySoundOnEntity(const Events::PlaySoundOnEntity & e)
{ {
Source* source = createSource(e.FilePath); Source* source = createSource(e.FilePath);
m_Sources[e.emitterID] = source; m_Sources[e.EmitterID] = source;
playSound(source); playSound(source);
return false; return false;
} }
@@ -233,6 +244,34 @@ bool SoundSystem::OnContinueSound(const Events::ContinueSound & e)
return true; return true;
} }
bool SoundSystem::OnPlayBackgroundMusic(const Events::PlayBackgroundMusic & e)
{
auto listenerComponents = m_World->GetComponents("Listener");
for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) {
auto emitterChild = m_World->CreateEntity((*it).EntityID);
auto emitter = m_World->AttachComponent(emitterChild, "SoundEmitter");
(bool&)emitter["Loop"] = true;
(std::string&)emitter["FilePath"] = e.FilePath;
m_World->AttachComponent(emitterChild, "Transform");
Source* source = createSource(e.FilePath);
m_Sources[emitterChild] = source;
playSound(source);
}
return false;
}
bool SoundSystem::OnSetBGMGain(const Events::SetBGMGain & e)
{
m_BGMVolumeChannel = e.Gain;
return true;
}
bool SoundSystem::OnSetSFXGain(const Events::SetSFXGain & e)
{
m_SFXVolumeChannel = e.Gain;
return true;
}
void SoundSystem::setListenerOri(glm::vec3 ori) void SoundSystem::setListenerOri(glm::vec3 ori)
{ {
glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0);
@@ -256,9 +295,14 @@ bool SoundSystem::isPlaying(ALuint source)
return (state == AL_PLAYING); return (state == AL_PLAYING);
} }
void SoundSystem::setGain(Source * source, float gain)
{
alSourcef(source->ALsource, AL_GAIN, gain);
}
void SoundSystem::setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance) void SoundSystem::setSoundProperties(ALuint source, float gain, float pitch, bool loop, float maxDistance, float rollOffFactor, float referenceDistance)
{ {
alSourcef(source, AL_GAIN, gain); alSourcef(source, AL_GAIN, gain * m_SFXVolumeChannel);
alSourcef(source, AL_PITCH, pitch); alSourcef(source, AL_PITCH, pitch);
alSourcei(source, AL_LOOPING, (int)loop); // YOLO alSourcei(source, AL_LOOPING, (int)loop); // YOLO
alSourcef(source, AL_MAX_DISTANCE, maxDistance); alSourcef(source, AL_MAX_DISTANCE, maxDistance);
+2 -13
View File
@@ -65,7 +65,7 @@ Game::Game(int argc, char* argv[])
networkFunction(); networkFunction();
} }
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Game::debugOnInputCommand);
m_SoundSystem = new SoundSystem(m_World, m_EventBroker); m_SoundSystem = new SoundSystem(m_World, m_EventBroker, m_Config->Get<bool>("Debug.EditorEnabled", false));
auto soundListenerCube = m_World->CreateEntity(); auto soundListenerCube = m_World->CreateEntity();
m_World->AttachComponent(soundListenerCube, "Listener"); m_World->AttachComponent(soundListenerCube, "Listener");
@@ -130,22 +130,11 @@ void Game::Tick()
bool Game::debugOnInputCommand(const Events::InputCommand & e) bool Game::debugOnInputCommand(const Events::InputCommand & e)
{ {
if (e.Command == "PlaySound" && e.Value > 0) { if (e.Command == "PlaySound" && e.Value > 0) {
Events::PlaySoundOnEntity e; Events::PlayBackgroundMusic e;
e.emitterID = 18;
e.FilePath = "Audio/crosscounter.wav"; e.FilePath = "Audio/crosscounter.wav";
//e.emitterID = 18; // rofl //e.emitterID = 18; // rofl
m_EventBroker->Publish(e); m_EventBroker->Publish(e);
} }
if (e.Command == "Reload" && e.Value > 0) {
Events::PauseSound e;
e.EmitterID = 18;
m_EventBroker->Publish(e);
}
if (e.Command == "Crouch" && e.Value > 0) {
Events::ContinueSound e;
e.EmitterID = 18;
m_EventBroker->Publish(e);
}
return true; return true;
} }