audio rework pt 1
[vg.git] / src / vg / vg_audio.h
index 024d87c7b23c27b13aeb8ccb8f3d080bd5e3f696..bb4d0920feb889361506d16db83393fefd861138 100644 (file)
-/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+
+#ifndef VG_AUDIO_H
+#define VG_AUDIO_H
 
 #define MA_NO_GENERATION
 #define MA_NO_DECODING
 #define MA_NO_ENCODING
 #include "dr_soft/miniaudio.h"
+#include "vg/vg.h"
+#include "vg/vg_stdint.h"
+#include "vg/vg_platform.h"
+#include "vg/vg_io.h"
+#include "vg/vg_m.h"
+#include "vg/vg_ui.h"
+#include "vg/vg_console.h"
+#include "vg/vg_store.h"
+
+#include <time.h>
 
 #define STB_VORBIS_MAX_CHANNELS 2
 #include "stb/stb_vorbis.h"
 
-#define SFX_MAX_SYSTEMS        32
-#define SFX_FLAG_STEREO                0x2
-#define SFX_FLAG_REPEAT        0x4
-#define SFX_FLAG_PERSISTENT 0x8
-#define FADEOUT_LENGTH                 4410
-#define FADEOUT_DIVISOR        (1.f/(float)FADEOUT_LENGTH)
+#define SFX_MAX_SYSTEMS       32
+#define AUDIO_FLAG_LOOP       0x1
+#define AUDIO_FLAG_ONESHOT    0x2
+#define AUDIO_FLAG_SPACIAL_3D 0x4
 
-typedef struct sfx_vol_control sfx_vol_control;
-typedef struct sfx_system sfx_system;
+#define FADEOUT_LENGTH        4410
+#define FADEOUT_DIVISOR       (1.0f/(float)FADEOUT_LENGTH)
 
-struct sfx_vol_control
+#define AUDIO_DECODE_SIZE     (1024*256)  /* 256 kb decoding buffers */
+
+enum audio_source_mode
 {
-       float val;
-       const char *name;
+   k_audio_source_mono,
+   k_audio_source_mono_compressed,
+   k_audio_source_stereo_compressed
 };
 
-struct sfx_system
+typedef struct audio_clip audio_clip;
+struct audio_clip
 {
-       sfx_system *persisitent_source;
-       int in_queue;
+   const char *path;
+   enum audio_source_mode source_mode;
 
-       /* Source buffer start */
-       float *source, *replacement;
-       
-       u32 clip_start, clip_end, buffer_length;
-       
-       /* Modifiers */
-       sfx_vol_control *vol_src;
-       float   vol, cvol, pan;
+   /* result */
+   void *data;
+   u32   len;  /* decompressed: sample count,
+                  compressed:   file size      */
+};
 
-   u32 delay;
-       
-       /* Info */
-       u32 ch, end, cur;
-       u32 flags;
-       
-       /* Effects */
-       u32 fadeout, fadeout_current;
-       
-       /* Diagnostic */
-       const char *name;
+typedef struct audio_mix_info audio_mix_info;
+struct audio_mix_info
+{
+   audio_clip *source;
+   v3f world_position;
+
+   float vol, pan;
+   u32 flags;
 };
 
-/* Set of up to 8 sound effects packed into one */
-typedef struct sfx_set sfx_set;
-struct sfx_set
+typedef struct audio_player audio_player;
+struct audio_player
 {
-       float *main;
-       char *sources;
-       
-       u32 segments[32];       /* from->to,from->to ... */
-       u32 numsegments;
-       u32 ch;
-       u32 flags;
+   aatree_ptr active_entity;  /* non-nil if currently playing */
+   audio_mix_info info;
+   int enqued;
+   
+   /* Diagnostic */
+   const char *name;
 };
 
-ma_device g_aud_device;
-ma_device_config g_aud_dconfig;
+typedef struct audio_entity audio_entity;
+struct audio_entity
+{
+   audio_player *player;
+   audio_mix_info info;
+
+   u32 length, cur;
+   
+   /* Effects */
+   u32 fadeout, fadeout_current;
+   const char *name;
+};
 
-/* 
- * Thread 1 - audio engine ( spawned from miniaudio.h )
- * ======================================================
+/*
+ * TODO list sunday
+ *
+ * play again: if already playing, leave in queue while it fadeouts
+ * oneshot:    create a ghost entity
+ *
  */
