OpenAL Seperation and Adjustments (#34)

* OpenAL adjustments and improvements mk.1

Seperated SoundSystemAl from the sdl section and into platform/openal.
Made PlatformDefinitions.hpp require a "USE_OPENAL" definition instead of "USE_SDL" definition for OpenAL
Added SoundSystemAl.cpp  and SoundSystemAL.hpp files to the actual solution file so it can be compiled properly.
Made the SoundSystemAl use headers from thirdparty/OpenAL/include folder.
Made the SoundSystemAl use #pragma comment(lib, "OpenAL32.lib") to include the library if needed without adding it to the linker input value in the solution file.
Added thirdparty/OpenAL/libs/Win64/thirdparty/OpenAL/libs/Win32 to the additional libraries so that it can find the OpenAL32.lib library.

* OpenAL adjustments and improvements mk.2

Removed specific USE_SDL preprocessor definitions related to OpenAL which were unhelpful/unneeded for multi-platform use.
Made the OpenAl sound system inherit from the SoundSystem class and implement the functions properly without requiring a custom update function.
Fixed an error that would happen when closing the game related to OpenAl closing the sound device.
Removed unnecessary is_ui check in OpenAL sound system, UI sounds work just fine without it.
Adjusted rolloff factor, not sure if this is just placebo but it felt a bit better?

* Update sdl/CMakeLists.txt for new OpenAL stuff

* CMakeLists name fix

* Minor fixes for SDL compatibility

* Added check for absolute zero position

Made the OpenAL sound system consider any sound that is at absolute zero non-spatial

Also removed unneeded ORIGINAL_CODE check in SoundSystem.hpp and SoundSystem.cpp

* Fixed OpenAL popping strangeness

OpenAL apparently makes weird popping noises when the gain of a sound is below 0, so I added a check to see if the distance is too far (for the new updated settings)

I did some graphing and the previous settings for OpenAL were limiting the sound distance to 8 blocks (which was causing popping when sounds were between 8-16 blocks away as the gain was 0 or below) so I changed the rolloff factor to 1.0f to make sounds falloff at 16 blocks instead which seems reasonable/like the original intention.

* Minor Fix to solution additional includes

* Forgot an sdl compat change
This commit is contained in:
Vruk
2023-08-08 01:12:08 -07:00
committed by GitHub
parent c0ef625972
commit 3447669f04
11 changed files with 160 additions and 132 deletions

View File

@@ -15,10 +15,10 @@
// Add sound system overrides here
#elif defined(USE_SDL)
#elif defined(USE_OPENAL)
// -- OpenAL based sound system for SDL
#include "sdl/SoundSystemAL.hpp"
// -- OpenAL based sound system
#include "openal/SoundSystemAL.hpp"
#define SOUND_SYSTEM_TYPE SoundSystemAL
#elif defined(_WIN32)

View File

@@ -67,11 +67,12 @@ SoundSystemAL::~SoundSystemAL()
// Close Device
alcCloseDevice(device);
err = alcGetError(device);
// Can't check for error because device is closed
/*err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Close Audio Device: %s", alcGetString(device, err));
}
}*/
}
// Error Checking
@@ -159,7 +160,30 @@ ALuint SoundSystemAL::get_buffer(const SoundDesc& sound)
}
}
void SoundSystemAL::update(float x, float y, float z, float yaw)
bool SoundSystemAL::isAvailable()
{
return loaded;
}
void SoundSystemAL::setListenerPos(float x, float y, float z)
{
// Update Listener Position
alListener3f(AL_POSITION, x, y, z);
AL_ERROR_CHECK();
lastListenerPos = Vec3(x, y, z);
update();
}
void SoundSystemAL::setListenerAngle(float yaw, float pitch)
{
// Update Listener Orientation
float radian_yaw = yaw * (M_PI / 180);
ALfloat orientation[] = { -sinf(radian_yaw), 0.0f, cosf(radian_yaw), 0.0f, 1.0f, 0.0f };
alListenerfv(AL_ORIENTATION, orientation);
AL_ERROR_CHECK();
}
void SoundSystemAL::update()
{
// Check
if (!loaded)
@@ -172,16 +196,6 @@ void SoundSystemAL::update(float x, float y, float z, float yaw)
alListenerf(AL_GAIN, volume);
AL_ERROR_CHECK();
// Update Listener Position
alListener3f(AL_POSITION, x, y, z);
AL_ERROR_CHECK();
// Update Listener Orientation
float radian_yaw = yaw * (M_PI / 180);
ALfloat orientation[] = {-sinf(radian_yaw), 0.0f, cosf(radian_yaw), 0.0f, 1.0f, 0.0f};
alListenerfv(AL_ORIENTATION, orientation);
AL_ERROR_CHECK();
// Clear Finished Sources
std::vector<ALuint>::iterator it = sources.begin();
while (it != sources.end())
@@ -226,7 +240,7 @@ void SoundSystemAL::update(float x, float y, float z, float yaw)
}
}
void SoundSystemAL::play(const SoundDesc& sound, float x, float y, float z, float volume, float pitch, bool is_ui)
void SoundSystemAL::playAt(const SoundDesc& sound, float x, float y, float z, float volume, float pitch)
{
// Check
if (!loaded)
@@ -234,65 +248,76 @@ void SoundSystemAL::play(const SoundDesc& sound, float x, float y, float z, floa
return;
}
if (volume <= 0.0f)
return;
float distance = Vec3(x, y, z).distanceTo(lastListenerPos);
if (distance >= MAX_DISTANCE)
return;
// Load Sound
ALuint buffer = get_buffer(sound);
if (volume > 0.0f && buffer)
if (!buffer)
return;
// Get Source
ALuint al_source;
if (idle_sources.size() > 0)
{
// Get Source
ALuint al_source;
if (idle_sources.size() > 0)
// Use Idle Source
al_source = idle_sources.back();
idle_sources.pop_back();
}
else
{
// Create Source
alGenSources(1, &al_source);
// Special Out-Of-Memory Handling
{
// Use Idle Source
al_source = idle_sources.back();
idle_sources.pop_back();
}
else
{
// Create Source
alGenSources(1, &al_source);
// Special Out-Of-Memory Handling
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY)
{
ALenum err = alGetError();
if (err == AL_OUT_OF_MEMORY)
{
return;
}
else
{
AL_ERROR_CHECK_MANUAL(err);
}
return;
}
else
{
AL_ERROR_CHECK_MANUAL(err);
}
}
// Set Properties
alSourcef(al_source, AL_PITCH, pitch);
AL_ERROR_CHECK();
alSourcef(al_source, AL_GAIN, volume);
AL_ERROR_CHECK();
alSource3f(al_source, AL_POSITION, x, y, z);
AL_ERROR_CHECK();
alSource3f(al_source, AL_VELOCITY, 0, 0, 0);
AL_ERROR_CHECK();
alSourcei(al_source, AL_LOOPING, AL_FALSE);
AL_ERROR_CHECK();
alSourcei(al_source, AL_SOURCE_RELATIVE, is_ui ? AL_TRUE : AL_FALSE);
AL_ERROR_CHECK();
// Set Attenuation
alSourcef(al_source, AL_MAX_DISTANCE, 16.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_ROLLOFF_FACTOR, 6.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_REFERENCE_DISTANCE, 5.0f);
AL_ERROR_CHECK();
// Set Buffer
alSourcei(al_source, AL_BUFFER, buffer);
AL_ERROR_CHECK();
// Play
alSourcePlay(al_source);
AL_ERROR_CHECK();
sources.push_back(al_source);
}
bool isUi = AL_FALSE;
if (x == 0 && y == 0 && z == 0)
isUi = AL_TRUE;
// Set Properties
alSourcef(al_source, AL_PITCH, pitch);
AL_ERROR_CHECK();
alSourcef(al_source, AL_GAIN, volume);
AL_ERROR_CHECK();
alSource3f(al_source, AL_POSITION, x, y, z);
AL_ERROR_CHECK();
alSource3f(al_source, AL_VELOCITY, 0, 0, 0);
AL_ERROR_CHECK();
alSourcei(al_source, AL_LOOPING, AL_FALSE);
AL_ERROR_CHECK();
alSourcei(al_source, AL_SOURCE_RELATIVE, isUi);
AL_ERROR_CHECK();
// Set Attenuation
alSourcef(al_source, AL_MAX_DISTANCE, MAX_DISTANCE);
AL_ERROR_CHECK();
alSourcef(al_source, AL_ROLLOFF_FACTOR, 1.0f);
AL_ERROR_CHECK();
alSourcef(al_source, AL_REFERENCE_DISTANCE, 5.0f);
AL_ERROR_CHECK();
// Set Buffer
alSourcei(al_source, AL_BUFFER, buffer);
AL_ERROR_CHECK();
// Play
alSourcePlay(al_source);
AL_ERROR_CHECK();
sources.push_back(al_source);
}

