Skip to content

Commit 132e752

Browse files
committed
fft: code optimization
1 parent b4a1ec4 commit 132e752

File tree

14 files changed

+330
-292
lines changed

14 files changed

+330
-292
lines changed

.gitmodules

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
[submodule "esp-idf"]
22
path = esp-idf
33
url = https://github.com/espressif/esp-idf
4-
[submodule "components/esp-dsp"]
5-
path = components/esp-dsp
6-
url = https://github.com/espressif/esp-dsp

components/esp-dsp

-1
This file was deleted.

main/inc/user/fft.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* fft.h
3+
*
4+
* Created on: 2021-01-14 17:00
5+
* Author: Jack Chen <redchenjs@live.com>
6+
*/
7+
8+
#ifndef INC_USER_FFT_H_
9+
#define INC_USER_FFT_H_
10+
11+
#include <stdint.h>
12+
#include <complex.h>
13+
14+
#define TWO_PI (6.2831853f)
15+
16+
#define BAND_N (12)
17+
#define BAND_FADE (2)
18+
#define BAND_DELAY (2)
19+
20+
#define FFT_N (512)
21+
#define FFT_OUT_N (64)
22+
#define FFT_BLOCK_SIZE (FFT_N * 4)
23+
24+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
25+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
26+
27+
typedef enum {
28+
FFT_CHANNEL_L = 0x00,
29+
FFT_CHANNEL_R = 0x01,
30+
FFT_CHANNEL_LR = 0x02
31+
} fft_channel_t;
32+
33+
extern void fft_compute_lin(uint16_t *data_out, uint16_t scale_factor, uint16_t max_val, uint16_t min_val);
34+
extern void fft_compute_log(uint16_t *data_out, uint16_t scale_factor, uint16_t max_val, uint16_t min_val);
35+
extern void fft_compute_bands(uint16_t *data_out, uint16_t scale_factor, uint16_t max_val, uint16_t min_val);
36+
37+
extern void fft_execute(void);
38+
extern void fft_load_data(const uint8_t *data_in, fft_channel_t channel);
39+
40+
extern void fft_init(void);
41+
42+
#endif /* INC_USER_FFT_H_ */

main/inc/user/vfx.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ typedef enum {
1919

2020
VFX_MODE_IDX_GIF_MAX,
2121

22-
VFX_MODE_IDX_12_BANDS_R = 0x0E,
23-
VFX_MODE_IDX_12_BANDS_G = 0x0F,
24-
VFX_MODE_IDX_SPECTRUM_R_N = 0x10,
25-
VFX_MODE_IDX_SPECTRUM_G_N = 0x11,
26-
VFX_MODE_IDX_SPECTRUM_M_N = 0x12,
27-
VFX_MODE_IDX_SPECTRUM_R_L = 0x13,
28-
VFX_MODE_IDX_SPECTRUM_G_L = 0x14,
29-
VFX_MODE_IDX_SPECTRUM_M_L = 0x15,
22+
VFX_MODE_IDX_12_BANDS_R = 0x0A,
23+
VFX_MODE_IDX_12_BANDS_G = 0x0B,
24+
VFX_MODE_IDX_SPECTRUM_R_N = 0x0C,
25+
VFX_MODE_IDX_SPECTRUM_G_N = 0x0D,
26+
VFX_MODE_IDX_SPECTRUM_M_N = 0x0E,
27+
VFX_MODE_IDX_SPECTRUM_R_L = 0x0F,
28+
VFX_MODE_IDX_SPECTRUM_G_L = 0x10,
29+
VFX_MODE_IDX_SPECTRUM_M_L = 0x11,
3030
#else
3131
VFX_MODE_IDX_RANDOM = 0x00,
3232
VFX_MODE_IDX_RAINBOW = 0x01,

main/inc/user/vfx_fft.h

-29
This file was deleted.

main/src/user/ain.c

+9-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "chip/i2s.h"
1919

2020
#include "user/ain.h"
21-
#include "user/vfx_fft.h"
21+
#include "user/fft.h"
2222

2323
#define TAG "ain"
2424

@@ -42,36 +42,17 @@ static void ain_task(void *pvParameters)
4242
i2s_read(CONFIG_AUDIO_INPUT_I2S_NUM, data, FFT_BLOCK_SIZE, &bytes_read, portMAX_DELAY);
4343

4444
#ifdef CONFIG_ENABLE_VFX
45-
// copy data to FFT input buffer
46-
uint32_t idx = 0;
47-
48-
#ifdef CONFIG_AUDIO_INPUT_FFT_ONLY_LEFT
49-
int16_t data_l = 0;
50-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
51-
data_l = data[idx + 1] << 8 | data[idx];
52-
53-
vfx_fft_data[k] = (float)data_l;
54-
}
55-
#elif defined(CONFIG_AUDIO_INPUT_FFT_ONLY_RIGHT)
56-
int16_t data_r = 0;
57-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
58-
data_r = data[idx + 3] << 8 | data[idx + 2];
59-
60-
vfx_fft_data[k] = (float)data_r;
61-
}
62-
#else
63-
int16_t data_l = 0, data_r = 0;
64-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
65-
data_l = data[idx + 1] << 8 | data[idx];
66-
data_r = data[idx + 3] << 8 | data[idx + 2];
67-
68-
vfx_fft_data[k] = (float)((data_l + data_r) / 2);
69-
}
70-
#endif
45+
#ifdef CONFIG_AUDIO_INPUT_FFT_ONLY_LEFT
46+
fft_load_data(data, FFT_CHANNEL_L);
47+
#elif defined(CONFIG_AUDIO_INPUT_FFT_ONLY_RIGHT)
48+
fft_load_data(data, FFT_CHANNEL_R);
49+
#else
50+
fft_load_data(data, FFT_CHANNEL_LR);
51+
#endif
7152

