added delta time

- updated readme requirements
- changed audio capture name
This commit is contained in:
2026-02-24 05:49:52 -06:00
parent cdd60c0036
commit b264506817
5 changed files with 41 additions and 20 deletions

View File

@@ -6,13 +6,18 @@ This is a small C SDL2 project making physics objects react to audio
## Requirements ## Requirements
- pipewire
- qpwgraph (optional. Makes it easier to force the audio capture input)
- clang
- pkg-config
### libraries
- SDL2 - SDL2
- SDL2_gfx - SDL2_gfx
- libpipewire - libpipewire
- libspa-2.0 - libspa-2.0
- clang
- libgcc - libgcc
- pkg-config
## building ## building
@@ -21,10 +26,10 @@ there is no install
## usage ## 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. 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 ## Todo

View File

@@ -15,7 +15,6 @@ bool running=true;
#define PROJECT_NAME "audio reactive" #define PROJECT_NAME "audio reactive"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
@@ -35,10 +34,10 @@ int main(int argc, char *argv[])
VOL_ARG volargs={ argc, argv, }; VOL_ARG volargs={ argc, argv, };
pthread_t vol_thread; 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; 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)); srand(time(NULL));
OBJECT_INER obj_storage[obj_n]; OBJECT_INER obj_storage[obj_n];
@@ -65,6 +64,7 @@ int main(int argc, char *argv[])
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
bool quit = false; bool quit = false;
Uint32 lastUpdate = 0;
SDL_Event e; SDL_Event e;
while (!quit) { while (!quit) {
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
@@ -88,9 +88,18 @@ int main(int argc, char *argv[])
stuff.avg_peak=mutVolume.avg_peak; stuff.avg_peak=mutVolume.avg_peak;
mutVolume.locked=false; mutVolume.locked=false;
} }
Uint64 start = SDL_GetPerformanceCounter();
//SDL_Log("vol: %f avg: %f",stuff.volume,stuff.avg_peak); //SDL_Log("vol: %f avg: %f",stuff.volume,stuff.avg_peak);
Uint32 current=SDL_GetTicks();
stuff.dt = (current - lastUpdate);
draw(&stuff); 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; running=false;

View File

@@ -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); 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_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){ 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_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}; obj->acc = (Point){0,0};
} }
@@ -74,7 +79,7 @@ float calcG(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){
G=5; G=5;
} }
if(G!=G) G=1; if(G!=G) G=5;
return G; return G;
} }
@@ -85,10 +90,10 @@ void attract(ATTRACTOR *attractor, OBJECT_INER *obj,STUFFS *stuff){
dist = constrain(dist,100,1000); dist = constrain(dist,100,1000);
float G = calcG(attractor,obj,stuff); 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); point_set_mag(&force, strength);
applyForce(obj,force); applyForce(stuff,obj,force);
} }
void atr_show(STUFFS *stuff,ATTRACTOR *attr){ void atr_show(STUFFS *stuff,ATTRACTOR *attr){
@@ -96,8 +101,8 @@ void atr_show(STUFFS *stuff,ATTRACTOR *attr){
} }
void draw(STUFFS *stuff){ void draw(STUFFS *stuff){
SDL_SetRenderDrawColor(stuff->renderer,0,0,0,30); SDL_SetRenderDrawColor(stuff->renderer,0,0,0,50);
//SDL_RenderClear(renderer); //SDL_RenderClear(stuff->renderer);
SDL_RenderFillRect(stuff->renderer, &(SDL_Rect){0,0,stuff->width,stuff->height}); SDL_RenderFillRect(stuff->renderer, &(SDL_Rect){0,0,stuff->width,stuff->height});
static int frame_i1=0; static int frame_i1=0;
@@ -107,16 +112,17 @@ void draw(STUFFS *stuff){
attract(&stuff->obj.attractor, stuff->obj.objs[i], stuff); attract(&stuff->obj.attractor, stuff->obj.objs[i], stuff);
float vol=stuff->volume; 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 rand_p={RandomFloat(-1, 1),RandomFloat(-1, 1)};
point_set_mag(&rand_p, stuff->volume*100); 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); point_add(&stuff->obj.objs[i]->vel,&rand_p);
} }
} }
SDL_SetRenderDrawColor(stuff->renderer, 100,20,20,180); SDL_SetRenderDrawColor(stuff->renderer, 100,20,20,180);
atr_show(stuff,&stuff->obj.attractor); atr_show(stuff,&stuff->obj.attractor);
if(frame_i1>200) frame_i1=0; if(frame_i1>100) frame_i1=0;
frame_i1++; frame_i1++;
SDL_RenderPresent(stuff->renderer); SDL_RenderPresent(stuff->renderer);
} }

View File

@@ -47,6 +47,7 @@ typedef struct{
OBJECT obj; OBJECT obj;
flaot volume; flaot volume;
flaot avg_peak; flaot avg_peak;
Uint32 dt;
} STUFFS; } STUFFS;
#endif #endif

View File

@@ -180,7 +180,7 @@ void *volume_input(void* threadarg){
data.stream = pw_stream_new_simple( data.stream = pw_stream_new_simple(
pw_main_loop_get_loop(data.loop), pw_main_loop_get_loop(data.loop),
"audio-capture", "audio_reactive",
props, props,
&stream_events, &stream_events,
&data); &data);