heisenbug
[vg.git] / src / vg / vg_audio.h
index 5b8def23ae2634f1939caa4023ea70b618853df2..0b4fc3ea87264e22b1a3980f0e714c7fadce92d0 100644 (file)
@@ -72,8 +72,8 @@ struct audio_clip
    const char *path;
    enum audio_source_mode source_mode;
 
-   void *data;
    u32 size;
+   void *data;
 };
 
 typedef struct audio_mix_info audio_mix_info;
@@ -232,7 +232,8 @@ VG_STATIC void vg_audio_init(void)
 
    /* 32mb fixed */
    vg_audio.audio_pool = 
-      vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32 );
+      vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32, 
+                                  VG_MEMORY_SYSTEM );
 
    /* fixed */
    u32 decode_size = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS;
@@ -519,8 +520,8 @@ 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);
+      dst[ i*2 + 0 ] = ((float)src[i]) * (1.0f/32767.0f);
+      dst[ i*2 + 1 ] = ((float)src[i]) * (1.0f/32767.0f);
    }
 }
 
@@ -564,8 +565,7 @@ stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
  * ........ more wrecked code sorry!
  */
 VG_STATIC int 
-stb_vorbis_get_samples_i16_interleaved_mono( stb_vorbis *f, i16 *buffer, 
-                                             int len )
+stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
 {
    int n = 0,
        c = VG_MIN( 1, f->channels - 1 );
@@ -621,7 +621,9 @@ VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
       
       if( source_mode == k_audio_source_mono )
       {
-         i16 *src = &((i16 *)ent->info.source->data)[ cursor ];
+         i16 *src_buffer = ent->info.source->data,
+             *src = &src_buffer[cursor];
+
          audio_decode_uncompressed_mono( src, samples_this_run, dst );
       }
       else if( source_mode == k_audio_source_compressed )
@@ -668,7 +670,7 @@ VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
 }
 
 VG_STATIC void audio_entity_mix( aatree_ptr id, float *buffer, 
-                              u32 frame_count )
+                                u32 frame_count )
 {
    audio_entity *ent = &vg_audio.active_players[id].ent;
 
@@ -769,13 +771,14 @@ VG_STATIC void audio_clip_load( audio_clip *clip )
    {
       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
       };
 
+      void *filedata = vg_file_read( vg_mem.scratch, clip->path, &fsize );
+
       int err;
       stb_vorbis *decoder = stb_vorbis_open_memory( 
                             filedata, fsize, &err, &alloc );
@@ -791,20 +794,28 @@ VG_STATIC void audio_clip_load( audio_clip *clip )
       u32 length_samples = stb_vorbis_stream_length_in_samples( decoder ),
           data_size      = length_samples * sizeof(i16);
 
+      audio_lock();
       clip->data = vg_linear_alloc( vg_audio.audio_pool, data_size );
       clip->size = length_samples;
+      audio_unlock();
 
-      int read_samples = stb_vorbis_get_samples_i16_interleaved_mono
+      int read_samples = stb_vorbis_get_samples_i16_downmixed
                               decoder, clip->data, length_samples );
 
+      if( read_samples != length_samples )
+         vg_fatal_exit_loop( "Decode error" );
+
       float mb = (float)(data_size) / (1024.0f*1024.0f);
-      vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb );
+      vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip->path, mb,
+               length_samples );
    }
 
    /* load in directly */
    else if( clip->source_mode == k_audio_source_compressed )
    {
+      audio_lock();
       clip->data = vg_file_read( vg_audio.audio_pool, clip->path, &clip->size );
+      audio_unlock();
 
       if( !clip->data )
          vg_fatal_exit_loop( "Audio failed to load" );
@@ -833,7 +844,7 @@ VG_STATIC void audio_player_commit( audio_player *sys )
 
    if( sys->enqued )
    {
-      vg_warn( "Audio commit spamming; already enqued (%s)\n", sys->name );
+      vg_warn( "[2] Audio commit spamming; already enqued (%s)\n", sys->name );
       return;
    }
    
@@ -873,6 +884,13 @@ VG_STATIC void audio_player_playclip( audio_player *player, audio_clip *clip )
       return;
    }
 
+   if( player->enqued )
+   {
+      vg_warn( "[1] Audio commit spamming; already enqued (%s)\n", 
+               player->name );
+      return;
+   }
+
    player->info.source = clip;
    audio_player_commit( player );
 }