surface props
[carveJwlIkooP6JGAAIwe30JlM.git] / player_audio.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef PLAYER_AUDIO_H
6 #define PLAYER_AUDIO_H
7
8 #include "player.h"
9
10 /*
11 * Audio
12 */
13 VG_STATIC void player_audio(void)
14 {
15 struct player_phys *phys = &player.phys;
16
17 static int _air = 0;
18
19 int l2 = _air;
20 _air = phys->in_air;
21
22 static double last_revert = -2000.0;
23
24
25
26
27 audio_lock();
28
29 double revert_delta = vg.time - last_revert;
30 if( phys->on_board && (!_air && l2) && (revert_delta > 0.7) &&
31 (player.air_time > 0.5f) )
32 {
33 audio_player_set_position( &audio_player_extra, phys->rb.co );
34 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
35 audio_player_set_vol( &audio_player_extra, 2.0f );
36
37 if( (fabsf(phys->slip) > 0.75f) )
38 {
39 audio_player_playclip( &audio_player_extra, &audio_lands[rand()%2+3] );
40 }
41 else
42 {
43 audio_player_playclip( &audio_player_extra, &audio_lands[rand()%3] );
44 }
45
46 last_revert = vg.time;
47 }
48
49 static float air = 0.0f;
50 air = vg_lerpf( air, phys->in_air? 1.0f: 0.0f, 5.0f*vg.time_delta );
51
52 /* Spacial info */
53 v3f ears = { 1.0f,0.0f,0.0f };
54 v3f delta;
55
56 float *cam = camera_pos,
57 *pos = phys->rb.co;
58
59 audio_player_set_position( &audio_player0, phys->rb.co );
60 audio_player_set_position( &audio_player1, phys->rb.co );
61 audio_player_set_position( &audio_player2, phys->rb.co );
62 audio_player_set_position( &audio_player_gate, world.render_gate_pos );
63 audio_player_set_vol( &audio_player_gate, 5.0f );
64
65 v3_sub( phys->rb.co, camera_pos, delta );
66 v3_normalize( delta );
67 m3x3_mulv( camera_mtx, ears, ears );
68
69 /* TODO, Make function */
70 v3_copy( ears, vg_audio.listener_ears );
71 v3_copy( camera_pos, vg_audio.listener_pos );
72
73 /* Tunnel / occlusion */
74 audio_sample_occlusion( camera_pos );
75
76 int sprite_avail = -1;
77 for( int i=0; i<vg_list_size(ambient_sprites); i++ )
78 {
79 if( !audio_player_is_playing( &ambient_sprites[i] ) )
80 {
81 sprite_avail = i;
82 break;
83 }
84 }
85
86 if( sprite_avail != -1 )
87 {
88 v3f waterpos;
89 enum audio_sprite_type sprite_type =
90 audio_sample_sprite_random( phys->rb.co, waterpos );
91
92 if( sprite_type != k_audio_sprite_type_none )
93 {
94 audio_player *avail = &ambient_sprites[ sprite_avail ];
95
96 audio_player_set_vol( avail, 20.0f );
97 audio_player_set_flags( avail, AUDIO_FLAG_SPACIAL_3D );
98 audio_player_set_position( avail, waterpos );
99
100 if( sprite_type == k_audio_sprite_type_grass )
101 {
102 audio_player_playclip( avail, &audio_grass[rand()%4] );
103 }
104 else if( sprite_type == k_audio_sprite_type_water )
105 {
106 if( world.water.enabled )
107 audio_player_playclip( avail, &audio_water[rand()%6] );
108 }
109 }
110 }
111
112 if( freecam || player.is_dead || !phys->on_board )
113 {
114 audio_player_set_vol( &audio_player0, 0.0f );
115 audio_player_set_vol( &audio_player1, 0.0f );
116 audio_player_set_vol( &audio_player2, 0.0f );
117 audio_player_set_vol( &audio_player3, 0.0f );
118
119 int walk_phase = 0;
120 if( vg_fractf(player.walk_timer) > 0.5f )
121 walk_phase = 1;
122 else
123 walk_phase = 0;
124
125 if( (player.step_phase != walk_phase) && !phys->in_air )
126 {
127 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
128 audio_player_set_position( &audio_player_extra, phys->rb.co );
129 audio_player_set_vol( &audio_player_extra, 6.0f );
130
131 if( player.surface_prop == k_surface_prop_concrete )
132 {
133 audio_player_playclip(
134 &audio_player_extra,
135 &audio_footsteps[rand()%vg_list_size(audio_footsteps)]
136 );
137 }
138 else if( player.surface_prop == k_surface_prop_grass )
139 {
140 audio_player_playclip(
141 &audio_player_extra,
142 &audio_footsteps_grass[rand()%vg_list_size(audio_footsteps_grass)]
143 );
144 }
145 else if( player.surface_prop == k_surface_prop_wood )
146 {
147 audio_player_playclip(
148 &audio_player_extra,
149 &audio_footsteps_wood[rand()%vg_list_size(audio_footsteps_wood)]
150 );
151 }
152 }
153
154 player.step_phase = walk_phase;
155 }
156 else
157 {
158 /* Composite */
159 float speed = vg_minf(v3_length( phys->rb.v )*0.1f,1.0f),
160 attn = speed,
161 slide = vg_clampf( fabsf(phys->slip), 0.0f, 1.0f ),
162 vol0 = (1.0f-air)*attn*(1.0f-slide),
163 vol1 = air *attn,
164 vol2 = (1.0f-air)*attn*slide;
165
166 audio_player_set_vol( &audio_player0, vol0*vg.time_rate );
167 audio_player_set_vol( &audio_player1, vol1*vg.time_rate );
168 audio_player_set_vol( &audio_player2, vol2*vg.time_rate );
169
170 float reverb_amt = vol0 * audio_occlusion_current * 0.5f;
171 audio_player_set_pan( &audio_player3, 0.0f );
172 audio_player_set_vol( &audio_player3, reverb_amt*vg.time_rate );
173 }
174
175 #if 0
176 world_audio_update( cam, ears );
177 #endif
178 audio_unlock();
179
180 if( player.phys.in_air )
181 player.air_time += vg.time_delta;
182 else
183 player.air_time = 0.0f;
184
185 }
186
187 #endif /* PLAYER_AUDIO_H */