-sfx_system     sfx_sys[SFX_MAX_SYSTEMS];
-int                    sfx_sys_len = 0;
+
+static struct vg_audio_system
+{
+   ma_device         miniaudio_device;
+   ma_device_config  miniaudio_dconfig;
+
+   void             *mem, *decode_mem;
+   u32               mem_current,
+                     mem_total;
+
+   /* synchro */
+   int               sync_locked;
+   MUTEX_TYPE        mutex_checker;
+   MUTEX_TYPE        mutex_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;
+
+   char              performance_info[128];
+   int               debug_ui;
+
+   v3f               listener_pos,
+                     listener_ears;
+}
+vg_audio;
+
+static void *audio_alloc( u32 size )
+{
+   u32 new_current = vg_audio.mem_current + size;
+   if( new_current > vg_audio.mem_total )
+   {
+      vg_error( "audio pool over budget!\n" );
+      free( vg_audio.mem );
+      return NULL;
+   }
+
+   void *ptr = vg_audio.mem + vg_audio.mem_current;
+   vg_audio.mem_current = new_current;
+
+   return ptr;
+}
+
 
 /* 
- * Thread 0 - Critical transfer section
- * ======================================================
+ * These functions are called from the main thread and used to prevent bad 
+ * access. TODO: They should be no-ops in release builds.
  */
-MUTEX_TYPE     sfx_mux_t01; /* Resources share: 0 & 1 */
+static int audio_lock_checker_load(void)
+{
+   int value;
+   MUTEX_LOCK( vg_audio.mutex_checker );
+   value = vg_audio.sync_locked;
+   MUTEX_UNLOCK( vg_audio.mutex_checker );
+   return value;
+}
 
-sfx_system     *sfx_q[SFX_MAX_SYSTEMS]; /* Stuff changed */
-int                     sfx_q_len = 0;
+static void audio_lock_checker_store( int value )
+{
+   MUTEX_LOCK( vg_audio.mutex_checker );
+   vg_audio.sync_locked = value;
+   MUTEX_UNLOCK( vg_audio.mutex_checker );
+}
 
-float g_master_volume = 1.f;
+static void audio_require_lock(void)
+{
+   if( audio_lock_checker_load() )
+      return;
 
-/* Decompress entire vorbis stream into buffer */
-static float *sfx_vorbis_stream( const unsigned char *data, int len, 
-      int channels, u32 *samples )
+   vg_exiterr( "Modifying sound effects systems requires locking\n" );
+}
+
+static void audio_lock(void)
 {
-       int err;
-       stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL );
-       
-       if( !pv )
-       {
-               vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err );
-               return NULL;
-       }
-               
-       u32 length_samples = stb_vorbis_stream_length_in_samples( pv );
-       float *buffer = (float *)malloc( length_samples * channels * sizeof(float));
-       
-       if( !buffer )
-       {
-               stb_vorbis_close( pv );
-               vg_error( "out of memory while allocating sound resource\n" );
-               return NULL;
-       }
-       
-       int read_samples = stb_vorbis_get_samples_float_interleaved( 
-         pv, channels, buffer, length_samples * channels );
+   MUTEX_LOCK( vg_audio.mutex_sync );
+   audio_lock_checker_store(1);
+}
 
-       if( read_samples != length_samples )
-       {
-               vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", 
-            length_samples, read_samples );
-               length_samples = read_samples;
-       }
-       
-       stb_vorbis_close( pv );
-       *samples = length_samples;
-       return buffer;
+static void audio_unlock(void)
+{
+   audio_lock_checker_store(0);
+   MUTEX_UNLOCK( vg_audio.mutex_sync );
 }
 
-static float *sfx_vorbis( const char *strFileName, int channels, u32 *samples )
+
+static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
+                                    const void *pInput, ma_uint32 frameCount );
+
+static int vg_audio_init(void)
 {
-       i64 len;
-       void *filedata = vg_asset_read_s( strFileName, &len );
-       
-       if( filedata )
-       {
-               float *wav = sfx_vorbis_stream( filedata, len, channels, samples );
-               free( filedata );
-               return wav;
-       }
-       else
-       {
-               vg_error( "OGG load failed\n" );
-               return NULL;
-       }
+   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
+   });
+
+   u32 decode_region    = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS;
+   vg_audio.mem_total   = 1024*1024*32;
+   vg_audio.mem_current = 0;
+   vg_audio.mem         = malloc( vg_audio.mem_total + decode_region );
+   vg_audio.decode_mem  = &((u8 *)vg_audio.mem)[vg_audio.mem_total];
+
+   /* 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 );
+
+   ma_device_config *dconf  = &vg_audio.miniaudio_dconfig;
+   ma_device        *device = &vg_audio.miniaudio_device;
+
+    *dconf = ma_device_config_init( ma_device_type_playback );
+   dconf->playback.format    = ma_format_f32;
+   dconf->playback.channels  = 2;
+   dconf->sampleRate         = 44100;
+   dconf->dataCallback       = audio_mixer_callback;
+
+   dconf->pUserData = NULL;
+
+   vg_info( "Starting audio engine\n" );
+   
+   if( ma_device_init( NULL, dconf, device) != MA_SUCCESS )
+   {
+      vg_error( "ma_device failed to initialize" );
+      return 0;
+   } 
+   else 
+   {   
+      if( ma_device_start( device ) != MA_SUCCESS )
+      {
+         ma_device_uninit( device );
+         vg_error( "ma_device failed to start" );
+         return 0;
+      }
+   }
+
+   return 1;
+}
+
+static void vg_audio_free(void)
+{
+   ma_device        *device = &vg_audio.miniaudio_device;
+   ma_device_uninit( device );
+
+   free( vg_audio.mem );
 }
 
 /* 
- * thread 0 / client code
+ * thread 1
  */
