Skip to content

Sound Manager to master #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d815b88
OpenAL cmake config
Wiesiek418 Feb 6, 2025
1f5c995
Constructors, Destructors [Sound, Source, Clip] and SoundListener
Wiesiek418 Feb 15, 2025
0928d0c
Load audio file OGG
Wiesiek418 Feb 23, 2025
5bb0282
Fix code format
Wiesiek418 Feb 23, 2025
47b5eff
Commit for test raytrace
Wiesiek418 Feb 25, 2025
ee5c287
Rebuild Sound Manager and playing sound
Wiesiek418 Feb 26, 2025
e675d64
Pause, Stop sound. Comments in manager
Wiesiek418 Feb 27, 2025
b22e01f
Delete sound thread
Wiesiek418 Mar 8, 2025
4b877ea
Changing the ums_sources parameter from sso name to sso object and ad…
Wiesiek418 Mar 10, 2025
6b8063e
Refactore and rebuild manager, soundClip, soundSource (soundClip key …
Wiesiek418 Mar 14, 2025
f151877
Rebuild of sound manager (WiP), change map to vector, shared/weak poi…
Wiesiek418 Mar 15, 2025
19a4cdc
Fix sound debug, detect expired pointer
Wiesiek418 Mar 23, 2025
dc90250
Replace throw to cerr in source, manager, clip
Wiesiek418 Mar 23, 2025
8e920eb
External public function from manager.cpp to functions.hpp and WiP gr…
Wiesiek418 Mar 24, 2025
634be22
Delete SoundGroupParameters and SoundGroupMovement, add functions.hpp…
Wiesiek418 Mar 28, 2025
d1b951c
Sound group as a offset in sso, adds all parameters, getting only mon…
Wiesiek418 Apr 1, 2025
c48d3b3
Fast commit before branch engine test
Wiesiek418 Apr 3, 2025
6f2a24f
Comments on sound hpp files
Wiesiek418 Apr 10, 2025
3f7a659
WiP load WAV file, add removeObject to SoundManager
Wiesiek418 Apr 11, 2025
3182ba2
Merge master to sound
Wiesiek418 Apr 11, 2025
83952e0
Hot fixs in sound system
Wiesiek418 Apr 15, 2025
4e74a7c
Hot fixes v2 sound system
Wiesiek418 Apr 15, 2025
cecbca1
WAV file support
Wiesiek418 Apr 16, 2025
172fe7d
Fixing memory leaks
Wiesiek418 Apr 28, 2025
0cd645f
Improve update parameters system, add debug only method in manager, c…
Wiesiek418 Apr 30, 2025
9522062
Change arytmetic for offset group in source parameters
Wiesiek418 Apr 30, 2025
3b90f5d
Delete test lines in main
Wiesiek418 Apr 30, 2025
d4012c0
remove malloc from convert channel, few small fix
Wiesiek418 Apr 30, 2025
fbbd1ee
Delete some alGetError
Wiesiek418 Apr 30, 2025
94fc20c
GetError
Wiesiek418 Apr 30, 2025
a90a882
Delete const
Wiesiek418 May 1, 2025
9f1a3d5
Merge master to sound
Wiesiek418 May 16, 2025
6291dae
Merge branch 'sound' of github.com:dark-tree/checklight into sound
Wiesiek418 May 16, 2025
071e8f0
Migrate some of the console output to the logger
magistermaks May 16, 2025
d713791
Ported output to use logger
magistermaks May 17, 2025
e74630d
Added windows/VS specific build logic
magistermaks May 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include(${CMAKE_SOURCE_DIR}/cmake/ShaderBuild.cmake)
find_package(Threads REQUIRED)
find_package(Freetype REQUIRED)
find_package(Vulkan REQUIRED)
find_package(OpenAL)

# Try: cmake . -B build && cmake --build build/
if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
Expand All @@ -33,6 +34,19 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
endif()
endif()

if (NOT OpenAL_FOUND)
message(STATUS "OpenAL was NOT found, falling back to OpenAL-Soft!")

FetchContent_Declare(
openal-soft
GIT_REPOSITORY https://github.com/kcat/openal-soft
GIT_TAG 1.23.1
GIT_PROGRESS TRUE
)

FetchContent_MakeAvailable(openal-soft)
endif()

# Don't fail on systems without X11 libraries
if (UNIX)
find_program(WAYLAND_SCANNER_EXECUTABLE NAMES wayland-scanner)
Expand Down Expand Up @@ -103,12 +117,18 @@ compile_glsl_directory(
VULKAN_TARGET "vulkan1.2"
)

if(NOT DEFINED OpenAL_INCLUDE_DIRS)
set(OpenAL_INCLUDE_DIRS "${OpenAL_SOURCE_DIR}/include")
message(STATUS "Applying OpenAL include directory fix")
endif()

set(CHECKLIGHT_LIBRARIES
${glm_SOURCE_DIR} # Headers in ./glm
${glfw_SOURCE_DIR}/include # Headers in ./GLFW
${stb_SOURCE_DIR} # Headers in ./
${Vulkan_INCLUDE_DIRS} # Headers in ./vulcan
${vma_SOURCE_DIR}/include # Headers in ./
${OpenAL_INCLUDE_DIRS} # Headers in ./AL
)

add_library(checklight_common INTERFACE)
Expand All @@ -122,13 +142,16 @@ file(GLOB_RECURSE CHECKLIGHT_ENGINE_SOURCES "src/engine/*.cpp")
add_library(checklight_engine_system ${CHECKLIGHT_ENGINE_SOURCES})

file(GLOB_RECURSE CHECKLIGHT_RENDER_SOURCES "src/render/*.cpp")
add_library(checklight_render_system ${CHECKLIGHT_RENDER_SOURCES})
add_library(checklight_render_system ${CHECKLIGHT_RENDER_SOURCES} )

file(GLOB_RECURSE CHECKLIGHT_SHARED_SOURCES "src/shared/*.cpp")
add_library(checklight_shared_system ${CHECKLIGHT_SHARED_SOURCES})
add_library(checklight_shared_system ${CHECKLIGHT_SHARED_SOURCES} )

file(GLOB_RECURSE CHECKLIGHT_INPUT_SOURCES "src/input/*.cpp")
add_library(checklight_input_system ${CHECKLIGHT_INPUT_SOURCES})
add_library(checklight_input_system ${CHECKLIGHT_INPUT_SOURCES} )

file(GLOB_RECURSE CHECKLIGHT_SOUND_SOURCES "src/sound/*.cpp")
add_library(checklight_sound_system ${CHECKLIGHT_SOUND_SOURCES} )

file(GLOB_RECURSE CHECKLIGHT_GUI_SOURCES "src/gui/*.cpp")
add_library(checklight_gui_system ${CHECKLIGHT_GUI_SOURCES})
Expand All @@ -138,7 +161,7 @@ add_library(checklight_external
lib/stb_image_write.cpp
lib/stb_vorbis.cpp
lib/vk_mem_alloc.cpp
)
)

