1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
5 #include "vg_platform.h"
10 #define AUDIO_FRAME_SIZE 512
11 #define AUDIO_MIX_FRAME_SIZE 256
13 #define AUDIO_CHANNELS 32
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_NO_DSP 0x10
21 #define AUDIO_FLAG_FORMAT 0x1E00
25 k_audio_format_mono
= 0x000u
,
26 k_audio_format_stereo
= 0x200u
,
27 k_audio_format_vorbis
= 0x400u
,
28 k_audio_format_none0
= 0x600u
,
29 k_audio_format_none1
= 0x800u
,
30 k_audio_format_none2
= 0xA00u
,
31 k_audio_format_none3
= 0xC00u
,
32 k_audio_format_none4
= 0xE00u
,
34 k_audio_format_bird
= 0x1000u
,
35 k_audio_format_gen
= 0x1200u
,
36 k_audio_format_none6
= 0x1400u
,
37 k_audio_format_none7
= 0x1600u
,
38 k_audio_format_none8
= 0x1800u
,
39 k_audio_format_none9
= 0x1A00u
,
40 k_audio_format_none10
= 0x1C00u
,
41 k_audio_format_none11
= 0x1E00u
,
44 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
45 #define AUDIO_MUTE_VOLUME 0.0f
46 #define AUDIO_BASE_VOLUME 1.0f
48 typedef struct audio_clip audio_clip
;
49 typedef struct audio_channel audio_channel
;
50 typedef struct audio_lfo audio_lfo
;
54 union { /* TODO oof.. */
69 struct vg_audio_system
71 SDL_AudioDeviceID sdl_output_device
;
72 vg_str device_choice
; /* buffer is null? use default from OS */
74 bool always_keep_compressed
;
83 SDL_SpinLock sl_checker
,
87 u32 time
, time_startframe
;
88 float sqrt_polynomial_coefficient
;
95 k_lfo_polynomial_bipolar
100 float polynomial_coefficient
;
103 u32 editble_state_write_mask
;
105 oscillators
[ AUDIO_LFOS
];
107 struct audio_channel
{
112 char name
[32]; /* only editable while allocated == 0 */
113 audio_clip
*source
; /* ... */
115 u32 colour
; /* ... */
117 /* internal non-readable state
118 * -----------------------------*/
119 u32 cursor
, source_length
;
121 float volume_movement_start
,
128 struct synth_bird
*bird
;
133 stb_vorbis_alloc vorbis_alloc
;
135 enum channel_activity
{
136 k_channel_activity_reset
, /* will advance if allocated==1, to wake */
137 k_channel_activity_wake
, /* will advance to either of next two */
138 k_channel_activity_alive
,
139 k_channel_activity_end
,
140 k_channel_activity_error
146 * editable structure, can be modified inside _lock and _unlock
147 * the edit mask tells which to copy into internal _, or to discard
148 * ----------------------------------------------------------------------
150 struct channel_state
{
153 float volume
, /* current volume */
154 volume_target
, /* target volume */
162 v4f spacial_falloff
; /* xyz, range */
168 u32 editble_state_write_mask
;
170 channels
[ AUDIO_CHANNELS
];
172 int debug_ui
, debug_ui_3d
, debug_dsp
, dsp_enabled
;
174 v3f internal_listener_pos
,
175 internal_listener_ears
,
176 internal_listener_velocity
,
178 external_listener_pos
,
179 external_listener_ears
,
180 external_lister_velocity
;
182 float internal_global_volume
,
183 external_global_volume
;
187 void audio_clip_load( audio_clip
*clip
, void *lin_alloc
);
188 void audio_clip_loadn( audio_clip
*arr
, int count
, void *lin_alloc
);
190 void vg_audio_register(void);
191 void vg_audio_device_init(void);
192 void vg_audio_init(void);
193 void vg_audio_free(void);
195 void audio_lock(void);
196 void audio_unlock(void);
198 void audio_channel_init( audio_channel
*ch
, audio_clip
*clip
, u32 flags
);
199 void audio_channel_group( audio_channel
*ch
, u16 group
);
200 void audio_channel_world( audio_channel
*ch
, u8 world_id
);
201 audio_channel
*audio_get_first_idle_channel(void);
202 audio_channel
*audio_get_group_idle_channel( u16 group
, u32 max_count
);
203 audio_channel
*audio_get_group_first_active_channel( u16 group
);
204 int audio_channel_finished( audio_channel
*ch
);
205 audio_channel
*audio_relinquish_channel( audio_channel
*ch
);
206 void audio_channel_slope_volume( audio_channel
*ch
, f32 length
, f32 new_vol
);
207 void audio_channel_set_sampling_rate( audio_channel
*ch
, float rate
);
208 void audio_channel_edit_volume( audio_channel
*ch
, f32 new_vol
, int instant
);
209 audio_channel
*audio_channel_fadeout( audio_channel
*ch
, float length
);
210 void audio_channel_fadein( audio_channel
*ch
, float length
);
211 audio_channel
*audio_channel_crossfade( audio_channel
*ch
,
212 audio_clip
*new_clip
,
213 float length
, u32 flags
);
214 void audio_channel_sidechain_lfo( audio_channel
*ch
, int lfo_id
, f32 amount
);
215 void audio_channel_set_spacial( audio_channel
*ch
, v3f co
, float range
);
216 int audio_oneshot_3d( audio_clip
*clip
, v3f position
, f32 range
, f32 volume
);
217 int audio_oneshot( audio_clip
*clip
, f32 volume
, f32 pan
);
218 void audio_set_lfo_wave( int id
, enum lfo_wave_type type
, f32 coefficient
);
219 void audio_set_lfo_frequency( int id
, float freq
);
220 int audio_channel_load_source( audio_channel
*ch
);