diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index 441afe0..d211c05 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -10,8 +10,11 @@ namespace Components struct SoundEmitter : Component { - float Volume; - float MaxRange; + float Gain; + float MaxDistance; + float ReferenceDistance; + float Pitch; + bool Loop; }; } diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index f2c42f7..5ba9649 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -40,7 +40,7 @@ - $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32;$(SolutionDir)\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) + $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Debug;$(SolutionDir)\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath) $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) @@ -50,7 +50,7 @@ $(BOOST_ROOT);$(SolutionDir)\Libraries\openal-soft-1.15.1-bin\include;$(SolutionDir)\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath) - $(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) + $(SolutionDir)\Libraries\openal-soft-1.15.1-bin\lib\Win32\Release;$(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath) $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) @@ -70,7 +70,7 @@ - opengl32.lib;glu32.lib;SOIL.lib;glew32sd.lib;glfw3.lib + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32sd.lib;glfw3.lib @@ -93,7 +93,7 @@ opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies) - opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib; + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib; diff --git a/Escape-the-Dawn/GameWorld.h b/Escape-the-Dawn/GameWorld.h index afe8dc8..c6a6140 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -57,7 +57,7 @@ void GameWorld::Initialize() //AddSystem("CollisionSystem"); //AddSystem("ParticleSystem"); AddSystem("PlayerSystem"); - //AddSystem("SoundSystem"); + AddSystem("SoundSystem"); AddSystem("RenderSystem"); std::shared_ptr transform; @@ -70,6 +70,14 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0.f, 0.f, 0.f); model = AddComponent(ent, "Model"); model->ModelFile = "ship.obj"; + auto soundEmitter = AddComponent(ent, "SoundEmitter"); + soundEmitter->Gain = 1; + soundEmitter->MaxDistance = 10; + soundEmitter->Loop = true; + soundEmitter->ReferenceDistance = 0.1; + soundEmitter->Pitch = 1; + GetSystem("SoundSystem")->PlaySound(soundEmitter, "Sounds/hallelujah.wav"); + ent = CreateEntity(); SetProperty(ent, "Name", std::string("Camera")); @@ -90,7 +98,7 @@ void GameWorld::RegisterSystems() //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); //m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); - //m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); + m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); } diff --git a/Escape-the-Dawn/OpenAL32.dll b/Escape-the-Dawn/OpenAL32.dll new file mode 100644 index 0000000..9c8932e Binary files /dev/null and b/Escape-the-Dawn/OpenAL32.dll differ diff --git a/Escape-the-Dawn/Sounds/hallelujah.wav b/Escape-the-Dawn/Sounds/hallelujah.wav new file mode 100644 index 0000000..f103f96 Binary files /dev/null and b/Escape-the-Dawn/Sounds/hallelujah.wav differ diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index f7e4a14..d56faf3 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -1,11 +1,15 @@ #ifndef SoundEmitter_h__ #define SoundEmitter_h__ -#include "AL/al.h" -#include "AL/alc.h" - #include "System.h" +#include "Components/Transform.h" #include "Components/SoundEmitter.h" +#include +#include +#include +#include +#include +#include namespace Systems { @@ -19,10 +23,23 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; void PlaySound(std::shared_ptr emitter, std::string fileName); - void LoadFile(std::string fileName); + ALuint LoadFile(std::string fileName); + ALuint CreateSource(); + private: ALCcontext* context; + //File-info + char type[4]; + DWORD size, chunkSize; + short formatType, channels; + DWORD sampleRate, avgBytesPerSec; + short bytesPerSample, bitsPerSample; + DWORD dataSize; + + + std::map m_Source; + std::map m_BufferCache; // string = fileName }; } diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index 92a415d..af04006 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -1,19 +1,26 @@ #include "SoundSystem.h" #include "World.h" -#include -#include +//#include + Systems::SoundSystem::SoundSystem(World* world) : System(world) { - ALCdevice* Device = alcOpenDevice(nullptr); + + //initialize OpenAL + ALCdevice* Device = alcOpenDevice(NULL); if(Device) { - context = alcCreateContext(Device, nullptr); + context = alcCreateContext(Device, NULL); alcMakeContextCurrent(context); } + else + { + LOG_ERROR("OMG OPEN AL FAIL"); + } + alGetError(); } @@ -24,18 +31,249 @@ void Systems::SoundSystem::Update(double dt) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); - if(soundEmitter == nullptr) + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (transformComponent == nullptr) return; + auto entityName = m_World->GetProperty(entity, "Name"); + if (entityName == "Camera") + { + glm::vec3 playerPos = transformComponent->Position; + ALfloat listenerPos[3] = { playerPos.x, playerPos.y, playerPos.z }; + + glm::vec3 playerVel = transformComponent->Velocity; + ALfloat listenerVel[3] = { playerVel.x, playerVel.y, playerVel.z }; + + glm::fquat playerQuatOri = transformComponent->Orientation; + glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1)); + glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0)); + ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z }; + + //Listener + alListenerfv(AL_POSITION, listenerPos); + ALenum error; + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alListenerfv(AL_VELOCITY, listenerVel); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alListenerfv(AL_ORIENTATION, listenerOri); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + } + + auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + if(soundEmitter != nullptr) + { + ALuint source = m_Source[soundEmitter.get()]; + + ALenum error; + alSourcef(source, AL_GAIN, soundEmitter->Gain); //Volume + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcef(source, AL_PITCH, soundEmitter->Pitch); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcei(source, AL_LOOPING, soundEmitter->Loop); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + + glm::vec3 emitterPos = transformComponent->Position; + ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, emitterPos.z }; + + glm::vec3 emitterVel= transformComponent->Position; + ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, emitterVel.z }; + + alSourcefv(source, AL_POSITION, sourcePos); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcefv(source, AL_VELOCITY, sourceVel); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + + + } } void Systems::SoundSystem::PlaySound(std::shared_ptr emitter, std::string fileName) { - LoadFile(fileName); + ALuint buffer = LoadFile(fileName); + ALuint source = m_Source[emitter.get()]; + alSourcei(source, AL_BUFFER, buffer); + ALenum error; + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + exit(EXIT_FAILURE); + } + alSourcePlay(m_Source[emitter.get()]); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + exit(EXIT_FAILURE); + + } } -void Systems::SoundSystem::LoadFile(std::string fileName) +void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr component) { - //loadWavFile() + if(type == "SoundEmitter") { + ALuint source = CreateSource(); + m_Source[component.get()] = source; + } +} + +ALuint Systems::SoundSystem::LoadFile(std::string fileName) +{ + if (m_BufferCache.find(fileName) != m_BufferCache.end()) + return m_BufferCache[fileName]; + + FILE *fp = NULL; + fp = fopen(fileName.c_str(), "rb"); + + //CHECK FOR VALID WAVE-FILE + fread(type, sizeof(char), 4, fp); + if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') { + LOG_ERROR("ERROR: No RIFF in WAVE-file"); + return 0; + } + + fread(&size, sizeof(DWORD), 1, fp); + fread(type, sizeof(char), 4, fp); + if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') { + LOG_ERROR("ERROR: Not WAVE-file"); + return 0; + } + + fread(type, sizeof(char), 4, fp); + if(type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') { + LOG_ERROR("ERROR: No fmt in WAVE-file"); + return 0; + } + + //READ THE DATA FROM WAVE-FILE + fread(&chunkSize, sizeof(DWORD), 1, fp); + fread(&formatType, sizeof(short), 1, fp); + fread(&channels, sizeof(short), 1, fp); + fread(&sampleRate, sizeof(DWORD), 1, fp); + fread(&avgBytesPerSec, sizeof(DWORD), 1, fp); + fread(&bytesPerSample, sizeof(short), 1, fp); + fread(&bitsPerSample, sizeof(short), 1, fp); + + fread(type, sizeof(char), 4, fp); + if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a') + { + LOG_ERROR("ERROR: WAVE-file Missing data"); + return 0; + } + + fread(&dataSize, sizeof(DWORD), 1, fp); + + unsigned char* buf = new unsigned char[dataSize]; + fread(buf, sizeof(BYTE), dataSize, fp); + fclose(fp); + + // Create buffer + ALuint format = 0; + if(bitsPerSample == 8) + { + if(channels == 1) + format = AL_FORMAT_MONO8; + else if(channels == 2) + format = AL_FORMAT_STEREO8; + } + if(bitsPerSample == 16) + { + if (channels == 1) + format = AL_FORMAT_MONO16; + else if (channels == 2) + format = AL_FORMAT_STEREO16; + } + ALuint buffer; + alGenBuffers(1, &buffer); + ALenum error; + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + exit(EXIT_FAILURE); + } + alBufferData(buffer, format, buf, dataSize, sampleRate); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + exit(EXIT_FAILURE); + + } + //delete[] buf; + + m_BufferCache[fileName] = buffer; + return buffer; +} + +ALuint Systems::SoundSystem::CreateSource() +{ + ALuint source; + alGenBuffers((ALuint)1, &source); + + ALenum error; + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + exit(EXIT_FAILURE); + + } + + alSourcef(source, AL_GAIN, 1); //Volume + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcef(source, AL_PITCH, 1); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSourcei(source, AL_LOOPING, AL_FALSE); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSource3f(source, AL_POSITION, 0, 0, 0); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + alSource3f(source, AL_VELOCITY, 0, 0, 0); + if ((error = alGetError()) != AL_NO_ERROR) + { + LOG_ERROR("AL Error: %d", error); + } + + return source; } \ No newline at end of file diff --git a/Executable/Executable.vcxproj b/Executable/Executable.vcxproj index 8ce4c7f..9a8c701 100644 --- a/Executable/Executable.vcxproj +++ b/Executable/Executable.vcxproj @@ -59,6 +59,7 @@ true + Console diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.dll b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.dll new file mode 100644 index 0000000..9c8932e Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.dll differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.exp b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.exp new file mode 100644 index 0000000..9813fee Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.exp differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.ilk b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.ilk new file mode 100644 index 0000000..0e2cc84 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.ilk differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.lib b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.lib new file mode 100644 index 0000000..688ad50 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.lib differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.pdb b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.pdb new file mode 100644 index 0000000..b28c60d Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/OpenAL32.pdb differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.exe b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.exe new file mode 100644 index 0000000..e1e33af Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.exe differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.ilk b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.ilk new file mode 100644 index 0000000..8beac13 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.ilk differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.pdb b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.pdb new file mode 100644 index 0000000..496d423 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/makehrtf.pdb differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.exe b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.exe new file mode 100644 index 0000000..90b3bc2 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.exe differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.ilk b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.ilk new file mode 100644 index 0000000..e74433b Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.ilk differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.pdb b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.pdb new file mode 100644 index 0000000..3c8ea25 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Debug/openal-info.pdb differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.dll b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.dll new file mode 100644 index 0000000..5dc4ef8 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.dll differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.exp b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.exp new file mode 100644 index 0000000..fd91812 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.exp differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.lib b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.lib new file mode 100644 index 0000000..93431b7 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/OpenAL32.lib differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/makehrtf.exe b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/makehrtf.exe new file mode 100644 index 0000000..afbdc03 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/makehrtf.exe differ diff --git a/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/openal-info.exe b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/openal-info.exe new file mode 100644 index 0000000..4263497 Binary files /dev/null and b/Libraries/openal-soft-1.15.1-bin/lib/Win32/Release/openal-info.exe differ