From 309af8f59e10dc4b4e18fd9864b929bd99e81ffd Mon Sep 17 00:00:00 2001 From: mid <> Date: Sat, 28 Jun 2025 13:17:58 +0300 Subject: [PATCH] stopsmooth --- src/k3mix.c | 19 ++++++++++++++++++- src/k3mix.h | 2 ++ src/luaapi.c | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/k3mix.c b/src/k3mix.c index 7d3067b..a62f0b2 100644 --- a/src/k3mix.c +++ b/src/k3mix.c @@ -161,12 +161,13 @@ static void queue_clone(struct k3MixWave *this, struct k3MixWave *new) { new->clone = queue_clone; new->close = queue_close; + new->end = this->end; new->loop = this->loop; new->dam = this->dam; new->volume = this->volume; } struct k3MixWave *k3MixQueue() { - struct k3MixWave *ret = malloc(sizeof(*ret)); + struct k3MixWave *ret = calloc(1, sizeof(*ret)); ret->refs = 1; ret->sampleRate = FinalSampleRate; ret->channels = FinalChannels; @@ -182,6 +183,7 @@ struct k3MixWave *k3MixQueue() { ret->loop = 0; ret->dam = 0; ret->volume = 1; + ret->end = false; return ret; } @@ -260,6 +262,7 @@ static void power_measurement_clone(struct k3MixWave *this, struct k3MixWave *ne new->clone = power_measurement_clone; new->close = power_measurement_close; + new->end = this->end; new->loop = this->loop; new->dam = this->dam; new->volume = this->volume; @@ -308,6 +311,10 @@ struct k3MixWave *k3MixPowerMeasurement(struct k3MixWave *child) { static size_t playingCount, playingCapacity; static struct k3MixWave **playings; +void k3MixStopSmooth(struct k3MixWave *wav) { + wav->fade = -0.01; +} + void k3MixStop(struct k3MixWave *wav) { for(size_t i = 0; i < playingCount; i++) { if(playings[i] == wav) { @@ -346,6 +353,16 @@ __attribute__((optimize("Ofast"))) static void k3MixDoYourThang(size_t sampleCou for(size_t i = 0; i < playingCount;) { intmax_t read = playings[i]->read(playings[i], sampleCount, FinalData); + if(playings[i]->fade) { + playings[i]->volume += playings[i]->fade; + if(playings[i]->volume > 1) { + playings[i]->volume = 1; + playings[i]->fade = 0; + } else if(playings[i]->volume < 0) { + playings[i]->end = true; + } + } + if(playings[i]->end) { k3MixStop(playings[i]); } else i++; diff --git a/src/k3mix.h b/src/k3mix.h index 1a45408..369c849 100644 --- a/src/k3mix.h +++ b/src/k3mix.h @@ -29,6 +29,7 @@ struct k3MixWave { bool end; uint16_t dam; float volume; + float fade; }; void k3MixInit(uint32_t sampleRate, uint8_t channels); @@ -44,6 +45,7 @@ struct k3MixWave *k3MixPowerMeasurement(struct k3MixWave *child); float k3MixPowerMeasurementGetRMS(struct k3MixWave*); void k3MixStop(struct k3MixWave*); +void k3MixStopSmooth(struct k3MixWave*); struct k3MixWave *k3MixPlay(struct k3MixWave*); void k3MixPlayDirect(struct k3MixWave*); diff --git a/src/luaapi.c b/src/luaapi.c index 46f8abe..b86cf2d 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -17,6 +17,8 @@ #include"net_hi.h" #include"k3particles.h" #include +#include"ssort.h" +#include /* * This is by far the least clean or well-maintained source in the @@ -722,6 +724,17 @@ static int dagame_mixstop(lua_State *L) { return 1; } +static int dagame_mixstopsmooth(lua_State *L) { + struct mixitem *i = lua_touserdata(L, 1); + + assert(i->type != MIXITEM_SOURCE); + k3MixStopSmooth(i->thing); + + lua_pushvalue(L, 1); + + return 1; +} + static int dagame_mixpower(lua_State *L) { struct mixitem *i = lua_touserdata(L, 1); @@ -2755,6 +2768,9 @@ void luaapi_init() { lua_pushcfunction(L, dagame_mixstop); lua_setfield(L, -2, "stop"); + lua_pushcfunction(L, dagame_mixstopsmooth); + lua_setfield(L, -2, "stopsmooth"); + lua_pushcfunction(L, game_mixqueue); lua_setfield(L, -2, "queue");