replace VG_STATIC -> static
[vg.git] / vg_audio.h
index 67cc3f100072d82a456c1ad89371bfb3245a17c0..39f494ffa0b843e501c28aaab94db4cfa7dd443e 100644 (file)
 #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 "vg/vg_profiler.h"
 #include "vg/vg_audio_synth_bird.h"
 
-#include <sys/time.h>
-#include <math.h>
-
 #ifdef __GNUC__
   #ifndef __clang__
     #pragma GCC push_options
@@ -83,13 +79,21 @@ typedef struct audio_channel audio_channel;
 typedef struct audio_lfo audio_lfo;
 
 struct audio_clip{
-   const char *path;
+   union {              /* TODO oof.. */
+      u64 _p64_;
+      const char *path;
+   };
+
    u32 flags;
    u32 size;
-   void *data;
+
+   union{ 
+      u64 _p64;
+      void *data;
+   };
 };
 
-static struct vg_audio_system{
+struct vg_audio_system{
    SDL_AudioDeviceID sdl_output_device;
 
    void             *audio_pool, 
@@ -200,7 +204,7 @@ static struct vg_audio_system{
    float             internal_global_volume,
                      external_global_volume;
 }
-vg_audio = { .external_global_volume = 1.0f };
+static vg_audio = { .external_global_volume = 1.0f };
 
 #include "vg/vg_audio_dsp.h"
 
@@ -219,7 +223,7 @@ static struct vg_profile
  * These functions are called from the main thread and used to prevent bad 
  * access. TODO: They should be no-ops in release builds.
  */
-VG_STATIC int audio_lock_checker_load(void)
+static int audio_lock_checker_load(void)
 {
    int value;
    SDL_AtomicLock( &vg_audio.sl_checker );
@@ -228,14 +232,14 @@ VG_STATIC int audio_lock_checker_load(void)
    return value;
 }
 
-VG_STATIC void audio_lock_checker_store( int value )
+static void audio_lock_checker_store( int value )
 {
    SDL_AtomicLock( &vg_audio.sl_checker );
    vg_audio.sync_locked = value;
    SDL_AtomicUnlock( &vg_audio.sl_checker );
 }
 
-VG_STATIC void audio_require_lock(void)
+static void audio_require_lock(void)
 {
    if( audio_lock_checker_load() )
       return;
@@ -244,20 +248,20 @@ VG_STATIC void audio_require_lock(void)
    abort();
 }
 
-VG_STATIC void audio_lock(void)
+static void audio_lock(void)
 {
    SDL_AtomicLock( &vg_audio.sl_sync );
    audio_lock_checker_store(1);
 }
 
-VG_STATIC void audio_unlock(void)
+static void audio_unlock(void)
 {
    audio_lock_checker_store(0);
    SDL_AtomicUnlock( &vg_audio.sl_sync );
 }
 
-VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int frame_count );
-VG_STATIC void vg_audio_init(void)
+static void audio_mixer_callback( void *user, u8 *stream, int frame_count );
+static void vg_audio_init(void)
 {
    /* TODO: Move here? */
    vg_console_reg_var( "debug_audio", &vg_audio.debug_ui, 
@@ -306,7 +310,7 @@ VG_STATIC void vg_audio_init(void)
    }
 }
 
-VG_STATIC void vg_audio_free(void)
+static void vg_audio_free(void)
 {
    vg_dsp_free();
    SDL_CloseAudioDevice( vg_audio.sdl_output_device );
@@ -619,7 +623,7 @@ static int audio_channel_load_source( audio_channel *ch )
    return 1;
 }
 
-VG_STATIC void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst )
+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);
@@ -630,7 +634,7 @@ VG_STATIC void audio_decode_uncompressed_mono( i16 *src, u32 count, float *dst )
 /* 
  * adapted from stb_vorbis.h, since the original does not handle mono->stereo
  */