-static int sfx_begin_edit( sfx_system *sys )
+
+static aatree_ptr audio_alloc_entity_internal(void)
 {
-       MUTEX_LOCK( sfx_mux_t01 );
-       
-       if( sfx_q_len >= SFX_MAX_SYSTEMS && !sys->in_queue )
-       {
-               MUTEX_UNLOCK( sfx_mux_t01 );
-               vg_warn( "Warning: No free space in sound queue\n" );           
-               return 0;
-       }
-       
-       return 1;
+   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;
 }
 
-static void sfx_end_edit( sfx_system *sys )
+static void audio_entity_free_internal( aatree_ptr id )
 {
-       MUTEX_UNLOCK( sfx_mux_t01 );
+   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 );
 }
 
-/* Mark change to be uploaded to queue system */
-static int sfx_push( sfx_system *sys )
+static void *audio_entity_vorbis_ptr( aatree_ptr entid )
 {
-       if( !sys->in_queue )
-       {
-               sfx_q[ sfx_q_len ++ ] = sys;
-               sys->in_queue = 1;
-       }
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
-       
-       return 1;
+   u8 *buf = (u8*)vg_audio.decode_mem,
+      *loc = &buf[AUDIO_DECODE_SIZE*entid];
+
+   return (void *)loc;
 }
 
-/* Edit a volume float, has to be function wrapped because of mutex */
-static void sfx_vol_fset( sfx_vol_control *src, float to )
+static void audio_entity_start( audio_entity *src )
 {
-       MUTEX_LOCK( sfx_mux_t01 );
+   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 = "todo";
+   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_mono_compressed ||
+       src->info.source->source_mode == k_audio_source_stereo_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->len, &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->len;
+   }
+}
 
-       src->val = to;
+/*
+ * Read everything from the queue 
+ */
+static void audio_system_enque(void)
+{
+   /* Process incoming sound queue */
+   audio_lock();
+   
+   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 = 1;
+               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;
+         }
+      }
+   }
+   
+   audio_unlock();
+}
 
-       MUTEX_UNLOCK( sfx_mux_t01 );
+/* 
+ * Redistribute sound systems 
+ */
+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();
 }
 
-/* thread-safe get volume value */
-static float sfx_vol_fget( sfx_vol_control *src )
+/* 
+ * Get effective volume and pan from this entity
+ */
+static void audio_entity_spacialize( audio_entity *ent, float *vol, float *pan )
 {
-       float val;
-       
-       MUTEX_LOCK( sfx_mux_t01 );
-       
-       val = src->val;
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
-       
-       return val;
+   v3f delta;
+   v3_sub( ent->info.world_position, vg_audio.listener_pos, delta );
+
+   float dist = v3_length( delta ),
+         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);
 }
 
-/* thread-safe set master volume */
-static void sfx_set_master( float to )
+static void audio_decode_uncompressed_mono( float *src, u32 count, float *dst )
 {
-       MUTEX_LOCK( sfx_mux_t01 );
-       
-       g_master_volume = to;
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
+   for( u32 i=0; i<count; i++ )
+   {
+      dst[ i*2 + 0 ] = src[i];
+      dst[ i*2 + 1 ] = src[i];
+   }
 }
 