7253
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
7354
if (!(uxBits & AUDIO_INPUT_RUN_BIT)) {
74-
memset(vfx_fft_data, 0x00, sizeof(vfx_fft_data));
55+
fft_init();
7556
}
7657

7758
xEventGroupClearBits(user_event_group, VFX_FFT_IDLE_BIT);

main/src/user/audio_render.c

+10-29
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
#include "core/os.h"
1919
#include "chip/i2s.h"
2020

21+
#include "user/fft.h"
2122
#include "user/bt_av.h"
22-
#include "user/vfx_fft.h"
2323

2424
#define TAG "audio_render"
2525

2626
RingbufHandle_t audio_buff = NULL;
2727

28+
static uint8_t buff_data[10 * 1024] = {0};
2829
static StaticRingbuffer_t buff_struct = {0};
29-
static uint8_t buff_data[FFT_BLOCK_SIZE * 5] = {0};
3030

3131
/* render callback for the libmad synth */
3232
void render_sample_block(short *sample_buff_ch0, short *sample_buff_ch1, int num_samples, unsigned int num_channels)
@@ -73,7 +73,7 @@ static void audio_render_task(void *pvParameter)
7373
if (!(uxBits & AUDIO_RENDER_CLR_BIT)) {
7474
#ifdef CONFIG_ENABLE_VFX
7575
if (!(uxBits & AUDIO_INPUT_RUN_BIT) && (uxBits & AUDIO_INPUT_FFT_BIT)) {
76-
memset(vfx_fft_data, 0x00, sizeof(vfx_fft_data));
76+
fft_init();
7777
xEventGroupClearBits(user_event_group, VFX_FFT_IDLE_BIT);
7878
}
7979
#endif
@@ -143,32 +143,13 @@ static void audio_render_task(void *pvParameter)
143143
continue;
144144
}
145145

146-
// copy data to FFT input buffer
147-
uint32_t idx = 0;
148-
149-
#ifdef CONFIG_BT_AUDIO_FFT_ONLY_LEFT
150-
int16_t data_l = 0;
151-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
152-
data_l = data[idx + 1] << 8 | data[idx];
153-
154-
vfx_fft_data[k] = (float)data_l;
155-
}
156-
#elif defined(CONFIG_BT_AUDIO_FFT_ONLY_RIGHT)
157-
int16_t data_r = 0;
158-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
159-
data_r = data[idx + 3] << 8 | data[idx + 2];
160-
161-
vfx_fft_data[k] = (float)data_r;
162-
}
163-
#else
164-
int16_t data_l = 0, data_r = 0;
165-
for (uint16_t k = 0; k < FFT_N; k++, idx += 4) {
166-
data_l = data[idx + 1] << 8 | data[idx];
167-
data_r = data[idx + 3] << 8 | data[idx + 2];
168-
169-
vfx_fft_data[k] = (float)((data_l + data_r) / 2);
170-
}
171-
#endif
146+
#ifdef CONFIG_BT_AUDIO_FFT_ONLY_LEFT
147+
fft_load_data(data, FFT_CHANNEL_L);
148+
#elif defined(CONFIG_BT_AUDIO_FFT_ONLY_RIGHT)
149+
fft_load_data(data, FFT_CHANNEL_R);
150+
#else
151+
fft_load_data(data, FFT_CHANNEL_LR);
152+
#endif
172153

173154
xEventGroupClearBits(user_event_group, VFX_FFT_IDLE_BIT);
174155
#endif

main/src/user/bt_app.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ esp_bd_addr_t last_remote_bda = {0};
2929

3030
/* event for handler "bt_app_hdl_stack_up */
3131
enum {
32-
BT_APP_EVT_STACK_UP = 0,
32+
BT_APP_EVT_STACK_UP = 0
3333
};
3434

3535
static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)

main/src/user/bt_app_core.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static xQueueHandle s_bt_app_task_queue = NULL;
2828
bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback)
2929
{
3030
bt_app_msg_t msg;
31+
3132
memset(&msg, 0, sizeof(bt_app_msg_t));
3233

3334
msg.sig = BT_APP_SIG_WORK_DISPATCH;

main/src/user/bt_av.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
#include "user/led.h"
2121
#include "user/key.h"
22+
#include "user/fft.h"
2223
#include "user/bt_app.h"
23-
#include "user/vfx_fft.h"
2424
#include "user/bt_app_core.h"
2525
#include "user/audio_player.h"
2626
#include "user/audio_render.h"
@@ -67,7 +67,6 @@ void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
6767
void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
6868
{
6969
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
70-
7170
if (uxBits & AUDIO_PLAYER_RUN_BIT) {
7271
return;
7372
}

0 commit comments

Comments
 (0)