X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=audio.h;h=448906be07e9af05c101b3c07766578dc7612f4d;hb=refs%2Fheads%2Frigidbody;hp=0572d6dcf52b623fc7a3f313c370e0d70e0f4d7c;hpb=d4746875c05dd3e077e1b266e50ffe4856b45502;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/audio.h b/audio.h index 0572d6d..448906b 100644 --- a/audio.h +++ b/audio.h @@ -1,149 +1,289 @@ -#ifndef AUDIO_H -#define AUDIO_H +/* + * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved + */ #include "common.h" -#include "world.h" -static float audio_occlusion_current = 0.0f, - k_audio_occlusion_rate = 1.0f; - -static int k_audio_debug_soundscape = 0; +#ifndef AUDIO_H +#define AUDIO_H -sfx_vol_control audio_vol_all = { .val = 1.0f, .name = "All" }; +#include "world.h" -sfx_set audio_board = +audio_clip audio_board[] = { - .sources = "sound/skate.ogg\0" - "sound/wheel.ogg\0" - "sound/slide.ogg\0" - "sound/reverb.ogg\0" + { .path="sound/skate_hpf.ogg" }, + { .path="sound/wheel.ogg" }, + { .path="sound/slide.ogg" }, + { .path="sound/grind_enter.ogg" }, + { .path="sound/grind_exit.ogg" }, + { .path="sound/grind_loop.ogg" }, + { .path="sound/woodslide.ogg" }, + { .path="sound/metalscrape.ogg" }, + { .path="sound/slidetap.ogg" } }; -sfx_system audio_player0 = +audio_clip audio_taps[] = { - .vol = 0.0f, - .ch = 1, - .vol_src = &audio_vol_all, - .name = "Player0", - .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT + { .path="sound/tap0.ogg" }, + { .path="sound/tap1.ogg" }, + { .path="sound/tap2.ogg" }, + { .path="sound/tap3.ogg" } }; -sfx_system audio_player1 = +audio_clip audio_flips[] = { - .vol = 0.0f, - .ch = 1, - .vol_src = &audio_vol_all, - .name = "Player1", - .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT + { .path="sound/lf0.ogg" }, + { .path="sound/lf1.ogg" }, + { .path="sound/lf2.ogg" }, + { .path="sound/lf3.ogg" }, }; -sfx_system audio_player2 = +audio_clip audio_hits[] = { - .vol = 0.0f, - .ch = 1, - .vol_src = &audio_vol_all, - .name = "Player2", - .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT + { .path="sound/hit0.ogg" }, + { .path="sound/hit1.ogg" }, + { .path="sound/hit2.ogg" }, + { .path="sound/hit3.ogg" }, + { .path="sound/hit4.ogg" } }; -sfx_system audio_player3 = -{ - .vol = 0.0f, - .ch = 1, - .vol_src = &audio_vol_all, - .name = "Player3", - .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT +audio_clip audio_splash = +{ .path = "sound/splash.ogg" }; + +audio_clip audio_jumps[] = { + { .path = "sound/jump0.ogg" }, + { .path = "sound/jump1.ogg" }, }; -static void audio_init(void) -{ - sfx_set_init( &audio_board, NULL ); - sfx_set_play( &audio_board, &audio_player0, 0 ); - sfx_set_play( &audio_board, &audio_player1, 1 ); - sfx_set_play( &audio_board, &audio_player2, 2 ); - sfx_set_play( &audio_board, &audio_player3, 3 ); - - vg_convar_push( (struct vg_convar){ - .name = "aud_debug_soundscape", - .data = &k_audio_debug_soundscape, - .data_type = k_convar_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=0 }, - .persistent = 1 - }); - - vg_convar_push( (struct vg_convar){ - .name = "aud_occlusion_rate", - .data = &k_audio_occlusion_rate, - .data_type = k_convar_dtype_f32, - .opt_f32 = { .clamp = 0 }, - .persistent = 1 - }); -} +audio_clip audio_footsteps[] = { + {.path = "sound/step_concrete0.ogg" }, + {.path = "sound/step_concrete1.ogg" }, + {.path = "sound/step_concrete2.ogg" }, + {.path = "sound/step_concrete3.ogg" } +}; -static void audio_free(void) -{ - sfx_set_free( &audio_board ); -} +audio_clip audio_footsteps_grass[] = { + {.path = "sound/step_bush0.ogg" }, + {.path = "sound/step_bush1.ogg" }, + {.path = "sound/step_bush2.ogg" }, + {.path = "sound/step_bush3.ogg" }, + {.path = "sound/step_bush4.ogg" }, + {.path = "sound/step_bush5.ogg" } +}; -static void audio_sample_occlusion( v3f origin ) +audio_clip audio_footsteps_wood[] = { + {.path = "sound/step_wood0.ogg" }, + {.path = "sound/step_wood1.ogg" }, + {.path = "sound/step_wood2.ogg" }, + {.path = "sound/step_wood3.ogg" }, + {.path = "sound/step_wood4.ogg" }, + {.path = "sound/step_wood5.ogg" } +}; + +audio_clip audio_lands[] = { + { .path = "sound/land0.ogg" }, + { .path = "sound/land1.ogg" }, + { .path = "sound/land2.ogg" }, + { .path = "sound/landsk0.ogg" }, + { .path = "sound/landsk1.ogg" }, + { .path = "sound/onto.ogg" }, + { .path = "sound/outo.ogg" }, +}; + +audio_clip audio_water[] = { + { .path = "sound/wave0.ogg" }, + { .path = "sound/wave1.ogg" }, + { .path = "sound/wave2.ogg" }, + { .path = "sound/wave3.ogg" }, + { .path = "sound/wave4.ogg" }, + { .path = "sound/wave5.ogg" } +}; + +audio_clip audio_grass[] = { + { .path = "sound/grass0.ogg" }, + { .path = "sound/grass1.ogg" }, + { .path = "sound/grass2.ogg" }, + { .path = "sound/grass3.ogg" }, +}; + +audio_clip audio_ambience[] = { - float d = 0.0f, - sample_dist = 880.0f; + { .path="sound/town_generic.ogg" } +}; - int sample_count = 8; +audio_clip audio_gate_pass = { + .path = "sound/gate_pass.ogg" +}; - for( int i=0; isl ); + f32 spd = data->speed; + SDL_AtomicUnlock( &data->sl ); + + f32 s0 = sinf(data->t*2.0f), + s1 = sinf(data->t*0.43f), + s2 = sinf(data->t*1.333f), + sm = vg_clampf( data->speed / 45.0f, 0, 1 ), + ft = (s0*s1*s2)*0.5f+0.5f, + f = vg_lerpf( 200.0f, 1200.0f, sm*0.7f + ft*0.3f ), + vol = 0.25f * sm; + + dsp_init_biquad_butterworth_lpf( &data->lpf, f ); + + for( u32 i=0; ilpf, v ); + + buf[i*2+0] = v; + buf[i*2+1] = v; } - float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))), - rate = ktimestep * k_audio_occlusion_rate, - target = powf( occlusion, 6.0f ); - audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate ); + data->t += (f32)(count)/44100.0f; +}; + +static audio_clip air_synth = { + .flags = k_audio_format_gen, + .size = 0, + .func = audio_air_synth_get_samples, + .data = &air_data +}; + +static void audio_init(void) +{ + audio_clip_loadn( audio_board, vg_list_size(audio_board), NULL ); + audio_clip_loadn( audio_taps, vg_list_size(audio_taps), NULL ); + audio_clip_loadn( audio_flips, vg_list_size(audio_flips), NULL ); + audio_clip_loadn( audio_hits, vg_list_size(audio_hits), NULL ); + audio_clip_loadn( audio_ambience, vg_list_size(audio_ambience), NULL ); + audio_clip_loadn( &audio_splash, 1, NULL ); + audio_clip_loadn( &audio_gate_pass, 1, NULL ); + audio_clip_loadn( &audio_gate_lap, 1, NULL ); + audio_clip_loadn( &audio_gate_ambient, 1, NULL ); + + audio_clip_loadn( audio_jumps, vg_list_size(audio_jumps), NULL ); + audio_clip_loadn( audio_lands, vg_list_size(audio_lands), NULL ); + audio_clip_loadn( audio_water, vg_list_size(audio_water), NULL ); + audio_clip_loadn( audio_grass, vg_list_size(audio_grass), NULL ); + audio_clip_loadn( audio_footsteps, vg_list_size(audio_footsteps), NULL ); + audio_clip_loadn( audio_footsteps_grass, + vg_list_size(audio_footsteps_grass), NULL ); + audio_clip_loadn( audio_footsteps_wood, + vg_list_size(audio_footsteps_wood), NULL ); + audio_clip_loadn( audio_rewind, vg_list_size(audio_rewind), NULL ); + audio_clip_loadn( audio_ui, vg_list_size(audio_ui), NULL ); + audio_clip_loadn( audio_challenge, vg_list_size(audio_challenge), NULL ); + + audio_lock(); + audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar, 80.0f ); + audio_set_lfo_frequency( 0, 20.0f ); + + skaterift.aud_air = audio_get_first_idle_channel(); + if( skaterift.aud_air ) + audio_channel_init( skaterift.aud_air, &air_synth, 0 ); + + audio_unlock(); +} + +static void audio_free(void) +{ + /* TODO! */ + vg_warn( "UNIMPLEMENTED: audio_free()\n" ); } -static void audio_debug_soundscapes(void) +enum audio_sprite_type +{ + k_audio_sprite_type_none, + k_audio_sprite_type_grass, + k_audio_sprite_type_water +}; + +static void audio_ambient_sprite_play( v3f co, audio_clip *clip ) { - if( !k_audio_debug_soundscape ) return; + audio_lock(); + u16 group_id = 0xfff0; + audio_channel *ch = audio_get_group_idle_channel( group_id, 4 ); + + if( ch ){ + audio_channel_init( ch, clip, AUDIO_FLAG_SPACIAL_3D ); + audio_channel_group( ch, group_id ); + audio_channel_set_spacial( ch, co, 80.0f ); + audio_channel_edit_volume( ch, 1.0f, 1 ); + ch = audio_relinquish_channel( ch ); + } + audio_unlock(); +} - char buf[64]; - snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current ); +static +enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output); +static void audio_ambient_sprites_update( world_instance *world, v3f co ) +{ + static float accum = 0.0f; + accum += vg.time_delta; - ui_global_ctx.cursor[0] = 10; - ui_global_ctx.cursor[1] = 10; - ui_global_ctx.cursor[2] = audio_occlusion_current * 200.0f; - ui_global_ctx.cursor[3] = 20; + if( accum > 0.1f ) + accum -= 0.1f; + else return; - gui_fill_rect( ui_global_ctx.cursor, 0x55cccccc ); - gui_text( ui_global_ctx.cursor, buf, 1, 0 ); + v3f sprite_pos; + enum audio_sprite_type sprite_type = + world_audio_sample_sprite_random( co, sprite_pos ); + + if( sprite_type != k_audio_sprite_type_none ){ + if( sprite_type == k_audio_sprite_type_grass ){ + audio_ambient_sprite_play( sprite_pos, + &audio_grass[vg_randu32(&vg.rand)%4] ); + } + else if( sprite_type == k_audio_sprite_type_water ){ + if( world->water.enabled ){ + audio_ambient_sprite_play( sprite_pos, + &audio_water[vg_randu32(&vg.rand)%6] ); + } + } + } } #endif /* AUDIO_H */