-/* thread-safe get master volume */
-static float sfx_get_master(void)
+static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
 {
-       float val;
+   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;
+
+      float *dst = &buf[ buffer_pos * 2 ]; 
+      
+      if( ent->info.source->source_mode == k_audio_source_mono )
+      {
+         float *src = &((float *)ent->info.source->data)[ cursor ];
+         audio_decode_uncompressed_mono( src, samples_this_run, dst );
+      }
+      else if( ent->info.source->source_mode == k_audio_source_mono_compressed )
+      {
+         int read_samples = stb_vorbis_get_samples_float_interleaved( 
+               aap->vorbis_handle,
+               2,
+               dst,
+               samples_this_run * 2 );
+
+         if( read_samples != samples_this_run )
+         {
+            vg_warn( "Invalid samples read (%s)\n", ent->info.source->path );
+         }
+      }
+
+      cursor += samples_this_run;
+      buffer_pos += samples_this_run;
+      
+      if( (ent->info.flags & AUDIO_FLAG_LOOP) && remaining )
+      {
+         if( ent->info.source->source_mode == k_audio_source_mono_compressed ||
+             ent->info.source->source_mode == k_audio_source_stereo_compressed )
+         {
+            stb_vorbis_seek_start( aap->vorbis_handle );
+         }
+
+         cursor = 0;
+         continue;
+      }
+      else
+         break;
+   }
+
+   while( remaining )
+   {
+      buf[ buffer_pos*2 + 0 ] = 0.0f;
+      buf[ buffer_pos*2 + 1 ] = 0.0f;
+      buffer_pos ++;
+
+      remaining --;
+   }
+
+   ent->cur = cursor;
+}
 
-       MUTEX_LOCK( sfx_mux_t01 );
-       
-       val = g_master_volume;
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
-       
-       return val;
+static void audio_entity_mix( aatree_ptr id, float *buffer, 
+                              u32 frame_count )
+{
+   audio_entity *ent = &vg_audio.active_players[id].ent;
+
+   u32 cursor = ent->cur, buffer_pos = 0;
+   float *pcf = alloca( frame_count * 2 * sizeof(float) );
+   
+   u32 frames_write = frame_count;
+   float fadeout_divisor = 1.0f / (float)ent->fadeout;
+
+   float vol = ent->info.vol,
+         pan = ent->info.pan;
+
+   audio_entity_get_samples( id, frame_count, pcf );
+
+   if( ent->info.flags & AUDIO_FLAG_SPACIAL_3D )
+      audio_entity_spacialize( ent, &vol, &pan );
+
+   for( u32 j=0; j<frame_count; j++ )
+   {
+      float frame_vol = vol;
+
+      if( ent->fadeout )
+      {
+         /* Force this system to be removed now */
+         if( ent->fadeout_current == 0 )
+         {
+            ent->info.flags = 0x00;
+            ent->cur = ent->length;
+            break;
+         }
+
+         frame_vol *= (float)ent->fadeout_current * fadeout_divisor;
+         ent->fadeout_current --;
+      }
+
+      float sl = 1.0f-pan,
+            sr = 1.0f+pan;
+
+      buffer[ buffer_pos*2+0 ] += pcf[ buffer_pos*2+0 ] * frame_vol * sl;
+      buffer[ buffer_pos*2+1 ] += pcf[ buffer_pos*2+1 ] * frame_vol * sr;
+      
+      buffer_pos ++;
+   }
 }
 
-void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
-      const void *pInput, ma_uint32 frameCount );
+static void vg_sleep_ms( long msec )
+{
+    struct timespec ts;
 
-static void vg_audio_init(void)
+    ts.tv_sec = msec / 1000;
+    ts.tv_nsec = (msec % 1000) * 1000000;
+    nanosleep( &ts, &ts );
+}
+
+/*
+ * callback from miniaudio.h interface
+ */
+static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, 
+                                  const void *pInput, ma_uint32 frame_count )
 {
-       g_aud_dconfig = ma_device_config_init( ma_device_type_playback );
-       g_aud_dconfig.playback.format   = ma_format_f32;
-       g_aud_dconfig.playback.channels         = 2;
-       g_aud_dconfig.sampleRate                        = 44100;
-       g_aud_dconfig.dataCallback              = audio_mixer_callback;
-       
-       g_aud_dconfig.pUserData = NULL;
-       
-       vg_info( "Starting audio engine\n" );
-       
-       if( ma_device_init( NULL, &g_aud_dconfig, &g_aud_device ) != MA_SUCCESS )
-       {
-               vg_exiterr( "ma_device failed to initialize" );
-       } 
-       else 
-       {       
-               if( ma_device_start( &g_aud_device ) != MA_SUCCESS )
-               {
-                       ma_device_uninit( &g_aud_device );
-                       vg_exiterr( "ma_device failed to start" );
-               }
-       }
+   struct timespec time_start, time_end;
+   clock_gettime( CLOCK_REALTIME, &time_start );
+
+   audio_system_enque();
+   
+   /* Clear buffer */
+   float *pOut32F = (float *)pOutBuf;
+   for( int i=0; i<frame_count*2; i ++ )
+      pOut32F[i] = 0.0f;
+
+   /* Mix all sounds */
+   for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
+   {
+      struct active_audio_player *aap = &vg_audio.active_players[i];
+
+      if( aap->active )
+         audio_entity_mix( i, pOut32F, frame_count );
+   }
+   
+#if 0
+   vg_sleep_ms( 20 );
+#endif
+
+   /* redistribute */
+   audio_system_cleanup();
+
+   /* TODO: what the hell is this? */
+   (void)pInput;
+   
+   /*
+    * Debug information
+    */
+   clock_gettime( CLOCK_REALTIME, &time_end );
+
+   double elapsed = 1000.0*time_end.tv_sec + 1e-6*time_end.tv_nsec
+                       - (1000.0*time_start.tv_sec + 1e-6*time_start.tv_nsec),
+          budget  = ((double)frame_count / 44100.0) * 1000.0,
+          percent = (elapsed/budget) * 100.0;
+
+   snprintf( vg_audio.performance_info, 127, 
+                  "%.1fms/%.1fms (%.1f%%) (%u frames)",
+                  elapsed, budget, percent, frame_count );
 }
 
