bird
[vg.git] / vg_audio.h
index fc0a278facf8c8c8756b866c8dab4c8a0110511a..0968957a64dbe7983f9e7d629960647bf3b83a0b 100644 (file)
@@ -116,6 +116,7 @@ static struct vg_audio_system
       char name[32];       /* only editable while allocated == 0 */
       audio_clip *source;  /* ... */
       u32 flags;           /* ... */
+      u32 colour;          /* ... */
 
       /* internal non-readable state 
        * -----------------------------*/
@@ -135,9 +136,11 @@ static struct vg_audio_system
          k_channel_activity_reset,   /* will advance if allocated==1, to wake */
          k_channel_activity_wake,    /* will advance to either of next two */
          k_channel_activity_alive,
+         k_channel_activity_end,
          k_channel_activity_error
       }
-      activity;
+      activity,
+      readable_activity;
    
       /* 
        * editable structure, can be modified inside _lock and _unlock
@@ -330,6 +333,7 @@ static audio_channel *audio_request_channel( audio_clip *clip, u32 flags )
       {
          ch->source = clip;
          ch->flags = flags;
+         ch->colour = 0x00333333;
          strcpy( ch->name, clip->path );
 
          ch->allocated = 1;
@@ -353,6 +357,14 @@ static audio_channel *audio_request_channel( audio_clip *clip, u32 flags )
    return NULL;
 }
 
+static int audio_channel_finished( audio_channel *ch )
+{
+   if( ch->readable_activity == k_channel_activity_end )
+      return 1;
+   else
+      return 0;
+}
+
 static audio_channel *audio_relinquish_channel( audio_channel *ch )
 {
    ch->editable_state.relinquished = 1;
@@ -430,7 +442,12 @@ static void audio_channel_set_spacial( audio_channel *ch, v3f co, float range )
    if( ch->flags & AUDIO_FLAG_SPACIAL_3D )
    {
       v3_copy( co, ch->editable_state.spacial_falloff );
-      ch->editable_state.spacial_falloff[3] = 1.0f/range;
+
+      if( range == 0.0f )
+         ch->editable_state.spacial_falloff[3] = 1.0f;
+      else
+         ch->editable_state.spacial_falloff[3] = 1.0f/range;
+
       ch->editble_state_write_mask |= AUDIO_EDIT_SPACIAL;
    }
    else
@@ -864,10 +881,19 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
       if( !ch->allocated )
          continue;
 
+      if( ch->activity == k_channel_activity_alive )
+      {
+         if( (ch->cursor >= ch->source_length) && 
+               !(ch->flags & AUDIO_FLAG_LOOP) )
+         {
+            ch->activity = k_channel_activity_end;
+         }
+      }
+
       /* process relinquishments */
       if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished )
       {
-         if( (ch->cursor >= ch->source_length && !(ch->flags & AUDIO_FLAG_LOOP)) 
+         if(   (ch->activity == k_channel_activity_end)
             || (ch->_.volume == 0.0f)
             || (ch->activity == k_channel_activity_error) )
          {
@@ -1044,12 +1070,14 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
 
    vg_profile_end( &_vg_prof_dsp );
 
-   /* 
-    * Relinquishing conditions
-    * ------------------------------------------------------------------
-    */
    audio_lock();
 
+   for( int i=0; i<AUDIO_CHANNELS; i ++ )
+   {
+      audio_channel *ch = &vg_audio.channels[i];
+      ch->readable_activity = ch->activity;
+   }
+
    /* Profiling information 
     * ----------------------------------------------- */
    vg_profile_increment( &_vg_prof_audio_decode );
@@ -1232,6 +1260,15 @@ VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
          "Vorbis"
       };
 
+      const char *activties[] =
+      {
+         "reset",
+         "wake ",
+         "alive",
+         "end  ",
+         "error"
+      };
+
       int format_index = 0;
 
       if( ch->source->flags & AUDIO_FLAG_STEREO )
@@ -1241,12 +1278,13 @@ VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
       else
          format_index = 1;
 
-      snprintf( perf, 127, "%02d %c%c%cD %s %4.2fv'%s'", 
+      snprintf( perf, 127, "%02d %c%c%cD %s [%s] %4.2fv'%s'", 
                i,
-               (ch->editable_state.relinquished)? 'r': ' ',
-               0?                                 'r': ' ',
+               (ch->editable_state.relinquished)? 'r': '_',
+               0?                                 'r': '_',
                0?                                 '3': '2',
                formats[format_index],
+               activties[ch->readable_activity],
                ch->editable_state.volume,
                ch->name );
 
@@ -1256,7 +1294,7 @@ VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
       }
       else
       {
-         ui_fill_rect( vg_uictx.cursor, 0xa0333333 );
+         ui_fill_rect( vg_uictx.cursor, 0xa0000000 | ch->colour );
       }
 
       vg_uictx.cursor[0] += 2;