View File

@@ -0,0 +1,46 @@
#pragma once
#ifdef _WIN32
#include <thirdparty/OpenAL/Include/al.h>
#include <thirdparty/OpenAL/Include/alc.h>
#pragma comment( lib, "OpenAl32.lib" )
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
#include <string>
#include <vector>
#include <unordered_map>
#include "client/sound/SoundSystem.hpp"
#include <client/common/Vec3.hpp>
#define MAX_IDLE_SOURCES 50
#define MAX_DISTANCE 16.0f
class SoundSystemAL : public SoundSystem
{
public:
SoundSystemAL();
~SoundSystemAL();
virtual bool isAvailable();
void update();
virtual void playAt(const SoundDesc& sound, float x, float y, float z, float volume, float pitch);
virtual void setListenerPos(float x, float y, float z);
virtual void setListenerAngle(float yaw, float pitch);
private:
void delete_sources();
void delete_buffers();
ALuint get_buffer(const SoundDesc& sound);
ALCdevice *device = NULL;
ALCcontext *context = NULL;
bool loaded = false;
std::vector<ALuint> sources;
std::vector<ALuint> idle_sources;
std::unordered_map<void *, ALuint> buffers;
Vec3 lastListenerPos;
};

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16.0)
project(reminecraftpe)
# SDL Build
add_compile_definitions(USE_SDL HANDLE_CHARS_SEPARATELY)
add_compile_definitions(USE_SDL USE_OPENAL HANDLE_CHARS_SEPARATELY)
# WASM
if(EMSCRIPTEN)
@@ -30,7 +30,7 @@ endif()
add_executable(reminecraftpe
main.cpp
AppPlatform_sdl.cpp
SoundSystemAL.cpp
../openal/SoundSystemAL.cpp
)
# Core