-static void vg_audio_free(void)
+/* Decompress entire vorbis stream into buffer */
+static float *audio_decompress_vorbis( const unsigned char *data, int len, 
+                                       int channels, u32 *samples )
 {
-       ma_device_uninit( &g_aud_device );
+   int err;
+   stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL );
+   
+   if( !pv )
+   {
+      vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err );
+      return NULL;
+   }
+      
+   u32 length_samples = stb_vorbis_stream_length_in_samples( pv );
+
+   vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
+               length_samples, 
+               (float)length_samples / (44100.0f*(float)channels),
+               (float)(length_samples*4*channels) / 1024.0f );
+   
+   float *buffer = audio_alloc( length_samples * channels * sizeof(float) );
+   if( !buffer )
+   {
+      stb_vorbis_close( pv );
+      vg_exit();
+   }
+   
+   int read_samples = stb_vorbis_get_samples_float_interleaved( 
+                           pv, channels, buffer, length_samples * channels );
+
+   if( read_samples != length_samples )
+   {
+      vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", 
+                  length_samples, read_samples );
+      length_samples = read_samples;
+   }
+   
+   stb_vorbis_close( pv );
+   *samples = length_samples;
+   return buffer;
 }
 
-/* 
- * thread 1
+static int audio_clip_load( audio_clip *clip )
+{
+   /* Load and decompress */
+   i64 file_len;
+   void *filedata = vg_asset_read_s( clip->path, &file_len );
+
+   if( !filedata )
+   {
+      vg_error( "OGG load failed (%s)\n", clip->path );
+      return 0;
+   }
+
+   if( clip->source_mode == k_audio_source_mono )
+   {
+      u32 samples;
+      float *sound = audio_decompress_vorbis( filedata, file_len, 1, &samples );
+      clip->data = sound;
+      clip->len = samples;
+
+      float seconds = (float)samples / 44100.0f,
+            mb      = (float)(samples*4) / (1024.0f*1024.0f);
+
+      vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n", 
+                                 clip->path, seconds, mb );
+   }
+   else if( clip->source_mode == k_audio_source_mono_compressed )
+   {
+      void *data = audio_alloc( file_len );
+      memcpy( data, filedata, file_len );
+
+      clip->data = data;
+      clip->len = file_len;
+
+      float mb = (float)(file_len) / (1024.0f*1024.0f);
+      vg_info( "Loaded audio clip[mono_compressed] '%s' (%.1fmb)\n", 
+                                          clip->path, mb );
+   }
+   else if( clip->source_mode == k_audio_source_stereo_compressed )
+   {
+      /* ... */
+
+      clip->data = NULL;
+      clip->len = 0;
+
+      vg_error( "Source mode (%u) currently unsupported\n", clip->source_mode );
+      return 0;
+   }
+   else
+   {
+      /* ... */
+
+      clip->data = NULL;
+      clip->len = 0;
+
+      vg_error( "Unkown source mode (%u)\n", clip->source_mode );
+      return 0;
+   }
+
+   return 1;
+}
+
+static void audio_clip_loadn( audio_clip *arr, int count )
+{
+   for( int i=0; i<count; i++ )
+      audio_clip_load( &arr[i] );
+}
+
+#if 0
+/*
+ * Client code
  */
