misc. remove dead code, assert macro
[vg.git] / vg_audio.h
1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
2
3 #pragma once
4
5 #include "vg_platform.h"
6 #include "vg_engine.h"
7 #include "vg_string.h"
8 #include "vg_vorbis.h"
9
10 #define AUDIO_FRAME_SIZE 512
11 #define AUDIO_MIX_FRAME_SIZE 256
12
13 #define AUDIO_CHANNELS 32
14 #define AUDIO_LFOS 8
15 #define AUDIO_FILTERS 16
16 #define AUDIO_FLAG_LOOP 0x1
17 #define AUDIO_FLAG_NO_DOPPLER 0x2
18 #define AUDIO_FLAG_SPACIAL_3D 0x4
19 #define AUDIO_FLAG_AUTO_START 0x8
20 #define AUDIO_FLAG_FORMAT 0x1E00
21
22 enum audio_format
23 {
24 k_audio_format_mono = 0x000u,
25 k_audio_format_stereo = 0x200u,
26 k_audio_format_vorbis = 0x400u,
27 k_audio_format_none0 = 0x600u,
28 k_audio_format_none1 = 0x800u,
29 k_audio_format_none2 = 0xA00u,
30 k_audio_format_none3 = 0xC00u,
31 k_audio_format_none4 = 0xE00u,
32
33 k_audio_format_bird = 0x1000u,
34 k_audio_format_gen = 0x1200u,
35 k_audio_format_none6 = 0x1400u,
36 k_audio_format_none7 = 0x1600u,
37 k_audio_format_none8 = 0x1800u,
38 k_audio_format_none9 = 0x1A00u,
39 k_audio_format_none10 = 0x1C00u,
40 k_audio_format_none11 = 0x1E00u,
41 };
42
43 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
44 #define AUDIO_MUTE_VOLUME 0.0f
45 #define AUDIO_BASE_VOLUME 1.0f
46
47 typedef struct audio_clip audio_clip;
48 typedef struct audio_channel audio_channel;
49 typedef struct audio_lfo audio_lfo;
50
51 struct audio_clip
52 {
53 union { /* TODO oof.. */
54 u64 _p64_;
55 const char *path;
56 void *func;
57 };
58
59 u32 flags;
60 u32 size;
61
62 union{
63 u64 _p64;
64 void *data;
65 };
66 };
67
68 struct vg_audio_system
69 {
70 SDL_AudioDeviceID sdl_output_device;
71 vg_str device_choice; /* buffer is null? use default from OS */
72
73 bool always_keep_compressed;
74
75 void *audio_pool,
76 *decode_buffer;
77 u32 samples_last;
78
79 /* synchro */
80 int sync_locked;
81
82 SDL_SpinLock sl_checker,
83 sl_sync;
84
85 struct audio_lfo{
86 u32 time, time_startframe;
87 float sqrt_polynomial_coefficient;
88
89 struct{
90 enum lfo_wave_type{
91 k_lfo_triangle,
92 k_lfo_square,
93 k_lfo_saw,
94 k_lfo_polynomial_bipolar
95 }
96 wave_type;
97
98 u32 period;
99 float polynomial_coefficient;
100 }
101 _, editable_state;
102 u32 editble_state_write_mask;
103 }
104 oscillators[ AUDIO_LFOS ];
105
106 struct audio_channel{
107 int allocated;
108 u16 group;
109 u8 world_id;
110
111 char name[32]; /* only editable while allocated == 0 */
112 audio_clip *source; /* ... */
113 u32 flags; /* ... */
114 u32 colour; /* ... */
115
116 /* internal non-readable state
117 * -----------------------------*/
118 u32 cursor, source_length;
119
120 float volume_movement_start,
121 pan_movement_start;
122
123 u32 volume_movement,
124 pan_movement;
125
126 union{
127 struct synth_bird *bird;
128 stb_vorbis *vorbis;
129 }
130 handle;
131
132 stb_vorbis_alloc vorbis_alloc;
133
134 enum channel_activity{
135 k_channel_activity_reset, /* will advance if allocated==1, to wake */
136 k_channel_activity_wake, /* will advance to either of next two */
137 k_channel_activity_alive,
138 k_channel_activity_end,
139 k_channel_activity_error
140 }
141 activity,
142 readable_activity;
143
144 /*
145 * editable structure, can be modified inside _lock and _unlock
146 * the edit mask tells which to copy into internal _, or to discard
147 * ----------------------------------------------------------------------
148 */
149 struct channel_state{
150 int relinquished;
151
152 float volume, /* current volume */
153 volume_target, /* target volume */
154 pan,
155 pan_target,
156 sampling_rate;
157
158 u32 volume_rate,
159 pan_rate;
160
161 v4f spacial_falloff; /* xyz, range */
162
163 audio_lfo *lfo;
164 float lfo_amount;
165 }
166 _, editable_state;
167 u32 editble_state_write_mask;
168 }
169 channels[ AUDIO_CHANNELS ];
170
171 int debug_ui, debug_ui_3d, debug_dsp, dsp_enabled;
172
173 v3f internal_listener_pos,
174 internal_listener_ears,
175 internal_listener_velocity,
176
177 external_listener_pos,
178 external_listener_ears,
179 external_lister_velocity;
180
181 float internal_global_volume,
182 external_global_volume;
183 }
184 extern vg_audio;
185
186 void audio_clip_load( audio_clip *clip, void *lin_alloc );
187 void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc );
188
189 void vg_audio_register(void);
190 void vg_audio_device_init(void);
191 void vg_audio_init(void);
192 void vg_audio_free(void);
193
194 void audio_lock(void);
195 void audio_unlock(void);
196
197 void audio_channel_init( audio_channel *ch, audio_clip *clip, u32 flags );
198 void audio_channel_group( audio_channel *ch, u16 group );
199 void audio_channel_world( audio_channel *ch, u8 world_id );
200 audio_channel *audio_get_first_idle_channel(void);
201 audio_channel *audio_get_group_idle_channel( u16 group, u32 max_count );
202 audio_channel *audio_get_group_first_active_channel( u16 group );
203 int audio_channel_finished( audio_channel *ch );
204 audio_channel *audio_relinquish_channel( audio_channel *ch );
205 void audio_channel_slope_volume( audio_channel *ch, f32 length, f32 new_vol );
206 void audio_channel_set_sampling_rate( audio_channel *ch, float rate );
207 void audio_channel_edit_volume( audio_channel *ch, f32 new_vol, int instant );
208 audio_channel *audio_channel_fadeout( audio_channel *ch, float length );
209 void audio_channel_fadein( audio_channel *ch, float length );
210 audio_channel *audio_channel_crossfade( audio_channel *ch,
211 audio_clip *new_clip,
212 float length, u32 flags );
213 void audio_channel_sidechain_lfo( audio_channel *ch, int lfo_id, f32 amount );
214 void audio_channel_set_spacial( audio_channel *ch, v3f co, float range );
215 int audio_oneshot_3d( audio_clip *clip, v3f position, f32 range, f32 volume );
216 int audio_oneshot( audio_clip *clip, f32 volume, f32 pan );
217 void audio_set_lfo_wave( int id, enum lfo_wave_type type, f32 coefficient );
218 void audio_set_lfo_frequency( int id, float freq );
219 int audio_channel_load_source( audio_channel *ch );
220
221 void audio_debug_ui(
222
223 #ifdef VG_3D
224 m4x4f
225 #else
226 m3x3f
227 #endif
228 mtx_pv );