soundsystem strular

This commit is contained in:
Adam
2014-03-10 03:46:41 +01:00
parent 93b2a18a6e
commit 00cff91a73
7 changed files with 137 additions and 25 deletions
+4 -4
View File
@@ -40,7 +40,7 @@
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LibraryPath>$(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)</LibraryPath> <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)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir> <OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir> <IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
<TargetName>$(ProjectName)-$(Configuration)</TargetName> <TargetName>$(ProjectName)-$(Configuration)</TargetName>
@@ -50,7 +50,7 @@
<IncludePath>$(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)</IncludePath> <IncludePath>$(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)</IncludePath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LibraryPath>$(SolutionDir)\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath> <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)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir> <OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir> <IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
<TargetName>$(ProjectName)-$(Configuration)</TargetName> <TargetName>$(ProjectName)-$(Configuration)</TargetName>
@@ -70,7 +70,7 @@
<PostBuildEvent /> <PostBuildEvent />
<PostBuildEvent /> <PostBuildEvent />
<Lib> <Lib>
<AdditionalDependencies>opengl32.lib;glu32.lib;SOIL.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies> <AdditionalDependencies>OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies>
<ForceSymbolReferences> <ForceSymbolReferences>
</ForceSymbolReferences> </ForceSymbolReferences>
</Lib> </Lib>
@@ -93,7 +93,7 @@
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
<Lib> <Lib>
<AdditionalDependencies>opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies> <AdditionalDependencies>OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies>
</Lib> </Lib>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -76,7 +76,7 @@ void GameWorld::Initialize()
soundEmitter->Loop = true; soundEmitter->Loop = true;
soundEmitter->ReferenceDistance = 0.1; soundEmitter->ReferenceDistance = 0.1;
soundEmitter->Pitch = 1; soundEmitter->Pitch = 1;
GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(soundEmitter, "hallelujah.wav"); GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(soundEmitter, "Sounds/hallelujah.wav");
ent = CreateEntity(); ent = CreateEntity();
Binary file not shown.
+1 -4
View File
@@ -1,9 +1,6 @@
#ifndef SoundEmitter_h__ #ifndef SoundEmitter_h__
#define SoundEmitter_h__ #define SoundEmitter_h__
#include "AL/al.h"
#include "AL/alc.h"
#include "System.h" #include "System.h"
#include "Components/Transform.h" #include "Components/Transform.h"
#include "Components/SoundEmitter.h" #include "Components/SoundEmitter.h"
@@ -41,7 +38,7 @@ private:
DWORD dataSize; DWORD dataSize;
std::map<Component*, ALuint> source; std::map<Component*, ALuint> m_Source;
std::map<std::string, ALuint> m_BufferCache; // string = fileName std::map<std::string, ALuint> m_BufferCache; // string = fileName
}; };
+130 -16
View File
@@ -8,13 +8,19 @@ Systems::SoundSystem::SoundSystem(World* world)
{ {
//initialize OpenAL //initialize OpenAL
ALCdevice* Device = alcOpenDevice(nullptr); ALCdevice* Device = alcOpenDevice(NULL);
if(Device) if(Device)
{ {
context = alcCreateContext(Device, nullptr); context = alcCreateContext(Device, NULL);
alcMakeContextCurrent(context); alcMakeContextCurrent(context);
} }
else
{
LOG_ERROR("OMG OPEN AL FAIL");
}
alGetError();
} }
@@ -30,7 +36,7 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
return; return;
auto entityName = m_World->GetProperty<std::string>(entity, "Name"); auto entityName = m_World->GetProperty<std::string>(entity, "Name");
if (entityName == "Player") if (entityName == "Camera")
{ {
glm::vec3 playerPos = transformComponent->Position; glm::vec3 playerPos = transformComponent->Position;
ALfloat listenerPos[3] = { playerPos.x, playerPos.y, playerPos.z }; ALfloat listenerPos[3] = { playerPos.x, playerPos.y, playerPos.z };
@@ -45,18 +51,54 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
//Listener //Listener
alListenerfv(AL_POSITION, listenerPos); alListenerfv(AL_POSITION, listenerPos);
ALenum error;
if ((error = alGetError()) != AL_NO_ERROR)
{
LOG_ERROR("AL Error: %d", error);
}
alListenerfv(AL_VELOCITY, listenerVel); alListenerfv(AL_VELOCITY, listenerVel);
if ((error = alGetError()) != AL_NO_ERROR)
{
LOG_ERROR("AL Error: %d", error);
}
alListenerfv(AL_ORIENTATION, listenerOri); alListenerfv(AL_ORIENTATION, listenerOri);
if ((error = alGetError()) != AL_NO_ERROR)
{
LOG_ERROR("AL Error: %d", error);
}
} }
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter"); auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr) if(soundEmitter != nullptr)
{ {
alSourcef(source[soundEmitter.get()], AL_GAIN, soundEmitter->Gain); //Volume ALuint source = m_Source[soundEmitter.get()];
alSourcei(source[soundEmitter.get()], AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcei(source[soundEmitter.get()], AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); ALenum error;
alSourcef(source[soundEmitter.get()], AL_PITCH, soundEmitter->Pitch); alSourcef(source, AL_GAIN, soundEmitter->Gain); //Volume
alSourcei(source[soundEmitter.get()], AL_LOOPING, soundEmitter->Loop); 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; glm::vec3 emitterPos = transformComponent->Position;
ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, emitterPos.z }; ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, emitterPos.z };
@@ -64,22 +106,47 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
glm::vec3 emitterVel= transformComponent->Position; glm::vec3 emitterVel= transformComponent->Position;
ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, emitterVel.z }; ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, emitterVel.z };
alSourcefv(source[soundEmitter.get()], AL_POSITION, sourcePos); alSourcefv(source, AL_POSITION, sourcePos);
alSourcefv(source[soundEmitter.get()], AL_VELOCITY, sourceVel); 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<Components::SoundEmitter> emitter, std::string fileName) void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{ {
ALuint buffer = LoadFile(fileName); ALuint buffer = LoadFile(fileName);
alSourcei(source[emitter.get()], AL_BUFFER, buffer); ALuint source = m_Source[emitter.get()];
alSourcePlay(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::OnComponentCreated(std::string type, std::shared_ptr<Component> component) void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{ {
if(type == "SoundEmitter") if(type == "SoundEmitter") {
source[component.get()] = CreateSource(); ALuint source = CreateSource();
m_Source[component.get()] = source;
}
} }
ALuint Systems::SoundSystem::LoadFile(std::string fileName) ALuint Systems::SoundSystem::LoadFile(std::string fileName)
@@ -88,7 +155,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName)
return m_BufferCache[fileName]; return m_BufferCache[fileName];
FILE *fp = NULL; FILE *fp = NULL;
fp = fopen(strcat("Sounds/", fileName.c_str()), "rb"); fp = fopen(fileName.c_str(), "rb");
//CHECK FOR VALID WAVE-FILE //CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp); fread(type, sizeof(char), 4, fp);
@@ -150,7 +217,20 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName)
} }
ALuint buffer; ALuint buffer;
alGenBuffers(1, &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); 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; m_BufferCache[fileName] = buffer;
return buffer; return buffer;
@@ -159,7 +239,41 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName)
ALuint Systems::SoundSystem::CreateSource() ALuint Systems::SoundSystem::CreateSource()
{ {
ALuint source; ALuint source;
alGenBuffers(1, &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; return source;
} }
+1
View File
@@ -59,6 +59,7 @@
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">