+static void audio_pack_play( audio_pack *source, audio_player *sys, int id )
+{
+   audio_require_lock();
+
+   sys->fadeout = 0;
+   sys->fadeout_current = 0;
+   sys->source = source->data;
+   sys->cur    = source->segments[ id*2 + 0 ];
+   sys->end    = source->segments[ id*2 + 1 ];
+   sys->ch     = source->ch;
+   sys->source_mode = source->source_mode;
+   
+   /* for diagnostics */
+   sys->clip_start = sys->cur;
+   sys->clip_end = sys->end;
+   sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ];
+   sys->is_playing = 1;
+   
+   audio_player_push( sys );
+}
 
-static sfx_system *sfx_alloc(void)
+#endif
+
+/* Mark change to be uploaded through queue system */
+static void audio_player_commit( audio_player *sys )
 {
-       if( sfx_sys_len >= SFX_MAX_SYSTEMS )
-               return NULL;
-       
-       /* 
-    * A conditional is done against this in localization step,
-    * Needs to be initialized.
-    */
-       sfx_sys[ sfx_sys_len ].source = NULL;
-       
-       return sfx_sys + (sfx_sys_len++);
+   audio_require_lock();
+   
+   if( vg_audio.queue_len >= vg_list_size( vg_audio.entity_queue ) )
+   {
+      vg_warn( "Audio commit queue full\n" );
+      return;
+   }
+
+   if( sys->enqued )
+   {
+      vg_warn( "Audio commit spamming; already enqued (%s)\n", sys->name );
+      return;
+   }
+   
+   sys->enqued = 1;
+   audio_entity *ent = &vg_audio.entity_queue[ vg_audio.queue_len ++ ];
+   ent->info = sys->info;
+   ent->player = sys;
+   sys->active_entity = AATREE_PTR_NIL;
 }
 
-/* Fetch samples into pcf */
-static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch )
+/* Play a clip using player. If its already playing something, it will 
+ * fadeout quickly and start the next sound */
+static void audio_player_playclip( audio_player *player, audio_clip *clip )
 {
-       if( ch == 2 )
-       {
-               pcf[0] = source[ cur*2+0 ];
-               pcf[1] = source[ cur*2+1 ];
-       }
-       else
-       {
-               pcf[0] = source[ cur ];
-               pcf[1] = source[ cur ];
-       }
+   audio_require_lock();
+
+   player->info.source = clip;
+   audio_player_commit( player );
+}
+
+static void audio_player_playoneshot( audio_player *player, audio_clip *clip )
+{
+   
 }
 
 /*
- * callback from miniaudio.h interface
+ * Effects
  */
-void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, 
-      const void *pInput, ma_uint32 frameCount )
+
+/*
+ * Safety enforced Get/set attributes
+ */
+
+static void audio_player_set_position( audio_player *sys, v3f pos )
 {
-       /* Process incoming sound queue */
-       MUTEX_LOCK( sfx_mux_t01 );
-       
-       while( sfx_q_len --> 0 )
-       {
-               sfx_system *src = sfx_q[sfx_q_len];
-               sfx_system *clone;
-               
-               src->in_queue = 0;
-               
-               clone = sfx_alloc();
-               *clone = *src;
-               
-               /* Links need to exist on persistent sounds */
-               clone->persisitent_source = src->flags & SFX_FLAG_PERSISTENT? src: NULL;
-       }
-       
-       sfx_q_len = 0;
-       
-       /* Volume modifiers */
-       for( int i = 0; i < sfx_sys_len; i ++ )
-       {
-               sfx_system *sys = sfx_sys + i;
-               
-               /* Apply persistent volume if linked */
-               if( sys->flags & SFX_FLAG_PERSISTENT )
-               {
-                       sys->vol = sys->persisitent_source->vol * g_master_volume;
-         sys->pan = sys->persisitent_source->pan;
-                       
-                       /* Fadeout effect ( + remove ) */
-                       if( sys->persisitent_source->fadeout )
-                       {
-                               sys->fadeout_current = sys->persisitent_source->fadeout_current;
-                               sys->fadeout = sys->persisitent_source->fadeout;
-                               
-                               sys->persisitent_source = NULL;
-                               sys->flags &= ~SFX_FLAG_PERSISTENT;
-                       }
-               }
-                       
-               /* Apply volume slider if it has one linked */
-               if( sys->vol_src ) 
-                       sys->cvol = sys->vol * sys->vol_src->val;
-               else
-                       sys->cvol = sys->vol;
-       }
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
-       
-       /* Clear buffer */
-       float *pOut32F = (float *)pOutBuf;
-       for( int i = 0; i < frameCount * 2; i ++ ){
-               pOut32F[i] = 0.f;
-       }
+   audio_require_lock();
+   v3_copy( pos, sys->info.world_position );
+}
 