target_include_directories(checklight_external PRIVATE
${CHECKLIGHT_LIBRARIES}
Expand Down Expand Up @@ -174,6 +197,12 @@ target_link_libraries(checklight_input_system PRIVATE
checklight_common
)

target_link_libraries(checklight_sound_system PRIVATE
checklight_common
OpenAL::OpenAL
checklight_external
)

target_link_libraries(checklight_gui_system PRIVATE
checklight_common
checklight_input_system
Expand All @@ -186,6 +215,7 @@ target_link_libraries(checklight PRIVATE
checklight_render_system
checklight_shared_system
checklight_input_system
checklight_sound_system
checklight_gui_system
checklight_engine_system
)
Expand Down Expand Up @@ -269,3 +299,11 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/assets")
add_dependencies(checklight_common copy-assets)
add_dependencies(checklight_common compile-shaders)
endif()

# Needed on MSVS to fix missing DLL errors during runtime
if (WIN32)
add_custom_command(TARGET checklight_sound_system POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${openal-soft_BINARY_DIR}/OpenAL32.dll"
"${CMAKE_BINARY_DIR}/OpenAL32.dll")
endif()
Binary file added assets/sounds/1.ogg
Binary file not shown.
Binary file added assets/sounds/2.ogg
Binary file not shown.
Binary file added assets/sounds/3.ogg
Binary file not shown.
Binary file added assets/sounds/4.ogg
Binary file not shown.
Binary file added assets/sounds/5.ogg
Binary file not shown.
Binary file added assets/sounds/testOGG.ogg
Binary file not shown.
Binary file added assets/sounds/testWAV.wav
Binary file not shown.
7 changes: 6 additions & 1 deletion src/external.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <cstdint>

// C++ libs
#include <iostream>
#include <vector>
#include <functional>
#include <list>
Expand Down Expand Up @@ -58,6 +57,12 @@
#include <glm/gtx/hash.hpp>
#include <glm/gtx/norm.hpp>


// OpenAL
#include <AL/al.h>
#include <AL/alc.h>


// Vulkan Format Database
#include "vk_format_info.hpp"

Expand Down
26 changes: 23 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include <gui/context.hpp>
#include <gui/debug/render.hpp>
#include <gui/theme/dark.hpp>
Expand All @@ -8,7 +7,7 @@
#include <gui/widget/text.hpp>
#include <shared/args.hpp>
#include <shared/logger.hpp>

#include "sound/sound.hpp"
#include "render/render.hpp"
#include "input/input.hpp"
#include "engine/engine.hpp"
Expand Down Expand Up @@ -100,6 +99,11 @@ static void entry(Args& args) {
objects.push_back(object);
}

system.getParameters().setAmbientLight(glm::vec3(0.0, 0.0, 0.0));
system.getParameters().setDenoise(true);
system.getParameters().setShadows(true);
system.getParameters().setGISamples(1);

system.getLightManager().createDirectionalLight(
{0.0, 3.5, -1.0},
{1.0, 1.0, 1.0},
Expand All @@ -114,6 +118,19 @@ static void entry(Args& args) {
true
);

SoundListener::setPosition(0, 0, 0);
SoundManager& sm = SoundManager::getInstance();
auto sso = std::make_shared<SoundSourceObject>();
sm.addSource(sso);
sm.createSoundClipAndAddToSourceObject("assets/sounds/testOGG.ogg", sso);
sso->setPosition(0.0f, 4.8f, 0.0f);
sso->setMaxDistance(10.0f);
sso->setReferenceDistance(1.0f);
sso->setRolloffFactor(1.0f);
sso->setMinGain(0.0f);
sso->setMaxGain(1.0f);
SoundListener::setDistanceModel(AL_LINEAR_DISTANCE);
//window.getInputDispatcher().registerListener(std::make_shared<DebugInputListener>());
while (!window.shouldClose()) {
window.poll();

Expand Down Expand Up @@ -143,9 +160,12 @@ static void entry(Args& args) {
point_light->vector = glm::vec3(3.0, 2.0, 18.0 * sin(glfwGetTime() / 8));
point_light->color = glm::vec3(sin(glfwGetTime() / 2) * 0.5 + 0.5, sin(glfwGetTime() / 3 + 2) * 0.5 + 0.5, sin(glfwGetTime() / 5 + 4) * 0.5 + 0.5);
system.getLightManager().flush();

// render the scene
system.draw();
sm.playSound(sso);
SoundListener::setPosition(current_board->getCamPos());
SoundListener::setOrientation(current_board->getCamForward(),glm::vec3(0.0f,1.0f,0.0f));
}

system.wait();
Expand Down
172 changes: 172 additions & 0 deletions src/sound/clip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include "clip.hpp"

#include "debug.hpp"
#include "shared/logger.hpp"

SoundClip::SoundClip(){
alGetError();
alGenBuffers(1, &buffer);
alCheckError("durring clip creation");
}

SoundClip::~SoundClip(){
alDeleteBuffers(1, &buffer);
}

void SoundClip::convertChannels(int* audio_size, int channels, short* data) {
/// Resize to 1 channel
int new_audio_size = *audio_size / channels;
/// Get data only from 1 channel
int j = 0;
int sum_audio = 0;
for (int i = 0; i < *audio_size; i++) {
sum_audio += data[i];
/// Check if we are at the end of the block of channels
/// If we are, get average of the channels and save to the output
/// All channels convert to mono as average of the channels
if (i % channels == (channels-1)) {
data[j] = sum_audio/channels;
sum_audio = 0;
j++;
}
}
*audio_size = new_audio_size;
}

void SoundClip::loadOGGFile(const char* filename) {
// clean up errors
alGetError();

int channels, sample_rate;
short* output;

// get output, channels, sample rate from filename and size of the data
ALsizei size_block = stb_vorbis_decode_filename(filename, &channels, &sample_rate, &output);

if (size_block == -1) {
out::error("Failed to decode OGG file '%s'!", filename);
free(output);
return;
}
this->path = filename;

/// Check of the file has more than 2 channels. If it has, convert it to mono
if (channels >= 2) {
convertChannels(&size_block, channels, output);
}

ALenum format = AL_FORMAT_MONO16;
ALsizei audio_size = size_block * sizeof(short);

// Create buffer
alBufferData(buffer, format, output, audio_size, sample_rate);
free(output);

if (alGetError() != AL_NO_ERROR) {
FAULT("Failed to upload the sound buffer!");
}

out::debug("Loaded sound '%s'", filename);
}

void SoundClip::loadWAVFile(const char* filename) {
alGetError();
/// WAV File Header
/// https://docs.fileformat.com/audio/wav/
/// http://soundfile.sapp.org/doc/WaveFormat/
struct WAVFile {
char chunk_ID[4];
uint32_t chunk_size;
char format[4];

char subchunk1_ID[4];
uint32_t subchunk1_size;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;

char subchunk2_ID[4];
uint32_t subchunk2_size;
};

std::ifstream wav_file(filename, std::ios::binary);
WAVFile wav_header;
/// Read header from file
if (!wav_file.read(reinterpret_cast<char*>(&wav_header), sizeof(WAVFile))) {
out::error("Failed decode WAV file '%s', can't read file header!", filename);
return;
}

ALsizei frequency = wav_header.sample_rate;
/// Read all audio data without header from file
std::vector<short> data;
data.resize(wav_header.subchunk2_size);

if (!wav_file.read(reinterpret_cast<char*>(data.data()), wav_header.subchunk2_size)) {
out::error("Failed decode WAV file '%s', can't read file chunk!", filename);
return;
}
ALsizei audio_size = wav_header.subchunk2_size;

/// Check of the file has more than 2 channels. If it has, convert it to mono
if (wav_header.num_channels >= 2) {
convertChannels(&audio_size, wav_header.num_channels, data.data());
}

ALenum format;
if (wav_header.bits_per_sample == 8) {
format = AL_FORMAT_MONO8;
}
else if (wav_header.bits_per_sample == 16) {
format = AL_FORMAT_MONO16;
} else {
out::error("Failed decode WAV file '%s', unsupported channel count, expected mono or stereo!", filename);
return;
}

alBufferData(buffer, format, data.data(), audio_size, frequency);

if (alGetError() != AL_NO_ERROR) {
FAULT("Failed to upload the sound buffer!");
}

out::debug("Loaded sound '%s'", filename);
}

void SoundClip::loadAudio(const char* filename) {
std::ifstream input_file {filename, std::ios::binary};

if (!input_file) {
out::error("Can't open sound file '%s'!", filename);
return;
}

char header[4];
input_file.read(header, 4);
input_file.close();

if (std::string(header, 4) == "OggS") {
loadOGGFile(filename);
return;
}

if (std::string(header, 4) == "RIFF" || std::string(header, 4) == "WAVE") {
loadWAVFile(filename);
return;
}

out::error("Can't open sound file '%s', unsupported or invalid format!", filename);

}

ALuint SoundClip::getBuffer() {
return buffer;
}

std::string SoundClip::getPath()
{
return path;
}
Loading