Skip to content

Commit 875e60b

Browse files
committed
main: code refactoring
1 parent 64cf8ec commit 875e60b

File tree

9 files changed

+91
-106
lines changed

9 files changed

+91
-106
lines changed

main/Kconfig.projbuild

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,6 @@ choice AUDIO_INPUT
6969
bool "I2S-1"
7070
endchoice
7171

72-
choice AUDIO_INPUT_CHANNEL
73-
prompt "Audio Input Channel"
74-
default AUDIO_INPUT_ONLY_LEFT
75-
depends on !AUDIO_INPUT_NONE
76-
help
77-
Select Audio Input Channel.
78-
79-
config AUDIO_INPUT_ONLY_LEFT
80-
bool "Left Channel"
81-
config AUDIO_INPUT_ONLY_RIGHT
82-
bool "Right Channel"
83-
config AUDIO_INPUT_BOTH
84-
bool "Both Channel"
85-
endchoice
86-
8772
config AUDIO_INPUT_I2S_NUM
8873
int
8974
default 0 if AUDIO_INPUT_I2S0 || AUDIO_INPUT_PDM
@@ -182,6 +167,36 @@ config SCREEN_PANEL_OUTPUT_MMAP
182167
bool "Display CUBE0414's Memory Mapping"
183168
endchoice
184169

170+
choice BT_AUDIO_FFT_CHANNEL
171+
prompt "Bluetooth Audio FFT Channel"
172+
default BT_AUDIO_FFT_BOTH
173+
depends on ENABLE_VFX
174+
help
175+
Select Bluetooth Audio FFT Channel.
176+
177+
config BT_AUDIO_FFT_ONLY_LEFT
178+
bool "Left Channel"
179+
config BT_AUDIO_FFT_ONLY_RIGHT
180+
bool "Right Channel"
181+
config BT_AUDIO_FFT_BOTH
182+
bool "Both Channel"
183+
endchoice
184+
185+
choice AUDIO_INPUT_FFT_CHANNEL
186+
prompt "Audio Input FFT Channel"
187+
default AUDIO_INPUT_FFT_ONLY_LEFT
188+
depends on ENABLE_VFX && !AUDIO_INPUT_NONE
189+
help
190+
Select Audio Input FFT Channel.
191+
192+
config AUDIO_INPUT_FFT_ONLY_LEFT
193+
bool "Left Channel"
194+
config AUDIO_INPUT_FFT_ONLY_RIGHT
195+
bool "Right Channel"
196+
config AUDIO_INPUT_FFT_BOTH
197+
bool "Both Channel"
198+
endchoice
199+
185200
config LIGHT_CUBE_DC_PIN
186201
int "Light Cube DC Pin"
187202
default 23

main/inc/chip/i2s.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ extern void i2s1_init(void);
1414
extern void i2s_output_init(void);
1515
extern void i2s_output_deinit(void);
1616

17-
extern void i2s_set_output_sample_rate(int rate);
18-
extern void i2s_set_input_sample_rate(int rate);
19-
20-
extern int i2s_get_output_sample_rate(void);
21-
extern int i2s_get_input_sample_rate(void);
22-
23-
extern int i2s_get_output_bits_per_sample(void);
24-
extern int i2s_get_input_bits_per_sample(void);
17+
extern void i2s_output_set_sample_rate(int rate);
2518

2619
#endif /* INC_CHIP_I2S_H_ */

main/inc/core/os.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ typedef enum user_event_group_bits {
2121
BT_OTA_RESTART_BIT = BIT6,
2222
BLE_OTA_LOCKED_BIT = BIT7,
2323
AUDIO_INPUT_RUN_BIT = BIT8,
24-
AUDIO_INPUT_LOOP_BIT = BIT9,
25-
AUDIO_PLAYER_RUN_BIT = BIT10,
26-
AUDIO_PLAYER_IDLE_BIT = BIT11,
24+
AUDIO_PLAYER_RUN_BIT = BIT9,
25+
AUDIO_PLAYER_IDLE_BIT = BIT10,
2726
} user_event_group_bits_t;
2827

