Hello,
I am using ESP32 DevKit V1 and got the delivery of 2 new Analog MEMS Microphone Breakout - SPH8878LR5H-1 a couple of days back. I wanted to record the audio from it and send it to the UDP Server for later Listening to the Audio. All I am listening to is noise no audio.
Code:
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "WIFISSID";
const char* password = "WIFIPASSWORD";
const char* udpServerAddress = "LOCALSYSTEMIP"; // Change to your UDP server address
const int udpServerPort = 57399; // Change to your UDP server port
const int microphonePin = 34; // Analog pin connected to the microphone output
const int bufferSize = 1024; // Size of the buffer for audio samples
const float sampleRate = 8000.0f; // Sample rate of 8000 Hz
WiFiUDP udp;
void setup() {
  Serial.begin(115200);
  connectToWiFi();
  connectToUDP();
}
void loop() {
  int16_t sampleArr[bufferSize]; // Array to store audio samples
  record(sampleArr); // Record audio samples
  sendAudioData(sampleArr, sizeof(sampleArr)); // Send recorded audio data over UDP
}
void connectToWiFi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}
void connectToUDP() {
  if (udp.begin(udpServerPort)) {
    Serial.println("UDP connection established on port " + String(udpServerPort));
  } else {
    Serial.println("Error establishing UDP connection");
  }
}
void record(int16_t* sampleArr) {
  for (int i = 0; i < bufferSize; i++) {
    // Record sound continuously
    float analogValue = analogRead(microphonePin); // Read analog value from the microphone
    // Apply low-pass filter to remove high-frequency noise
    analogValue = lowPassFilter(analogValue);
    // Scale the analog value to fit within the range of int16_t
    sampleArr[i] = static_cast<int16_t>((analogValue / 4095.0) * 32767); // Scale to fit within [-32768, 32767]
    delayMicroseconds(1000000 / sampleRate); // Calculate delay based on sample rate
  }
}
float lowPassFilter(float input) {
  static float filteredValue = 0.0f;
  const float alpha = 0.1f; // Adjust this value for the desired filter strength
  filteredValue = (alpha * input) + ((1.0f - alpha) * filteredValue);
  return filteredValue;
}
void sendAudioData(int16_t* data, size_t size) {
  udp.beginPacket(udpServerAddress, udpServerPort);
  udp.write((uint8_t*)data, size); // Send audio data as bytes
  udp.endPacket();
}
Please help me fix the issue.