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