2928
extern EventGroupHandle_t user_event_group;

main/src/chip/i2s.c

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
#define I2S0_TAG "i2s-0"
1414
#define I2S1_TAG "i2s-1"
1515

16-
static int i2s_output_sample_rate = 44100;
17-
static int i2s_output_bits_per_sample = 16;
18-
19-
static int i2s_input_sample_rate = 44100;
20-
static int i2s_input_bits_per_sample = 16;
21-
2216
static i2s_config_t i2s_output_config = {
2317
.mode = I2S_MODE_MASTER | I2S_MODE_TX
2418
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == CONFIG_AUDIO_INPUT_I2S_NUM)
@@ -28,14 +22,15 @@ static i2s_config_t i2s_output_config = {
2822
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
2923
.use_apll = 1, // Use APLL
3024
.sample_rate = 44100,
31-
.bits_per_sample = 16,
25+
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
3226
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL3,
3327
.tx_desc_auto_clear = true, // Auto clear tx descriptor on underflow
3428
.dma_buf_count = 8,
3529
.dma_buf_len = 128,
3630
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels
3731
};
3832

33+
#if !defined(CONFIG_AUDIO_INPUT_NONE) && (CONFIG_AUDIO_OUTPUT_I2S_NUM != CONFIG_AUDIO_INPUT_I2S_NUM)
3934
static i2s_config_t i2s_input_config = {
4035
.mode = I2S_MODE_MASTER | I2S_MODE_RX
4136
#ifdef CONFIG_AUDIO_INPUT_PDM
@@ -45,17 +40,12 @@ static i2s_config_t i2s_input_config = {
4540
.communication_format = I2S_COMM_FORMAT_I2S_MSB | I2S_COMM_FORMAT_I2S,
4641
.use_apll = 0, // Use PLL_D2
4742
.sample_rate = 44100,
48-
.bits_per_sample = 16,
43+
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
4944
.dma_buf_count = 2,
5045
.dma_buf_len = 128,
51-
#ifdef CONFIG_AUDIO_INPUT_ONLY_LEFT
52-
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // left channel only
53-
#elif defined(CONFIG_AUDIO_INPUT_ONLY_RIGHT)
54-
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, // right channel only
55-
#else
5646
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels
57-
#endif
5847
};
48+
#endif
5949

6050
#if (CONFIG_AUDIO_OUTPUT_I2S_NUM == 0) || (CONFIG_AUDIO_INPUT_I2S_NUM == 0)
6151
void i2s0_init(void)
@@ -164,40 +154,10 @@ void i2s_output_deinit(void)
164154
#endif
165155
}
166156

