X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg_audio_dsp.h;h=c2bed10ebde2a7935fc2c53ddf8167743bb14350;hb=27b74ba6426c84c98ae777a095be31377e87037d;hp=0eaeb544ae6855da0687b106ad10eb8e0d4f3b15;hpb=bafc8bc648b83f3b5f5567eedde1fc4533660e4f;p=vg.git diff --git a/vg_audio_dsp.h b/vg_audio_dsp.h index 0eaeb54..c2bed10 100644 --- a/vg_audio_dsp.h +++ b/vg_audio_dsp.h @@ -4,8 +4,9 @@ #define VG_GAME #include "vg/vg.h" -static struct vg_dsp -{ +//#define VG_ECHO_LPF_BUTTERWORTH + +static struct vg_dsp{ float *buffer; u32 allocations; @@ -16,6 +17,8 @@ static struct vg_dsp echo_tunings[8], reverb_wet_mix, reverb_dry_mix; + + vg_rand rand; } vg_dsp; @@ -56,12 +59,35 @@ struct dsp_schroeder float gain; }; -static inline void dsp_read_delay( struct dsp_delay *delay, float *s ) -{ - u32 index = delay->cur+1; +struct dsp_biquad { + f32 a0, a1, a2, b1, b2, c0, d0, + xnz1, xnz2, ynz1, ynz2, offset; +}; + +static f32 dsp_biquad_process( struct dsp_biquad *bq, f32 xn ){ + f32 yn = + bq->a0*xn + bq->a1*bq->xnz1 + bq->a2*bq->xnz2 + - bq->b1*bq->ynz1 - bq->b2*bq->ynz2; + bq->xnz2 = bq->xnz1; + bq->xnz1 = xn; + bq->ynz2 = bq->ynz1; + bq->ynz1 = yn; + return yn + bq->offset; +} + +static void dsp_init_biquad_butterworth_lpf( struct dsp_biquad *bq, f32 fc ){ + f32 c = 1.0f/tanf(VG_PIf*fc / 44100.0f); + bq->a0 = 1.0f / (1.0f + sqrtf(2.0f)*c + powf(c, 2.0f) ); + bq->a1 = 2.0f * bq->a0; + bq->a2 = bq->a0; + bq->b1 = 2.0f * bq->a0*(1.0f - powf(c, 2.0f)); + bq->b2 = bq->a0 * (1.0f - sqrtf(2.0f)*c + powf(c, 2.0f) ); +} + +static inline void dsp_read_delay( struct dsp_delay *delay, float *s, u32 t ){ + u32 index = delay->cur+t; if( index >= delay->length ) - index = 0; + index -= delay->length; *s = delay->buffer[ index ]; } @@ -123,7 +149,7 @@ static inline void dsp_process_schroeder( struct dsp_schroeder *sch, float dry = *input; float delay_output; - dsp_read_delay( &sch->M, &delay_output ); + dsp_read_delay( &sch->M, &delay_output, 1 ); float feedback_attenuated = delay_output * sch->gain, input_feedback_sum = dry + feedback_attenuated; @@ -136,7 +162,12 @@ static inline void dsp_process_schroeder( struct dsp_schroeder *sch, /* temporary global design */ static struct dsp_lpf __lpf_mud_free; static struct dsp_delay __echos[8]; + +#ifdef VG_ECHO_LPF_BUTTERWORTH +static struct dsp_biquad __echos_lpf[8]; +#else static struct dsp_lpf __echos_lpf[8]; +#endif static struct dsp_schroeder __diffusion_chain[8]; static void async_vg_dsp_alloc_texture( void *payload, u32 size ) @@ -149,8 +180,8 @@ static void async_vg_dsp_alloc_texture( void *payload, u32 size ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); } -static void vg_dsp_init( void ) -{ +static void vg_dsp_init( void ){ + vg_rand_seed( &vg_dsp.rand, 461 ); vg_dsp.buffer = vg_linear_alloc( vg_mem.rtmemory, 1024*1024*1 ); vg_dsp.view_texture_buffer = vg_linear_alloc( vg_mem.rtmemory, 512*512 ); @@ -162,18 +193,23 @@ static void vg_dsp_init( void ) float sizes[] = { 2.0f, 4.0f, 8.0f, 16.0f, 32.0f, 64.0f, 128.0f, 256.0f }; - float reflection_variance = 0.1f; + float variance = 0.1f; for( int i=0; i<8; i++ ){ float reflection_time = ((sizes[i])/343.0f) * 1000.0f; - float var = 1.0f + (vg_randf64()*2.0f - 1.0f) * reflection_variance, + float var = 1.0f + (vg_randf64(&vg_dsp.rand)*2.0f - 1.0f) * variance, total = reflection_time * var; dsp_init_delay( &__echos[i], total / 1000.0f ); float freq = vg_lerpf( 800.0f, 350.0f, sizes[i] / 256.0f ); + +#ifdef VG_ECHO_LPF_BUTTERWORTH + dsp_init_biquad_butterworth_lpf( &__echos_lpf[i], freq ); +#else dsp_init_lpf( &__echos_lpf[i], freq ); +#endif } float diffusions[] = { 187.0f, 159.0f, 143.0f, 121.0f, @@ -190,10 +226,15 @@ static void vg_dsp_process( float *stereo_in, float *stereo_out ) float recieved = 0.0f; for( int i=0; i<8; i++ ){ - float echo; - dsp_read_delay( __echos+i, &echo ); + f32 echo; + dsp_read_delay( __echos+i, &echo, 1 ); + +#ifdef VG_ECHO_LPF_BUTTERWORTH + echo = dsp_biquad_process( __echos_lpf+i, echo ); +#else dsp_write_lpf( __echos_lpf+i, &echo ); dsp_read_lpf( __echos_lpf+i, &echo ); +#endif recieved += echo * vg_dsp.echo_tunings[i]*0.98; } @@ -272,7 +313,12 @@ static void dsp_update_tunings(void) for( int i=0; i<8; i++ ){ float freq = vg_lerpf( 200.0f, 500.0f, vg_dsp.echo_tunings[i] ); + +#ifdef VG_ECHO_LPF_BUTTERWORTH + dsp_init_biquad_butterworth_lpf( &__echos_lpf[i], freq ); +#else dsp_update_lpf( &__echos_lpf[i], freq ); +#endif } for( int i=0;i<8; i++ ){