From 7bc371eaa08c31539b4803e08bc2492cf916f742 Mon Sep 17 00:00:00 2001 From: hgn Date: Wed, 26 Oct 2022 02:40:06 +0100 Subject: [PATCH] yabadabadoo --- src/tools.sh | 2 +- src/vg/vg_audio.h | 194 +++++++++++++++++++++++++++++++++-------- src/vg/vg_io.h | 14 ++- src/vg/vg_mem.h | 8 +- src/vg/vg_steam_auth.h | 4 +- src/vg/vg_tex.h | 5 +- 6 files changed, 177 insertions(+), 50 deletions(-) diff --git a/src/tools.sh b/src/tools.sh index d7bc893..c59bd37 100644 --- a/src/tools.sh +++ b/src/tools.sh @@ -34,4 +34,4 @@ tool_qoiconv(){ tool_shader tool_fontcomp tool_qoiconv -tool_texsheet +#tool_texsheet diff --git a/src/vg/vg_audio.h b/src/vg/vg_audio.h index e4513d4..cb6f41c 100644 --- a/src/vg/vg_audio.h +++ b/src/vg/vg_audio.h @@ -49,16 +49,25 @@ #define AUDIO_FLAG_ONESHOT 0x2 #define AUDIO_FLAG_SPACIAL_3D 0x4 #define AUDIO_FLAG_AUTO_START 0x8 +#define AUDIO_FLAG_KILL 0x10 #define FADEOUT_LENGTH 1100 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH) #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */ +enum audio_source_mode +{ + k_audio_source_mono, + k_audio_source_compressed, +}; + typedef struct audio_clip audio_clip; struct audio_clip { const char *path; + enum audio_source_mode source_mode; + void *data; u32 size; }; @@ -337,33 +346,40 @@ VG_STATIC void audio_entity_start( audio_entity *src ) src->player->active_entity = entid; } - /* 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 - }; + 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 ); + 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 ); + if( !decoder ) + { + vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n", + src->info.source->path, err ); - audio_entity_free_internal( entid ); - return; + audio_entity_free_internal( entid ); + return; + } + else + { + ent->length = stb_vorbis_stream_length_in_samples( decoder ); + } + + aap->vorbis_handle = decoder; } else { - ent->length = stb_vorbis_stream_length_in_samples( decoder ); + ent->length = src->info.source->size; } - - aap->vorbis_handle = decoder; } /* @@ -422,6 +438,12 @@ VG_STATIC void audio_system_enque(void) 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; + } } } } @@ -489,12 +511,12 @@ VG_STATIC void audio_entity_spacialize( audio_entity *ent, float *vol, float *pa } } -VG_STATIC void audio_decode_uncompressed_mono( float *src, u32 count, float *dst ) +VG_STATIC void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst ) { for( u32 i=0; ichannels - 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 ); @@ -552,14 +613,24 @@ VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) float *dst = &buf[ buffer_pos * 2 ]; - int read_samples = stb_vorbis_get_samples_float_interleaved_stereo( - aap->vorbis_handle, - dst, - samples_this_run ); - - if( read_samples != samples_this_run ) + int source_mode = ent->info.source->source_mode; + + if( source_mode == k_audio_source_mono ) + { + i16 *src = &((i16 *)ent->info.source->data)[ cursor ]; + audio_decode_uncompressed_mono( src, samples_this_run, dst ); + } + else if( source_mode == k_audio_source_compressed ) { - vg_warn( "Invalid samples read (%s)\n", ent->info.source->path ); + int read_samples = stb_vorbis_get_samples_float_interleaved_stereo( + aap->vorbis_handle, + dst, + samples_this_run ); + + if( read_samples != samples_this_run ) + { + vg_warn( "Invalid samples read (%s)\n", ent->info.source->path ); + } } cursor += samples_this_run; @@ -567,7 +638,11 @@ VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) if( (ent->info.flags & AUDIO_FLAG_LOOP) && remaining ) { - stb_vorbis_seek_start( aap->vorbis_handle ); + if( source_mode == k_audio_source_compressed ) + { + stb_vorbis_seek_start( aap->vorbis_handle ); + } + cursor = 0; continue; } @@ -686,14 +761,53 @@ VG_STATIC void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, VG_STATIC void audio_clip_load( audio_clip *clip ) { - clip->data = vg_file_read( vg_audio.audio_pool, clip->path ); - clip->size = vg_file_size( vg_audio.audio_pool ); + if( clip->source_mode == k_audio_source_mono ) + { + vg_linear_clear( vg_mem.scratch ); + u32 fsize; + void *filedata = vg_file_read( vg_mem.scratch, clip->path, &fsize ); + + stb_vorbis_alloc alloc = { + .alloc_buffer = vg_linear_alloc( vg_mem.scratch, AUDIO_DECODE_SIZE ), + .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE + }; + + int err; + stb_vorbis *decoder = stb_vorbis_open_memory( + filedata, fsize, &err, &alloc ); + + if( !decoder ) + { + vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n", + clip->path, err ); + vg_fatal_exit_loop( "Vorbis decode error" ); + } + + /* only mono is supported in uncompressed */ + u32 length_samples = stb_vorbis_stream_length_in_samples( decoder ), + data_size = length_samples * sizeof(i16); + + clip->data = vg_linear_alloc( vg_audio.audio_pool, data_size ); + clip->size = length_samples; - if( !clip->data ) - vg_fatal_exit_loop( "Audio failed to load" ); + int read_samples = stb_vorbis_get_samples_i16_interleaved_mono( + decoder, clip->data, length_samples ); - float mb = (float)(clip->size) / (1024.0f*1024.0f); - vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb ); + float mb = (float)(data_size) / (1024.0f*1024.0f); + vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb ); + } + + /* load in directly */ + else if( clip->source_mode == k_audio_source_compressed ) + { + clip->data = vg_file_read( vg_audio.audio_pool, clip->path, &clip->size ); + + if( !clip->data ) + vg_fatal_exit_loop( "Audio failed to load" ); + + float mb = (float)(clip->size) / (1024.0f*1024.0f); + vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb ); + } } VG_STATIC void audio_clip_loadn( audio_clip *arr, int count ) @@ -749,6 +863,12 @@ VG_STATIC void audio_player_playclip( audio_player *player, audio_clip *clip ) audio_require_init( player ); audio_require_clip_loaded( clip ); + if( player->info.flags & AUDIO_FLAG_KILL ) + { + vg_error( "Can't start audio clip on player that is/has disconnected" ); + return; + } + player->info.source = clip; audio_player_commit( player ); } diff --git a/src/vg/vg_io.h b/src/vg/vg_io.h index c789300..b5440ec 100644 --- a/src/vg/vg_io.h +++ b/src/vg/vg_io.h @@ -33,7 +33,7 @@ VG_STATIC void vg_file_print_invalid( FILE *fp ) } /* read entire binary file */ -VG_STATIC void *vg_file_read( void *lin_alloc, const char *path ) +VG_STATIC void *vg_file_read( void *lin_alloc, const char *path, u32 *size ) { FILE *f = fopen( path, "rb" ); if( f ) @@ -73,6 +73,8 @@ VG_STATIC void *vg_file_read( void *lin_alloc, const char *path ) buffer = vg_linear_resize( lin_alloc, buffer, current ); fclose( f ); + + *size = (u32)current; return buffer; } @@ -84,23 +86,27 @@ VG_STATIC void *vg_file_read( void *lin_alloc, const char *path ) } /* get the size of the file just loaded */ +#if 0 VG_STATIC u32 vg_linear_last_size( void *allocator ); /* ? */ VG_STATIC u32 vg_file_size( void *lin_alloc ) { return vg_linear_last_size( lin_alloc ); } +#endif /* read entire file and append a null on the end */ -VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path ) +VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path, u32 *sz ) { - char *str = vg_file_read( lin_alloc, path ); + u32 size; + char *str = vg_file_read( lin_alloc, path, &size ); if( !str ) return NULL; /* include null terminator */ str = vg_linear_extend( lin_alloc, str, 1 ); - str[ vg_file_size(lin_alloc) ] = '\0'; + str[ size ] = '\0'; + *sz = size+1; return str; } diff --git a/src/vg/vg_mem.h b/src/vg/vg_mem.h index 1ef1e77..a3c7cd6 100644 --- a/src/vg/vg_mem.h +++ b/src/vg/vg_mem.h @@ -5,6 +5,7 @@ #include "vg_platform.h" #include +#include typedef struct vg_linear_allocator vg_linear_allocator; @@ -15,7 +16,6 @@ struct } static vg_mem; -#pragma pack(push,1) struct vg_linear_allocator { u32 size, cur; @@ -24,11 +24,9 @@ struct vg_linear_allocator void *last_alloc; u32 last_alloc_size; }; -#pragma pack(pop) /* * TODO: Fallback on libc - * TODO: 8 byte alignment */ VG_STATIC void vg_error(const char *fmt, ...); @@ -37,6 +35,10 @@ VG_STATIC void vg_error(const char *fmt, ...); __attribute__((warn_unused_result)) VG_STATIC void *vg_linear_alloc( void *allocator, u32 size ) { + size += 7; + size >>= 3; + size <<= 3; + if( allocator == NULL ) vg_fatal_exit_loop( "Null allocator" ); diff --git a/src/vg/vg_steam_auth.h b/src/vg/vg_steam_auth.h index 8a6c400..9ad1a2d 100644 --- a/src/vg/vg_steam_auth.h +++ b/src/vg/vg_steam_auth.h @@ -115,11 +115,11 @@ static u8 vg_char_base16( char c ) static int vg_load_steam_symetric_key( const char *path, u8 *buf ) { vg_linear_clear( vg_mem.scratch ); - char *src = vg_file_read( vg_mem.scratch, path ); + u32 size; + char *src = vg_file_read( vg_mem.scratch, path, &size ); if( src ) { - u32 size = vg_file_size(vg_mem.rtmemory); if( size < k_nSteamEncryptedAppTicketSymmetricKeyLen ) { vg_error( "Application key was invalid size\n" ); diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 6c7c403..7a43450 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -84,13 +84,12 @@ VG_STATIC GLuint vg_tex2d_rgba( const char *path ) glBindTexture( GL_TEXTURE_2D, texture_name ); vg_linear_clear( vg_mem.scratch ); - void *file = vg_file_read( vg_mem.scratch, path ); + u32 size; + void *file = vg_file_read( vg_mem.scratch, path, &size ); if( file ) { qoi_desc info; - - u32 size = vg_file_size( vg_mem.scratch ); u8 *tex_buffer = qoi_decode( file, size, &info, 4 ); if( tex_buffer ) -- 2.25.1