perlin
[vg.git] / vg_audio.h
index 279b7f4ea57316c6c9680cf5fdf9be631ee0c8e0..bff400f340b345eed97f81e31c16f0aeffa7245d 100644 (file)
@@ -474,8 +474,7 @@ static void audio_channel_sidechain_lfo( audio_channel *ch, int lfo_id,
 
 static void audio_channel_set_spacial( audio_channel *ch, v3f co, float range )
 {
-   if( ch->flags & AUDIO_FLAG_SPACIAL_3D )
-   {
+   if( ch->flags & AUDIO_FLAG_SPACIAL_3D ){
       v3_copy( co, ch->editable_state.spacial_falloff );
 
       if( range == 0.0f )
@@ -485,8 +484,7 @@ static void audio_channel_set_spacial( audio_channel *ch, v3f co, float range )
 
       ch->editble_state_write_mask |= AUDIO_EDIT_SPACIAL;
    }
-   else
-   {
+   else{
       vg_warn( "Tried to set spacialization paramaters for 2D channel (%s)\n",
                ch->name );
    }
@@ -497,8 +495,7 @@ static int audio_oneshot_3d( audio_clip *clip, v3f position,
 {
    audio_channel *ch = audio_request_channel( clip, AUDIO_FLAG_SPACIAL_3D );
 
-   if( ch )
-   {
+   if( ch ){
       audio_channel_set_spacial( ch, position, range );
       audio_channel_edit_volume( ch, volume, 1 );
       ch = audio_relinquish_channel( ch );
@@ -513,8 +510,7 @@ static int audio_oneshot( audio_clip *clip, float volume, float pan )
 {
    audio_channel *ch = audio_request_channel( clip, 0x00 );
 
-   if( ch )
-   {
+   if( ch ){
       audio_channel_edit_volume( ch, volume, 1 );
       ch = audio_relinquish_channel( ch );
 
@@ -542,7 +538,6 @@ static void audio_set_lfo_frequency( int id, float freq )
 }
 
 
-
 /* 
  * Committers
  * -----------------------------------------------------------------------------
@@ -660,15 +655,13 @@ stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
    int n = 0,
        c = VG_MIN( 1, f->channels - 1 );
 
-   while( n < len ) 
-   {
+   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 ) 
-      {
+      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];
 
@@ -699,8 +692,7 @@ static inline float audio_lfo_pull_sample( audio_lfo *lfo )
    float t  = lfo->time;
          t /= (float)lfo->_.period;
 
-   if( lfo->_.wave_type == k_lfo_polynomial_bipolar )
-   {
+   if( lfo->_.wave_type == k_lfo_polynomial_bipolar ){
       /*
        *           #
        *          # #
@@ -722,8 +714,7 @@ static inline float audio_lfo_pull_sample( audio_lfo *lfo )
               
              ) * (1.0f-fabsf(t));
    }
-   else
-   {
+   else{
       return 0.0f;
    }
 }
@@ -738,45 +729,37 @@ static void audio_channel_get_samples( audio_channel *ch,
 
    u32 format = ch->source->flags & AUDIO_FLAG_FORMAT;
 
-   while( remaining )
-   {
+   while( remaining ){
       u32 samples_this_run = VG_MIN(remaining, ch->source_length - ch->cursor);
       remaining -= samples_this_run;
 
       float *dst = &buf[ buffer_pos * 2 ]; 
       
-      if( format == k_audio_format_stereo )
-      {
-         for( int i=0;i<samples_this_run; i++ )
-         {
+      if( format == k_audio_format_stereo ){
+         for( int i=0;i<samples_this_run; i++ ){
             dst[i*2+0] = 0.0f;
             dst[i*2+1] = 0.0f;
          }
       }
-      else if( format == k_audio_format_vorbis )
-      {
+      else if( format == k_audio_format_vorbis ){
          int read_samples = stb_vorbis_get_samples_float_interleaved_stereo( 
                ch->vorbis_handle,
                dst,
                samples_this_run );
 
-         if( read_samples != samples_this_run )
-         {
+         if( read_samples != samples_this_run ){
             vg_warn( "Invalid samples read (%s)\n", ch->source->path );
 
-            for( int i=0; i<samples_this_run; i++ )
-            {
+            for( int i=0; i<samples_this_run; i++ ){
                dst[i*2+0] = 0.0f;
                dst[i*2+1] = 0.0f;
             }
          }
       }
-      else if( format == k_audio_format_bird )
-      {
+      else if( format == k_audio_format_bird ){
          synth_bird_generate_samples( ch->bird_handle, dst, samples_this_run );
       }
-      else
-      {
+      else{
          i16 *src_buffer = ch->source->data,
              *src        = &src_buffer[ch->cursor];
 
@@ -786,8 +769,7 @@ static void audio_channel_get_samples( audio_channel *ch,
       ch->cursor += samples_this_run;
       buffer_pos += samples_this_run;
       
-      if( (ch->flags & AUDIO_FLAG_LOOP) && remaining )
-      {
+      if( (ch->flags & AUDIO_FLAG_LOOP) && remaining ){
          if( format == k_audio_format_vorbis )
             stb_vorbis_seek_start( ch->vorbis_handle );
          else if( format == k_audio_format_bird )
@@ -800,8 +782,7 @@ static void audio_channel_get_samples( audio_channel *ch,
          break;
    }
 
-   while( remaining )
-   {
+   while( remaining ){
       buf[ buffer_pos*2 + 0 ] = 0.0f;
       buf[ buffer_pos*2 + 1 ] = 0.0f;
       buffer_pos ++;
@@ -819,39 +800,24 @@ static void audio_channel_mix( audio_channel *ch, float *buffer )
 
    float frame_samplerate = ch->_.sampling_rate;
 
-   if( ch->flags & AUDIO_FLAG_SPACIAL_3D )
-   {
-      if( !vg_validf(vg_audio.listener_pos[0]) ||
-          !vg_validf(vg_audio.listener_pos[1]) ||
-          !vg_validf(vg_audio.listener_pos[2]) ||
-          !vg_validf(vg_audio.listener_velocity[0]) ||
-          !vg_validf(vg_audio.listener_velocity[1]) ||
-          !vg_validf(vg_audio.listener_velocity[2]) ||
-          !vg_validf(ch->_.spacial_falloff[0]) ||
-          !vg_validf(ch->_.spacial_falloff[1]) ||
-          !vg_validf(ch->_.spacial_falloff[2]) )
-      {
-         vg_error( "NaN listener/world position (%s)\n", ch->name );
+   if( ch->flags & AUDIO_FLAG_SPACIAL_3D ){
+      v3f delta;
+      v3_sub( ch->_.spacial_falloff, vg_audio.listener_pos, delta );
+
+      float dist = v3_length( delta ),
+            vol  = vg_maxf( 0.0f, 1.0f - ch->_.spacial_falloff[3]*dist );
 
-         framevol_l = 0.0f;
-         framevol_r = 0.0f;
+      if( dist <= 0.01f ){
+         
       }
       else{
-         v3f delta;
-         v3_sub( ch->_.spacial_falloff, vg_audio.listener_pos, delta );
-
-         float dist = v3_length( delta ),
-               vol  = vg_maxf( 0.0f, 1.0f - ch->_.spacial_falloff[3]*dist );
-
          v3_muls( delta, 1.0f/dist, delta );
-         float pan  = v3_dot( vg_audio.listener_ears, delta );
+         float pan = v3_dot( vg_audio.listener_ears, delta );
          vol = powf( vol, 5.0f );
 
          framevol_l *= (vol * 0.5f) * (1.0f - pan);
          framevol_r *= (vol * 0.5f) * (1.0f + pan);
 
-         v3_muls( delta, 1.0f/dist, delta );
-
          const float vs = 100.0f;
          float doppler = (vs+v3_dot(delta,vg_audio.listener_velocity))/vs;
                doppler = vg_clampf( doppler, 0.6f, 1.4f );
@@ -859,11 +825,15 @@ static void audio_channel_mix( audio_channel *ch, float *buffer )
          if( fabsf(doppler-1.0f) > 0.01f )
             frame_samplerate *= doppler;
       }
+
+      if( !vg_validf( framevol_l ) ) vg_fatal_exit_loop( "NaN left channel" );
+      if( !vg_validf( framevol_r ) ) vg_fatal_exit_loop( "NaN right channel" );
+      if( !vg_validf( frame_samplerate ) ) 
+         vg_fatal_exit_loop( "NaN sample rate" );
    }
 
    u32 buffer_length = AUDIO_MIX_FRAME_SIZE;
-   if( frame_samplerate != 1.0f )
-   {
+   if( frame_samplerate != 1.0f ){
       float l = ceilf( (float)(AUDIO_MIX_FRAME_SIZE) * frame_samplerate );
       buffer_length = l+1;
    }
@@ -882,8 +852,7 @@ static void audio_channel_mix( audio_channel *ch, float *buffer )
    const float volume_start  = ch->volume_movement_start;
    const float volume_target = ch->_.volume_target;
 
-   for( u32 j=0; j<AUDIO_MIX_FRAME_SIZE; j++ )
-   {
+   for( u32 j=0; j<AUDIO_MIX_FRAME_SIZE; j++ ){
       /*
        * there is some REALLY weird behaviour with minss,
        * i cannot begin to guess what the cause is, but the bahaviour when
@@ -949,15 +918,13 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
     * Copy data and move edit flags to commit flags
     * ------------------------------------------------------------- */
    audio_lock();
-   for( int i=0; i<AUDIO_CHANNELS; i++ )
-   {
+   for( int i=0; i<AUDIO_CHANNELS; i++ ){
       audio_channel *ch = &vg_audio.channels[i];
 
       if( !ch->allocated )
          continue;
 
-      if( ch->activity == k_channel_activity_alive )
-      {
+      if( ch->activity == k_channel_activity_alive ){
          if( (ch->cursor >= ch->source_length) && 
                !(ch->flags & AUDIO_FLAG_LOOP) )
          {
@@ -966,8 +933,7 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
       }
 
       /* process relinquishments */
-      if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished )
-      {
+      if( (ch->activity != k_channel_activity_reset) && ch->_.relinquished ){
          if(   (ch->activity == k_channel_activity_end)
             || (ch->_.volume == 0.0f)
             || (ch->activity == k_channel_activity_error) )
@@ -980,8 +946,7 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
       }
 
       /* process new channels */
-      if( ch->activity == k_channel_activity_reset )
-      {
+      if( ch->activity == k_channel_activity_reset ){
          ch->_ = ch->editable_state;
          ch->cursor = 0;
          ch->source_length = 0;
@@ -994,27 +959,23 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
          ch->editable_state.relinquished = ch->_.relinquished;
 
 
-      if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME )
-      {
+      if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME ){
          ch->_.volume = ch->editable_state.volume;
          ch->_.volume_target = ch->editable_state.volume;
       }
-      else
-      {
+      else{
          ch->editable_state.volume = ch->_.volume;
       }
       
 
-      if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE )
-      {
+      if( ch->editble_state_write_mask & AUDIO_EDIT_VOLUME_SLOPE ){
          ch->volume_movement_start = ch->_.volume;
          ch->volume_movement = 0;
          
          ch->_.volume_target = ch->editable_state.volume_target;
          ch->_.volume_rate   = ch->editable_state.volume_rate;
       }
-      else
-      {
+      else{
          ch->editable_state.volume_target = ch->_.volume_target;
          ch->editable_state.volume_rate   = ch->_.volume_rate;
       }
@@ -1026,13 +987,11 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
          ch->editable_state.sampling_rate = ch->_.sampling_rate;
 
 
-      if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT )
-      {
+      if( ch->editble_state_write_mask & AUDIO_EDIT_LFO_ATTACHMENT ){
          ch->_.lfo        = ch->editable_state.lfo;
          ch->_.lfo_amount = ch->editable_state.lfo_amount;
       }
-      else
-      {
+      else{
          ch->editable_state.lfo        = ch->_.lfo;
          ch->editable_state.lfo_amount = ch->_.lfo_amount;
       }
@@ -1050,12 +1009,10 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
       ch->editble_state_write_mask  = 0x00;
    }
 
-   for( int i=0; i<AUDIO_LFOS; i++ )
-   {
+   for( int i=0; i<AUDIO_LFOS; i++ ){
       audio_lfo *lfo = &vg_audio.oscillators[ i ];
 
-      if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE )
-      {
+      if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_WAVE ){
          lfo->_.wave_type = lfo->editable_state.wave_type;
 
          if( lfo->_.wave_type == k_lfo_polynomial_bipolar )
@@ -1067,18 +1024,15 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
          }
       }
 
-      if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD )
-      {
-         if( lfo->_.period )
-         {
+      if( lfo->editble_state_write_mask & AUDIO_EDIT_LFO_PERIOD ){
+         if( lfo->_.period ){
             float t = lfo->time;
                   t/= (float)lfo->_.period;
 
             lfo->_.period = lfo->editable_state.period;
             lfo->time = lfo->_.period * t;
          }
-         else
-         {
+         else{
             lfo->time = 0;
             lfo->_.period = lfo->editable_state.period;
          }