167-
void i2s_set_output_sample_rate(int rate)
157+
void i2s_output_set_sample_rate(int rate)
168158
{
169-
if (rate != i2s_output_sample_rate) {
170-
i2s_set_sample_rates(CONFIG_AUDIO_OUTPUT_I2S_NUM, rate);
171-
i2s_output_sample_rate = rate;
159+
if (rate != i2s_output_config.sample_rate) {
160+
i2s_output_config.sample_rate = rate;
161+
i2s_set_sample_rates(CONFIG_AUDIO_OUTPUT_I2S_NUM, i2s_output_config.sample_rate);
172162
}
173163
}
174-
175-
void i2s_set_input_sample_rate(int rate)
176-
{
177-
#if !defined(CONFIG_AUDIO_INPUT_NONE) && (CONFIG_AUDIO_OUTPUT_I2S_NUM != CONFIG_AUDIO_INPUT_I2S_NUM)
178-
if (rate != i2s_input_sample_rate) {
179-
i2s_set_sample_rates(CONFIG_AUDIO_INPUT_I2S_NUM, rate);
180-
i2s_input_sample_rate = rate;
181-
}
182-
#endif
183-
}
184-
185-
int i2s_get_output_sample_rate(void)
186-
{
187-
return i2s_output_sample_rate;
188-
}
189-
190-
int i2s_get_input_sample_rate(void)
191-
{
192-
return i2s_input_sample_rate;
193-
}
194-
195-
int i2s_get_output_bits_per_sample(void)
196-
{
197-
return i2s_output_bits_per_sample;
198-
}
199-
200-
int i2s_get_input_bits_per_sample(void)
201-
{
202-
return i2s_input_bits_per_sample;
203-
}

main/src/user/audio_input.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
#define TAG "audio_input"
1818

19+
#define BUFF_SIZE (FFT_N * 4)
20+
1921
static uint8_t audio_input_mode = 0;
2022

2123
static void audio_input_task_handle(void *pvParameters)
2224
{
2325
#ifndef CONFIG_AUDIO_INPUT_NONE
24-
size_t bytes_read = 0, bytes_written = 0;
25-
char data[FFT_N * 4] = {0};
26+
size_t bytes_read = 0;
27+
char data[BUFF_SIZE] = {0};
2628

2729
ESP_LOGI(TAG, "started.");
2830

@@ -35,29 +37,41 @@ static void audio_input_task_handle(void *pvParameters)
3537
portMAX_DELAY
3638
);
3739

38-
i2s_read(CONFIG_AUDIO_INPUT_I2S_NUM, data, FFT_N * 4, &bytes_read, portMAX_DELAY);
39-
40-
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
41-
if (uxBits & AUDIO_INPUT_LOOP_BIT) {
42-
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, data, FFT_N * 4, &bytes_written, portMAX_DELAY);
43-
}
40+
i2s_read(CONFIG_AUDIO_INPUT_I2S_NUM, data, BUFF_SIZE, &bytes_read, portMAX_DELAY);
4441

4542
#ifdef CONFIG_ENABLE_VFX
43+
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
4644
if (uxBits & VFX_FFT_EXEC_BIT || uxBits & VFX_RELOAD_BIT) {
4745
continue;
4846
}
4947

5048
// Copy data to FFT input buffer
5149
if (vfx_fft_plan) {
5250
uint32_t idx = 0;
53-
int16_t data_l = 0, data_r = 0;
5451

52+
#ifdef CONFIG_AUDIO_INPUT_FFT_ONLY_LEFT
53+
int16_t data_l = 0;
54+
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
55+
data_l = data[idx+1] << 8 | data[idx];
56+
57+
vfx_fft_plan->input[k] = (float)data_l;
58+
}
59+
#elif defined(CONFIG_AUDIO_INPUT_FFT_ONLY_RIGHT)
60+
int16_t data_r = 0;
61+
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
62+
data_r = data[idx+3] << 8 | data[idx+2];
63+
64+
vfx_fft_plan->input[k] = (float)data_r;
65+
}
66+
#else
67+
int16_t data_l = 0, data_r = 0;
5568
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
5669
data_l = data[idx+1] << 8 | data[idx];
5770
data_r = data[idx+3] << 8 | data[idx+2];
5871

5972
vfx_fft_plan->input[k] = (float)((data_l + data_r) / 2);
6073
}
74+
#endif
6175

6276
xEventGroupSetBits(user_event_group, VFX_FFT_FULL_BIT);
6377
}
@@ -71,13 +85,10 @@ void audio_input_set_mode(uint8_t idx)
7185
audio_input_mode = idx;
7286
ESP_LOGI(TAG, "mode %u", audio_input_mode);
7387