View File

@@ -1,32 +0,0 @@
#pragma once
#include <AL/al.h>
#include <AL/alc.h>
#include <string>
#include <vector>
#include <unordered_map>
#include "client/sound/SoundData.hpp"
#define MAX_IDLE_SOURCES 50
class SoundSystemAL
{
public:
SoundSystemAL();
~SoundSystemAL();
void update(float x, float y, float z, float yaw);
void play(const SoundDesc& sound, float x, float y, float z, float volume, float pitch, bool is_ui);
private:
void delete_sources();
void delete_buffers();
ALuint get_buffer(const SoundDesc& sound);
ALCdevice *device = NULL;
ALCcontext *context = NULL;
bool loaded = false;
std::vector<ALuint> sources;
std::vector<ALuint> idle_sources;
std::unordered_map<void *, ALuint> buffers;
};

View File

@@ -611,12 +611,8 @@ void Minecraft::tick()
#ifndef ORIGINAL_CODE
if (m_pMobPersp)
{
#ifdef USE_SDL
m_pSoundEngine->m_soundSystem.update(m_pMobPersp->m_pos.x, m_pMobPersp->m_pos.y, m_pMobPersp->m_pos.z, m_pMobPersp->m_yaw);
#else
m_pSoundEngine->m_soundSystem.setListenerPos(m_pMobPersp->m_pos.x, m_pMobPersp->m_pos.y, m_pMobPersp->m_pos.z);
m_pSoundEngine->m_soundSystem.setListenerAngle(m_pMobPersp->m_yaw, m_pMobPersp->m_pitch);
#endif
}
#endif

