aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2024-09-06 00:21:51 +0200
committerDaniil Cherednik <dan.cherednik@gmail.com>2024-09-06 00:21:51 +0200
commitc201ca5d3825f932ba66fab3f029e3eec2cdf3b3 (patch)
treeafdbc954e13b576ecf0d2402d73c0d046584ac54
parent03f74a32089ed70767ebdca3a60566dd098f9b9f (diff)
downloadlibgha-c201ca5d3825f932ba66fab3f029e3eec2cdf3b3.tar.gz
Remove resuidal cb from global ctx. Just add it into adjust_info function
-rw-r--r--include/libgha.h11
-rw-r--r--src/gha.c22
-rw-r--r--test/dtmf.c12
-rw-r--r--test/ut.c10
4 files changed, 17 insertions, 38 deletions
diff --git a/include/libgha.h b/include/libgha.h
index 56243a9..04ebc1b 100644
--- a/include/libgha.h
+++ b/include/libgha.h
@@ -16,6 +16,7 @@ extern "C"
#include <stddef.h>
typedef struct gha_ctx *gha_ctx_t;
+typedef void (*resuidal_cb_t)(FLOAT* resuidal, size_t size, void* user_ctx);
struct gha_info {
FLOAT frequency;
@@ -84,15 +85,7 @@ void gha_extract_many_simple(FLOAT* pcm, struct gha_info* info, size_t k, gha_ct
* where n is number of samples to anayze, k is number of harmonics to extract
*
*/
-int gha_adjust_info(const FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx);
-
-/*
- * Set callback to perform action on resuidal pcm signal.
- *
- * user_ctx is used to pass user context in to callback
- *
- */
-void gha_set_user_resuidal_cb(void (*cb)(FLOAT* resuidal, size_t size, void* user_ctx), void* user_ctx, gha_ctx_t ctx);
+int gha_adjust_info(const FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx, resuidal_cb_t cb, void* user_ctx);
const FLOAT* gha_get_analyzed(gha_ctx_t ctx);
diff --git a/src/gha.c b/src/gha.c
index 9c30be3..d2365c9 100644
--- a/src/gha.c
+++ b/src/gha.c
@@ -19,9 +19,6 @@ struct gha_ctx {
FLOAT* tmp_buf;
FLOAT max_magnitude;
-
- void (*resuidal_cb)(FLOAT* resuidal, size_t size, void* user_ctx);
- void* user_ctx;
};
static void gha_init_window(gha_ctx_t ctx)
@@ -42,8 +39,6 @@ gha_ctx_t gha_create_ctx(size_t size)
ctx->size = size;
ctx->max_loops = 7;
ctx->max_magnitude = 1;
- ctx->resuidal_cb = NULL;
- ctx->user_ctx = NULL;
ctx->fftr = kiss_fftr_alloc(size, 0, NULL, NULL);
if (!ctx->fftr)
@@ -91,12 +86,6 @@ void gha_set_max_magnitude(gha_ctx_t ctx, FLOAT magnitude)
ctx->max_magnitude = magnitude;
}
-void gha_set_user_resuidal_cb(void (*cb)(FLOAT* resuidal, size_t size, void* user_ctx), void* user_ctx, gha_ctx_t ctx)
-{
- ctx->user_ctx = user_ctx;
- ctx->resuidal_cb = cb;
-}
-
void gha_free_ctx(gha_ctx_t ctx)
{
free(ctx->fft_out);
@@ -398,9 +387,6 @@ void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx)
for (i = 0; i < ctx->size; i++)
pcm[i] -= ctx->tmp_buf[i] * magnitude;
-
- if (ctx->resuidal_cb)
- ctx->resuidal_cb(pcm, ctx->size, ctx->user_ctx);
}
void gha_extract_many_simple(FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx)
@@ -411,16 +397,16 @@ void gha_extract_many_simple(FLOAT* pcm, struct gha_info* info, size_t k, gha_ct
}
}
-int gha_adjust_info(const FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx)
+int gha_adjust_info(const FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx, resuidal_cb_t cb, void* user_ctx)
{
int rv = gha_adjust_info_newton_md(pcm, info, k, ctx);
- if (ctx->resuidal_cb)
- ctx->resuidal_cb(ctx->tmp_buf, ctx->size, ctx->user_ctx);
+ if (cb)
+ cb(ctx->tmp_buf, ctx->size, user_ctx);
return rv;
}
const FLOAT* gha_get_analyzed(gha_ctx_t ctx)
{
- return ctx->tmp_buf;
+ return ctx->tmp_buf;
}
diff --git a/test/dtmf.c b/test/dtmf.c
index 1a9db3f..f7dd487 100644
--- a/test/dtmf.c
+++ b/test/dtmf.c
@@ -46,8 +46,6 @@ int main(int argc, char** argv) {
memcpy(buf2, buf, sizeof(FLOAT) * len);
ctx = gha_create_ctx(len);
- FLOAT resuidal;
- gha_set_user_resuidal_cb(&calc_resuidal, &resuidal, ctx);
if (!ctx) {
fprintf(stderr, "Unable to create gha ctx\n");
free(buf);
@@ -57,7 +55,8 @@ int main(int argc, char** argv) {
struct gha_info res[2];
gha_extract_many_simple(buf, &res[0], 2, ctx);
- FLOAT resuidal_1 = resuidal;
+ FLOAT resuidal_simple;
+ calc_resuidal(buf, len, &resuidal_simple);
if (res[0].frequency > res[1].frequency) {
struct gha_info tmp;
@@ -66,10 +65,11 @@ int main(int argc, char** argv) {
memcpy(&res[1], &tmp, sizeof(struct gha_info));
}
- gha_adjust_info(buf2, res, 2, ctx);
+ FLOAT resuidal_adjusted;
+ gha_adjust_info(buf2, res, 2, ctx, &calc_resuidal, &resuidal_adjusted);
- if (resuidal > resuidal_1) {
- fprintf(stderr, "gha_adjust_info wrong result\n");
+ if (resuidal_adjusted > resuidal_simple) {
+ fprintf(stderr, "gha_adjust_info wrong result: %f %f\n", resuidal_simple, resuidal_adjusted);
return 1;
}
diff --git a/test/ut.c b/test/ut.c
index 773195a..9f17a8d 100644
--- a/test/ut.c
+++ b/test/ut.c
@@ -71,7 +71,7 @@ FCT_BGN()
UT_CHECK_EQ_FLOAT(res.magnitude, 1.0);
UT_CHECK_EQ_FLOAT(res.frequency, 1.5707963705);
- gha_adjust_info(buf, &res, 1, ctx);
+ gha_adjust_info(buf, &res, 1, ctx, NULL, NULL);
fprintf(stderr, "Result: freq: %.10f, phase: %f, magn: %f\n", res.frequency, res.phase, res.magnitude);
fct_chk_eq_int(0, compare_phase(0, res.phase, 0.01));
UT_CHECK_EQ_FLOAT(res.magnitude, 1.0);
@@ -97,7 +97,7 @@ FCT_BGN()
UT_CHECK_EQ_FLOAT(res.magnitude, 32768.0);
UT_CHECK_EQ_FLOAT(res.frequency, 1.5707963705);
- gha_adjust_info(buf, &res, 1, ctx);
+ gha_adjust_info(buf, &res, 1, ctx, NULL, NULL);
fct_chk_eq_int(0, compare_phase(0, res.phase, 0.01));
UT_CHECK_EQ_FLOAT(res.magnitude, 32768.0);
@@ -122,7 +122,7 @@ FCT_BGN()
gen(11025.0, 32768, buf, 128);
- gha_adjust_info(buf, &res, 1, ctx);
+ gha_adjust_info(buf, &res, 1, ctx, NULL, NULL);
fct_chk_eq_int(0, compare_phase(0, res.phase, 0.01));
UT_CHECK_EQ_FLOAT(res.magnitude, 32768.0);
@@ -152,7 +152,7 @@ FCT_BGN()
gen(11025.0, 32768, buf, 128);
gen(5000.0, 32768, buf, 128);
- gha_adjust_info(buf, res, 2, ctx);
+ gha_adjust_info(buf, res, 2, ctx, NULL, NULL);
fct_chk_eq_int(0, compare_phase(0, res[0].phase, 0.01));
UT_CHECK_EQ_FLOAT(round(res[0].magnitude), 32768.0);
@@ -186,7 +186,7 @@ FCT_BGN()
gen(11025.0, 16384, buf, 128);
gen(5512.5, 32768, buf, 128);
- gha_adjust_info(buf, res, 2, ctx);
+ gha_adjust_info(buf, res, 2, ctx, NULL, NULL);
fct_chk_eq_int(0, compare_phase(0, res[0].phase, 0.01));
UT_CHECK_EQ_FLOAT(round(res[0].magnitude), 16384.0);