-       for( int i = 0; i < sfx_sys_len; i ++ )
-       {
-               sfx_system *sys = sfx_sys + i;
-               
-               u32 cursor = sys->cur, buffer_pos = 0;          
-               float pcf[2] = { 0.f, 0.0f };
-               
-               u32 frames_write = frameCount;
-               float fadeout_divisor = 1.0f / (float)sys->fadeout;
-               
-               while( frames_write )
-               {
-                       u32 samples_this_run = VG_MIN( frames_write, sys->end - cursor );
-               
-                       if( sys->fadeout )
-                       {
-                               /* Force this system to be removed now */
-                               if( sys->fadeout_current == 0 )
-                               {
-                                       sys->flags &= 0x00000000;
-                                       sys->cur = sys->end;
-                                       break;
-                               }
-                               
-                               samples_this_run = VG_MIN( samples_this_run, sys->fadeout_current );
-                       }
-                       
-                       for( u32 j=0; j<samples_this_run; j++ )
-                       {
-                               audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch );
-                               
-                               float vol = sys->cvol;
-
-                               if( sys->fadeout )
-                               {
-                                       vol *= (float)sys->fadeout_current * fadeout_divisor;
-                                       sys->fadeout_current --;
-                               }
-                               
-                               if( buffer_pos >= frameCount )
-                               {
-                                       break;
-                               }
-
-            float sl = 1.0f-sys->pan,
-                  sr = 1.0f+sys->pan;
-
-                               pOut32F[ buffer_pos*2+0 ] += pcf[0] * vol * sl;
-                               pOut32F[ buffer_pos*2+1 ] += pcf[1] * vol * sr;
-                               
-                               cursor ++;
-                               buffer_pos ++;
-                       }
-                       
-                       frames_write -= samples_this_run;
-                       
-                       if( sys->flags & SFX_FLAG_REPEAT )
-                       {
-                               if( frames_write )
-                               {
-                                       cursor = sys->clip_start;
-                                       continue;
-                               }
-                       }
-
-                       sys->cur = cursor;
-                       break;
-               }
-       }
+static void audio_player_set_vol( audio_player *sys, float vol )
+{
+   audio_require_lock();
+   sys->info.vol = vol;
+}
 
-       /* Redistribute sound systems */
-       MUTEX_LOCK( sfx_mux_t01 );
+static float audio_player_get_vol( audio_player *sys )
+{
+   audio_require_lock();
+   return sys->info.vol;
+}
 
-       u32 idx = 0, wr = 0;
-       while( idx != sfx_sys_len )
-       {
-               sfx_system *src = sfx_sys + idx;
-               
-               /* Keep only if cursor is before end or repeating */
-               if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) ) 
-               {
-                       sfx_sys[ wr ++ ] = sfx_sys[ idx ];
-               }
-               
-               idx ++ ;
-       }
-       sfx_sys_len = wr;
-       
-       MUTEX_UNLOCK( sfx_mux_t01 );
-       
-       (void)pInput;
+static void audio_player_set_pan( audio_player *sys, float pan )
+{
+   audio_require_lock();
+   sys->info.pan = pan;
 }
 
-/* 
- * Load strings into sfx_set's memory
- * String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0"
- */
-static void sfx_set_strings( sfx_set *dest, char *strSources, 
-      u32 flags, int bAsync )
+static float audio_player_get_pan( audio_player *sys )
 {
-       dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1;
-       
-       dest->main = NULL;
-       dest->numsegments = 0;
-       char *source = strSources;
-       
-       u32 total = 0;
-       int len;
-       while( (len = strlen( source )) )
-       {
-               u32 samples;
-               float *sound = sfx_vorbis( source, dest->ch, &samples );
-               
-               if( !sound )
-               {
-                       free( dest->main );
-                       dest->numsegments = 0;
-                       return;
-               }
-               
-               total += samples;
-               
-               float *nbuf = realloc( dest->main, total * dest->ch * sizeof(float) );
-               
-               if( nbuf )
-               {
-                       dest->main = nbuf;
-                       memcpy( dest->main + (total-samples)*dest->ch, 
-               sound, samples*dest->ch*sizeof(float) );
-                       free( sound );
-                       
-                       dest->segments[ dest->numsegments*2+0 ] = total-samples;
-                       dest->segments[ dest->numsegments*2+1 ] = total;
-               }
-               else
-               {
-                       vg_error( "realloc() failed\n" );
-                       free( sound );
-                       return;
-               }
-               
-               source += len +1;
-               dest->numsegments ++;
-       }
+   audio_require_lock();
+   return sys->info.pan;
 }
 