-VG_STATIC int 
+static int 
 stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer, 
                                                  int len )
 {
@@ -664,7 +668,7 @@ stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer,
 /* 
  * ........ more wrecked code sorry!
  */
-VG_STATIC int 
+static int 
 stb_vorbis_get_samples_i16_downmixed( stb_vorbis *f, i16 *buffer, int len )
 {
    int n = 0,
@@ -845,10 +849,25 @@ static void audio_channel_mix( audio_channel *ch, float *buffer )
          }
       }
 
-      if( !vg_validf( framevol_l ) ) vg_fatal_error( "NaN left channel" );
-      if( !vg_validf( framevol_r ) ) vg_fatal_error( "NaN right channel" );
-      if( !vg_validf( frame_samplerate ) ) 
-         vg_fatal_error( "NaN sample rate" );
+      if( !vg_validf( framevol_l ) || 
+          !vg_validf( framevol_r ) ||
+          !vg_validf( frame_samplerate ) ){
+         vg_fatal_error( "Invalid sampling conditions.\n"
+                         "This crash is to protect your ears.\n"
+                         "  channel: %p (%s)\n"
+                         "  sample_rate: %f\n"
+                         "  volume: L%f R%f\n"
+                         "  listener: %.2f %.2f %.2f [%.2f %.2f %.2f]\n",
+                         ch, ch->name, frame_samplerate, 
+                         framevol_l, framevol_r,
+                         vg_audio.internal_listener_pos[0],
+                         vg_audio.internal_listener_pos[1],
+                         vg_audio.internal_listener_pos[2],
+                         vg_audio.internal_listener_ears[0],
+                         vg_audio.internal_listener_ears[1],
+                         vg_audio.internal_listener_ears[2]
+                         );
+      }
    }
 
    u32 buffer_length = AUDIO_MIX_FRAME_SIZE;
@@ -916,7 +935,7 @@ static void audio_channel_mix( audio_channel *ch, float *buffer )
    vg_profile_end( &_vg_prof_audio_mix );
 }
 
-VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
+static void audio_mixer_callback( void *user, u8 *stream, int byte_count )
 {
    /*
     * Copy data and move edit flags to commit flags
@@ -1134,7 +1153,7 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int byte_count )
    audio_unlock();
 }
 
-VG_STATIC void audio_clip_load( audio_clip *clip, void *lin_alloc )
+static void audio_clip_load( audio_clip *clip, void *lin_alloc )
 {
    if( lin_alloc == NULL )
       lin_alloc = vg_audio.audio_pool;
@@ -1227,19 +1246,21 @@ VG_STATIC void audio_clip_load( audio_clip *clip, void *lin_alloc )
       if( read_samples != length_samples )
          vg_fatal_error( "Decode error" );
 
+#if 0
       float mb = (float)(data_size) / (1024.0f*1024.0f);
       vg_info( "Loaded audio clip '%s' (%.1fmb) %u samples\n", clip->path, mb,
                length_samples );
+#endif
    }
 }
 
-VG_STATIC void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
+static void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
 {
    for( int i=0; i<count; i++ )
       audio_clip_load( &arr[i], lin_alloc );
 }
 
-VG_STATIC void audio_require_clip_loaded( audio_clip *clip )
+static void audio_require_clip_loaded( audio_clip *clip )
 {
    if( clip->data && clip->size )
       return;
@@ -1252,7 +1273,7 @@ VG_STATIC void audio_require_clip_loaded( audio_clip *clip )
  * Debugging
  */
 
-VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
+static void audio_debug_ui( m4x4f mtx_pv )
 {
    if( !vg_audio.debug_ui )
       return;
@@ -1270,13 +1291,11 @@ VG_STATIC void audio_debug_ui( m4x4f mtx_pv )
     */
 
    float budget = ((double)vg_audio.samples_last / 44100.0) * 1000.0;
-#if 0
    vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_audio_decode,
                                               &vg_prof_audio_mix,
                                               &vg_prof_audio_dsp}, 3, 
                      budget, (ui_rect){ 4, VG_PROFILE_SAMPLE_COUNT*2 + 8,
                                         512, 0 }, 3 );
-#endif
 
 
    char perf[128];