replace struct with params

dev
xufulong 3 years ago
parent dff47a5a5c
commit 43f68741c3
  1. 21
      app/src/main/cpp/audio_player.cpp
  2. 20
      app/src/main/cpp/visualizer/execute_fft.c
  3. 2
      app/src/main/cpp/visualizer/execute_fft.h

@ -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"); jmethodID fft_method = env->GetMethodID(player_class, "fftCallbackFromJNI", "([S)V");
filter_sys_t *fft_filter = static_cast<filter_sys_t *>(malloc(sizeof(filter_sys_t))); auto *fft_filter = static_cast<filter_sys_t *>(malloc(sizeof(filter_sys_t)));
init_visualizer(fft_filter); init_visualizer(fft_filter);
auto *block = static_cast<block_t *>(malloc(sizeof(block_t))); uint8_t *data = nullptr;
block->i_nb_samples = 0; int nb_samples = 0;
int16_t *output = static_cast<int16_t *>(malloc(FFT_BUFFER_SIZE * sizeof(int16_t))); auto *output = static_cast<int16_t *>(malloc(FFT_BUFFER_SIZE * sizeof(int16_t)));
//read audio frame //read audio frame
while (av_read_frame(pFormatCtx, packet) >= 0 && !filter_release) { 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 (got_frame > 0) {
if (block->i_nb_samples == 0 && frame->nb_samples > 100) { if (frame->nb_samples > 256) {
auto *data = static_cast<uint8_t *>(malloc(frame->nb_samples * sizeof(uint8_t))); nb_samples = frame->nb_samples;
block->p_buffer = data; data = static_cast<uint8_t *>(malloc(frame->nb_samples * sizeof(uint8_t)));
block->i_nb_samples = static_cast<unsigned int>(frame->nb_samples);
} }
if (block->i_nb_samples == frame->nb_samples) { if (nb_samples == frame->nb_samples) {
memcpy(block->p_buffer, frame->data[0], static_cast<size_t>(frame->nb_samples)); memcpy(data, frame->data[0], static_cast<size_t>(frame->nb_samples));
fft_once(fft_filter, block, output); fft_once(fft_filter, data, nb_samples, output);
fft_callback(env, thiz, fft_method, output); fft_callback(env, thiz, fft_method, output);
} }

@ -208,7 +208,7 @@ void release_visualizer(filter_sys_t *p_filter)
free(p_sys); 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; 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 */ float p_output[FFT_BUFFER_SIZE]; /* Raw FFT Result */
int16_t p_buffer1[FFT_BUFFER_SIZE]; /* Buffer on which we perform int16_t p_buffer1[FFT_BUFFER_SIZE]; /* Buffer on which we perform
the FFT (first channel) */ 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_buffs; /* int16_t converted buffer */
int16_t *p_s16_buff; /* 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..."); LOGE("no samples yet...");
goto release; goto release;
} }
/* Allocate the buffer only if the number of samples change */ /* 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); 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 * p_sys->i_channels *
sizeof(int16_t)); sizeof(int16_t));
if (!p_sys->p_prev_s16_buff) if (!p_sys->p_prev_s16_buff)
goto release; 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; p_buffs = p_s16_buff = p_sys->p_prev_s16_buff;
/* Convert the buffer to int16_t */ /* 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; 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_buffer1[i] = *p_buffs;
p_buffs += p_sys->i_channels; 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; p_buffs = p_s16_buff;
} }
window_scale_in_place (p_buffer1, &wind_ctx); window_scale_in_place (p_buffer1, &wind_ctx);
fft_perform (p_buffer1, p_output, p_state); fft_perform (p_buffer1, p_output, p_state);
for (i = 0; i< FFT_BUFFER_SIZE; ++i) for (i = 0; i < FFT_BUFFER_SIZE; ++i)
output[i] = p_output[i] * (2 ^ 16) output[i] = p_output[i] * (2 ^ 16)
/ ((FFT_BUFFER_SIZE / 2 * 32768) ^ 2); / ((FFT_BUFFER_SIZE / 2 * 32768) ^ 2);
LOGE("out[100]=%d,out[101]=%d,out[102]=%d", output[100], output[101], output[102]); LOGE("out[100]=%d,out[101]=%d,out[102]=%d", output[100], output[101], output[102]);

@ -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 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); int init_visualizer(filter_sys_t *p_sys);

Loading…
Cancel
Save