1212
1313#define ADC_MIC_DEBUG_PRINT (...) mp_printf(&mp_plat_print, __VA_ARGS__)
1414
15- static mp_obj_t adc_mic_read (mp_obj_t chunk_samples_obj ) {
16- // Extract chunk_samples from argument
17- size_t chunk_samples = mp_obj_get_int (chunk_samples_obj );
15+ static mp_obj_t adc_mic_read (size_t n_args , const mp_obj_t * args ) {
16+ // Extract arguments
17+ // args[0]: chunk_samples
18+ // args[1]: unit_id
19+ // args[2]: adc_channel_list
20+ // args[3]: adc_channel_num
21+ // args[4]: sample_rate_hz
22+ // args[5]: atten
23+
24+ size_t chunk_samples = mp_obj_get_int (args [0 ]);
25+ int unit_id = mp_obj_get_int (args [1 ]);
1826
27+ mp_obj_t channel_list_obj = args [2 ];
28+ size_t channel_list_len ;
29+ mp_obj_t * channel_list_items ;
30+ mp_obj_get_array (channel_list_obj , & channel_list_len , & channel_list_items );
31+
32+ int adc_channel_num = mp_obj_get_int (args [3 ]);
33+ int sample_rate_hz = mp_obj_get_int (args [4 ]);
34+ int atten = mp_obj_get_int (args [5 ]);
35+
1936 ADC_MIC_DEBUG_PRINT ("Starting adc_mic_read...\n" );
2037 ADC_MIC_DEBUG_PRINT ("CONFIG_ADC_MIC_TASK_CORE: %d\n" , CONFIG_ADC_MIC_TASK_CORE );
2138
22- // Configuration (your current manual setup with 2.5 dB atten)
39+ if (adc_channel_num > 10 ) {
40+ mp_raise_ValueError ("Too many channels (max 10)" );
41+ }
42+ if (channel_list_len < adc_channel_num ) {
43+ mp_raise_ValueError ("adc_channel_list shorter than adc_channel_num" );
44+ }
45+
46+ uint8_t channels [10 ];
47+ for (size_t i = 0 ; i < adc_channel_num ; i ++ ) {
48+ channels [i ] = (uint8_t )mp_obj_get_int (channel_list_items [i ]);
49+ }
50+
51+ // Configuration
2352 audio_codec_adc_cfg_t cfg = {
2453 .handle = NULL ,
2554 .max_store_buf_size = 1024 * 2 ,
2655 .conv_frame_size = 1024 ,
27- .unit_id = ADC_UNIT_1 ,
28- .adc_channel_list = ((uint8_t []){ADC_CHANNEL_0 }),
29- .adc_channel_num = 1 ,
30- .sample_rate_hz = 16000 ,
31- //.atten = ADC_ATTEN_DB_0, // values always 16380
32- //.atten = ADC_ATTEN_DB_2_5, // values always 16380
33- .atten = ADC_ATTEN_DB_6 , // values around 12500 +/- 320 (silence) or 4000 (loud talk)
34- //.atten = ADC_ATTEN_DB_11, // values around -1130 +/- 160 (silence)
56+ .unit_id = (adc_unit_t )unit_id ,
57+ .adc_channel_list = channels ,
58+ .adc_channel_num = adc_channel_num , // can probably just count adc_channel_list
59+ .sample_rate_hz = sample_rate_hz ,
60+ .atten = (adc_atten_t )atten ,
3561 };
36- ADC_MIC_DEBUG_PRINT ("Config created for channel %d, sample rate %d, atten %d\n" , ADC_CHANNEL_0 , 16000 , cfg . atten );
62+ ADC_MIC_DEBUG_PRINT ("Config created for unit %d, channels %d, sample rate %d, atten %d\n" , unit_id , adc_channel_num , sample_rate_hz , atten );
3763
3864 // ────────────────────────────────────────────────
3965 // Initialization (same as before)
@@ -55,8 +81,8 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
5581 }
5682
5783 esp_codec_dev_sample_info_t fs = {
58- .sample_rate = 16000 ,
59- .channel = 1 ,
84+ .sample_rate = sample_rate_hz ,
85+ .channel = adc_channel_num ,
6086 .bits_per_sample = 16 ,
6187 };
6288 esp_err_t open_ret = esp_codec_dev_open (dev , & fs );
@@ -69,9 +95,9 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
6995 // ────────────────────────────────────────────────
7096 // Small reusable buffer + tracking variables
7197 // ────────────────────────────────────────────────
72- const size_t buf_size = chunk_samples * sizeof (int16_t );
98+ const size_t buf_size = chunk_samples * sizeof (int16_t ) * adc_channel_num ;
7399 int16_t * audio_buffer = heap_caps_malloc (buf_size , MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT );
74- //int16_t *audio_buffer = heap_caps_thread.start_new_thread(testit, ())_malloc_prefer (buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT);
100+ //int16_t *audio_buffer = heap_caps_malloc_prefer (buf_size, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT);
75101 if (audio_buffer == NULL ) {
76102 esp_codec_dev_close (dev );
77103 esp_codec_dev_delete (dev );
@@ -99,10 +125,6 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
99125 break ;
100126 }
101127 vTaskDelay (pdMS_TO_TICKS (1 )); // 1 ms yield
102- //if (ret != (int)buf_size) {
103- // ADC_MIC_DEBUG_PRINT("Partial read at chunk %d: got %d bytes (expected %zu)\n",
104- // chunk, ret, buf_size);
105- //}
106128
107129 // Update global min/max
108130 for (size_t i = 0 ; i < chunk_samples ; i ++ ) {
@@ -146,7 +168,7 @@ static mp_obj_t adc_mic_read(mp_obj_t chunk_samples_obj) {
146168
147169 return last_buf_obj ? last_buf_obj : mp_obj_new_bytes (NULL , 0 );
148170}
149- MP_DEFINE_CONST_FUN_OBJ_1 (adc_mic_read_obj , adc_mic_read );
171+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (adc_mic_read_obj , 6 , 6 , adc_mic_read );
150172
151173static const mp_rom_map_elem_t adc_mic_module_globals_table [] = {
152174 { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_adc_mic ) },
0 commit comments