yabadabadoo
authorhgn <hgodden00@gmail.com>
Wed, 26 Oct 2022 01:40:06 +0000 (02:40 +0100)
committerhgn <hgodden00@gmail.com>
Wed, 26 Oct 2022 01:40:06 +0000 (02:40 +0100)
src/tools.sh
src/vg/vg_audio.h
src/vg/vg_io.h
src/vg/vg_mem.h
src/vg/vg_steam_auth.h
src/vg/vg_tex.h

index d7bc89319fcfbbd1fc8d5c929e915a76a339e293..c59bd373350490e5884a9c6a796ed73b788ff556 100644 (file)
@@ -34,4 +34,4 @@ tool_qoiconv(){
 tool_shader
 tool_fontcomp
 tool_qoiconv
-tool_texsheet
+#tool_texsheet
index e4513d429a3b02e412de9c0173b2d5e9208827e9..cb6f41c3e99f5d4094d6de3b32e133dbbad71ba2 100644 (file)
 #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; i<count; i++ )
    {
-      dst[ i*2 + 0 ] = src[i];
-      dst[ i*2 + 1 ] = src[i];
+      dst[ i*2 + 0 ] = (float)src[i] * (1.0f/32767.0f);
+      dst[ i*2 + 1 ] = (float)src[i] * (1.0f/32767.0f);
    }
 }
 
@@ -534,6 +556,45 @@ stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
    return n;
 }
 
+/* 
+ * ........ more wrecked code sorry!
+ */
+VG_STATIC int 
+stb_vorbis_get_samples_i16_interleaved_mono( 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 );
@@ -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 );
 }
index c78930075c135fac28ec6565b95e1dcbdeda8b12..b5440ec201be29fd95a234054efd1e594819e16b 100644 (file)
@@ -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;
 }
index 1ef1e77260eecd2e49ebbb4f069b6167a241352a..a3c7cd6e8d92b343b2627e0d1a29efb343e1d296 100644 (file)
@@ -5,6 +5,7 @@
 #include "vg_platform.h"
 
 #include <stdlib.h>
+#include <malloc.h>
 
 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" );
 
index 8a6c400ebc02dc573c150a37b71bcc6cb2f1d014..9ad1a2d36d69160b876b0b00f3ec7b42929f120f 100644 (file)
@@ -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" );
index 6c7c40335a6d0e7c28ea2c57f558d89822ef9990..7a434507ca7cb53541eb643c1a108521b1c03721 100644 (file)
@@ -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 )