Skip to content

Commit

Permalink
Web: Working OpenAL test
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Mar 9, 2024
1 parent f55cbbb commit 46fff5b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ else()
endif()

if (BUILD_FOR_WEB OR HYPERSOMNIA_BARE)
set(BUILD_FREETYPE ON) # Slowly enabling one by one
set(BUILD_SOUND_FORMAT_DECODERS ON) # Slowly enabling one by one
# Slowly enabling one by one
set(BUILD_FREETYPE ON)
set(BUILD_SOUND_FORMAT_DECODERS ON)

set(BUILD_OPENGL ON) # Slowly enabling one by one
set(BUILD_WINDOW_FRAMEWORK ON) # Slowly enabling one by one
set(BUILD_OPENGL ON)
set(BUILD_OPENAL ON)
set(BUILD_WINDOW_FRAMEWORK ON)
endif()

## Static allocation switches. Possible values are one or zero.
Expand Down Expand Up @@ -1368,6 +1370,10 @@ if(BUILD_FOR_WEB)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mbulk-memory")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${ADDITIONAL_WEB_FLAGS}")

if (BUILD_OPENAL)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lopenal")
endif()

# Embed all files
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --embed-file ${HYPERSOMNIA_EXE_RESOURCES_DIR}/content@/content")
Expand Down Expand Up @@ -1555,7 +1561,7 @@ add_library(lua ${LUA_CPPS})

include(add_include_dirs_of)

if(BUILD_OPENAL)
if(BUILD_OPENAL AND NOT BUILD_FOR_WEB)
# We build OpenAL Soft. We need to set some variables beforehand.

set(LIBTYPE "STATIC")
Expand Down Expand Up @@ -1944,7 +1950,7 @@ if(BUILD_WINDOW_FRAMEWORK)
endif()
endif()