74-
if (audio_input_mode == 1) {
88+
if (audio_input_mode) {
7589
xEventGroupSetBits(user_event_group, AUDIO_INPUT_RUN_BIT);
76-
xEventGroupClearBits(user_event_group, AUDIO_INPUT_LOOP_BIT);
77-
} else if (audio_input_mode == 2) {
78-
xEventGroupSetBits(user_event_group, AUDIO_INPUT_RUN_BIT | AUDIO_INPUT_LOOP_BIT);
7990
} else {
80-
xEventGroupClearBits(user_event_group, AUDIO_INPUT_RUN_BIT | AUDIO_INPUT_LOOP_BIT);
91+
xEventGroupClearBits(user_event_group, AUDIO_INPUT_RUN_BIT);
8192
}
8293

8394
xEventGroupSetBits(user_event_group, VFX_RELOAD_BIT | VFX_FFT_FULL_BIT);

main/src/user/audio_player.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ void audio_player_play_file(uint8_t idx)
102102
}
103103
mp3_file_index = idx;
104104
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
105-
if (uxBits & AUDIO_INPUT_LOOP_BIT) {
106-
return;
107-
}
108105
if (uxBits & AUDIO_PLAYER_RUN_BIT) {
109106
// Previous playback is still not complete
110107
playback_pending = 1;

main/src/user/audio_render.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ void render_sample_block(short *sample_buff_ch0, short *sample_buff_ch1, int num
4343
/* Called by the NXP modifications of libmad. Sets the needed output sample rate. */
4444
void set_dac_sample_rate(int rate)
4545
{
46-
i2s_set_output_sample_rate(rate);
46+
i2s_output_set_sample_rate(rate);
4747
}

main/src/user/ble_app_gatts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga
184184
uint8_t vfx_backlight = param->write.value[1];
185185
vfx_set_backlight(vfx_backlight);
186186
} else if (param->write.value[0] == 0x05 && param->write.len == 2) {
187-
uint8_t audio_input_mode = param->write.value[1] % 0x03;
187+
uint8_t audio_input_mode = param->write.value[1] % 2;
188188
audio_input_set_mode(audio_input_mode);
189189
} else {
190190
ESP_LOGW(BLE_GATTS_TAG, "unknown command");

main/src/user/bt_app_av.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
7979
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
8080
#endif
8181

82-
#ifndef CONFIG_AUDIO_INPUT_NONE
83-
if (uxBits & AUDIO_INPUT_LOOP_BIT) {
84-
return;
85-
}
86-
#endif
87-
8882
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
8983
if (uxBits & BT_A2DP_IDLE_BIT || uxBits & AUDIO_PLAYER_RUN_BIT) {
9084
return;
@@ -108,14 +102,30 @@ void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
108102
// Copy data to FFT input buffer
109103
if (vfx_fft_plan) {
110104
uint32_t idx = 0;
111-
int16_t data_l = 0, data_r = 0;
112105

113-
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
114-
data_l = data[idx+1] << 8 | data[idx];
115-
data_r = data[idx+3] << 8 | data[idx+2];
106+
#ifdef CONFIG_BT_AUDIO_FFT_ONLY_LEFT
107+
int16_t data_l = 0;
108+
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
109+
data_l = data[idx+1] << 8 | data[idx];
116110

117-
vfx_fft_plan->input[k] = (float)((data_l + data_r) / 2);
118-
}
111+
vfx_fft_plan->input[k] = (float)data_l;
112+
}
113+
#elif defined(CONFIG_BT_AUDIO_FFT_ONLY_RIGHT)
114+
int16_t data_r = 0;
115+
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
116+
data_r = data[idx+3] << 8 | data[idx+2];
117+
118+
vfx_fft_plan->input[k] = (float)data_r;
119+
}
120+
#else
121+
int16_t data_l = 0, data_r = 0;
122+
for (uint16_t k=0; k<FFT_N; k++,idx+=4) {
123+
data_l = data[idx+1] << 8 | data[idx];
124+
data_r = data[idx+3] << 8 | data[idx+2];
125+
126+
vfx_fft_plan->input[k] = (float)((data_l + data_r) / 2);
127+
}
128+
#endif
119129

120130
xEventGroupSetBits(user_event_group, VFX_FFT_FULL_BIT);
121131
}
@@ -224,7 +234,7 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
224234
sample_rate = 48000;
225235
}
226236

227-
i2s_set_output_sample_rate(sample_rate);
237+
i2s_output_set_sample_rate(sample_rate);
228238

229239
ESP_LOGI(BT_A2D_TAG, "configure audio player %x-%x-%x-%x",
230240
a2d->audio_cfg.mcc.cie.sbc[0],

0 commit comments

Comments
 (0)