From 61131656f87123c109bf4c57ecae5f93751144f3 Mon Sep 17 00:00:00 2001 From: xufulong Date: Sat, 18 Sep 2021 12:07:39 +0800 Subject: [PATCH] move param to struct --- app/src/main/cpp/audio_player.cpp | 12 +++++------- app/src/main/cpp/visualizer/execute_fft.c | 13 ++++++++++--- app/src/main/cpp/visualizer/execute_fft.h | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/cpp/audio_player.cpp b/app/src/main/cpp/audio_player.cpp index 74b67ff..71ec542 100644 --- a/app/src/main/cpp/audio_player.cpp +++ b/app/src/main/cpp/audio_player.cpp @@ -287,8 +287,6 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { auto *fft_filter = static_cast(malloc(sizeof(filter_sys_t))); init_visualizer(fft_filter); - uint8_t *data = nullptr; - int nb_samples = 0; auto *output = static_cast(malloc(FFT_BUFFER_SIZE * sizeof(int16_t))); //read audio frame @@ -313,12 +311,12 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { if (got_frame > 0) { if (frame->nb_samples > 256) { - nb_samples = frame->nb_samples; - data = static_cast(malloc(frame->nb_samples * sizeof(uint8_t))); + fft_filter->nb_samples = frame->nb_samples; + fft_filter->data = static_cast(malloc(frame->nb_samples * sizeof(uint8_t))); } - if (nb_samples == frame->nb_samples) { - memcpy(data, frame->data[0], static_cast(frame->nb_samples)); - fft_once(fft_filter, data, nb_samples, output); + if (fft_filter->nb_samples == frame->nb_samples) { + memcpy(fft_filter->data, frame->data[0], static_cast(frame->nb_samples)); + fft_once(fft_filter, output); fft_callback(env, thiz, fft_method, output); } diff --git a/app/src/main/cpp/visualizer/execute_fft.c b/app/src/main/cpp/visualizer/execute_fft.c index 4672aae..7e5038b 100644 --- a/app/src/main/cpp/visualizer/execute_fft.c +++ b/app/src/main/cpp/visualizer/execute_fft.c @@ -203,14 +203,21 @@ int init_visualizer(filter_sys_t *p_sys) void release_visualizer(filter_sys_t *p_filter) { filter_sys_t *p_sys = p_filter; - free(p_sys->p_prev_s16_buff); + if (!p_sys) return; + if (p_sys->p_prev_s16_buff) { + free(p_sys->p_prev_s16_buff); + } free(&p_sys->wind_param); + if (p_sys->data) { + free(p_sys->data); + } free(p_sys); } -void fft_once(void *p_data, uint8_t *p_buffer, int nb_samples, int16_t *output) +void fft_once(void *p_data, int16_t *output) { filter_sys_t *p_sys = (filter_sys_t*)p_data; + int nb_samples = p_sys->nb_samples; fft_state *p_state = NULL; /* internal FFT data */ DEFINE_WIND_CONTEXT(wind_ctx); /* internal window data */ @@ -219,7 +226,7 @@ void fft_once(void *p_data, uint8_t *p_buffer, int nb_samples, int16_t *output) float p_output[FFT_BUFFER_SIZE]; /* Raw FFT Result */ int16_t p_buffer1[FFT_BUFFER_SIZE]; /* Buffer on which we perform the FFT (first channel) */ - float *p_buffl = (float*)p_buffer; /* Original buffer */ + float *p_buffl = (float*)p_sys->data; /* Original buffer */ int16_t *p_buffs; /* int16_t converted buffer */ int16_t *p_s16_buff; /* int16_t converted buffer */ diff --git a/app/src/main/cpp/visualizer/execute_fft.h b/app/src/main/cpp/visualizer/execute_fft.h index 3cdf482..d30f670 100644 --- a/app/src/main/cpp/visualizer/execute_fft.h +++ b/app/src/main/cpp/visualizer/execute_fft.h @@ -35,6 +35,9 @@ typedef struct /* FFT window parameters */ window_param wind_param; + + uint8_t *data; + int nb_samples; } filter_sys_t; static void *fft_thread(void *); @@ -45,7 +48,7 @@ block_t *filter_audio(filter_sys_t *p_sys, void *p_in_buf); void close_visualizer(filter_sys_t *p_filter); -void fft_once(void *p_data, uint8_t *p_buffer, int nb_samples, int16_t *output); +void fft_once(void *p_data, int16_t *output); int init_visualizer(filter_sys_t *p_sys);