if(BUILD_OPENAL)
if(BUILD_OPENAL AND NOT BUILD_FOR_WEB)
list(APPEND HYPERSOMNIA_LIBS OpenAL)
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/application/gui/settings_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int performance_settings::get_default_num_pool_workers() {
+ main_threads
+ openal_threads
#if PLATFORM_WEB
+ 4 // to be on the safe side to ensure responsiveness
+ 2 // to be on the safe side so we don't deadlock because of new threads created
#endif
;

Expand Down
14 changes: 11 additions & 3 deletions src/augs/audio/audio_backend.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "augs/audio/audio_backend.h"
#include "augs/log.h"
#include "augs/audio/sound_source.h"
#include "augs/audio/audio_command.h"
#include "augs/templates/always_false.h"

#if BUILD_OPENAL
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
#if !PLATFORM_WEB
#include <AL/efx.h>
#endif
#endif

/* A shortcut which will be heavily used from now on */

Expand All @@ -19,15 +21,19 @@ constexpr bool same = std::is_same_v<A, B>;

namespace augs {
audio_backend::audio_backend() {
#if !PLATFORM_WEB
#if BUILD_OPENAL
alGenFilters(1, &lowpass_filter_id);
alFilteri(lowpass_filter_id, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
#endif
#endif
}

audio_backend::~audio_backend() {
#if !PLATFORM_WEB
#if BUILD_OPENAL
alDeleteFilters(1, &lowpass_filter_id);
#endif
#endif
}

Expand Down Expand Up @@ -59,12 +65,12 @@ namespace augs {

if (t.is_direct_listener) {
source.set_relative_and_zero_vel_pos();
source.set_air_absorption_factor(t.air_absorption_factor);
source.set_air_absorption_factor(0.f);
}
else {
source.set_relative(false);
source.set_position(si, t.position);
source.set_air_absorption_factor(0.f);
source.set_air_absorption_factor(t.air_absorption_factor);
}

source.set_spatialize(!t.is_direct_listener);
Expand All @@ -91,13 +97,15 @@ namespace augs {
source.set_distance_model(t.model);
source.set_looping(t.looping);

#if !PLATFORM_WEB
if (t.lowpass_gainhf >= 0.f) {
alFilterf(lowpass_filter_id, AL_LOWPASS_GAINHF, t.lowpass_gainhf);
alSourcei(source.get_id(), AL_DIRECT_FILTER, lowpass_filter_id);
}
else {
alSourcei(source.get_id(), AL_DIRECT_FILTER, AL_FILTER_NULL);
}
#endif
}
else if constexpr(same<C, update_flash_noise>) {
auto& source = flash_noise_source;
Expand Down
41 changes: 35 additions & 6 deletions src/augs/audio/audio_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@
#if BUILD_OPENAL
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/efx.h>
#if PLATFORM_WEB

#define AL_SOURCE_DISTANCE_MODEL 0x200

#define ALC_HRTF_SOFT 0x1992
#define ALC_HRTF_STATUS_SOFT 0x1993
#define ALC_HRTF_DISABLED_SOFT 0x0000
#define ALC_HRTF_ENABLED_SOFT 0x0001
#define ALC_HRTF_DENIED_SOFT 0x0002
#define ALC_HRTF_REQUIRED_SOFT 0x0003
#define ALC_HRTF_HEADPHONES_DETECTED_SOFT 0x0004
#define ALC_HRTF_UNSUPPORTED_FORMAT_SOFT 0x0005
#else
#include <AL/alext.h>
#include <AL/efx.h>
#endif
#endif

#include "augs/log.h"
Expand Down Expand Up @@ -143,16 +157,30 @@ namespace augs {
#endif
}

void audio_device::reset_device(audio_settings settings) {
#if BUILD_OPENAL
ALCint attrs[] = {
static std::array<ALCint, 9> make_attrs(const audio_settings& settings) {
#if PLATFORM_WEB
return {
ALC_HRTF_SOFT, settings.enable_hrtf, /* request HRTF */
ALC_MONO_SOURCES, static_cast<ALCint>(settings.max_number_of_sound_sources),
0
};
#else
return {
ALC_HRTF_SOFT, settings.enable_hrtf, /* request HRTF */
ALC_MONO_SOURCES, static_cast<ALCint>(settings.max_number_of_sound_sources),
ALC_OUTPUT_LIMITER_SOFT, AL_TRUE,
0 /* end of list */
0
};
#endif
}
#endif

void audio_device::reset_device(audio_settings settings) {
#if BUILD_OPENAL && !PLATFORM_WEB
auto attrs = make_attrs(settings);

alcResetDeviceSOFT(device, attrs);
alcResetDeviceSOFT(device, &attrs[0]);
AL_CHECK_DEVICE(device);
log_hrtf_status();
#else
Expand All @@ -164,7 +192,8 @@ namespace augs {
: device(settings.output_device_name)
{
#if BUILD_OPENAL
context = alcCreateContext(device, nullptr);
auto attrs = make_attrs(settings);
context = alcCreateContext(device, &attrs[0]);

if (!context || !set_as_current()) {
if (context) {
Expand Down
5 changes: 5 additions & 0 deletions src/augs/audio/audio_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include "augs/templates/exception_templates.h"
#include "augs/audio/audio_settings.h"

#if PLATFORM_WEB
typedef struct ALCcontext_struct ALCcontext;
typedef struct ALCdevice_struct ALCdevice;
#else
/** Opaque device handle */
typedef struct ALCdevice ALCdevice;
/** Opaque context handle */
typedef struct ALCcontext ALCcontext;
#endif

namespace augs {
struct audio_error : error_with_typesafe_sprintf {
Expand Down
9 changes: 8 additions & 1 deletion src/augs/audio/sound_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#if BUILD_OPENAL
#include <AL/al.h>
#include <AL/alc.h>
#if PLATFORM_WEB
#define AL_SOURCE_SPATIALIZE_SOFT 0x1214
#else
#include <AL/alext.h>
#include <AL/efx.h>
#endif
#endif

#include "augs/math/vec2.h"
Expand Down Expand Up @@ -189,9 +192,11 @@ namespace augs {

void sound_source::set_air_absorption_factor(const float absorption) const {
(void)absorption;
#if !PLATFORM_WEB
AL_CHECK(alSourcef(id, AL_AIR_ABSORPTION_FACTOR, absorption));
#if TRACE_PARAMETERS
LOG_NVPS(absorption);
#endif
#endif
}

Expand Down Expand Up @@ -268,7 +273,9 @@ namespace augs {

void sound_source::set_direct_channels(const bool flag) const {
(void)flag;
#if !PLATFORM_WEB
AL_CHECK(alSourcei(id, AL_DIRECT_CHANNELS_SOFT, flag ? 1 : 0));
#endif

#if TRACE_PARAMETERS
LOG_NVPS(flag);
Expand Down
4 changes: 2 additions & 2 deletions src/view/frame_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ struct frame_profiler : public augs::profiler_mixin<frame_profiler> {
augs::time_measurements light_rendering;
augs::time_measurements particles_rendering;
augs::time_measurements advance_setup;
augs::time_measurements main_help;
augs::time_measurements main_wait;
augs::time_measurements help_tasks;
augs::time_measurements wait_swap;
augs::time_measurements synced_op;

augs::time_measurements camera_visibility_query;
Expand Down
4 changes: 2 additions & 2 deletions src/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4588,7 +4588,7 @@ work_result work(

thread_pool.submit();

auto scope = measure_scope(game_thread_performance.main_help);
auto scope = measure_scope(game_thread_performance.help_tasks);

thread_pool.help_until_no_tasks();
thread_pool.wait_for_all_tasks_to_complete();
Expand Down Expand Up @@ -4670,7 +4670,7 @@ work_result work(
#endif
}

auto scope = measure_scope(game_thread_performance.main_wait);
auto scope = measure_scope(game_thread_performance.wait_swap);
finalize_frame_and_swap();
}
};
Expand Down

0 comments on commit 46fff5b

Please sign in to comment.