View File

@@ -66,11 +66,7 @@ void SoundEngine::play(const std::string& name)
SoundDesc sd;
if (m_repository.get(name, sd)) {
#ifdef USE_SDL
m_soundSystem.play(sd, 0, 0, 0, 1, 1, true);
#else
m_soundSystem.playAt(sd, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f);
#endif
}
}
@@ -82,10 +78,6 @@ void SoundEngine::play(const std::string& name, float a, float b, float c, float
SoundDesc sd;
if (m_repository.get(name, sd)) {
#ifdef USE_SDL
m_soundSystem.play(sd, a, b, c, d, e, false);
#else
m_soundSystem.playAt(sd, a, b, c, d, e);
#endif
}
}

View File

@@ -17,15 +17,9 @@ void SoundSystem::setListenerPos(float x, float y, float z)
{
}
#ifndef ORIGINAL_CODE
void SoundSystem::setListenerAngle(float yaw, float pitch)
{
}
#else
void SoundSystem::setListenerAngle(float yaw)
{
}
#endif
void SoundSystem::load(const std::string& sound)
{

View File

@@ -16,11 +16,7 @@ class SoundSystem
public:
virtual bool isAvailable();
virtual void setListenerPos(float x, float y, float z);
#ifndef ORIGINAL_CODE
virtual void setListenerAngle(float yaw, float pitch);
#else
virtual void setListenerAngle(float yaw);
#endif
virtual void load(const std::string& sound);
virtual void play(const std::string& sound);
virtual void pause(const std::string& sound);

View File

@@ -27,6 +27,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\platforms\openal\SoundSystemAL.hpp" />
<ClInclude Include="..\platforms\PlatformDefinitions.hpp" />
<ClInclude Include="..\platforms\windows\AppPlatform_windows.hpp" />
<ClInclude Include="..\platforms\windows\SoundSystemWindows.hpp" />
@@ -328,6 +329,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\compat\GLExt.cpp" />
<ClCompile Include="..\platforms\openal\SoundSystemAL.cpp" />
<ClCompile Include="..\platforms\windows\AppPlatform_windows.cpp" />
<ClCompile Include="..\platforms\windows\main.cpp" />
<ClCompile Include="..\platforms\windows\SoundSystemWindows.cpp" />
@@ -775,7 +777,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win32</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugAsan|Win32'">
@@ -794,7 +796,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win32</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -817,7 +819,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win32</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -836,7 +838,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win64</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugAsan|x64'">
@@ -855,7 +857,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win64</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -878,7 +880,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glu32.lib;ws2_32.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(CONTRIB_PATH)\SDK\Lib\x64\;$(SolutionDir)..\thirdparty\OpenAL\libs\Win64</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -91,6 +91,9 @@
<Filter Include="source\platforms\windows">
<UniqueIdentifier>{4cc1a780-f0a9-41b1-bee2-dc28f00fb958}</UniqueIdentifier>
</Filter>
<Filter Include="source\platforms\openal">
<UniqueIdentifier>{cc57a0f6-733d-44fa-8478-b1826b074d66}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Third Party">
<UniqueIdentifier>{d754d82a-1cf9-4e4a-9dfd-2cc79f4d1fc1}</UniqueIdentifier>
</Filter>
@@ -990,6 +993,9 @@
<ClInclude Include="..\platforms\PlatformDefinitions.hpp">
<Filter>source\platforms</Filter>
</ClInclude>
<ClInclude Include="..\platforms\openal\SoundSystemAL.hpp">
<Filter>source\platforms\openal</Filter>
</ClInclude>
<ClInclude Include="..\thirdparty\GL\glext.h">
<Filter>Header Files\Third Party</Filter>
</ClInclude>
@@ -1955,6 +1961,9 @@
<ClCompile Include="..\compat\GLExt.cpp">
<Filter>thirdparty</Filter>
</ClCompile>
<ClCompile Include="..\platforms\openal\SoundSystemAL.cpp">
<Filter>source\platforms\openal</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\thirdparty\raknet\CMakeLists.txt">