- SDL_mutex *mux_checker,
- *mux_sync;
-
- /* Audio engine, thread 1 */
- struct active_audio_player
- {
- int active;
- union
- {
- audio_entity ent;
- aatree_pool_node pool_node;
- };
-
- stb_vorbis *vorbis_handle;
- stb_vorbis_alloc vorbis_alloc;
- }
- active_players[ SFX_MAX_SYSTEMS ];
-
- aatree active_pool_info; /* note: just using the pool */
- aatree_ptr active_pool_head;
-
- /* System queue, and access from thread 0 */
- audio_entity entity_queue[SFX_MAX_SYSTEMS];
- int queue_len;
- int debug_ui, debug_ui_3d;
-
- v3f listener_pos,
- listener_ears;
-
- float volume,
- volume_target,
- volume_target_internal,
- volume_console;
-}
-vg_audio = { .volume_console = 1.0f };
-
-static struct vg_profile
- _vg_prof_audio_decode = {.mode = k_profile_mode_accum,
- .name = "[T2] audio_decode()"},
- _vg_prof_audio_mix = {.mode = k_profile_mode_accum,
- .name = "[T2] audio_mix()"},
- vg_prof_audio_decode,
- vg_prof_audio_mix;
-
-/*
- * These functions are called from the main thread and used to prevent bad
- * access. TODO: They should be no-ops in release builds.
- */
-VG_STATIC int audio_lock_checker_load(void)
-{
- int value;
- SDL_LockMutex( vg_audio.mux_checker );
- value = vg_audio.sync_locked;
- SDL_UnlockMutex( vg_audio.mux_checker );
- return value;
-}
-
-VG_STATIC void audio_lock_checker_store( int value )
-{
- SDL_LockMutex( vg_audio.mux_checker );
- vg_audio.sync_locked = value;
- SDL_UnlockMutex( vg_audio.mux_checker );
-}
-
-VG_STATIC void audio_require_lock(void)
-{
- if( audio_lock_checker_load() )
- return;
-
- vg_error( "Modifying sound effects systems requires locking\n" );
- abort();
-}
-
-VG_STATIC void audio_lock(void)
-{
- SDL_LockMutex( vg_audio.mux_sync );
- audio_lock_checker_store(1);
-}
-
-VG_STATIC void audio_unlock(void)
-{
- audio_lock_checker_store(0);
- SDL_UnlockMutex( vg_audio.mux_sync );
-}
-
-VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int frame_count );
-VG_STATIC void vg_audio_init(void)
-{
- vg_audio.mux_checker = SDL_CreateMutex();
- vg_audio.mux_sync = SDL_CreateMutex();
-
- /* TODO: Move here? */
- vg_convar_push( (struct vg_convar){
- .name = "debug_audio",
- .data = &vg_audio.debug_ui,
- .data_type = k_convar_dtype_i32,
- .opt_i32 = { .min=0, .max=1, .clamp=1 },
- .persistent = 1
- });
-
- vg_convar_push( (struct vg_convar){
- .name = "volume",
- .data = &vg_audio.volume_console,
- .data_type = k_convar_dtype_f32,
- .opt_f32 = { .min=0.0f, .max=2.0f, .clamp=1 },
- .persistent = 1
- });
-
- /* allocate memory */
-
- /* 32mb fixed */
- vg_audio.audio_pool =
- vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32,
- VG_MEMORY_SYSTEM );
-
- /* fixed */
- u32 decode_size = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS;
- vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size );
-
- /* setup pool */
- vg_audio.active_pool_info.base = vg_audio.active_players;
- vg_audio.active_pool_info.offset = offsetof(struct active_audio_player,
- pool_node );
- vg_audio.active_pool_info.stride = sizeof(struct active_audio_player);
- vg_audio.active_pool_info.p_cmp = NULL;
- aatree_init_pool( &vg_audio.active_pool_info, SFX_MAX_SYSTEMS );
-
- SDL_AudioSpec spec_desired, spec_got;
- spec_desired.callback = audio_mixer_callback;
- spec_desired.channels = 2;
- spec_desired.format = AUDIO_F32;
- spec_desired.freq = 44100;
- spec_desired.padding = 0;
- spec_desired.samples = 512;
- spec_desired.silence = 0;
- spec_desired.size = 0;
- spec_desired.userdata = NULL;
-
- vg_audio.sdl_output_device =
- SDL_OpenAudioDevice( NULL, 0, &spec_desired, &spec_got,
- SDL_AUDIO_ALLOW_SAMPLES_CHANGE );
-
- if( vg_audio.sdl_output_device )
- {
- SDL_PauseAudioDevice( vg_audio.sdl_output_device, 0 );
- }
- else
- {
- vg_fatal_exit_loop(
- "SDL_OpenAudioDevice failed. Your default audio device must support:\n"
- " Frequency: 44100 hz\n"
- " Buffer size: 512\n"
- " Channels: 2\n"
- " Format: s16 or f32\n" );
- }
-
- vg_success( "Ready\n" );
-}
-
-VG_STATIC void vg_audio_free(void)
-{
- SDL_CloseAudioDevice( vg_audio.sdl_output_device );
-}
-
-/*
- * thread 1
- */
-
-static aatree_ptr audio_alloc_entity_internal(void)
-{
- aatree_ptr playerid = aatree_pool_alloc( &vg_audio.active_pool_info,
- &vg_audio.active_pool_head );
-
- if( playerid == AATREE_PTR_NIL )
- return AATREE_PTR_NIL;
-
- struct active_audio_player *aap = &vg_audio.active_players[ playerid ];
- aap->active = 1;
-
- return playerid;
-}
-
-VG_STATIC void audio_entity_free_internal( aatree_ptr id )
-{
- struct active_audio_player *aap = &vg_audio.active_players[ id ];
- aap->active = 0;
-
- /* Notify player that we've finished */
- if( aap->ent.player )
- aap->ent.player->active_entity = AATREE_PTR_NIL;
-
- /* delete */
- aatree_pool_free( &vg_audio.active_pool_info, id,
- &vg_audio.active_pool_head );
-}
-
-VG_STATIC void *audio_entity_vorbis_ptr( aatree_ptr entid )
-{
- u8 *buf = (u8*)vg_audio.decode_buffer,
- *loc = &buf[AUDIO_DECODE_SIZE*entid];
-
- return (void *)loc;
-}
-
-VG_STATIC void audio_entity_start( audio_entity *src )
-{
- aatree_ptr entid = audio_alloc_entity_internal();
- if( entid == AATREE_PTR_NIL )
- return;
-
- audio_entity *ent = &vg_audio.active_players[ entid ].ent;
-
- ent->info = src->info;
- ent->name = src->info.source->path;
- ent->cur = 0;
- ent->player = src->player;
-
- ent->fadeout = 0;
- ent->fadeout_current = 0;
-
- /* Notify main player we are dequeud and playing */
- if( src->player )
- {
- src->player->enqued = 0;
- src->player->active_entity = entid;
- }
-
- if( src->info.source->source_mode == k_audio_source_compressed )
- {
- /* Setup vorbis decoder */
- struct active_audio_player *aap = &vg_audio.active_players[ entid ];
-
- stb_vorbis_alloc alloc = {
- .alloc_buffer = (char *)audio_entity_vorbis_ptr( entid ),
- .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
- };
-
- int err;
- stb_vorbis *decoder = stb_vorbis_open_memory(
- src->info.source->data,
- src->info.source->size, &err, &alloc );
-
- if( !decoder )
- {
- vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
- src->info.source->path, err );
-
- audio_entity_free_internal( entid );
- return;
- }
- else
- {
- ent->length = stb_vorbis_stream_length_in_samples( decoder );
- }
-
- aap->vorbis_handle = decoder;
- }
- else
- {
- ent->length = src->info.source->size;
- }
-}
-
-/*
- * Read everything from the queue
- */
-VG_STATIC void audio_system_enque(void)
-{
- /* Process incoming sound queue */
- audio_lock();
-
- vg_audio.volume_target_internal = vg_audio.volume_target;
-
- int wr = 0;
- for( int i=0; i<vg_audio.queue_len; i++ )
- {
- audio_entity *src = &vg_audio.entity_queue[ i ];
-
- if( src->player )
- {
- /* Start new */
- if( src->player->active_entity == AATREE_PTR_NIL )
- {
- audio_entity_start( src );
- }
- else
- {
- /* Otherwise try start fadeout but dont remove from queue */
-
- aatree_ptr entid = src->player->active_entity;
- audio_entity *ent = &vg_audio.active_players[ entid ].ent;
- if( !ent->fadeout )
- {
- ent->fadeout = FADEOUT_LENGTH;
- ent->fadeout_current = FADEOUT_LENGTH;
- }
-
- vg_audio.entity_queue[ wr ++ ] = *src;
- }
- }
- else
- {
- audio_entity_start( src );
- }
- }
-
- vg_audio.queue_len = wr;
-
- /* Localize others memory */
- for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
- {
- struct active_audio_player *aap = &vg_audio.active_players[i];
- if( !aap->active )
- continue;
-
- if( aap->ent.player )
- {
- /* Only copy information in whilst not requeing */
- if( aap->ent.player->enqued == 0 )
- {
- aap->ent.info = aap->ent.player->info;
-
- if( (aap->ent.info.flags & AUDIO_FLAG_KILL) && !aap->ent.fadeout )
- {
- aap->ent.fadeout = FADEOUT_LENGTH;
- aap->ent.fadeout_current = FADEOUT_LENGTH;
- }
- }
- }
- }
-
- audio_unlock();
-}
-
-/*
- * Redistribute sound systems
- */
-VG_STATIC void audio_system_cleanup(void)
-{
- audio_lock();
-
- for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
- {
- struct active_audio_player *aap = &vg_audio.active_players[i];
- if( aap->active )
- {
- audio_entity *src = &aap->ent;
- if( src->cur < src->length || (src->info.flags & AUDIO_FLAG_LOOP ))
- {
- /* Good to keep */
- }
- else
- {
- audio_entity_free_internal( i );
- }
- }
- }
-
- audio_unlock();
-}
-
-/*
- * Get effective volume and pan from this entity
- */
-VG_STATIC void audio_entity_spacialize( audio_entity *ent,
- float *vol, float *pan )
-{
- if( ent->info.vol < 0.01f )
- {
- *vol = ent->info.vol;
- *pan = 0.0f;
- return;
- }
-
- if( !vg_validf(vg_audio.listener_pos[0]) ||
- !vg_validf(vg_audio.listener_pos[1]) ||
- !vg_validf(vg_audio.listener_pos[2]) ||
- !vg_validf(ent->info.world_position[0]) ||
- !vg_validf(ent->info.world_position[1]) ||
- !vg_validf(ent->info.world_position[2]) )
- {
- vg_error( "NaN listener/world position (%s)\n", ent->name );
- *vol = 0.0f;
- *pan = 0.0f;
- return;
- }
-
- v3f delta;
- v3_sub( ent->info.world_position, vg_audio.listener_pos, delta );
-
- float dist2 = v3_length2( delta );
-
- if( dist2 < 0.0001f )
- {
- *pan = 0.0f;
- *vol = 1.0f;
- }
- else
- {
- float dist = sqrtf( dist2 ),
- attn = (dist / ent->info.vol) +1.0f;
-
- v3_muls( delta, 1.0f/dist, delta );
- *pan = v3_dot( vg_audio.listener_ears, delta );
- *vol = 1.0f/(attn*attn);
- }
-}
-
-VG_STATIC void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst )
-{
- for( u32 i=0; i<count; i++ )
- {
- dst[ i*2 + 0 ] = ((float)src[i]) * (1.0f/32767.0f);
- dst[ i*2 + 1 ] = ((float)src[i]) * (1.0f/32767.0f);
- }
-}
-
-/*
- * adapted from stb_vorbis.h, since the original does not handle mono->stereo
- */
-VG_STATIC int
-stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
- int len )
-{
- int n = 0,
- c = VG_MIN( 1, f->channels - 1 );
-
- while( n < len )
- {
- int k = f->channel_buffer_end - f->channel_buffer_start;
-
- if( n+k >= len )
- k = len - n;
-
- for( int j=0; j < k; ++j )
- {
- *buffer++ = f->channel_buffers[ 0 ][f->channel_buffer_start+j];
- *buffer++ = f->channel_buffers[ c ][f->channel_buffer_start+j];
- }
-
- n += k;
- f->channel_buffer_start += k;
-
- if( n == len )
- break;
-
- if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
- break;
- }
-
- return n;
-}
-
-/*
- * ........ more wrecked code sorry!
- */
-VG_STATIC int
-stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
-{
- int n = 0,
- c = VG_MIN( 1, f->channels - 1 );
-
- while( n < len )
- {
- int k = f->channel_buffer_end - f->channel_buffer_start;
-
- if( n+k >= len )
- k = len - n;
-
- for( int j=0; j < k; ++j )
- {
- float sl = f->channel_buffers[ 0 ][f->channel_buffer_start+j],
- sr = f->channel_buffers[ c ][f->channel_buffer_start+j];
-
- *buffer++ = vg_clampf( 0.5f*(sl+sr), -1.0f, 1.0f ) * 32767.0f;
- //*buffer++ = vg_clampf( sr, -1.0f, 1.0f ) * 32767.0f;
- }
-
- n += k;
- f->channel_buffer_start += k;
-
- if( n == len )
- break;
-
- if( !stb_vorbis_get_frame_float( f, NULL, NULL ))
- break;
- }
-
- return n;
-}
-
-VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
-{
- vg_profile_begin( &_vg_prof_audio_decode );
-
- struct active_audio_player *aap = &vg_audio.active_players[id];
- audio_entity *ent = &aap->ent;
-
- u32 remaining = count;
- u32 cursor = ent->cur;
- u32 buffer_pos = 0;
-
- while( remaining )
- {
- u32 samples_this_run = VG_MIN( remaining, ent->length - cursor );
- remaining -= samples_this_run;