From b264506817f65660d57a7cdf24fcdef85a2f473e Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Tue, 24 Feb 2026 05:49:52 -0600 Subject: [PATCH] added delta time - updated readme requirements - changed audio capture name --- README.md | 13 +++++++++---- src/main.c | 17 +++++++++++++---- src/objects.c | 28 +++++++++++++++++----------- src/objects.h | 1 + src/pipe.c | 2 +- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 86789bf..c54ca3b 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,18 @@ This is a small C SDL2 project making physics objects react to audio ## Requirements +- pipewire +- qpwgraph (optional. Makes it easier to force the audio capture input) +- clang +- pkg-config + +### libraries + - SDL2 - SDL2_gfx - libpipewire - libspa-2.0 -- clang - libgcc -- pkg-config ## building @@ -21,10 +26,10 @@ there is no install ## usage -You can specify the reading device. I have no clue how it works. +You can specify the reading device in the args. I have no clue how it works. Just run audio_reactive binary. -Then use something like qpwgraph to to force change the input of ``audio-capture`` +Then use something like qpwgraph to to force change the input of ``audio_reactive`` ## Todo diff --git a/src/main.c b/src/main.c index 6e3dd89..c5439e5 100755 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,6 @@ bool running=true; #define PROJECT_NAME "audio reactive" - int main(int argc, char *argv[]) { if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) @@ -35,10 +34,10 @@ int main(int argc, char *argv[]) VOL_ARG volargs={ argc, argv, }; pthread_t vol_thread; - int thread_err=pthread_create(&vol_thread, NULL, volume_input, (void *)&volargs); + pthread_create(&vol_thread, NULL, volume_input, (void *)&volargs); pthread_t peak_reduc; - int thread_err2=pthread_create(&peak_reduc, NULL, avg_peak, NULL); + pthread_create(&peak_reduc, NULL, avg_peak, NULL); srand(time(NULL)); OBJECT_INER obj_storage[obj_n]; @@ -65,6 +64,7 @@ int main(int argc, char *argv[]) SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); bool quit = false; + Uint32 lastUpdate = 0; SDL_Event e; while (!quit) { while (SDL_PollEvent(&e)) { @@ -88,9 +88,18 @@ int main(int argc, char *argv[]) stuff.avg_peak=mutVolume.avg_peak; mutVolume.locked=false; } + + Uint64 start = SDL_GetPerformanceCounter(); //SDL_Log("vol: %f avg: %f",stuff.volume,stuff.avg_peak); + Uint32 current=SDL_GetTicks(); + stuff.dt = (current - lastUpdate); draw(&stuff); - SDL_Delay(5); + lastUpdate=current; + + Uint64 end = SDL_GetPerformanceCounter(); + float elapsedMS = (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f; + SDL_Delay(floor(16.666f - elapsedMS)); + //SDL_Delay(rand() % 25); } running=false; diff --git a/src/objects.c b/src/objects.c index 1107c1d..ccc9e25 100755 --- a/src/objects.c +++ b/src/objects.c @@ -31,15 +31,20 @@ void init_stuffs(int w, int h, OBJECT_INER *objs,STUFFS *stuff){ attractor_constructor(w,h,&stuff->obj.attractor,(float)w/2,(float)h/2,100); } -void applyForce(OBJECT_INER *obj, Point force){ +void applyForce(STUFFS *stuff, OBJECT_INER *obj, Point force){ point_div(&force, &(Point){obj->mass,obj->mass}); - point_add(&obj->acc, &force); + Point add_vec=force; + point_add(&obj->acc, &add_vec); } void update(STUFFS *stuff, OBJECT_INER *obj){ - point_add(&obj->vel,&obj->acc); + Point add_vec=obj->acc; + point_add(&obj->vel,&add_vec); point_limit(&obj->vel,10); - point_add(&obj->pos, &obj->vel); + + add_vec=obj->vel; + point_mul(&add_vec,&(Point){(float)stuff->dt/10,(float)stuff->dt/10}); + point_add(&obj->pos, &add_vec); obj->acc = (Point){0,0}; } @@ -74,7 +79,7 @@ float calcG(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){ G=5; } - if(G!=G) G=1; + if(G!=G) G=5; return G; } @@ -85,10 +90,10 @@ void attract(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){ dist = constrain(dist,100,1000); float G = calcG(attractor,obj,stuff); - float strength = G * (attractor->mass * obj->mass) / dist; + float strength = ((float)stuff->dt/10) * G * (attractor->mass * obj->mass) / dist; point_set_mag(&force, strength); - applyForce(obj,force); + applyForce(stuff,obj,force); } void atr_show(STUFFS *stuff,ATTRACTOR *attr){ @@ -96,8 +101,8 @@ void atr_show(STUFFS *stuff,ATTRACTOR *attr){ } void draw(STUFFS *stuff){ - SDL_SetRenderDrawColor(stuff->renderer,0,0,0,30); - //SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(stuff->renderer,0,0,0,50); + //SDL_RenderClear(stuff->renderer); SDL_RenderFillRect(stuff->renderer, &(SDL_Rect){0,0,stuff->width,stuff->height}); static int frame_i1=0; @@ -107,16 +112,17 @@ void draw(STUFFS *stuff){ attract(&stuff->obj.attractor, stuff->obj.objs[i], stuff); float vol=stuff->volume; - if(vol==vol && frame_i1>200){ + if(vol==vol && frame_i1>100){ Point rand_p={RandomFloat(-1, 1),RandomFloat(-1, 1)}; point_set_mag(&rand_p, stuff->volume*100); + point_mul(&rand_p,&(Point){(float)stuff->dt/10,(float)stuff->dt/10}); point_add(&stuff->obj.objs[i]->vel,&rand_p); } } SDL_SetRenderDrawColor(stuff->renderer, 100,20,20,180); atr_show(stuff,&stuff->obj.attractor); - if(frame_i1>200) frame_i1=0; + if(frame_i1>100) frame_i1=0; frame_i1++; SDL_RenderPresent(stuff->renderer); } diff --git a/src/objects.h b/src/objects.h index d1faa02..ecbda75 100755 --- a/src/objects.h +++ b/src/objects.h @@ -47,6 +47,7 @@ typedef struct{ OBJECT obj; flaot volume; flaot avg_peak; + Uint32 dt; } STUFFS; #endif diff --git a/src/pipe.c b/src/pipe.c index 60824ea..d73c657 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -180,7 +180,7 @@ void *volume_input(void* threadarg){ data.stream = pw_stream_new_simple( pw_main_loop_get_loop(data.loop), - "audio-capture", + "audio_reactive", props, &stream_events, &data);