功能是APP与ESP32 建立实时语音通讯。
最近刚完成ESP32-CAM与ANDROID视频流直播,遗憾的是0.5-0.8秒的延迟,虽然能凑合用但是明显不够完美。原因大概是ESP32-CAM性能问题,要么是OV2640的解码速度不够快,若能搞定语音通讯也打算换M5试试,或者OV7670据说速度快一些。
音频实时通讯这块我的想法是:
MAX9814->ESP32-WIFI-SERVER->RTMP->ANDROID->EAR
我也不是很确定这个方案行不行,毕竟没有什么可以参考的东西,这里我的疑惑是实时通讯一定要用RTMP吗?下面的代码感兴趣的朋友可以一起研究一下。
#include
#include
#include
#define AUDIO_BUFFER_MAX 800
uint8_t audioBuffer[AUDIO_BUFFER_MAX];
uint8_t transmitBuffer[AUDIO_BUFFER_MAX];
uint32_t bufferPointer = 0;
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* host = "SERVER IP ADDRESS";
bool transmitNow = false;
WiFiClient client;
hw_timer_t * timer = NULL; // our timer
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
// says that we want to run critical
// code and don't want to be interrupted
int adcVal = adc1_get_voltage(ADC1_CHANNEL_0); // reads the ADC
uint8_t value = map(adcVal, 0 , 4096, 0, 255); // converts the value to
// 0..255 (8bit)
audioBuffer[bufferPointer] = value; // stores the value
bufferPointer++;
if (bufferPointer == AUDIO_BUFFER_MAX) { // when the buffer is full
bufferPointer = 0;
memcpy(transmitBuffer, audioBuffer, AUDIO_BUFFER_MAX); // copy buffer
// into a second buffer
transmitNow = true; // sets the value true so we know that we can
// transmit now
}
portEXIT_CRITICAL_ISR(&timerMux); // says that we have run our critical
// code
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("MY IP address: ");
Serial.println(WiFi.localIP());
adc1_config_width(ADC_WIDTH_12Bit); // configure the analogue to digital
// converter
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_0db); // connects the
// ADC 1 with channel 0 (GPIO 36)
Serial.println("ADC Configured.");
const int port = 4444;
while (!client.connect(host, port)) {
Serial.println("connection failed");
delay(1000);
}
Serial.println("connected to server");
timer = timerBegin(0, 80, true); // 80 Prescaler
timerAttachInterrupt(timer, &onTimer, true); // binds the handling
// function to our timer
timerAlarmWrite(timer, 125, true);
timerAlarmEnable(timer);
}
void loop() {
if (transmitNow) { // checks if the buffer is full
transmitNow = false;
client.write((const uint8_t *)audioBuffer, sizeof(audioBuffer)); //
// sending the buffer to our server
}
}
|