From ae4f56ff37b739db48c0fce0f2035d1a060d3ae2 Mon Sep 17 00:00:00 2001 From: mattiasgustavsson Date: Thu, 29 Feb 2024 21:06:08 +0100 Subject: [PATCH] Updated to latest version, added audiosys.h --- README.md | 1 + app.h | 22 +- audiosys.h | 1280 ++++++++++++++++++++++++++++++++++++++++++++++++++++ crtemu.h | 961 ++++++++++++++++++++++++++++++--------- 4 files changed, 2047 insertions(+), 217 deletions(-) create mode 100644 audiosys.h diff --git a/README.md b/README.md index cd84b8d..bd428ec 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Single-file public domain libraries for C/C++ (dual licensed under MIT). More libs, work-in-progress, some are unfinished, some are complete but lacking documentation. * array.h - Dynamic array library for C/C++. +* audiosys.h - Sound mixer library for C/C++. * buffer.h - Memory buffer with read/write operations, for C/C++. * crtemu.h - Cathode ray tube emulation shader for C/C++. * cstr.h - String interning and manipulation library for C/C++. diff --git a/app.h b/app.h index 2f85b24..bc6669c 100644 --- a/app.h +++ b/app.h @@ -187,6 +187,9 @@ Here's a basic sample program which starts a windowed app and plots random pixel return app_run( app_proc, NULL, NULL, NULL, NULL ); } + // pass-through so the program will build with either /SUBSYSTEM:WINDOWS or /SUBSYSTEN:CONSOLE + extern "C" int __stdcall WinMain( struct HINSTANCE__*, struct HINSTANCE__*, char*, int ) { return main( __argc, __argv ); } + API Documentation @@ -1105,9 +1108,9 @@ static int app_internal_opengl_present( struct app_internal_opengl_t* gl, APP_U3 gl->TexParameteri( APP_GL_TEXTURE_2D, APP_GL_TEXTURE_MAG_FILTER, APP_GL_NEAREST ); } - float r = ( ( border_xbgr >> 16 ) & 0xff ) / 255.0f; + float b = ( ( border_xbgr >> 16 ) & 0xff ) / 255.0f; float g = ( ( border_xbgr >> 8 ) & 0xff ) / 255.0f; - float b = ( ( border_xbgr ) & 0xff ) / 255.0f; + float r = ( ( border_xbgr ) & 0xff ) / 255.0f; gl->ClearColor( r, g, b, 1.0f ); gl->Clear( APP_GL_COLOR_BUFFER_BIT ); gl->DrawArrays( APP_GL_TRIANGLE_FAN, 0, 4 ); @@ -2024,7 +2027,6 @@ static BOOL CALLBACK app_internal_monitorenumproc( HMONITOR hmonitor, HDC dc, LP #else MONITORINFOEXA mi; memset( &mi, 0, sizeof( MONITORINFOEXA ) ); - mi.cbSize = sizeof( MONITORINFOEXA ); BOOL res = GetMonitorInfoA( hmonitor, (LPMONITORINFO)&mi ); if( res && strlen( mi.szDevice ) >= sizeof( display->id ) ) res = FALSE; strcpy( display->id, res ? mi.szDevice : "" ) ; @@ -4589,7 +4591,7 @@ int, app_js_audio_needed, (bool has_focus), if (audio_ctx === false) return 0; try { (audio_ctx = new (alias(window,"","",'AudioContext'))()).createBuffer(1,1,44100).getChannelData(0); } catch (e) { } if (!audio_ctx) { audio_ctx = false; WA.print('Warning: WebAudio not supported\n'); return 0; } - for (var i = 0; i != 10; i++) audio_bufs[i] = audio_ctx.createBuffer(2, 512, 44100); + for (var i = 0; i != 10; i++) audio_bufs[i] = audio_ctx.createBuffer(2, 2048, 44100); if (!start_audio()) { set_start_audio_event('click'); set_start_audio_event('touchstart'); set_start_audio_event('keydown'); } } if (!start_audio() && !start_audio()) return 0; @@ -4598,13 +4600,13 @@ int, app_js_audio_needed, (bool has_focus), { if (has_focus && (audio_miss += 2) > 7) { - audio_latency += 512; + audio_latency += 2048; audio_miss = 0; } audio_done = ct; } else if (audio_miss > 1) audio_miss--; - return ((ct - audio_done) * 44100 + .5 + audio_latency * (has_focus ? 1 : 2) + 511)>>9; + return ((ct - audio_done) * 44100 + .5 + audio_latency * (has_focus ? 1 : 2) + 2047)>>11; }) WAJIC(int, app_js_audio_push, (APP_S16* sample_pairs, int volume), @@ -4613,7 +4615,7 @@ WAJIC(int, app_js_audio_push, (APP_S16* sample_pairs, int volume), var buf = audio_bufs[audio_bufidx = ((audio_bufidx + 1) % 10)]; var left = buf.getChannelData(0), right = buf.getChannelData(1); var f = (1 / 32768) * (volume / 255); - for (var i = 0; i != 512; i++) + for (var i = 0; i != 2048; i++) { left[i] = sample_pairs[i*2] * f; right[i] = sample_pairs[i*2+1] * f; @@ -4622,7 +4624,7 @@ WAJIC(int, app_js_audio_push, (APP_S16* sample_pairs, int volume), source.connect(audio_ctx.destination); source.buffer = buf; source[source.start ? 'start' : 'noteOn'](0.005+audio_done); - audio_done += 512/44100; + audio_done += 2048/44100; }) @@ -4631,7 +4633,7 @@ void app_present( app_t* app, APP_U32 const* pixels_xbgr, int width, int height, if( pixels_xbgr ) app_internal_opengl_present( &app->gl, pixels_xbgr, width, height, mod_xbgr, border_xbgr ); for( int needed = app_js_audio_needed(app->has_focus); app->sound_callback && needed--;) { - app->sound_callback(app->sound_buffer, 512, app->sound_user_data); + app->sound_callback(app->sound_buffer, 2048, app->sound_user_data); app_js_audio_push(app->sound_buffer, app->volume); } if( app->has_focus ) @@ -4646,7 +4648,7 @@ void app_sound( app_t* app, int sample_pairs_count, void (*sound_callback)( APP_ app->sound_callback = sound_callback; app->sound_user_data = user_data; if( sound_callback && !app->sound_buffer ) - app->sound_buffer = (APP_S16*) APP_MALLOC( app->memctx, sizeof(APP_S16) * 512 * 2 ); + app->sound_buffer = (APP_S16*) APP_MALLOC( app->memctx, sizeof(APP_S16) * 2048 * 2 ); else if( !sound_callback && app->sound_buffer ) APP_FREE( app->memctx, app->sound_buffer ); } diff --git a/audiosys.h b/audiosys.h new file mode 100644 index 0000000..38ea1dc --- /dev/null +++ b/audiosys.h @@ -0,0 +1,1280 @@ +/* +------------------------------------------------------------------------------ + Licensing information can be found at the end of the file. +------------------------------------------------------------------------------ + +audiosys.h - v0.1 - Sound and music playback (mixing only) for C/C++. + +Do this: + #define AUDIOSYS_IMPLEMENTATION +before you include this file in *one* C/C++ file to create the implementation. +*/ + +#ifndef audiosys_h +#define audiosys_h + +#ifndef AUDIOSYS_S16 + #define AUDIOSYS_S16 signed short +#endif + +#ifndef AUDIOSYS_U64 + #define AUDIOSYS_U64 unsigned long long +#endif + +#define AUDIOSYS_DEFAULT_VOICE_COUNT 16 + +typedef struct audiosys_audio_source_t { + void* instance; + void (*release)( void* instance ); + int (*read_samples)( void* instance, float* sample_pairs, int sample_pairs_count ); + void (*restart)( void* instance ); + void (*set_position)( void* instance, int position_in_sample_pairs ); + int (*get_position)( void* instance ); +} audiosys_audio_source_t; + + +typedef struct audiosys_t audiosys_t; + +audiosys_t* audiosys_create( int active_voice_count, void* memctx ); +void audiosys_destroy( audiosys_t* audiosys ); + +void audiosys_render( audiosys_t* audiosys, AUDIOSYS_S16* output_sample_pairs, int sample_pairs_count ); + +void audiosys_master_volume_set( audiosys_t* audiosys, float volume ); +float audiosys_master_volume( audiosys_t* audiosys ); + +void audiosys_gain_set( audiosys_t* audiosys, float gain ); +float audiosys_gain( audiosys_t* audiosys ); + +void audiosys_pause( audiosys_t* audiosys ); +void audiosys_resume( audiosys_t* audiosys ); + +void audiosys_stop_all( audiosys_t* audiosys ); + +typedef enum audiosys_paused_t { + AUDIOSYS_NOT_PAUSED, + AUDIOSYS_PAUSED, +} audiosys_paused_t; + +audiosys_paused_t audiosys_paused( audiosys_t* audiosys ); + + +typedef enum audiosys_loop_t { + AUDIOSYS_LOOP_OFF, + AUDIOSYS_LOOP_ON, +} audiosys_loop_t; + +void audiosys_music_play( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_in_time ); +void audiosys_music_stop( audiosys_t* audiosys, float fade_out_time ); +void audiosys_music_pause( audiosys_t* audiosys ); +void audiosys_music_resume( audiosys_t* audiosys ); +void audiosys_music_switch( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_out_time, float fade_in_time ); +void audiosys_music_cross_fade( audiosys_t* audiosys, audiosys_audio_source_t source, float cross_fade_time ); +void audiosys_music_position_set( audiosys_t* audiosys, float position ); +float audiosys_music_position( audiosys_t* audiosys ); +audiosys_audio_source_t audiosys_music_source( audiosys_t* audiosys ); +void audiosys_music_loop_set( audiosys_t* audiosys, audiosys_loop_t loop ); +audiosys_loop_t audiosys_music_loop( audiosys_t* audiosys ); +void audiosys_music_volume_set( audiosys_t* audiosys, float volume ); +float audiosys_music_volume( audiosys_t* audiosys ); +void audiosys_music_pan_set( audiosys_t* audiosys, float pan ); +float audiosys_music_pan( audiosys_t* audiosys ); + +void audiosys_ambience_play( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_in_time ); +void audiosys_ambience_stop( audiosys_t* audiosys, float fade_out_time ); +void audiosys_ambience_pause( audiosys_t* audiosys ); +void audiosys_ambience_resume( audiosys_t* audiosys ); +void audiosys_ambience_switch( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_out_time, float fade_in_time ); +void audiosys_ambience_cross_fade( audiosys_t* audiosys, audiosys_audio_source_t source, float cross_fade_time ); +void audiosys_ambience_position_set( audiosys_t* audiosys, float position ); +float audiosys_ambience_position( audiosys_t* audiosys ); +audiosys_audio_source_t audiosys_ambience_source( audiosys_t* audiosys ); +void audiosys_ambience_loop_set( audiosys_t* audiosys, audiosys_loop_t loop ); +audiosys_loop_t audiosys_ambience_loop( audiosys_t* audiosys ); +void audiosys_ambience_volume_set( audiosys_t* audiosys, float volume ); +float audiosys_ambience_volume( audiosys_t* audiosys ); +void audiosys_ambience_pan_set( audiosys_t* audiosys, float pan ); +float audiosys_ambience_pan( audiosys_t* audiosys ); + +AUDIOSYS_U64 audiosys_sound_play( audiosys_t* audiosys, audiosys_audio_source_t source, float priority, float fade_in_time ); +void audiosys_sound_stop( audiosys_t* audiosys, AUDIOSYS_U64 handle, float fade_out_time ); +void audiosys_sound_pause( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +void audiosys_sound_resume( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +void audiosys_sound_position_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float position ); +float audiosys_sound_position( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +audiosys_audio_source_t audiosys_sound_source( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +void audiosys_sound_loop_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, audiosys_loop_t loop ); +audiosys_loop_t audiosys_sound_loop( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +void audiosys_sound_volume_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float volume ); +float audiosys_sound_volume( audiosys_t* audiosys, AUDIOSYS_U64 handle ); +void audiosys_sound_pan_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float pan ); +float audiosys_sound_pan( audiosys_t* audiosys, AUDIOSYS_U64 handle ); + + +typedef enum audiosys_sound_valid_t { + AUDIOSYS_SOUND_INVALID, + AUDIOSYS_SOUND_VALID, +} audiosys_sound_valid_t; + +audiosys_sound_valid_t audiosys_sound_valid(audiosys_t* audiosys, AUDIOSYS_U64 handle ); + +#endif /* audiosys_h */ + +/* +---------------------- + IMPLEMENTATION +---------------------- +*/ + +#ifdef AUDIOSYS_IMPLEMENTATION +#undef AUDIOSYS_IMPLEMENTATION + +#ifndef AUDIOSYS_MALLOC + #define _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_SECURE_NO_WARNINGS + #include + #define AUDIOSYS_MALLOC( ctx, size ) ( malloc( size ) ) + #define AUDIOSYS_FREE( ctx, ptr ) ( free( ptr ) ) +#endif + +#ifndef AUDIOSYS_ASSERT + #undef _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_NONSTDC_NO_DEPRECATE + #undef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #include + #define AUDIOSYS_ASSERT( expression, message ) assert( ( expression ) && ( message ) ) +#endif + +#ifndef AUDIOSYS_MEMCPY + #define _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_SECURE_NO_WARNINGS + #include + #define AUDIOSYS_MEMCPY( dst, src, cnt ) ( memcpy( (dst), (src), (cnt) ) ) +#endif + +#ifndef AUDIOSYS_MEMSET + #define _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_SECURE_NO_WARNINGS + #include + #define AUDIOSYS_MEMSET( dst, val, cnt ) ( memset( (dst), (val), (cnt) ) ) +#endif + +#ifndef AUDIOSYS_MEMMOVE + #define _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_SECURE_NO_WARNINGS + #include + #define AUDIOSYS_MEMMOVE( dst, src, cnt ) ( memmove((dst), (src), (cnt) ) ) +#endif + + +typedef struct audiosys_internal_handles_data_t { + int index; + int counter; +} audiosys_internal_handles_data_t; + + +typedef struct audiosys_internal_handles_t { + void* memctx; + struct audiosys_internal_handles_data_t* data; + int capacity; + int count; + int freelist_head; + int freelist_tail; +} audiosys_internal_handles_t; + + +static void audiosys_internal_handles_init( audiosys_internal_handles_t* handles, int initial_capacity, void* memctx ) { + handles->memctx = memctx; + handles->count = 0; + handles->capacity = initial_capacity <= 0 ? 1 : initial_capacity; + handles->freelist_head = -1; + handles->freelist_tail = -1; + handles->data = (struct audiosys_internal_handles_data_t*) AUDIOSYS_MALLOC( memctx, handles->capacity * sizeof( *handles->data ) ); + AUDIOSYS_ASSERT( handles->data, "Allocation failed" ); +} + + +static void audiosys_internal_handles_term( audiosys_internal_handles_t* handles ) { + //AUDIOSYS_FREE( handles->memctx, handles->data ); +} + + +static int audiosys_internal_handles_alloc( audiosys_internal_handles_t* handles, int index ) { + int handle; + + if( handles->count < handles->capacity ) { + handle = handles->count; + handles->data[ handles->count ].counter = 0; + ++handles->count; + } else if( handles->freelist_head >= 0 ) { + handle = handles->freelist_head; + if( handles->freelist_tail == handles->freelist_head ) handles->freelist_tail = handles->data[ handles->freelist_head ].index; + handles->freelist_head = handles->data[ handles->freelist_head ].index; + } else { + handles->capacity *= 2; + struct audiosys_internal_handles_data_t* new_data = (struct audiosys_internal_handles_data_t*) AUDIOSYS_MALLOC( handles->memctx, handles->capacity * sizeof( *handles->data ) ); + AUDIOSYS_ASSERT( new_data, "Allocation failed" ); + AUDIOSYS_MEMCPY( new_data, handles->data, handles->count * sizeof( *handles->data ) ); + AUDIOSYS_FREE( handles->memctx, handles->data ); + handles->data = new_data; + handle = handles->count; + handles->data[ handles->count ].counter = 0; + ++handles->count; + } + + handles->data[ handle ].index = index; + return handle; +} + + +static void audiosys_internal_handles_release( audiosys_internal_handles_t* handles, int handle ) { + if( handles->freelist_tail < 0 ) { + AUDIOSYS_ASSERT( handles->freelist_head < 0, "Freelist invalid" ); + handles->freelist_head = handle; + handles->freelist_tail = handle; + } else { + handles->data[ handles->freelist_tail ].index = handle; + handles->freelist_tail = handle; + } + ++handles->data[ handle ].counter; // invalidate handle via counter + handles->data[ handle ].index = -1; +} + + +static int audiosys_internal_handles_index( audiosys_internal_handles_t* handles, int handle ) { + if( handle >= 0 && handle < handles->count ) { + return handles->data[ handle ].index; + } else { + return -1; + } +} + + +static void audiosys_internal_handles_update( audiosys_internal_handles_t* handles, int handle, int index ) { + handles->data[ handle ].index = index; +} + + +static uint64_t audiosys_internal_to_u64( int handle, int counter ) { + uint64_t i = (uint64_t) ( handle + 1 ); + uint64_t c = (uint64_t) counter; + return ( c << 32ull ) | ( i ); +} + + +static int audiosys_internal_u64_to_counter( uint64_t u ) { + return (int) ( u >> 32ull ) ; +} + + +static int audiosys_internal_u64_to_handle( uint64_t u ) { + return ( (int) ( u & 0xffffffffull ) ) - 1; +} + + +static uint64_t audiosys_internal_handles_to_u64( audiosys_internal_handles_t* handles, int handle ) { + return audiosys_internal_to_u64( handle, handles->data[ handle ].counter ); +} + + +static int audiosys_internal_handles_from_u64( audiosys_internal_handles_t* handles, uint64_t u ) { + int handle = audiosys_internal_u64_to_handle( u ); + if( handle >= 0 && handle < handles->count && handles->data[ handle ].counter == audiosys_internal_u64_to_counter( u ) ) { + return handle; + } else { + return -1; + } +} + + +typedef enum audiosys_internal_voice_state_t { + AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED, + AUDIOSYS_INTERNAL_VOICE_STATE_PLAYING, + AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN, + AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT, + AUDIOSYS_INTERNAL_VOICE_STATE_CROSSFADING, + AUDIOSYS_INTERNAL_VOICE_STATE_QUEUED, +} audiosys_internal_voice_state_t; + + +typedef struct audiosys_internal_voice_t { + AUDIOSYS_U64 handle; + audiosys_internal_voice_state_t state; + audiosys_audio_source_t source; + int initialized; + int paused; + int loop; + float priority; + float volume; + float pan; + float fade_in_time; + float fade_out_time; + float fade_progress; + + float current_fade_volume; + float current_fade_delta; +} audiosys_internal_voice_t; + + +struct audiosys_t { + void* memctx; + + int use_soft_clip; + int active_voice_count; + float master_volume; + float gain; + int paused; + + audiosys_internal_voice_t music; + audiosys_internal_voice_t music_crossfade; + float music_crossfade_time; + + audiosys_internal_voice_t ambience; + audiosys_internal_voice_t ambience_crossfade; + float ambience_crossfade_time; + + int sounds_capacity; + int sounds_count; + AUDIOSYS_U64* sounds_by_priority; + audiosys_internal_voice_t* sounds; + audiosys_internal_handles_t sounds_handles; + + int mixing_buffer_size; + float* mixing_buffer; + float* sample_buffer; +}; + + +static void audiosys_internal_release_source( audiosys_audio_source_t* source ) { + if( source->release ) { + source->release( source->instance ); + } + source->instance = NULL; + source->release = NULL; + source->read_samples = NULL; + source->restart = NULL; + source->set_position = NULL; + source->get_position = NULL; +} + + +audiosys_t* audiosys_create( int active_voice_count, void* memctx ) { + audiosys_t* audiosys = (audiosys_t*) AUDIOSYS_MALLOC( memctx, sizeof( audiosys_t ) ); + AUDIOSYS_MEMSET( audiosys, 0, sizeof( audiosys_t ) ); + + audiosys->memctx = memctx; + + audiosys->use_soft_clip = true; + audiosys->active_voice_count= active_voice_count; + audiosys->master_volume = 1.0f; + audiosys->gain = 1.0f; + audiosys->paused = 0; + + audiosys->music.volume = 1.0f; + audiosys->music.loop = true; + audiosys->music_crossfade.volume = 1.0f; + audiosys->music_crossfade.loop = true; + + audiosys->ambience.volume = 1.0f; + audiosys->ambience.loop = true; + audiosys->ambience_crossfade.volume = 1.0f; + audiosys->ambience_crossfade.loop = true; + + audiosys->sounds_capacity = 64; + audiosys->sounds_count = 0; + audiosys->sounds_by_priority = (AUDIOSYS_U64*) AUDIOSYS_MALLOC( memctx, sizeof( AUDIOSYS_U64 ) * audiosys->sounds_capacity ); + AUDIOSYS_MEMSET( audiosys->sounds_by_priority, 0, sizeof( AUDIOSYS_U64 ) * audiosys->sounds_capacity ); + audiosys->sounds = (audiosys_internal_voice_t*) AUDIOSYS_MALLOC( memctx, sizeof( audiosys_internal_voice_t ) * audiosys->sounds_capacity ); + audiosys_internal_handles_init( &audiosys->sounds_handles, audiosys->sounds_capacity, memctx ); + + return audiosys; +} + + +void audiosys_destroy( audiosys_t* audiosys ) { + audiosys_internal_release_source( &audiosys->music.source ); + audiosys_internal_release_source( &audiosys->music_crossfade.source ); + audiosys_internal_release_source( &audiosys->ambience.source ); + audiosys_internal_release_source( &audiosys->ambience_crossfade.source ); + for( int i = 0; i < audiosys->sounds_count; ++i ) { + audiosys_internal_release_source( &audiosys->sounds[ i ].source ); + } + + audiosys_internal_handles_term( &audiosys->sounds_handles ); + AUDIOSYS_FREE( audiosys->memctx, audiosys->sounds_by_priority ); + AUDIOSYS_FREE( audiosys->memctx, audiosys->sounds ); + + if( audiosys->mixing_buffer ) { + AUDIOSYS_FREE( audiosys->memctx, audiosys->mixing_buffer ); + } + + if( audiosys->sample_buffer ) { + AUDIOSYS_FREE( audiosys->memctx, audiosys->sample_buffer ); + } + + AUDIOSYS_FREE( audiosys->memctx, audiosys ); +} + + +static audiosys_internal_voice_t* audiosys_internal_get_sound( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + int index = audiosys_internal_handles_index( &audiosys->sounds_handles, audiosys_internal_handles_from_u64( &audiosys->sounds_handles, handle ) ); + if( index < 0 ) { + return 0; + } + return &audiosys->sounds[ index ]; +} + + +static AUDIOSYS_U64 audiosys_internal_add_sound( audiosys_t* audiosys, audiosys_internal_voice_t** sound, float priority ) { + if( audiosys->sounds_count >= audiosys->sounds_capacity ) { + int new_capacity = audiosys->sounds_capacity * 2; + AUDIOSYS_U64* new_sounds_by_priority = (AUDIOSYS_U64*) AUDIOSYS_MALLOC( memctx, sizeof( AUDIOSYS_U64 ) * new_capacity ); + audiosys_internal_voice_t* new_sounds = (audiosys_internal_voice_t*) AUDIOSYS_MALLOC( memctx, sizeof( audiosys_internal_voice_t ) * new_capacity ); + AUDIOSYS_MEMCPY( new_sounds_by_priority, audiosys->sounds_by_priority, sizeof( AUDIOSYS_U64 ) * audiosys->sounds_count ); + AUDIOSYS_MEMCPY( new_sounds, audiosys->sounds, sizeof( audiosys_internal_voice_t ) * audiosys->sounds_count ); + AUDIOSYS_FREE( audiosys->memctx, audiosys->sounds_by_priority ); + AUDIOSYS_FREE( audiosys->memctx, audiosys->sounds ); + audiosys->sounds_capacity = new_capacity; + audiosys->sounds_by_priority = new_sounds_by_priority; + audiosys->sounds = new_sounds; + } + + int index = audiosys->sounds_count; + if( audiosys->sounds_count > 0 ) { + int min_index = 0; + int max_index = audiosys->sounds_count - 1; + while( min_index <= max_index ) { + int center = ( max_index - min_index ) / 2 + min_index; + audiosys_internal_voice_t* snd = audiosys_internal_get_sound( audiosys, audiosys->sounds_by_priority[ center ] ); + float center_prio = snd->priority; + if( center_prio <= priority ) { + max_index = center - 1; + } else { + min_index = center + 1; + } + } + index = min_index; + } + + ++audiosys->sounds_count; + *sound = &audiosys->sounds[ audiosys->sounds_count - 1 ]; + AUDIOSYS_U64 handle = audiosys_internal_handles_to_u64( &audiosys->sounds_handles, audiosys_internal_handles_alloc( &audiosys->sounds_handles, audiosys->sounds_count - 1 ) ); + + for( int i = audiosys->sounds_count - 1; i > index ; --i ) { + audiosys->sounds_by_priority[ i ] = audiosys->sounds_by_priority[ i - 1 ]; + } + + audiosys->sounds_by_priority[ index ] = handle; + return handle; +} + + +static void audiosys_internal_remove_sound( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + int index = audiosys_internal_handles_index( &audiosys->sounds_handles, audiosys_internal_handles_from_u64( &audiosys->sounds_handles, handle ) ); + if( index < 0 ) { + return; + } + + audiosys_internal_handles_release( &audiosys->sounds_handles, audiosys_internal_handles_from_u64( &audiosys->sounds_handles, handle ) ); + audiosys_internal_voice_t* sound_to_remove = &audiosys->sounds[ index ]; + audiosys_internal_release_source( &sound_to_remove->source ); + + for( int j = 0; j < audiosys->sounds_count; ++j ) { + if( audiosys->sounds_by_priority[ j ] == handle ) { + for( int i = j; i < audiosys->sounds_count - 1; ++i ) { + audiosys->sounds_by_priority[ i ] = audiosys->sounds_by_priority[ i + 1 ]; + } + break; + } + } + + audiosys_internal_voice_t* last_sound = &audiosys->sounds[ audiosys->sounds_count - 1 ]; + audiosys_internal_handles_update( &audiosys->sounds_handles, audiosys_internal_handles_from_u64( &audiosys->sounds_handles, last_sound->handle ), index ); + --audiosys->sounds_count; + + *sound_to_remove = *last_sound; +} + + +void audiosys_internal_update_fading( audiosys_internal_voice_t* voice, float delta_time ) { + if( !voice ) { + return; + } + + float fade_volume = 1.0f; + float fade_delta = 0.0f; + if( voice->state == AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT && voice->fade_out_time > 0.0f ) { + voice->fade_progress -= delta_time / voice->fade_out_time; + if( voice->fade_progress <= 0.0f ) { + voice->fade_progress = 0.0f; + voice->state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + fade_volume = 0.0f; + audiosys_internal_release_source( &voice->source ); + } else { + float volume_dec_per_second = 1.0f / voice->fade_out_time; + float volume_dec_per_sample = volume_dec_per_second / 44100.0f; + fade_volume = voice->fade_progress; + fade_volume = fade_volume < 0.0f ? 0.0f : fade_volume > 1.0f ? 1.0f : fade_volume; + fade_delta = -volume_dec_per_sample; + } + } else if( voice->state == AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN && voice->fade_in_time > 0.0f ) { + voice->fade_progress += delta_time / voice->fade_in_time; + if( voice->fade_progress >= 1.0f ) { + voice->fade_progress = 1.0f; + voice->state = AUDIOSYS_INTERNAL_VOICE_STATE_PLAYING; + fade_volume = 1.0f; + } else { + float volume_inc_per_second = 1.0f / voice->fade_in_time; + float volume_inc_per_sample = volume_inc_per_second / 44100.0f; + fade_volume = voice->fade_progress; + fade_volume = fade_volume < 0.0f ? 0.0f : fade_volume > 1.0f ? 1.0f : fade_volume; + fade_delta = volume_inc_per_sample; + } + } + + voice->current_fade_volume = fade_volume; + voice->current_fade_delta = fade_delta; +} + + +void audiosys_internal_update_from_source( audiosys_t* audiosys, audiosys_internal_voice_t* voice, float* output_sample_pairs, int sample_pairs_count ) { + int samples_to_write = sample_pairs_count; + if( !voice->initialized ) { + audiosys_internal_update_fading( voice, 0.0f ); + voice->initialized = 1; + } else { + audiosys_internal_update_fading( voice, sample_pairs_count / 44100.0f ); + } + + if( !voice->source.read_samples || voice->state == AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED ) { + AUDIOSYS_MEMSET( output_sample_pairs, 0, sample_pairs_count * sizeof( float ) * 2 ); + return; + } + + float* out = output_sample_pairs; + int count_written = voice->source.read_samples( voice->source.instance, out, samples_to_write ); + out += count_written * 2; + while( count_written < samples_to_write ) { + if( voice->loop && voice->source.restart ) { + voice->source.restart( voice->source.instance ); + int written = voice->source.read_samples( voice->source.instance, out, samples_to_write - count_written ); + out += written * 2; + count_written += written; + } else { + audiosys_internal_release_source( &voice->source ); + for( int i = 0; i < ( samples_to_write - count_written ) * 2; ++i ) { + out[ i ] = 0.0f; + } + return; + } + } +} + + +void audiosys_internal_mix_voice( audiosys_t* audiosys, audiosys_internal_voice_t* voice, float* sample_pairs, int sample_pairs_count ) { + if( !voice ) { + return; + } + + audiosys_internal_update_from_source( audiosys, voice, sample_pairs, sample_pairs_count ); + + float fade_volume = voice->current_fade_volume; + float fade_delta = voice->current_fade_delta; + + if( voice->pan < 0.0f && fade_delta != 0.0f ) { + float a = -voice->pan; + float ia = 1.0f - a; + for( int i = 0; i < sample_pairs_count; ++i ) { + float l = sample_pairs[ i * 2 + 0 ]; + float r = sample_pairs[ i * 2 + 1 ]; + float left = l + a * r; + float right = r * ia; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + fade_volume += fade_delta; + fade_volume = fade_volume < 0.0f ? 0.0f : fade_volume > 1.0f ? 1.0f : fade_volume; + } + } else if( voice->pan > 0.0f && fade_delta != 0.0f ) { + float a = voice->pan; + float ia = 1.0f - a; + for( int i = 0; i < sample_pairs_count; ++i ) { + float l = sample_pairs[ i * 2 + 0 ]; + float r = sample_pairs[ i * 2 + 1 ]; + float right = l * a + r; + float left = l * ia; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + fade_volume += fade_delta; + fade_volume = fade_volume < 0.0f ? 0.0f : fade_volume > 1.0f ? 1.0f : fade_volume; + } + } else if( voice->pan < 0.0f && fade_delta == 0.0f ) { + float a = -voice->pan; + float ia = 1.0f - a; + for( int i = 0; i < sample_pairs_count; ++i ) { + float l = sample_pairs[ i * 2 + 0 ]; + float r = sample_pairs[ i * 2 + 1 ]; + float left = l + a * r; + float right = r * ia; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + } + } else if( voice->pan > 0.0f && fade_delta == 0.0f ) { + float a = voice->pan; + float ia = 1.0f - a; + for( int i = 0; i < sample_pairs_count; ++i ) { + float l = sample_pairs[ i * 2 + 0 ]; + float r = sample_pairs[ i * 2 + 1 ]; + float right = l * a + r; + float left = l * ia; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + } + } else if( voice->pan == 0.0f && fade_delta != 0.0f ) { + for( int i = 0; i < sample_pairs_count; ++i ) { + float left = sample_pairs[ i * 2 + 0 ]; + float right = sample_pairs[ i * 2 + 1 ]; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + fade_volume += fade_delta; + fade_volume = fade_volume < 0.0f ? 0.0f : fade_volume > 1.0f ? 1.0f : fade_volume; + } + } else if( voice->pan == 0.0f && fade_delta == 0.0f ) { + for( int i = 0; i < sample_pairs_count; ++i ) { + float left = sample_pairs[ i * 2 + 0 ]; + float right = sample_pairs[ i * 2 + 1 ]; + left *= voice->volume * fade_volume * audiosys->master_volume; + right *= voice->volume * fade_volume * audiosys->master_volume; + audiosys->mixing_buffer[ i * 2 + 0 ] += left; + audiosys->mixing_buffer[ i * 2 + 1 ] += right; + } + } else { + AUDIOSYS_ASSERT( false, "Invalid mix parameters" ); + } +} + + +void audiosys_render( audiosys_t* audiosys, AUDIOSYS_S16* output_sample_pairs, int sample_pairs_count ) { + if( audiosys->paused ) { + AUDIOSYS_MEMSET( output_sample_pairs, 0, sample_pairs_count * sizeof( AUDIOSYS_S16 ) * 2 ); + return; + } + + if( audiosys->mixing_buffer_size < sample_pairs_count ) { + if( audiosys->mixing_buffer ) { + AUDIOSYS_FREE( audiosys->memctx, audiosys->mixing_buffer ); + } + if( audiosys->sample_buffer ) { + AUDIOSYS_FREE( audiosys->memctx, audiosys->sample_buffer ); + } + audiosys->mixing_buffer_size = sample_pairs_count; + audiosys->mixing_buffer = (float*) AUDIOSYS_MALLOC( audiosys->memctx, sample_pairs_count * sizeof( float ) * 2 ); + audiosys->sample_buffer = (float*) AUDIOSYS_MALLOC( audiosys->memctx, sample_pairs_count * sizeof( float ) * 2 ); + } + + // remove sounds which have finished playing + for( int i = audiosys->sounds_count - 1; i >= 0; --i ) { + audiosys_internal_voice_t* voice = &audiosys->sounds[ i ]; + if( voice->source.read_samples == NULL ) audiosys_internal_remove_sound( audiosys, voice->handle ); + } + + // clear buffer for mixing + for( int i = 0; i < sample_pairs_count * 2; ++i ) { + audiosys->mixing_buffer[ i ] = 0.0f; + } + + // mix all active voices + audiosys_internal_mix_voice( audiosys, &audiosys->music, audiosys->sample_buffer, sample_pairs_count ); + audiosys_internal_mix_voice( audiosys, &audiosys->music_crossfade, audiosys->sample_buffer, sample_pairs_count ); + audiosys_internal_mix_voice( audiosys, &audiosys->ambience, audiosys->sample_buffer, sample_pairs_count ); + audiosys_internal_mix_voice( audiosys, &audiosys->ambience_crossfade, audiosys->sample_buffer, sample_pairs_count ); + + int sounds_count = audiosys->active_voice_count; + for( int i = 0; i < sounds_count; ++i ) { + audiosys_internal_mix_voice( audiosys, audiosys_internal_get_sound( audiosys, audiosys->sounds_by_priority[ i ] ), audiosys->sample_buffer, sample_pairs_count ); + } + + if( audiosys->use_soft_clip ) { + for( int i = 0; i < sample_pairs_count * 2; ++i ) { + float s = audiosys->mixing_buffer[ i ]; + s *= audiosys->gain * 0.5f; + s = s < -1.0f ? -2.0f / 3.0f : s > 1.0f ? 2.0f / 3.0f : s - ( s * s * s ) / 3; // soft clip + s *= 32000.0f; + output_sample_pairs[ i ] = (AUDIOSYS_S16)( s ); + } + } else { + for( int i = 0; i < sample_pairs_count * 2; ++i ) { + float s = audiosys->mixing_buffer[ i ]; + s *= audiosys->gain * 0.5f; + s = s < -1.0f ? -1.0f : s > 1.0f ? 1.0f : s; + s *= 32000.0f; + output_sample_pairs[ i ] = (AUDIOSYS_S16)( s ); + } + } +} + + +void audiosys_master_volume_set( audiosys_t* audiosys, float volume ) { + audiosys->master_volume = volume < 0.0f ? 0.0f : volume > 1.0f ? 1.0f : volume; +} + + +float audiosys_master_volume( audiosys_t* audiosys ) { + return audiosys->master_volume; +} + + +void audiosys_gain_set( audiosys_t* audiosys, float gain ) { + audiosys->gain = gain; +} + + +float audiosys_gain( audiosys_t* audiosys ) { + return audiosys->gain; +} + + +void audiosys_pause( audiosys_t* audiosys ) { + audiosys->paused = 1; +} + + +void audiosys_resume( audiosys_t* audiosys ) { + audiosys->paused = 0; +} + + +void audiosys_stop_all( audiosys_t* audiosys ) { + audiosys_music_stop( audiosys, 0.0f ); + audiosys_ambience_stop( audiosys, 0.0f ); + + for( int i = 0; i < audiosys->sounds_count; ++i ) { + audiosys_internal_voice_t* sound = &audiosys->sounds[ i ]; + sound->state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + audiosys_internal_remove_sound( audiosys, sound->handle ); + audiosys_internal_release_source( &sound->source ); + } +} + + +audiosys_paused_t audiosys_paused( audiosys_t* audiosys ) { + return audiosys->paused ? AUDIOSYS_PAUSED : AUDIOSYS_NOT_PAUSED; +} + + +static void audiosys_internal_init_voice( audiosys_internal_voice_t* voice, audiosys_audio_source_t source, int is_sound ) { + voice->handle = 0; + voice->initialized = 0; + voice->paused = 0; + voice->state = AUDIOSYS_INTERNAL_VOICE_STATE_PLAYING; + voice->source = source; + if( is_sound ) { + voice->loop = 0; + voice->volume = 1.0f; + voice->pan = 0.0f; + } + voice->fade_in_time = 0.0f; + voice->fade_out_time = 0.0f; + voice->priority = 0.0f; + voice->fade_progress = 1.0f; + voice->current_fade_volume = 1.0f; + voice->current_fade_delta = 0.0f; +} + + +void audiosys_music_play( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_in_time ) { + audiosys_internal_release_source( &audiosys->music.source ); + audiosys_internal_init_voice( &audiosys->music, source, 0 ); + audiosys->music.fade_in_time = fade_in_time; + if( fade_in_time > 0.0f ) { + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN; + audiosys->music.fade_progress = 0.0f; + } +} + + +void audiosys_music_stop( audiosys_t* audiosys, float fade_out_time ) { + if( fade_out_time > 0.0f ) { + audiosys->music.fade_out_time = fade_out_time; + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + audiosys->music.fade_progress = 1.0f; + } else { + audiosys_internal_release_source( &audiosys->music.source ); + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + } +} + + +void audiosys_music_pause( audiosys_t* audiosys ) { + if( audiosys->music.paused == 1 ) { + return; + } + audiosys->music.paused = 1; +} + + +void audiosys_music_resume( audiosys_t* audiosys ) { + if( audiosys->music.paused == 0 ) { + return; + } + audiosys->music.paused = 0; +} + + +void audiosys_music_switch( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_out_time, float fade_in_time ) { + if( fade_out_time > 0.0f ) { + audiosys->music.fade_out_time = fade_out_time; + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + audiosys->music.fade_progress = 1.0f; + } else { + audiosys_internal_release_source( &audiosys->music.source ); + audiosys_music_play( audiosys, source, fade_in_time ); + return; + } + audiosys_internal_voice_t temp = audiosys->music; + audiosys->music = audiosys->music_crossfade; + audiosys->music_crossfade = temp; + + audiosys_internal_init_voice( &audiosys->music, source, 0 ); + audiosys->music.fade_in_time = fade_in_time; + if( fade_in_time > 0.0f ) { + audiosys->music.fade_progress = 0.0f; + } + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_QUEUED; +} + + +void audiosys_music_cross_fade( audiosys_t* audiosys, audiosys_audio_source_t source, float cross_fade_time ) { + if( cross_fade_time > 0.0f ) { + audiosys->music.fade_out_time = cross_fade_time; + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + audiosys->music.fade_progress = 1.0f; + } else { + audiosys_internal_release_source( &audiosys->music.source ); + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + } + audiosys_internal_voice_t temp = audiosys->music; + audiosys->music = audiosys->music_crossfade; + audiosys->music_crossfade = temp; + + audiosys_internal_init_voice( &audiosys->music, source, 0 ); + audiosys->music.fade_in_time = cross_fade_time; + if( cross_fade_time > 0.0f ) { + audiosys->music.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN; + audiosys->music.fade_progress = 0.0f; + } +} + + +void audiosys_music_position_set( audiosys_t* audiosys, float position ) { + if( !audiosys->music.source.set_position ) { + return; + } + + audiosys->music.source.set_position( audiosys->music.source.instance, (int)( position * 44100.0f ) ); +} + + +float audiosys_music_position( audiosys_t* audiosys ) { + if( !audiosys->music.source.get_position ) { + return 0.0f; + } + + return audiosys->music.source.get_position( audiosys->music.source.instance ) / 44100.0f; +} + + +audiosys_audio_source_t audiosys_music_source( audiosys_t* audiosys ) { + return audiosys->music.source; +} + + +void audiosys_music_loop_set( audiosys_t* audiosys, audiosys_loop_t loop ) { + audiosys->music.loop = loop == AUDIOSYS_LOOP_ON ? 1 : 0; +} + + +audiosys_loop_t audiosys_music_loop( audiosys_t* audiosys ) { + return audiosys->music.loop ? AUDIOSYS_LOOP_ON : AUDIOSYS_LOOP_OFF; +} + + +void audiosys_music_volume_set( audiosys_t* audiosys, float volume ) { + audiosys->music.volume = volume; +} + + +float audiosys_music_volume( audiosys_t* audiosys ) { + return audiosys->music.volume; +} + + +void audiosys_music_pan_set( audiosys_t* audiosys, float pan ) { + audiosys->music.pan = pan < -1.0f ? -1.0f : pan > 1.0f ? 1.0f : pan; +} + + +float audiosys_music_pan( audiosys_t* audiosys ) { + return audiosys->music.pan; +} + + +void audiosys_ambience_play( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_in_time ) { + audiosys_internal_release_source( &audiosys->ambience.source ); + audiosys_internal_init_voice( &audiosys->ambience, source, 0 ); + audiosys->ambience.fade_in_time = fade_in_time; + if( fade_in_time > 0.0f ) { + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN; + audiosys->ambience.fade_progress = 0.0f; + } +} + + +void audiosys_ambience_stop( audiosys_t* audiosys, float fade_out_time ) { + if( fade_out_time > 0.0f ) { + audiosys->ambience.fade_out_time = fade_out_time; + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + audiosys->ambience.fade_progress = 1.0f; + } else { + audiosys_internal_release_source( &audiosys->ambience.source ); + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + } +} + + +void audiosys_ambience_pause( audiosys_t* audiosys ) { + if( audiosys->ambience.paused == 1 ) { + return; + } + audiosys->ambience.paused = 1; +} + + +void audiosys_ambience_resume( audiosys_t* audiosys ) { + if( audiosys->ambience.paused == 0 ) { + return; + } + audiosys->ambience.paused = 0; +} + + +void audiosys_ambience_switch( audiosys_t* audiosys, audiosys_audio_source_t source, float fade_out_time, float fade_in_time ) { + if( fade_out_time > 0.0f ) { + audiosys->ambience.fade_out_time = fade_out_time; + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + } else { + audiosys_internal_release_source( &audiosys->ambience.source ); + audiosys_ambience_play( audiosys, source, fade_in_time ); + return; + } + audiosys_internal_voice_t temp = audiosys->ambience; + audiosys->ambience = audiosys->ambience_crossfade; + audiosys->ambience_crossfade = temp; + + audiosys_internal_init_voice( &audiosys->ambience, source, 0 ); + audiosys->ambience.fade_in_time = fade_in_time; + if( fade_in_time > 0.0f ) { + audiosys->ambience.fade_progress = 0.0f; + } + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_QUEUED; +} + + +void audiosys_ambience_cross_fade( audiosys_t* audiosys, audiosys_audio_source_t source, float cross_fade_time ) { + if( cross_fade_time > 0.0f ) { + audiosys->ambience.fade_out_time = cross_fade_time; + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + audiosys->ambience.fade_progress = 1.0f; + } else { + audiosys_internal_release_source( &audiosys->ambience.source ); + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + } + audiosys_internal_voice_t temp = audiosys->ambience; + audiosys->ambience = audiosys->ambience_crossfade; + audiosys->ambience_crossfade = temp; + + audiosys_internal_init_voice( &audiosys->ambience, source, 0 ); + audiosys->ambience.fade_in_time = cross_fade_time; + if( cross_fade_time > 0.0f ) { + audiosys->ambience.state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN; + audiosys->ambience.fade_progress = 0.0f; + } +} + + +void audiosys_ambience_position_set( audiosys_t* audiosys, float position ) { + if( !audiosys->ambience.source.set_position ) { + return; + } + + audiosys->ambience.source.set_position( audiosys->ambience.source.instance, (int)( position * 44100.0f ) ); +} + + +float audiosys_ambience_position( audiosys_t* audiosys ) { + if( !audiosys->ambience.source.get_position ) { + return 0.0f; + } + + return audiosys->ambience.source.get_position( audiosys->ambience.source.instance ) / 44100.0f; +} + +audiosys_audio_source_t audiosys_ambience_source( audiosys_t* audiosys ) { + return audiosys->ambience.source; +} + + +void audiosys_ambience_loop_set( audiosys_t* audiosys, audiosys_loop_t loop ) { + audiosys->ambience.loop = loop == AUDIOSYS_LOOP_ON ? 1 : 0; +} + + +audiosys_loop_t audiosys_ambience_loop( audiosys_t* audiosys ) { + return audiosys->ambience.loop ? AUDIOSYS_LOOP_ON : AUDIOSYS_LOOP_OFF; +} + + +void audiosys_ambience_volume_set( audiosys_t* audiosys, float volume ) { + audiosys->ambience.volume = volume; +} + + +float audiosys_ambience_volume( audiosys_t* audiosys ) { + return audiosys->ambience.volume; +} + + +void audiosys_ambience_pan_set( audiosys_t* audiosys, float pan ) { + audiosys->ambience.pan = pan < -1.0f ? -1.0f : pan > 1.0f ? 1.0f : pan; +} + + +float audiosys_ambience_pan( audiosys_t* audiosys ) + { + return audiosys->ambience.pan; + } + + +AUDIOSYS_U64 audiosys_sound_play( audiosys_t* audiosys, audiosys_audio_source_t source, float priority, float fade_in_time ) { + audiosys_internal_voice_t* sound; + AUDIOSYS_U64 handle = audiosys_internal_add_sound( audiosys, &sound, priority ); + audiosys_internal_init_voice( sound, source, 1 ); + sound->handle = handle; + sound->priority = priority; + sound->fade_in_time = fade_in_time; + if( fade_in_time > 0.0f ) { + sound->state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_IN; + sound->fade_progress = 0.0f; + } + return handle; +} + + +void audiosys_sound_stop( audiosys_t* audiosys, AUDIOSYS_U64 handle, float fade_out_time ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + + if( fade_out_time > 0.0f ) { + sound->fade_out_time = fade_out_time; + sound->state = AUDIOSYS_INTERNAL_VOICE_STATE_FADING_OUT; + sound->fade_progress = 1.0f; + } else { + sound->state = AUDIOSYS_INTERNAL_VOICE_STATE_STOPPED; + audiosys_internal_remove_sound( audiosys, handle ); + audiosys_internal_release_source( &sound->source ); + } +} + + +void audiosys_sound_pause( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + if( sound->paused == 1 ) { + return; + } + sound->paused = 1; +} + + +void audiosys_sound_resume( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + if( sound->paused == 0 ) { + return; + } + sound->paused = 0; +} + + +void audiosys_sound_position_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float position ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + if( !sound->source.set_position ) { + return; + } + + sound->source.set_position( sound->source.instance, (int)( position * 44100.0f ) ); +} + + +float audiosys_sound_position( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return 0.0f; + } + if( !sound->source.get_position ) { + return 0.0f; + } + + return sound->source.get_position( sound->source.instance ) / 44100.0f; +} + + +audiosys_audio_source_t audiosys_sound_source( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + audiosys_audio_source_t null_source; + AUDIOSYS_MEMSET( &null_source, 0, sizeof( null_source ) ); + return null_source; + } + return sound->source; +} + + +void audiosys_sound_loop_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, audiosys_loop_t loop ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + sound->loop = loop == AUDIOSYS_LOOP_ON ? 1 : 0; +} + + +audiosys_loop_t audiosys_sound_loop( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return AUDIOSYS_LOOP_OFF; + } + return sound->loop ? AUDIOSYS_LOOP_ON : AUDIOSYS_LOOP_OFF; +} + + +void audiosys_sound_volume_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float volume ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + sound->volume = volume; +} + + +float audiosys_sound_volume( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return 0.0f; + } + return sound->volume; +} + + +void audiosys_sound_pan_set( audiosys_t* audiosys, AUDIOSYS_U64 handle, float pan ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return; + } + sound->pan = pan < -1.0f ? -1.0f : pan > 1.0f ? 1.0f : pan; +} + + +float audiosys_sound_pan( audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return 0.0f; + } + return sound->pan; +} + + +audiosys_sound_valid_t audiosys_sound_valid(audiosys_t* audiosys, AUDIOSYS_U64 handle ) { + audiosys_internal_voice_t* sound = audiosys_internal_get_sound( audiosys, handle ); + if( !sound ) { + return AUDIOSYS_SOUND_INVALID; + } + return AUDIOSYS_SOUND_VALID; +} + +#endif /* AUDIOSYS_IMPLEMENTATION */ + +/* +------------------------------------------------------------------------------ + +This software is available under 2 licenses - you may choose the one you like. + +------------------------------------------------------------------------------ + +ALTERNATIVE A - MIT License + +Copyright (c) 2017 Mattias Gustavsson + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +------------------------------------------------------------------------------ + +ALTERNATIVE B - Public Domain (www.unlicense.org) + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +------------------------------------------------------------------------------ +*/ diff --git a/crtemu.h b/crtemu.h index a0bfbde..1c93286 100644 --- a/crtemu.h +++ b/crtemu.h @@ -21,27 +21,24 @@ before you include this file in *one* C/C++ file to create the implementation. #define CRTEMU_U64 unsigned long long #endif +typedef enum crtemu_type_t { + CRTEMU_TYPE_TV, + CRTEMU_TYPE_PC, + CRTEMU_TYPE_LITE, +} crtemu_type_t; + typedef struct crtemu_t crtemu_t; -crtemu_t* crtemu_create( void* memctx ); +crtemu_t* crtemu_create( crtemu_type_t type, void* memctx ); void crtemu_destroy( crtemu_t* crtemu ); -typedef struct crtemu_config_t - { - float curvature, scanlines, shadow_mask, separation, ghosting, noise, flicker, vignette, distortion, aspect_lock, - hpos, vpos, hsize, vsize, contrast, brightness, saturation, blur, degauss; // range -1.0f to 1.0f, default=0.0f - } crtemu_config_t; - -void crtemu_config(crtemu_t* crtemu, crtemu_config_t const* config); - void crtemu_frame( crtemu_t* crtemu, CRTEMU_U32* frame_abgr, int frame_width, int frame_height ); void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pixels_xbgr, int width, int height, CRTEMU_U32 mod_xbgr, CRTEMU_U32 border_xbgr ); void crtemu_coordinates_window_to_bitmap( crtemu_t* crtemu, int width, int height, int* x, int* y ); -void crtemu_coordinates_bitmap_to_window( crtemu_t* crtemu, int width, int height, int* x, int* y ); #endif /* crtemu_h */ @@ -207,13 +204,13 @@ void crtemu_coordinates_bitmap_to_window( crtemu_t* crtemu, int width, int heigh #endif -struct crtemu_t - { +struct crtemu_t { + crtemu_type_t type; void* memctx; - crtemu_config_t config; CRTEMU_GLuint vertexbuffer; CRTEMU_GLuint backbuffer; + CRTEMU_GLuint fbo_backbuffer; CRTEMU_GLuint accumulatetexture_a; CRTEMU_GLuint accumulatetexture_b; @@ -286,11 +283,10 @@ struct crtemu_t #ifdef CRTEMU_REPORT_SHADER_ERRORS void (CRTEMU_GLCALLTYPE* GetShaderInfoLog) (CRTEMU_GLuint shader, CRTEMU_GLsizei bufSize, CRTEMU_GLsizei *length, CRTEMU_GLchar *infoLog); #endif - }; +}; -static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* vs_source, char const* fs_source ) - { +static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* vs_source, char const* fs_source ) { #ifdef CRTEMU_REPORT_SHADER_ERRORS char error_message[ 1024 ]; #endif @@ -300,8 +296,7 @@ static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* crtemu->CompileShader( vs ); CRTEMU_GLint vs_compiled; crtemu->GetShaderiv( vs, CRTEMU_GL_COMPILE_STATUS, &vs_compiled ); - if( !vs_compiled ) - { + if( !vs_compiled ) { #ifdef CRTEMU_REPORT_SHADER_ERRORS char const* prefix = "Vertex Shader Error: "; strcpy( error_message, prefix ); @@ -312,15 +307,14 @@ static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* CRTEMU_REPORT_ERROR( error_message ); #endif return 0; - } + } CRTEMU_GLuint fs = crtemu->CreateShader( CRTEMU_GL_FRAGMENT_SHADER ); crtemu->ShaderSource( fs, 1, (char const**) &fs_source, NULL ); crtemu->CompileShader( fs ); CRTEMU_GLint fs_compiled; crtemu->GetShaderiv( fs, CRTEMU_GL_COMPILE_STATUS, &fs_compiled ); - if( !fs_compiled ) - { + if( !fs_compiled ) { #ifdef CRTEMU_REPORT_SHADER_ERRORS char const* prefix = "Fragment Shader Error: "; strcpy( error_message, prefix ); @@ -331,7 +325,7 @@ static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* CRTEMU_REPORT_ERROR( error_message ); #endif return 0; - } + } CRTEMU_GLuint prg = crtemu->CreateProgram(); @@ -342,8 +336,7 @@ static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* CRTEMU_GLint linked; crtemu->GetProgramiv( prg, CRTEMU_GL_LINK_STATUS, &linked ); - if( !linked ) - { + if( !linked ) { #ifdef CRTEMU_REPORT_SHADER_ERRORS char const* prefix = "Shader Link Error: "; strcpy( error_message, prefix ); @@ -354,15 +347,13 @@ static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* CRTEMU_REPORT_ERROR( error_message ); #endif return 0; - } - - return prg; } + return prg; +} -crtemu_t* crtemu_create( void* memctx ) - { +bool crtemu_shaders_tv( crtemu_t* crtemu ) { char const* vs_source = #ifdef CRTEMU_WEBGL "precision highp float;\n\n" @@ -606,30 +597,492 @@ crtemu_t* crtemu_create( void* memctx ) " } " ""; + crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source ); + if( crtemu->crt_shader == 0 ) return false; + + crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source ); + if( crtemu->blur_shader == 0 ) return false; + + crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source ); + if( crtemu->accumulate_shader == 0 ) return false; + + crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source ); + if( crtemu->blend_shader == 0 ) return false; + + crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source ); + if( crtemu->copy_shader == 0 ) return false; + + return true; +} + + +bool crtemu_shaders_pc( crtemu_t* crtemu ) { + char const* vs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "attribute vec4 pos;" + "varying vec2 uv;" + "" + "void main( void )" + " {" + " gl_Position = vec4( pos.xy, 0.0, 1.0 );" + " uv = pos.zw;" + " }"; + + char const* crt_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "\n" + "varying vec2 uv;\n" + "\n" + "uniform vec3 modulate;\n" + "uniform vec2 resolution;\n" + "uniform vec2 size;\n" + "uniform float time;\n" + "uniform sampler2D backbuffer;\n" + "uniform sampler2D blurbuffer;\n" + "uniform sampler2D frametexture;\n" + "uniform float use_frame;\n" + "\n" + #ifdef CRTEMU_WEBGL + // WebGL does not support GL_CLAMP_TO_BORDER so we overwrite texture2D + // with this function which emulates the clamp-to-border behavior + "vec4 texture2Dborder(sampler2D samp, vec2 tc)\n" + " {\n" + " float borderdist = .502-max(abs(.5-tc.x), abs(.5-tc.y));\n" + " float borderfade = clamp(borderdist * 400.0, 0.0, 1.0);\n" + " return texture2D( samp, tc ) * borderfade;\n" + " }\n" + "#define texture2D texture2Dborder\n" + #endif + "vec3 tsample( sampler2D samp, vec2 tc, float offs, vec2 resolution )\n" + " {\n" + " tc = tc * vec2(1.156, 1.156) - vec2( 0.078 + 0.003, 0.078 );\n" + " vec3 s = pow( abs( texture2D( samp, vec2( tc.x, 1.0-tc.y ) ).rgb), vec3( 2.2 ) );\n" + " return s*vec3(1.25);\n" + " }\n" + "\n" + "vec3 filmic( vec3 LinearColor )\n" + " {\n" + " vec3 x = max( vec3(0.0), LinearColor-vec3(0.004));\n" + " return (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);\n" + " }\n" + "\n" + "vec2 curve( vec2 uv )\n" + " {\n" + " uv = (uv - 0.5) * 2.0;\n" + " uv *= 1.1; \n" + " uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);\n" + " uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);\n" + " uv = (uv / 2.0) + 0.5;\n" + " uv = uv *0.92 + 0.04;\n" + " return uv;\n" + " }\n" + "\n" + "float rand(vec2 co)\n" + " {\n" + " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n" + " }\n" + " \n" + "void main(void)\n" + " {\n" + " /* Curve */\n" + " vec2 curved_uv = mix( curve( uv ), uv, 0.8 );\n" + " float scale = 0.04;\n" + " vec2 scuv = curved_uv*(1.0-scale)+scale/2.0+vec2(0.003, -0.001);\n" + "\n" + " /* Main color, Bleed */\n" + " vec3 col;\n" + " float x = sin(0.1*time+curved_uv.y*13.0)*sin(0.23*time+curved_uv.y*19.0)*sin(0.3+0.11*time+curved_uv.y*23.0)*0.0012;\n" + " float o =sin(gl_FragCoord.y*1.5)/resolution.x;\n" + " x+=o*0.25;\n" + " x *= 0.2;\n" + " col.r = tsample(backbuffer,vec2(x+scuv.x+0.0009*0.25,scuv.y+0.0009*0.25),resolution.y/800.0, resolution ).x+0.02;\n" + " col.g = tsample(backbuffer,vec2(x+scuv.x+0.0000*0.25,scuv.y-0.0011*0.25),resolution.y/800.0, resolution ).y+0.02;\n" + " col.b = tsample(backbuffer,vec2(x+scuv.x-0.0015*0.25,scuv.y+0.0000*0.25),resolution.y/800.0, resolution ).z+0.02;\n" + " float i = clamp(col.r*0.299 + col.g*0.587 + col.b*0.114, 0.0, 1.0 ); \n" + " i = pow( 1.0 - pow(i,2.0), 1.0 );\n" + " i = (1.0-i) * 0.85 + 0.15; \n" + "\n" + " /* Ghosting */\n" + " float ghs = 0.05;\n" + " vec3 r = tsample(blurbuffer, vec2(x-0.014*1.0, -0.027)*0.45+0.007*vec2( 0.35*sin(1.0/7.0 + 15.0*curved_uv.y + 0.9*time), \n" + " 0.35*sin( 2.0/7.0 + 10.0*curved_uv.y + 1.37*time) )+vec2(scuv.x+0.001,scuv.y+0.001),\n" + " 5.5+1.3*sin( 3.0/9.0 + 31.0*curved_uv.x + 1.70*time),resolution).xyz*vec3(0.5,0.25,0.25);\n" + " vec3 g = tsample(blurbuffer, vec2(x-0.019*1.0, -0.020)*0.45+0.007*vec2( 0.35*cos(1.0/9.0 + 15.0*curved_uv.y + 0.5*time), \n" + " 0.35*sin( 2.0/9.0 + 10.0*curved_uv.y + 1.50*time) )+vec2(scuv.x+0.000,scuv.y-0.002),\n" + " 5.4+1.3*sin( 3.0/3.0 + 71.0*curved_uv.x + 1.90*time),resolution).xyz*vec3(0.25,0.5,0.25);\n" + " vec3 b = tsample(blurbuffer, vec2(x-0.017*1.0, -0.003)*0.35+0.007*vec2( 0.35*sin(2.0/3.0 + 15.0*curved_uv.y + 0.7*time), \n" + " 0.35*cos( 2.0/3.0 + 10.0*curved_uv.y + 1.63*time) )+vec2(scuv.x-0.002,scuv.y+0.000),\n" + " 5.3+1.3*sin( 3.0/7.0 + 91.0*curved_uv.x + 1.65*time),resolution).xyz*vec3(0.25,0.25,0.5);\n" + "\n" + " col += vec3(ghs*(1.0-0.299))*pow(clamp(vec3(3.0)*r,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n" + " col += vec3(ghs*(1.0-0.587))*pow(clamp(vec3(3.0)*g,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n" + " col += vec3(ghs*(1.0-0.114))*pow(clamp(vec3(3.0)*b,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n" + "\n" + " /* Level adjustment (curves) */\n" + " col *= vec3(0.95,0.95,0.95);\n" + " col = clamp(col*1.3 + 0.75*col*col + 1.25*col*col*col*col*col,vec3(0.0),vec3(10.0));\n" + "\n" + " /* Vignette */\n" + " float vig = (0.1 + 1.0*16.0*curved_uv.x*curved_uv.y*(1.0-curved_uv.x)*(1.0-curved_uv.y));\n" + " vig = 1.3*pow(vig,0.5);\n" + " col *= vig;\n" + "\n" + " /* Scanlines */\n" + " float scans = clamp( 0.5+0.2*sin(cos(20.0*time)*0.32+curved_uv.y*size.y*1.75), 0.0, 1.0);\n" + " float s = pow(scans,0.9);\n" + " col = col * vec3(s);\n" + "\n" + " /* Vertical lines (shadow mask) */\n" + " col*=1.0-0.23*(clamp((mod(gl_FragCoord.xy.x, 3.0))/2.0,0.0,1.0));\n" + "\n" + " /* Tone map */\n" + " col = filmic( col );\n" + "\n" + " /* Noise */\n" + " //vec2 seed = floor(curved_uv*resolution.xy*vec2(0.5))/resolution.xy;\n" + " vec2 seed = curved_uv*resolution.xy;;\n" + " /* seed = curved_uv; */\n" + " col -= 0.015*pow(vec3(rand( seed +time ), rand( seed +time*2.0 ), rand( seed +time * 3.0 ) ), vec3(1.5) );\n" + "\n" + " /* Flicker */\n" + " col *= (1.0-0.004*(sin(50.0*time+curved_uv.y*2.0)*0.5+0.5));\n" + "\n" + " /* Clamp */\n" + " if (curved_uv.x < 0.0 || curved_uv.x > 1.0)\n" + " col *= 0.0;\n" + " if (curved_uv.y < 0.0 || curved_uv.y > 1.0)\n" + " col *= 0.0;\n" + " col*=modulate; \n" + " /* Frame */\n" + " vec2 fuv=vec2( uv.x, 1.0 - uv.y);\n" + " vec4 f=texture2D(frametexture, fuv);\n" + " col = mix( col, mix( max( col, 0.0), f.xyz, f.w), vec3( use_frame) );\n" + " \n" + " gl_FragColor = vec4( col, 1.0 );\n" + " }\n" + " \n" + ""; + + char const* blur_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform vec2 blur;" + "uniform sampler2D texture;" + "" + "void main( void )" + " {" + " vec4 sum = texture2D( texture, uv ) * 0.2270270270;" + " sum += texture2D(texture, vec2( uv.x - 4.0 * blur.x, uv.y - 4.0 * blur.y ) ) * 0.0162162162;" + " sum += texture2D(texture, vec2( uv.x - 3.0 * blur.x, uv.y - 3.0 * blur.y ) ) * 0.0540540541;" + " sum += texture2D(texture, vec2( uv.x - 2.0 * blur.x, uv.y - 2.0 * blur.y ) ) * 0.1216216216;" + " sum += texture2D(texture, vec2( uv.x - 1.0 * blur.x, uv.y - 1.0 * blur.y ) ) * 0.1945945946;" + " sum += texture2D(texture, vec2( uv.x + 1.0 * blur.x, uv.y + 1.0 * blur.y ) ) * 0.1945945946;" + " sum += texture2D(texture, vec2( uv.x + 2.0 * blur.x, uv.y + 2.0 * blur.y ) ) * 0.1216216216;" + " sum += texture2D(texture, vec2( uv.x + 3.0 * blur.x, uv.y + 3.0 * blur.y ) ) * 0.0540540541;" + " sum += texture2D(texture, vec2( uv.x + 4.0 * blur.x, uv.y + 4.0 * blur.y ) ) * 0.0162162162;" + " gl_FragColor = sum;" + " } " + ""; + + + char const* accumulate_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "uniform sampler2D tex1;" + "uniform float modulate;" + "" + "void main( void )" + " {" + " vec4 a = texture2D( tex0, uv ) * vec4( modulate );" + " vec4 b = texture2D( tex1, uv );" + "" + " gl_FragColor = max( a, b * 0.96 );" + " } " + ""; + + char const* blend_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "uniform sampler2D tex1;" + "uniform float modulate;" + "" + "void main( void )" + " {" + " vec4 a = texture2D( tex0, uv ) * vec4( modulate );" + " vec4 b = texture2D( tex1, uv );" + "" + " gl_FragColor = max( a, b * 0.24 );" + " } " + ""; + + char const* copy_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "" + "void main( void )" + " {" + " gl_FragColor = texture2D( tex0, uv );" + " } " + ""; + + crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source ); + if( crtemu->crt_shader == 0 ) return false; + + crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source ); + if( crtemu->blur_shader == 0 ) return false; + + crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source ); + if( crtemu->accumulate_shader == 0 ) return false; + + crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source ); + if( crtemu->blend_shader == 0 ) return false; + + crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source ); + if( crtemu->copy_shader == 0 ) return false; + + return true; +} + + +bool crtemu_shaders_lite( crtemu_t* crtemu ) { + char const* vs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "attribute vec4 pos;" + "varying vec2 uv;" + "" + "void main( void )" + " {" + " gl_Position = vec4( pos.xy, 0.0, 1.0 );" + " uv = pos.zw;" + " }"; + + char const* crt_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "\n" + "varying vec2 uv;\n" + "\n" + "uniform vec3 modulate;\n" + "uniform vec2 resolution;\n" + "uniform vec2 size;\n" + "uniform float time;\n" + "uniform sampler2D backbuffer;\n" + "uniform sampler2D blurbuffer;\n" + "uniform sampler2D frametexture;\n" + "uniform float use_frame;\n" + "\n" + /* #ifdef CRTEMU_WEBGL + // WebGL does not support GL_CLAMP_TO_BORDER so we overwrite texture2D + // with this function which emulates the clamp-to-border behavior + "vec4 texture2Dborder(sampler2D samp, vec2 tc)\n" + " {\n" + " float borderdist = .502-max(abs(.5-tc.x), abs(.5-tc.y));\n" + " float borderfade = clamp(borderdist * 400.0, 0.0, 1.0);\n" + " return texture2D( samp, tc ) * borderfade;\n" + " }\n" + "#define texture2D texture2Dborder\n" + #endif*/ + "vec3 tsample( sampler2D samp, vec2 tc )\n" + " {\n" + " vec3 s = pow( abs( texture2D( samp, vec2( tc.x, 1.0-tc.y ) ).rgb), vec3( 2.2 ) );\n" + " return s;\n" + " }\n" + "\n" + "vec3 filmic( vec3 LinearColor )\n" + " {\n" + " vec3 x = max( vec3(0.0), LinearColor-vec3(0.004));\n" + " return (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);\n" + " }\n" + "\n" + "void main(void)\n" + " {\n" + " // Main color\n" + " vec3 col;\n" + " col = mix( tsample(backbuffer,uv ), tsample(frametexture,uv ), 0.45 );\n" + " col = 3.0*col + pow( col, vec3( 3.0 ) );\n" + " col += tsample(blurbuffer,uv )*0.3;\n" + "\n" + " // Scanlines\n" + " float scans = clamp( 0.5-0.5*cos( uv.y * 6.28319 * (size.y ) ), 0.0, 1.0);\n" + " float s = pow(scans,1.3);\n" + " col = mix( col, col * vec3(s), 0.7 );\n" + "\n" + " // Vertical lines (shadow mask)\n" + " col*=1.0-0.23*(clamp((mod(gl_FragCoord.xy.x, 3.0))/2.0,0.0,1.0));\n" + "\n" + " // Vignette\n" + " float vig = (0.1 + 1.0*16.0*uv.x*uv.y*(1.0-uv.x)*(1.0-uv.y));\n" + " vig = 1.3*pow(vig,0.5);\n" + " col = mix( col, col*vig, 0.2 );\n" + "\n" + " // Tone map\n" + " col = mix( pow( col, vec3(1.0 / 2.2) ), filmic( col ), 0.5 );\n" + "\n" + " col*=modulate; \n" + " gl_FragColor = vec4( col, 1.0 );\n" + " }\n" + " \n" + ""; + + char const* blur_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform vec2 blur;" + "uniform sampler2D texture;" + "" + "void main( void )" + " {" + " vec4 sum = texture2D( texture, uv ) * 0.2270270270;" + " sum += texture2D(texture, vec2( uv.x - 4.0 * blur.x, uv.y - 4.0 * blur.y ) ) * 0.0162162162;" + " sum += texture2D(texture, vec2( uv.x - 3.0 * blur.x, uv.y - 3.0 * blur.y ) ) * 0.0540540541;" + " sum += texture2D(texture, vec2( uv.x - 2.0 * blur.x, uv.y - 2.0 * blur.y ) ) * 0.1216216216;" + " sum += texture2D(texture, vec2( uv.x - 1.0 * blur.x, uv.y - 1.0 * blur.y ) ) * 0.1945945946;" + " sum += texture2D(texture, vec2( uv.x + 1.0 * blur.x, uv.y + 1.0 * blur.y ) ) * 0.1945945946;" + " sum += texture2D(texture, vec2( uv.x + 2.0 * blur.x, uv.y + 2.0 * blur.y ) ) * 0.1216216216;" + " sum += texture2D(texture, vec2( uv.x + 3.0 * blur.x, uv.y + 3.0 * blur.y ) ) * 0.0540540541;" + " sum += texture2D(texture, vec2( uv.x + 4.0 * blur.x, uv.y + 4.0 * blur.y ) ) * 0.0162162162;" + " gl_FragColor = sum;" + " } " + ""; + + + char const* accumulate_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "uniform sampler2D tex1;" + "uniform float modulate;" + "" + "void main( void )" + " {" + " vec4 a = texture2D( tex0, uv ) * vec4( modulate );" + " vec4 b = texture2D( tex1, uv );" + "" + " gl_FragColor = max( a, b * 0.96 );" + " } " + ""; + + char const* blend_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "uniform sampler2D tex1;" + "uniform float modulate;" + "" + "void main( void )" + " {" + " vec4 a = texture2D( tex0, uv ) * vec4( modulate );" + " vec4 b = texture2D( tex1, uv );" + "" + " gl_FragColor = max( a, b * 0.2 );" + " } " + ""; + + char const* copy_fs_source = + #ifdef CRTEMU_WEBGL + "precision highp float;\n\n" + #else + "#version 120\n\n" + #endif + "" + "varying vec2 uv;" + "" + "uniform sampler2D tex0;" + "" + "void main( void )" + " {" + " gl_FragColor = texture2D( tex0, uv );" + " } " + ""; + + crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source ); + if( crtemu->crt_shader == 0 ) return false; + + crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source ); + if( crtemu->blur_shader == 0 ) return false; + + crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source ); + if( crtemu->accumulate_shader == 0 ) return false; + + crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source ); + if( crtemu->blend_shader == 0 ) return false; + + crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source ); + if( crtemu->copy_shader == 0 ) return false; + + return true; + +} + + +crtemu_t* crtemu_create( crtemu_type_t type, void* memctx ) { crtemu_t* crtemu = (crtemu_t*) CRTEMU_MALLOC( memctx, sizeof( crtemu_t ) ); memset( crtemu, 0, sizeof( crtemu_t ) ); + crtemu->type = type; crtemu->memctx = memctx; - - crtemu->config.curvature = 0.0f; - crtemu->config.scanlines = 0.0f; - crtemu->config.shadow_mask = 0.0f; - crtemu->config.separation = 0.0f; - crtemu->config.ghosting = 0.0f; - crtemu->config.noise = 0.0f; - crtemu->config.flicker = 0.0f; - crtemu->config.vignette = 0.0f; - crtemu->config.distortion = 0.0f; - crtemu->config.aspect_lock = 0.0f; - crtemu->config.hpos = 0.0f; - crtemu->config.vpos = 0.0f; - crtemu->config.hsize = 0.0f; - crtemu->config.vsize = 0.0f; - crtemu->config.contrast = 0.0f; - crtemu->config.brightness = 0.0f; - crtemu->config.saturation = 0.0f; - crtemu->config.blur = 0.0f; - crtemu->config.degauss = 0.0f; - + crtemu->use_frame = 0.0f; crtemu->last_present_width = 0; @@ -826,21 +1279,17 @@ crtemu_t* crtemu_create( void* memctx ) if( !crtemu->GetShaderInfoLog ) goto failed; #endif - crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source ); - if( crtemu->crt_shader == 0 ) goto failed; - - crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source ); - if( crtemu->blur_shader == 0 ) goto failed; - - crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source ); - if( crtemu->accumulate_shader == 0 ) goto failed; - - crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source ); - if( crtemu->blend_shader == 0 ) goto failed; - - crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source ); - if( crtemu->copy_shader == 0 ) goto failed; - + switch( type ) { + case CRTEMU_TYPE_TV: { + if( !crtemu_shaders_tv( crtemu ) ) goto failed; + } break; + case CRTEMU_TYPE_PC: { + if( !crtemu_shaders_pc( crtemu ) ) goto failed; + } break; + case CRTEMU_TYPE_LITE: { + if( !crtemu_shaders_lite( crtemu ) ) goto failed; + } break; + } crtemu->GenTextures( 1, &crtemu->accumulatetexture_a ); crtemu->GenFramebuffers( 1, &crtemu->accumulatebuffer_a ); @@ -867,6 +1316,7 @@ crtemu_t* crtemu_create( void* memctx ) crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MAG_FILTER, CRTEMU_GL_LINEAR ); crtemu->GenTextures( 1, &crtemu->backbuffer ); + crtemu->GenFramebuffers( 1, &crtemu->fbo_backbuffer ); #ifndef CRTEMU_WEBGL // This enable call is not necessary when using fragment shaders, avoid logged warnings in WebGL crtemu->Enable( CRTEMU_GL_TEXTURE_2D ); @@ -923,11 +1373,10 @@ failed: #endif CRTEMU_FREE( crtemu->memctx, crtemu ); return 0; - } +} -void crtemu_destroy( crtemu_t* crtemu ) - { +void crtemu_destroy( crtemu_t* crtemu ) { crtemu->DeleteTextures( 1, &crtemu->accumulatetexture_a ); crtemu->DeleteFramebuffers( 1, &crtemu->accumulatebuffer_a ); crtemu->DeleteTextures( 1, &crtemu->accumulatetexture_b ); @@ -943,30 +1392,26 @@ void crtemu_destroy( crtemu_t* crtemu ) FreeLibrary( crtemu->gl_dll ); #endif CRTEMU_FREE( crtemu->memctx, crtemu ); +} + + +void crtemu_frame( crtemu_t* crtemu, CRTEMU_U32* frame_abgr, int frame_width, int frame_height ) { + if( crtemu->type != CRTEMU_TYPE_LITE ) { + crtemu->ActiveTexture( CRTEMU_GL_TEXTURE3 ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->frametexture ); + crtemu->TexImage2D( CRTEMU_GL_TEXTURE_2D, 0, CRTEMU_GL_RGBA, frame_width, frame_height, 0, CRTEMU_GL_RGBA, CRTEMU_GL_UNSIGNED_BYTE, frame_abgr ); + if( frame_abgr ) { + crtemu->use_frame = 1.0f; + } else { + crtemu->use_frame = 0.0f; + } } - - -void crtemu_config(crtemu_t* crtemu, crtemu_config_t const* config) - { - crtemu->config = *config; - } - - -void crtemu_frame( crtemu_t* crtemu, CRTEMU_U32* frame_abgr, int frame_width, int frame_height ) - { - crtemu->ActiveTexture( CRTEMU_GL_TEXTURE3 ); - crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->frametexture ); - crtemu->TexImage2D( CRTEMU_GL_TEXTURE_2D, 0, CRTEMU_GL_RGBA, frame_width, frame_height, 0, CRTEMU_GL_RGBA, CRTEMU_GL_UNSIGNED_BYTE, frame_abgr ); - if( frame_abgr ) - crtemu->use_frame = 1.0f; - else - crtemu->use_frame = 0.0f; - } +} static void crtemu_internal_blur( crtemu_t* crtemu, CRTEMU_GLuint source, CRTEMU_GLuint blurbuffer_a, CRTEMU_GLuint blurbuffer_b, - CRTEMU_GLuint blurtexture_b, float r, int width, int height ) - { + CRTEMU_GLuint blurtexture_b, float r, int width, int height ) { + crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, blurbuffer_b ); crtemu->UseProgram( crtemu->blur_shader ); crtemu->Uniform2f( crtemu->GetUniformLocation( crtemu->blur_shader, "blur" ), r / (float) width, 0 ); @@ -992,17 +1437,50 @@ static void crtemu_internal_blur( crtemu_t* crtemu, CRTEMU_GLuint source, CRTEMU crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 0 ); crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, 0 ); - } +} void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pixels_xbgr, int width, int height, - CRTEMU_U32 mod_xbgr, CRTEMU_U32 border_xbgr ) - { + CRTEMU_U32 mod_xbgr, CRTEMU_U32 border_xbgr ) { + int viewport[ 4 ]; crtemu->GetIntegerv( CRTEMU_GL_VIEWPORT, viewport ); + + // Copy to backbuffer + if( pixels_xbgr ) { + crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer ); + crtemu->TexImage2D( CRTEMU_GL_TEXTURE_2D, 0, CRTEMU_GL_RGBA, width, height, 0, CRTEMU_GL_RGBA, CRTEMU_GL_UNSIGNED_BYTE, pixels_xbgr ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 0 ); + } else { + if( width != crtemu->last_present_width || height != crtemu->last_present_height ) { + crtemu->ActiveTexture( CRTEMU_GL_TEXTURE1 ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer ); + crtemu->TexImage2D( CRTEMU_GL_TEXTURE_2D, 0, CRTEMU_GL_RGB, width, height, 0, CRTEMU_GL_RGB, CRTEMU_GL_UNSIGNED_BYTE, 0 ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer ); + crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, crtemu->fbo_backbuffer ); + crtemu->FramebufferTexture2D( CRTEMU_GL_FRAMEBUFFER, CRTEMU_GL_COLOR_ATTACHMENT0, CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer, 0 ); + } + crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, crtemu->fbo_backbuffer ); + crtemu->Viewport( 0, 0, width, height ); + crtemu->UseProgram( crtemu->copy_shader ); + crtemu->Uniform1i( crtemu->GetUniformLocation( crtemu->copy_shader, "tex0" ), 0 ); + CRTEMU_GLfloat vertices[] = { + -1.0f, -1.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 0.0f, 1.0f, + }; + crtemu->BufferData( CRTEMU_GL_ARRAY_BUFFER, 4 * 4 * sizeof( CRTEMU_GLfloat ), vertices, CRTEMU_GL_STATIC_DRAW ); + crtemu->BindBuffer( CRTEMU_GL_ARRAY_BUFFER, crtemu->vertexbuffer ); + crtemu->DrawArrays( CRTEMU_GL_TRIANGLE_FAN, 0, 4 ); + crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 0 ); + crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, 0 ); + crtemu->Viewport( viewport[ 0 ], viewport[ 1 ], viewport[ 2 ], viewport[ 3 ] ); + } - if( width != crtemu->last_present_width || height != crtemu->last_present_height ) - { + if( width != crtemu->last_present_width || height != crtemu->last_present_height ) { crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->accumulatetexture_a ); @@ -1028,28 +1506,21 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, crtemu->blurbuffer_b ); crtemu->FramebufferTexture2D( CRTEMU_GL_FRAMEBUFFER, CRTEMU_GL_COLOR_ATTACHMENT0, CRTEMU_GL_TEXTURE_2D, crtemu->blurtexture_b, 0 ); crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, 0 ); - } + } crtemu->last_present_width = width; crtemu->last_present_height = height; - CRTEMU_GLfloat vertices[] = - { + CRTEMU_GLfloat vertices[] = { -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, - }; + }; crtemu->BufferData( CRTEMU_GL_ARRAY_BUFFER, 4 * 4 * sizeof( CRTEMU_GLfloat ), vertices, CRTEMU_GL_STATIC_DRAW ); crtemu->BindBuffer( CRTEMU_GL_ARRAY_BUFFER, crtemu->vertexbuffer ); - // Copy to backbuffer - crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); - crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer ); - crtemu->TexImage2D( CRTEMU_GL_TEXTURE_2D, 0, CRTEMU_GL_RGBA, width, height, 0, CRTEMU_GL_RGBA, CRTEMU_GL_UNSIGNED_BYTE, pixels_xbgr ); - crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 0 ); - crtemu->Viewport( 0, 0, width, height ); // Blur the previous accumulation buffer @@ -1109,7 +1580,8 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix // Add slight blur to backbuffer - crtemu_internal_blur( crtemu, crtemu->accumulatetexture_a, crtemu->accumulatebuffer_a, crtemu->blurbuffer_b, crtemu->blurtexture_b, 0.17f, width, height ); + float backbuffer_blur = crtemu->type == CRTEMU_TYPE_TV ? 0.17f : 0.0f; + crtemu_internal_blur( crtemu, crtemu->accumulatetexture_a, crtemu->accumulatebuffer_a, crtemu->blurbuffer_b, crtemu->blurtexture_b, backbuffer_blur, width, height ); // Create fully blurred version of backbuffer crtemu_internal_blur( crtemu, crtemu->accumulatetexture_a, crtemu->blurbuffer_a, crtemu->blurbuffer_b, crtemu->blurtexture_b, 1.0f, width, height ); @@ -1126,16 +1598,13 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix int aspect_width = (int)( ( window_height * 4 ) / 3 ); int aspect_height= (int)( ( window_width * 3 ) / 4 ); int target_width, target_height; - if( aspect_height <= window_height ) - { + if( aspect_height <= window_height ) { target_width = window_width; target_height = aspect_height; - } - else - { + } else { target_width = aspect_width; target_height = window_height; - } + } float hscale = target_width / (float) width; float vscale = target_height / (float) height; @@ -1152,13 +1621,12 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix y1 = ( y1 / window_height ) * 2.0f - 1.0f; y2 = ( y2 / window_height ) * 2.0f - 1.0f; - CRTEMU_GLfloat screen_vertices[] = - { + CRTEMU_GLfloat screen_vertices[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - }; + }; screen_vertices[ 0 ] = x1; screen_vertices[ 1 ] = y1; screen_vertices[ 4 ] = x2; @@ -1168,12 +1636,14 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix screen_vertices[ 12 ] = x1; screen_vertices[ 13 ] = y2; - crtemu->BufferData( CRTEMU_GL_ARRAY_BUFFER, 4 * 4 * sizeof( CRTEMU_GLfloat ), screen_vertices, CRTEMU_GL_STATIC_DRAW ); crtemu->BindBuffer( CRTEMU_GL_ARRAY_BUFFER, crtemu->vertexbuffer ); + crtemu->EnableVertexAttribArray( 0 ); + crtemu->VertexAttribPointer( 0, 4, CRTEMU_GL_FLOAT, CRTEMU_GL_FALSE, 4 * sizeof( CRTEMU_GLfloat ), 0 ); + crtemu->BufferData( CRTEMU_GL_ARRAY_BUFFER, 4 * 4 * sizeof( CRTEMU_GLfloat ), screen_vertices, CRTEMU_GL_STATIC_DRAW ); - float r = ( ( border_xbgr >> 16 ) & 0xff ) / 255.0f; + float b = ( ( border_xbgr >> 16 ) & 0xff ) / 255.0f; float g = ( ( border_xbgr >> 8 ) & 0xff ) / 255.0f; - float b = ( ( border_xbgr ) & 0xff ) / 255.0f; + float r = ( ( border_xbgr ) & 0xff ) / 255.0f; crtemu->ClearColor( r, g, b, 1.0f ); crtemu->Clear( CRTEMU_GL_COLOR_BUFFER_BIT ); @@ -1185,32 +1655,17 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "use_frame" ), crtemu->use_frame ); crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "time" ), 1.5f * (CRTEMU_GLfloat)( ( (double) time_us ) / 1000000.0 ) ); crtemu->Uniform2f( crtemu->GetUniformLocation( crtemu->crt_shader, "resolution" ), (float) window_width, (float) window_height ); - crtemu->Uniform2f( crtemu->GetUniformLocation( crtemu->crt_shader, "size" ), (float) target_width, (float) target_height ); + if( crtemu->type == CRTEMU_TYPE_LITE ) { + crtemu->Uniform2f( crtemu->GetUniformLocation( crtemu->crt_shader, "size" ), (float)( target_width / 2 >= width ? width : target_width / 2 ), (float) ( target_height / 2 >= height ? height : target_height / 2 ) ); + } else { + crtemu->Uniform2f( crtemu->GetUniformLocation( crtemu->crt_shader, "size" ), (float) target_width, (float) target_height ); + } float mod_r = ( ( mod_xbgr >> 16 ) & 0xff ) / 255.0f; float mod_g = ( ( mod_xbgr >> 8 ) & 0xff ) / 255.0f; float mod_b = ( ( mod_xbgr ) & 0xff ) / 255.0f; crtemu->Uniform3f( crtemu->GetUniformLocation( crtemu->crt_shader, "modulate" ), mod_r, mod_g, mod_b ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_curvature" ), crtemu->config.curvature ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_scanlines" ), crtemu->config.scanlines ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_shadow_mask" ), crtemu->config.shadow_mask ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_separation" ), crtemu->config.separation ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_ghosting" ), crtemu->config.ghosting ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_noise" ), crtemu->config.noise ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_flicker" ), crtemu->config.flicker ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_vignette" ), crtemu->config.vignette ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_distortion" ), crtemu->config.distortion ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_aspect_lock" ), crtemu->config.aspect_lock ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_hpos" ), crtemu->config.hpos ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_vpos" ), crtemu->config.vpos ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_hsize" ), crtemu->config.hsize ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_vsize" ), crtemu->config.vsize ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_contrast" ), crtemu->config.contrast ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_brightness" ), crtemu->config.brightness ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_saturation" ), crtemu->config.saturation ); - crtemu->Uniform1f( crtemu->GetUniformLocation( crtemu->crt_shader, "cfg_degauss" ), crtemu->config.degauss ); - float color[] = { 0.0f, 0.0f, 0.0f, 0.0f }; crtemu->ActiveTexture( CRTEMU_GL_TEXTURE0 ); @@ -1234,9 +1689,16 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix #endif crtemu->ActiveTexture( CRTEMU_GL_TEXTURE3 ); - crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->frametexture ); - crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MIN_FILTER, CRTEMU_GL_LINEAR ); - crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MAG_FILTER, CRTEMU_GL_LINEAR ); + if( crtemu->type == CRTEMU_TYPE_LITE ) { + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->backbuffer ); + crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MIN_FILTER, CRTEMU_GL_NEAREST ); + crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MAG_FILTER, CRTEMU_GL_NEAREST ); + } else { + crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, crtemu->frametexture ); + crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MIN_FILTER, CRTEMU_GL_LINEAR ); + crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_MAG_FILTER, CRTEMU_GL_LINEAR ); + } + crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_WRAP_S, CRTEMU_GL_CLAMP_TO_BORDER ); crtemu->TexParameteri( CRTEMU_GL_TEXTURE_2D, CRTEMU_GL_TEXTURE_WRAP_T, CRTEMU_GL_CLAMP_TO_BORDER ); #ifndef CRTEMU_WEBGL @@ -1250,77 +1712,162 @@ void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pix crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 1 ); crtemu->BindTexture( CRTEMU_GL_TEXTURE_2D, 2 ); crtemu->BindFramebuffer( CRTEMU_GL_FRAMEBUFFER, 0 ); +} + + +void crtemu_coordinates_window_to_bitmap( crtemu_t* crtemu, int width, int height, int* x, int* y ) { + switch( crtemu->type ) { + case CRTEMU_TYPE_TV: { + CRTEMU_GLint viewport[ 4 ]; + crtemu->GetIntegerv( CRTEMU_GL_VIEWPORT, viewport ); + + int window_width = viewport[ 2 ] - viewport[ 0 ]; + int window_height = viewport[ 3 ] - viewport[ 1 ]; + + int aspect_width = (int)( ( window_height * 4 ) / 3 ); + int aspect_height= (int)( ( window_width * 3 ) / 4 ); + int target_width, target_height; + if( aspect_height <= window_height ) { + target_width = window_width; + target_height = aspect_height; + } else { + target_width = aspect_width; + target_height = window_height; + } + + float hscale = target_width / (float) width; + float vscale = target_height / (float) height; + + float hborder = ( window_width - hscale * width ) / 2.0f; + float vborder = ( window_height - vscale * height ) / 2.0f; + + float xp = ( ( *x - hborder ) / hscale ) / (float) width; + float yp = ( ( *y - vborder ) / vscale ) / (float) height; + + /* TODO: Common params for shader and this */ + float xc = ( xp - 0.5f ) * 2.0f; + float yc = ( yp - 0.5f ) * 2.0f; + xc *= 1.1f; + yc *= 1.1f; + //xc *= 1.0f + powf( ( fabsf( yc ) / 5.0f ), 2.0f); + //yc *= 1.0f + powf( ( fabsf( xc ) / 4.0f ), 2.0f); + float yt = ( yc >= 0.0f ? yc : -yc ) / 5.0f; + float xt = ( xc >= 0.0f ? xc : -xc ) / 4.0f; + xc *= 1.0f + ( yt * yt ); + yc *= 1.0f + ( xt * xt ); + xc = ( xc / 2.0f ) + 0.5f; + yc = ( yc / 2.0f ) + 0.5f; + xc = xc * 0.92f + 0.04f; + yc = yc * 0.92f + 0.04f; + xp = xc * 0.6f + xp * 0.4f; + yp = yc * 0.6f + yp * 0.4f; + + xp = xp * ( 1.0f - 0.04f ) + 0.04f / 2.0f + 0.003f; + yp = yp * ( 1.0f - 0.04f ) + 0.04f / 2.0f - 0.001f; + + xp = xp * 1.025f - 0.0125f; + yp = yp * 0.92f + 0.04f; + + xp *= width; + yp *= height; + + *x = (int) ( xp ); + *y = (int) ( yp ); + } break; + case CRTEMU_TYPE_PC: { + CRTEMU_GLint viewport[ 4 ]; + crtemu->GetIntegerv( CRTEMU_GL_VIEWPORT, viewport ); + + int window_width = viewport[ 2 ] - viewport[ 0 ]; + int window_height = viewport[ 3 ] - viewport[ 1 ]; + + int aspect_width = (int)( ( window_height * 4 ) / 3 ); + int aspect_height= (int)( ( window_width * 3 ) / 4 ); + int target_width, target_height; + if( aspect_height <= window_height ) { + target_width = window_width; + target_height = aspect_height; + } else { + target_width = aspect_width; + target_height = window_height; + } + + float hscale = target_width / (float) width; + float vscale = target_height / (float) height; + + float hborder = ( window_width - hscale * width ) / 2.0f; + float vborder = ( window_height - vscale * height ) / 2.0f; + + float xp = ( ( *x - hborder ) / hscale ) / (float) width; + float yp = ( ( *y - vborder ) / vscale ) / (float) height; + + /* TODO: Common params for shader and this */ + float xc = ( xp - 0.5f ) * 2.0f; + float yc = ( yp - 0.5f ) * 2.0f; + xc *= 1.1f; + yc *= 1.1f; + //xc *= 1.0f + powf( ( fabsf( yc ) / 5.0f ), 2.0f); + //yc *= 1.0f + powf( ( fabsf( xc ) / 4.0f ), 2.0f); + float yt = ( yc >= 0.0f ? yc : -yc ) / 5.0f; + float xt = ( xc >= 0.0f ? xc : -xc ) / 4.0f; + xc *= 1.0f + ( yt * yt ); + yc *= 1.0f + ( xt * xt ); + xc = ( xc / 2.0f ) + 0.5f; + yc = ( yc / 2.0f ) + 0.5f; + xc = xc * 0.92f + 0.04f; + yc = yc * 0.92f + 0.04f; + xp = xc * 0.2f + xp * 0.8f; + yp = yc * 0.2f + yp * 0.8f; + + xp = xp * ( 1.0f - 0.04f ) + 0.04f / 2.0f + 0.003f; + yp = yp * ( 1.0f - 0.04f ) + 0.04f / 2.0f - 0.001f; + + xp = xp * 1.156f - ( 0.078f + 0.003f ); + yp = yp * 1.156f - 0.078f; + + xp *= width; + yp *= height; + + *x = (int) ( xp ); + *y = (int) ( yp ); + } break; + case CRTEMU_TYPE_LITE: { + CRTEMU_GLint viewport[ 4 ]; + crtemu->GetIntegerv( CRTEMU_GL_VIEWPORT, viewport ); + + int window_width = viewport[ 2 ] - viewport[ 0 ]; + int window_height = viewport[ 3 ] - viewport[ 1 ]; + + int aspect_width = (int)( ( window_height * 4 ) / 3 ); + int aspect_height= (int)( ( window_width * 3 ) / 4 ); + int target_width, target_height; + if( aspect_height <= window_height ) { + target_width = window_width; + target_height = aspect_height; + } else { + target_width = aspect_width; + target_height = window_height; + } + + float hscale = target_width / (float) width; + float vscale = target_height / (float) height; + + float hborder = ( window_width - hscale * width ) / 2.0f; + float vborder = ( window_height - vscale * height ) / 2.0f; + + float xp = ( ( *x - hborder ) / hscale ) / (float) width; + float yp = ( ( *y - vborder ) / vscale ) / (float) height; + + xp *= width; + yp *= height; + + *x = (int) ( xp ); + *y = (int) ( yp ); + } break; } +} -void crtemu_coordinates_window_to_bitmap( crtemu_t* crtemu, int width, int height, int* x, int* y ) - { - CRTEMU_GLint viewport[ 4 ]; - crtemu->GetIntegerv( CRTEMU_GL_VIEWPORT, viewport ); - - int window_width = viewport[ 2 ] - viewport[ 0 ]; - int window_height = viewport[ 3 ] - viewport[ 1 ]; - - int aspect_width = (int)( ( window_height * 4 ) / 3 ); - int aspect_height= (int)( ( window_width * 3 ) / 4 ); - int target_width, target_height; - if( aspect_height <= window_height ) - { - target_width = window_width; - target_height = aspect_height; - } - else - { - target_width = aspect_width; - target_height = window_height; - } - - float hscale = target_width / (float) width; - float vscale = target_height / (float) height; - - float hborder = ( window_width - hscale * width ) / 2.0f; - float vborder = ( window_height - vscale * height ) / 2.0f; - - float xp = ( ( *x - hborder ) / hscale ) / (float) width; - float yp = ( ( *y - vborder ) / vscale ) / (float) height; - - /* TODO: Common params for shader and this */ - float xc = ( xp - 0.5f ) * 2.0f; - float yc = ( yp - 0.5f ) * 2.0f; - xc *= 1.1f; - yc *= 1.1f; - //xc *= 1.0f + powf( ( fabsf( yc ) / 5.0f ), 2.0f); - //yc *= 1.0f + powf( ( fabsf( xc ) / 4.0f ), 2.0f); - float yt = ( yc >= 0.0f ? yc : -yc ) / 5.0f; - float xt = ( xc >= 0.0f ? xc : -xc ) / 4.0f; - xc *= 1.0f + ( yt * yt ); - yc *= 1.0f + ( xt * xt ); - xc = ( xc / 2.0f ) + 0.5f; - yc = ( yc / 2.0f ) + 0.5f; - xc = xc * 0.92f + 0.04f; - yc = yc * 0.92f + 0.04f; - xp = xc * 0.6f + xp * 0.4f; - yp = yc * 0.6f + yp * 0.4f; - - xp = xp * ( 1.0f - 0.04f ) + 0.04f / 2.0f + 0.003f; - yp = yp * ( 1.0f - 0.04f ) + 0.04f / 2.0f - 0.001f; - - xp = xp * 1.025f - 0.0125f; - yp = yp * 0.92f + 0.04f; - - xp *= width; - yp *= height; - - *x = (int) ( xp ); - *y = (int) ( yp ); - } - - -void crtemu_coordinates_bitmap_to_window( crtemu_t* crtemu, int width, int height, int* x, int* y ) - { - (void) crtemu, (void) width, (void) height, (void) x, (void) y; // TODO: implement - } - #endif /* CRTEMU_IMPLEMENTATION */