Skip to content

Commit

Permalink
Updated to latest version, added audiosys.h
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasgustavsson committed Feb 29, 2024
1 parent e9672c5 commit ae4f56f
Show file tree
Hide file tree
Showing 4 changed files with 2,045 additions and 215 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++.
Expand Down
22 changes: 12 additions & 10 deletions app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 : "" ) ;
Expand Down Expand Up @@ -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;
Expand All @@ -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),
Expand All @@ -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;
Expand All @@ -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;
})


Expand All @@ -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 )
Expand All @@ -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 );
}
Expand Down
Loading

0 comments on commit ae4f56f

Please sign in to comment.