diff --git a/app/src/main/cpp/audio_player.cpp b/app/src/main/cpp/audio_player.cpp index 7af1b48..74b67ff 100644 --- a/app/src/main/cpp/audio_player.cpp +++ b/app/src/main/cpp/audio_player.cpp @@ -285,11 +285,11 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { jmethodID fft_method = env->GetMethodID(player_class, "fftCallbackFromJNI", "([S)V"); - filter_sys_t *fft_filter = static_cast(malloc(sizeof(filter_sys_t))); + auto *fft_filter = static_cast(malloc(sizeof(filter_sys_t))); init_visualizer(fft_filter); - auto *block = static_cast(malloc(sizeof(block_t))); - block->i_nb_samples = 0; - int16_t *output = static_cast(malloc(FFT_BUFFER_SIZE * sizeof(int16_t))); + uint8_t *data = nullptr; + int nb_samples = 0; + auto *output = static_cast(malloc(FFT_BUFFER_SIZE * sizeof(int16_t))); //read audio frame while (av_read_frame(pFormatCtx, packet) >= 0 && !filter_release) { @@ -312,14 +312,13 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { } if (got_frame > 0) { - if (block->i_nb_samples == 0 && frame->nb_samples > 100) { - auto *data = static_cast(malloc(frame->nb_samples * sizeof(uint8_t))); - block->p_buffer = data; - block->i_nb_samples = static_cast(frame->nb_samples); + if (frame->nb_samples > 256) { + nb_samples = frame->nb_samples; + data = static_cast(malloc(frame->nb_samples * sizeof(uint8_t))); } - if (block->i_nb_samples == frame->nb_samples) { - memcpy(block->p_buffer, frame->data[0], static_cast(frame->nb_samples)); - fft_once(fft_filter, block, output); + if (nb_samples == frame->nb_samples) { + memcpy(data, frame->data[0], static_cast(frame->nb_samples)); + fft_once(fft_filter, data, nb_samples, 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 a96c8ca..4672aae 100644 --- a/app/src/main/cpp/visualizer/execute_fft.c +++ b/app/src/main/cpp/visualizer/execute_fft.c @@ -208,7 +208,7 @@ void release_visualizer(filter_sys_t *p_filter) free(p_sys); } -void fft_once(void *p_data, block_t *block, int16_t *output) +void fft_once(void *p_data, uint8_t *p_buffer, int nb_samples, int16_t *output) { filter_sys_t *p_sys = (filter_sys_t*)p_data; @@ -219,31 +219,31 @@ void fft_once(void *p_data, block_t *block, 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*)block->p_buffer; /* Original buffer */ + float *p_buffl = (float*)p_buffer; /* Original buffer */ int16_t *p_buffs; /* int16_t converted buffer */ int16_t *p_s16_buff; /* int16_t converted buffer */ - if (!block->i_nb_samples) { + if (!nb_samples) { LOGE("no samples yet..."); goto release; } /* Allocate the buffer only if the number of samples change */ - if (block->i_nb_samples != p_sys->i_prev_nb_samples) + if (nb_samples != p_sys->i_prev_nb_samples) { free(p_sys->p_prev_s16_buff); - p_sys->p_prev_s16_buff = malloc(block->i_nb_samples * + p_sys->p_prev_s16_buff = malloc(nb_samples * p_sys->i_channels * sizeof(int16_t)); if (!p_sys->p_prev_s16_buff) goto release; - p_sys->i_prev_nb_samples = block->i_nb_samples; + p_sys->i_prev_nb_samples = nb_samples; } p_buffs = p_s16_buff = p_sys->p_prev_s16_buff; /* Convert the buffer to int16_t */ - for (i = block->i_nb_samples * p_sys->i_channels; i--;) + for (i = nb_samples * p_sys->i_channels; i--;) { union {float f; int32_t i;} u; @@ -275,14 +275,14 @@ void fft_once(void *p_data, block_t *block, int16_t *output) p_buffer1[i] = *p_buffs; p_buffs += p_sys->i_channels; - if (p_buffs >= &p_s16_buff[block->i_nb_samples * p_sys->i_channels]) + if (p_buffs >= &p_s16_buff[nb_samples * p_sys->i_channels]) p_buffs = p_s16_buff; } window_scale_in_place (p_buffer1, &wind_ctx); fft_perform (p_buffer1, p_output, p_state); - for (i = 0; i< FFT_BUFFER_SIZE; ++i) - output[i] = p_output[i] * (2 ^ 16) + for (i = 0; i < FFT_BUFFER_SIZE; ++i) + output[i] = p_output[i] * (2 ^ 16) / ((FFT_BUFFER_SIZE / 2 * 32768) ^ 2); LOGE("out[100]=%d,out[101]=%d,out[102]=%d", output[100], output[101], output[102]); diff --git a/app/src/main/cpp/visualizer/execute_fft.h b/app/src/main/cpp/visualizer/execute_fft.h index bc63825..078f13c 100644 --- a/app/src/main/cpp/visualizer/execute_fft.h +++ b/app/src/main/cpp/visualizer/execute_fft.h @@ -45,7 +45,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, block_t *block, int16_t *output); +void fft_once(void *p_data, uint8_t *p_buffer, int nb_samples, int16_t *output); int init_visualizer(filter_sys_t *p_sys);