Sending wireless audio from ESP32 to Python

tegu_sg
Posts: 4
Joined: Fri Jan 08, 2021 12:45 am

Sending wireless audio from ESP32 to Python

Postby tegu_sg » Wed Jan 27, 2021 2:55 am

Hi, i'm trying take an audio signal with the ESP32 and send it tough wifi to my pc (python code) faster and high quality as possible, which means i need minimum 12 bites resolution by ADC(1), and a sampling rate 441000 Hz, the idea is to listen almost in real time you understand, now i'm using UDP but i got a lot of delay, i don't know if i'm not implementing right or this protocol isn't aproppiedied.
I'm gonna try it with Esp-Now (suggest by a member), but what do you recommend for this application, for example i've seen the ESP-CAM send the video low resolution but faster and i don't know what protocol use, i saw the library but i don't understand it.
Later i wanna use an ADC for audio acquisition 16 bits, two channels, 44100hz; i don't know if the ESP32 is enough or i'll need a better microprocessor, please i listen your suggestions.

Code: Select all

#include <WiFi.h>
#include <WiFiUdp.h>
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"

const char* ssid = ".......";
const char* password = ".......";
uint16_t analog=0; 
uint8_t data1[1460]; //data buffer
uint8_t data2[1460]; //data buffer copy
bool ready1=false;

WiFiUDP Udp;
TaskHandle_t Task1, Task2;
//SemaphoreHandle_t baton; //version with semaphore

void setup() {
  
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_STA); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    //Serial.print(".");
  delay(500);
  }

  //baton = xSemaphoreCreateMutex();   //version with semaphore
  xTaskCreatePinnedToCore(codeForTask1, "Task1", 10000, NULL, 1, &Task1, 1);
  delay(500); 
  xTaskCreatePinnedToCore(codeForTask2, "Task2", 10000, NULL, 1, &Task2, 0);
  delay(500);
}

void loop() {
  
      vTaskDelete(NULL);
}

void codeForTask1( void * parameter )
{
    while(1) {

     TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;  //to avoid use vTasKdelay(1)
	 TIMERG0.wdt_feed=1;
	 TIMERG0.wdt_wprotect=0;
	 	 
    int x=0;
  for(int i=0; i<730;i++){ 				//buffer 1460, but real 730 (the analogic data 16 bits)
     int old=micros();
     analog = analogRead(34);   		//the audio signal
     //analog2=analog;
     data1[x]=(uint8_t(analog)); 		//take 8 bits
     data1[x+1]=(uint8_t(analog>>8));	//take the last 8 bits
     x=x+2;
     while(micros()-old<20); 			//22uSec = 1Sec/44100Hz - 2uSec almost the sampling rate
		}
	    while(ready1==true);				//wait to make a copy data buffer
	}
}

void codeForTask2( void * parameter )
{
  while(1) {

		ready1==true;
		for(int i=0; i<1460;i++){			//copy of buffer
		data2[i]=data1[i];
		}
		ready1==false;
		
      Udp.beginPacket("192.168.0.106", 4321);
            	
      for(int i=0; i<1460;i++){						//data to send
        Udp.write(data2[i]);
        }
      
      Udp.endPacket();
     }
}

Who is online

Users browsing this forum: No registered users and 13 guests