How to efficiently perform i2s_read() on esp-eye

pandaFang
Posts: 1
Joined: Mon Feb 03, 2020 3:34 am

How to efficiently perform i2s_read() on esp-eye

Postby pandaFang » Sat Feb 15, 2020 7:52 am

I was trying to capture image and audio data to be sent through websocketserver at different ports using esp-eye. Managed to send image data and audio data individually.

However, I encountered a problem when trying to send both image and audio data simultaneously in the single device. Only image data sends successfully. Audio data doesn’t send, and there were no errors being logged.

After various tests, I have found out that whenever image capturing starts (2 captures per second), the audio task will halt and never get back to work. For this I suspected it was due to how I sample the audio data (not sure). Currently I’m calling i2s_read() that will capture a sample of 4 bytes at a time.

Is there a better way to do this audio capture, for example using DMA? DMA is supposed to be able to capture data without CPU involvement, but I was not able to find out how to do that.

Thank you in advance

Code: Select all

//Configuration for the mic im using, able to capture one sample at a time
#define SAMPLE_RATE  (16000)
#define BUFCOUNT     (2)
#define BUFLEN       (1024)

    const i2s_config_t i2s_config = {  
        .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
        .sample_rate = I2S_CONFIG_SAMPLE_RATE,
        .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
        .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
        .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
        .dma_buf_count = I2S_CONFIG_BUFCOUNT,
        .dma_buf_len = I2S_CONFIG_BUFLEN
    };

Code: Select all

//from main.c
	...
//Image Capture Task
	...
//Audio Task
const i2s_port_t I2S_PORT0 = I2S_NUM_0;
esp_websocket_client_handle_t client2;
static void TaskAudio(void *pvParameters){
    (void) pvParameters;
    i2s_mic_init();
    ESP_LOGI(TAG_AUDIO, "Connecting to %s...", WEBSOCKET_ECHO_ENDPOINT);
    const esp_websocket_client_config_t websocket_cfg2 = {
        .uri = WEBSOCKET_ECHO_ENDPOINT, 
        .port = 8080
    };

    client2 = esp_websocket_client_init(&websocket_cfg2);
    esp_websocket_register_events(client2, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client2);
    esp_websocket_client_start(client2);

    esp_err_t err;
    size_t bytes_read = 0;
    int32_t buffer = 0;
    int sent = 0;
    bool getTime = true;
    char strBufBlock[4100];
    uint strBufBlockIndex = 0;
    int size = sizeof(long long int);
    long long int timeSend;
    while(1){
        if (!esp_websocket_client_is_connected(client2)) {
            ESP_LOGE(TAG_AUDIO, "NOT CONNECTED to AUDIO websocket");
        }
        //Taking timestamp for the start of the data sampling
        if(getTime){
            timeSend = timeMicro();
            ESP_LOGI(TAG_AUDIO,"Unix Epoch: %lld", timeSend);
            for(int i=0; i<size ; i++){
                strBufBlock[strBufBlockIndex++] = *(((char*)(&timeSend))+i);
            }
            getTime = false;
        }
        for(strBufBlockIndex = 8; strBufBlockIndex < 4100; strBufBlockIndex += 4){
            err = i2s_read(I2S_PORT0, &strBufBlock[strBufBlockIndex], sizeof(int32_t), &bytes_read, portMAX_DELAY);
        }
        if(err == ESP_OK){
            ESP_LOGE(TAG_AUDIO,"Audio block 4092");
            if (esp_websocket_client_is_connected(client2)) {
                sent = esp_websocket_client_send(client2, strBufBlock, 4100, portMAX_DELAY);
                if(sent != -1){
                    ESP_LOGI(TAG_AUDIO, "Websocket AUDIO Data Sent Successfully!");
                }else{
                    ESP_LOGI(TAG_AUDIO, "Sending error...");
                }
            }
            strBufBlockIndex = 0;
            getTime = true;
        }
    }
    esp_websocket_client_stop(client2);
    ESP_LOGI(TAG_AUDIO, "Websocket Stopped");
    esp_websocket_client_destroy(client2);
}

//app_main()
    void app_main(void){
    wifi_app();
    xTaskCreatePinnedToCore(TaskAudio,"audio",16383,NULL,5,NULL,0);
    xTaskCreatePinnedToCore(TaskCapture,"Capture",16383,NULL,2,NULL,1);

}


Who is online

Users browsing this forum: lstpvtltd and 120 guests