Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion application.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "application/sound.h"
#include "application/audio_device.h"
#include "application/audio_music.h"
#include "application/audio_sfx.h"
#include "application/music_config.h"
#include "font/glfreetypefont.h"

Expand Down
54 changes: 39 additions & 15 deletions application/audio_music.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ typedef struct music_layer_t music_layer_t;
typedef struct music_theme_t music_theme_t;
typedef vec2f_t music_curve_point_t;

typedef struct {
const void *pcm_data;
u64 frame_count;
u32 channels;
u32 sample_rate;
} music_audio_info_t;

struct music_layer_t{
str_t filepath;
ma_sound sound;
ma_audio_buffer buffer;
ma_sound sound;
music_curve_point_t curve[8];
u32 curve_count;
f32 current_db;
bool loaded;
u32 curve_count;
f32 current_db;
bool loaded;
};

struct music_theme_t {
Expand All @@ -30,7 +37,7 @@ struct music_theme_t {

global music_theme_t global_music = {0};

bool music_theme_load(str_t base_path, const str_t layer_files[], u32 count,
bool music_theme_load(const music_audio_info_t audio_info[], u32 count,
const music_curve_point_t curves[][8], const u32 curve_counts[]);
void music_theme_play(void);
void music_theme_stop(f32 fade_sec);
Expand Down Expand Up @@ -77,8 +84,7 @@ INTERNAL void music__internal__apply_intensity(music_theme_t *m)
}

bool music_theme_load(
const str_t base_path,
const str_t layer_files[],
const music_audio_info_t audio_info[],
u32 count,
const music_curve_point_t curves[][8],
const u32 curve_counts[])
Expand All @@ -102,18 +108,35 @@ bool music_theme_load(
for (u32 i = 0; i < count; i++) {
music_layer_t *layer = &global_music.layers[i];

char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%.*s/%.*s", STR_ARG(base_path), STR_ARG(layer_files[i]));
layer->filepath = str(fullpath);

layer->curve_count = curve_counts[i];
memcpy(layer->curve, curves[i], sizeof(music_curve_point_t) * curve_counts[i]);
layer->current_db = 0.0f;

ma_result result = ma_sound_init_from_file(engine, fullpath,
MA_SOUND_FLAG_STREAM | MA_SOUND_FLAG_DECODE, NULL, NULL, &layer->sound);
if (!audio_info[i].pcm_data || audio_info[i].frame_count == 0) {
eprint("[audio] music: layer %u has no audio data\n", i);
layer->loaded = false;
continue;
}

ma_audio_buffer_config buf_cfg = ma_audio_buffer_config_init(
ma_format_f32,
audio_info[i].channels,
audio_info[i].frame_count,
(void *)audio_info[i].pcm_data,
NULL);

ma_result result = ma_audio_buffer_init(&buf_cfg, &layer->buffer);
if (result != MA_SUCCESS) {
eprint("[audio] music: buffer init failed layer %u: %d\n", i, result);
layer->loaded = false;
continue;
}

result = ma_sound_init_from_data_source(engine,
&layer->buffer, 0, NULL, &layer->sound);
if (result != MA_SUCCESS) {
eprint("[audio] music: failed to load `%s`: %d\n", fullpath, result);
eprint("[audio] music: sound init failed layer %u: %d\n", i, result);
ma_audio_buffer_uninit(&layer->buffer);
layer->loaded = false;
continue;
}
Expand Down Expand Up @@ -188,6 +211,7 @@ void music_theme_unload(void)
for (u32 i = 0; i < global_music.layer_count; i++) {
if (global_music.layers[i].loaded) {
ma_sound_uninit(&global_music.layers[i].sound);
ma_audio_buffer_uninit(&global_music.layers[i].buffer);
global_music.layers[i].loaded = false;
}
}
Expand Down
137 changes: 0 additions & 137 deletions application/audio_sfx.h

This file was deleted.

71 changes: 31 additions & 40 deletions application/music_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "audio_music.h"
#include "audio_sfx.h"
#include "poglib/util/assetmanager.h"

/* music_config.h — Wwise-project-derived music definitions (NO Wwise SDK)
*
Expand All @@ -10,7 +10,6 @@
* Maps 1:1 to Wwise authoring data:
* - 4 layers (bed + 3 stems) at 80 BPM
* - RTPC Parkour_intensity (0..100) → layer volume curves
* - Stinger_impacts set (Impact_KL_01..04)
*
* Edit this file when curves/assets change in Wwise authoring.
* Do NOT parse .wwu / .wproj / SoundBanks at runtime.
Expand Down Expand Up @@ -46,64 +45,56 @@ static const u32 MUSIC_LAYER_CURVE_COUNTS[] = {
2, 4, 4, 4,
};

static const str_t MUSIC_SFX_FILES[] = {
str_lit("sfx/Impact_KL_01.wav"),
str_lit("sfx/Impact_KL_02.wav"),
str_lit("sfx/Impact_KL_03.wav"),
str_lit("sfx/Impact_KL_04.wav"),
};

static const u32 MUSIC_SFX_COUNT = 4;

#ifndef IGNORE_MUSIC_CONFIG_IMPLEMENTATION

global u32 g_sfx_impact_set = (u32)-1;

bool music_config_load(void)
bool music_config_load(assetmanager_t *assets)
{
ma_engine *engine = audio_device_get_engine();
if (!engine) return false;

if (!sfx_system_init()) return false;
music_audio_info_t audio_info[8] = {0};

for (u32 i = 0; i < MUSIC_LAYER_COUNT; i++) {
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%.*s/%.*s",
STR_ARG(MUSIC_BASE_PATH), STR_ARG(MUSIC_LAYER_FILES[i]));

u32 asset_id = assetmanager_load_audio(assets, str(fullpath));
if (asset_id == INVALID_ASSET_ID) {
eprint("[audio] music_config: failed to load `%s`\n", fullpath);
return false;
}

const audio_asset_t *asset = assetmanager_get_assetresource(
assets, ASSET_TYPE_AUDIO, asset_id);
if (!asset) {
eprint("[audio] music_config: asset not ready `%s`\n", fullpath);
return false;
}

audio_info[i] = (music_audio_info_t){
.pcm_data = asset->pcm_data,
.frame_count = asset->frame_count,
.channels = asset->channels,
.sample_rate = asset->sample_rate,
};
}

/* Load music layers */
bool ok = music_theme_load(
MUSIC_BASE_PATH,
MUSIC_LAYER_FILES,
audio_info,
MUSIC_LAYER_COUNT,
MUSIC_LAYER_CURVES,
MUSIC_LAYER_CURVE_COUNTS
);
if (!ok) return false;

/* Load impact SFX */
g_sfx_impact_set = sfx_set_create(str("impacts"));
for (u32 i = 0; i < MUSIC_SFX_COUNT; i++) {
char fullpath[512];
snprintf(fullpath, sizeof(fullpath), "%.*s/%.*s",
STR_ARG(MUSIC_BASE_PATH), STR_ARG(MUSIC_SFX_FILES[i]));
u32 id = sfx_register(MUSIC_SFX_FILES[i], str(fullpath), engine);
if (id != (u32)-1) {
sfx_set_add(g_sfx_impact_set, id);
}
}

logging("[audio] music config loaded (Wwise hybrid)");
logging("[audio] music config loaded (asset manager)");
return true;
}

void music_config_play_impact(void)
{
if (g_sfx_impact_set != (u32)-1) {
sfx_play_from_set(g_sfx_impact_set);
}
}

void music_config_unload(void)
{
music_theme_unload();
sfx_system_unload();
g_sfx_impact_set = (u32)-1;
}

#endif
10 changes: 10 additions & 0 deletions util/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef enum asset_type {
ASSET_TYPE_GLSL_SHADER = 1,
ASSET_TYPE_TEXTURE = 2,
ASSET_TYPE_TEXTURE_SPRITE_ATLAS = 3,
ASSET_TYPE_AUDIO = 4,
ASSET_TYPE_COUNT
} asset_type;

Expand Down Expand Up @@ -42,12 +43,21 @@ struct gpu_asset__internal_upload_task_t {
void *processed_data;
};

typedef struct {
void *pcm_data;
u64 frame_count;
u32 format;
u32 channels;
u32 sample_rate;
} audio_asset_t;

#define INVALID_ASSET_ID 0

const bool ASSET_ASYNC_LOADING_SUPPORT[ASSET_TYPE_COUNT] = {
[ASSET_TYPE_MODEL] = true,
[ASSET_TYPE_GLSL_SHADER] = false, //Requires opengl to compile shaders
[ASSET_TYPE_TEXTURE] = false,
[ASSET_TYPE_TEXTURE_SPRITE_ATLAS] = false,
[ASSET_TYPE_AUDIO] = false,
};

Loading