-static void sfx_set_init( sfx_set *dest, char *sources )
+static void audio_player_set_flags( audio_player *sys, u32 flags )
 {
-       if( !sources )
-               sfx_set_strings( dest, dest->sources, dest->flags, 0 );
-       else
-               sfx_set_strings( dest, sources, dest->flags, 0 );
+   audio_require_lock();
+   sys->info.flags = flags;
 }
 
-static void sfx_set_play( sfx_set *source, sfx_system *sys, int id )
+static u32 audio_player_get_flags( audio_player *sys )
 {
-       if( sfx_begin_edit( sys ) )
-       {
-               sys->fadeout = 0;
-               sys->fadeout_current = 0;
-               sys->source = source->main;
-               sys->cur        = source->segments[ id*2 + 0 ];
-               sys->end        = source->segments[ id*2 + 1 ];
-               sys->ch                 = source->ch;
-               
-      /* for diagnostics */
-               sys->clip_start = sys->cur;
-               sys->clip_end = sys->end;
-               sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ];
-               
-               sfx_push( sys );
-       }
+   audio_require_lock();
+   return sys->info.flags;
 }
 
-/* Pick a random sound from the buffer and play it into system */
-static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, 
-      int min_id, int max_id )
+
+/* 
+ * Debugging
+ */
+
+static void audio_debug_ui(void)
 {
-       if( !source->numsegments )
-               return;
+   if( !vg_audio.debug_ui )
+      return;
 
-       if( max_id > source->numsegments )
+   /* Get data */
+       struct sound_info
        {
-               vg_error( "Max ID out of range for playrnd\n" );
-               return; 
+               const char *name;
+               u32 cursor, flags, length;
+      float vol;
        }
+       infos[ SFX_MAX_SYSTEMS ];
+       int num_systems = 0;
 
-       int pick = (rand() % (max_id-min_id)) + min_id;
+   char perf[128];
        
-       sfx_set_play( source, sys, pick );
-}
+   audio_lock();
+       
+       for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
+       {
+      struct active_audio_player *aap = &vg_audio.active_players[i];
 
-static void sfx_system_fadeout( sfx_system *sys, u32 length_samples )
-{
-       if( sfx_begin_edit( sys ) )
+      if( !aap->active )
+         continue;
+
+      audio_entity *ent = &aap->ent;
+               struct sound_info *snd = &infos[ num_systems ++ ];
+               
+               snd->name = ent->name;
+               snd->cursor = ent->cur;
+               snd->flags = ent->info.flags;
+               snd->length = ent->length;
+      snd->vol = ent->info.vol*100.0f;
+       }
+   strcpy( perf, vg_audio.performance_info );
+   audio_unlock();
+
+   /* Draw UI */
+   ui_global_ctx.cursor[0] = 10;
+   ui_global_ctx.cursor[1] = 10;
+   ui_global_ctx.cursor[2] = 150;
+   ui_global_ctx.cursor[3] = 12;
+   ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
+   
+   float usage = (float)vg_audio.mem_current / (1024.0f*1024.0f),
+         total = (float)vg_audio.mem_total   / (1024.0f*1024.0f),
+         percent = (usage/total) * 100.0f;
+   snprintf( perf, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage, total, percent );
+
+   ui_global_ctx.cursor[1] += 20;
+   ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
+
+   ui_global_ctx.cursor[1] += 20;
+
+       /* Draw audio stack */
+       for( int i=0; i<num_systems; i ++ )
        {
-               sys->fadeout_current = length_samples;
-               sys->fadeout = length_samples;
+      struct sound_info *inf = &infos[i];
+
+               ui_global_ctx.cursor[2] = 150;
+               ui_global_ctx.cursor[3] = 12;
                
-               sfx_end_edit( sys );
+               u32 alpha = 0xa0000000;
+
+               ui_new_node( &ui_global_ctx );
+               {               
+                       ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x00333333|alpha );
+
+                       ui_px baseline = ui_global_ctx.cursor[0],
+               w  = 150,
+               c  = baseline + ((float)inf->cursor / (float)inf->length) * w;
+                       
+                       /* cursor */
+                       ui_global_ctx.cursor[2] = 2;
+                       ui_global_ctx.cursor[0] = c;
+                       ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0xffffffff );
+                       
+                       ui_global_ctx.cursor[0] = baseline + 2;
+                       ui_global_ctx.cursor[1] += 2;
+         snprintf( perf, 127, "%s %.1f%%", infos[i].name, infos[i].vol );
+                       ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
+               }
+
+               ui_end_down( &ui_global_ctx );
+               ui_global_ctx.cursor[1] += 1;
        }
 }
 
-static void sfx_set_free( sfx_set *set )
-{
-       free( set->main );
-}
+#endif /* VG_AUDIO_H */