I am working on an audio playback project using an ESP32 configured with I2S. The code compiles and uploads successfully, and the ESP32 is connected directly to a speaker. The audio plays fine, but there is persistent noise accompanying the audio.
Here’s what I’ve tried to resolve the issue:
-
Amplifier Circuit: I suspected that overcurrent from the ESP32 might be causing the noise, so I built an amplifier circuit. However, the noise was still present and amplified along with the audio.
-
Filter Circuits: I used low-pass and high-pass filters, but the noise persisted. The filters only altered the noise's intensity, not eliminating it.
I would like to understand:
What could be causing this noise?
Are there hardware or software solutions to eliminate the noise while maintaining clear audio output?
Below is the code I am using for reference:
#include <WiFi.h>
#include <AudioFileSourceHTTPStream.h>
#include <AudioGeneratorMP3.h>
#include <AudioOutputI2S.h>
const char* ssid = "Aswana_03";
const char* password = "Ashu310_";
const char* audio_url = "http://192.168.215.138:5500/mp3_song.mp3";
AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioOutputI2S *out;
float volume = 0.5; // Default volume level (range: 0.0 to 1.0)
// Smoothing factor for volume adjustments
float targetVolume = 0.5; // Target volume level
float smoothingFactor = 0.1; // Adjust how smoothly volume changes (0.0 to 1.0)
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
// Configure audio output to DAC
file = new AudioFileSourceHTTPStream(audio_url);
out = new AudioOutputI2S();
out->SetOutputModeMono(true); // Use a single DAC for mono audio
out->SetPinout(0, 0, 25); // DAC output on GPIO25
out->SetGain(volume); // Set initial volume
mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);
Serial.println("Audio playback started. Use '+' to increase volume, '-' to decrease volume.");
}
void loop() {
if (mp3->isRunning()) {
mp3->loop();
// Smoothly adjust volume
if (abs(volume - targetVolume) > 0.01) {
volume += (targetVolume - volume) * smoothingFactor;
out->SetGain(volume);
}
// Check for serial input to change target volume
if (Serial.available() > 0) {
char command = Serial.read();
if (command == '+') {
targetVolume += 0.1;
if (targetVolume > 1.0) targetVolume = 1.0;
Serial.printf("Target volume increased to: %.2f\n", targetVolume);
} else if (command == '-') {
targetVolume -= 0.1;
if (targetVolume < 0.0) targetVolume = 0.0;
Serial.printf("Target volume decreased to: %.2f\n", targetVolume);
}
}
} else {
mp3->stop();
while (1); // Stop program after playback
}
}
Any suggestions or solutions would be greatly appreciated!