audio occlusion
[carveJwlIkooP6JGAAIwe30JlM.git] / audio.h
1 #ifndef AUDIO_H
2 #define AUDIO_H
3
4 #include "common.h"
5 #include "world.h"
6
7 static float audio_occlusion_current = 0.0f,
8 k_audio_occlusion_rate = 1.0f;
9
10 static int k_audio_debug_soundscape = 0;
11
12 sfx_vol_control audio_vol_all = { .val = 1.0f, .name = "All" };
13
14 sfx_set audio_board =
15 {
16 .sources = "sound/skate.ogg\0"
17 "sound/wheel.ogg\0"
18 "sound/slide.ogg\0"
19 "sound/reverb.ogg\0"
20 };
21
22 sfx_system audio_player0 =
23 {
24 .vol = 0.0f,
25 .ch = 1,
26 .vol_src = &audio_vol_all,
27 .name = "Player0",
28 .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
29 };
30
31 sfx_system audio_player1 =
32 {
33 .vol = 0.0f,
34 .ch = 1,
35 .vol_src = &audio_vol_all,
36 .name = "Player1",
37 .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
38 };
39
40 sfx_system audio_player2 =
41 {
42 .vol = 0.0f,
43 .ch = 1,
44 .vol_src = &audio_vol_all,
45 .name = "Player2",
46 .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
47 };
48
49 sfx_system audio_player3 =
50 {
51 .vol = 0.0f,
52 .ch = 1,
53 .vol_src = &audio_vol_all,
54 .name = "Player3",
55 .flags = SFX_FLAG_REPEAT | SFX_FLAG_PERSISTENT
56 };
57
58 static void audio_init(void)
59 {
60 sfx_set_init( &audio_board, NULL );
61 sfx_set_play( &audio_board, &audio_player0, 0 );
62 sfx_set_play( &audio_board, &audio_player1, 1 );
63 sfx_set_play( &audio_board, &audio_player2, 2 );
64 sfx_set_play( &audio_board, &audio_player3, 3 );
65
66 vg_convar_push( (struct vg_convar){
67 .name = "aud_debug_soundscape",
68 .data = &k_audio_debug_soundscape,
69 .data_type = k_convar_dtype_i32,
70 .opt_i32 = { .min=0, .max=1, .clamp=0 },
71 .persistent = 1
72 });
73
74 vg_convar_push( (struct vg_convar){
75 .name = "aud_occlusion_rate",
76 .data = &k_audio_occlusion_rate,
77 .data_type = k_convar_dtype_f32,
78 .opt_f32 = { .clamp = 0 },
79 .persistent = 1
80 });
81 }
82
83 static void audio_free(void)
84 {
85 sfx_set_free( &audio_board );
86 }
87
88 static void audio_sample_occlusion( v3f origin )
89 {
90 float d = 0.0f,
91 sample_dist = 880.0f;
92
93 int sample_count = 8;
94
95 for( int i=0; i<sample_count; i++ )
96 {
97 v3f dir;
98 dir[0] = vg_randf();
99 dir[1] = vg_randf();
100 dir[2] = vg_randf();
101
102 v3_muls( dir, 2.0f, dir );
103 v3_sub( dir, (v3f){1.0f,1.0f,1.0f}, dir );
104
105 v3_normalize( dir );
106
107 ray_hit contact;
108 contact.dist = 15.0f;
109
110 if( ray_world( origin, dir, &contact ) )
111 {
112 d += contact.dist;
113
114 vg_line( origin, contact.pos, 0xff0000ff );
115 vg_line_pt3( contact.pos, 0.1f, 0xff0000ff );
116 }
117 else
118 {
119 v3f p1;
120 v3_muladds( origin, dir, sample_dist, p1 );
121 vg_line( origin, p1, 0xffcccccc );
122
123 d += sample_dist;
124 }
125 }
126
127 float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))),
128 rate = ktimestep * k_audio_occlusion_rate,
129 target = powf( occlusion, 6.0f );
130 audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate );
131 }
132
133 static void audio_debug_soundscapes(void)
134 {
135 if( !k_audio_debug_soundscape ) return;
136
137 char buf[64];
138 snprintf( buf, 31, "occlusion: %.5f", audio_occlusion_current );
139
140 ui_global_ctx.cursor[0] = 10;
141 ui_global_ctx.cursor[1] = 10;
142 ui_global_ctx.cursor[2] = audio_occlusion_current * 200.0f;
143 ui_global_ctx.cursor[3] = 20;
144
145 gui_fill_rect( ui_global_ctx.cursor, 0x55cccccc );
146 gui_text( ui_global_ctx.cursor, buf, 1, 0 );
147 }
148
149 #endif /* AUDIO_H */