earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
1.98k stars 432 forks source link

Mixing 2 mp3s from SD becomes really noisy after a few seconds depends on dma buf count and samples #664

Closed lacid01 closed 4 months ago

lacid01 commented 4 months ago

Hello,

i got a few problems with mixing two mp3s together. I have a background sound and some sounds on top of it. My problem is, that after a few seconds it becomes really really noisy. I realized, that it's necessary to increase the dma buf count and the number of samples. But it's just a shift in the future until the noise starts. I read in other topics, that it may help to convert the mp3s to wav files. But I have no idea. Does anyone have a suggestion for me?

My Code looks like this. It's very similar to the mixer example:

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#include <SD.h>

// IOs
#define SD_CS         16

// function definitions
uint16_t counter();
void printDirectory(File dir, int numTabs);

// variables
Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

#include "AudioOutputI2S.h"
#include "AudioGeneratorMP3.h"
#include "AudioFileSourceSD.h"
#include <AudioOutputMixer.h>

AudioGeneratorMP3 *mp3_1;
AudioGeneratorMP3 *mp3_2;
AudioFileSourceSD *file_1;
AudioFileSourceSD *file_2;
AudioOutputI2S *out = new AudioOutputI2S(0, 0, 128, 0);
AudioOutputMixer *mixer;
AudioOutputMixerStub *stub[2];

bool init_sd = false;
uint16_t count = 0;
unsigned long repeater;
bool start_mp3_success = false;
uint8_t mp3_track = 0;

void setup() {
  Serial.begin(9600);

  pixels.begin(); // init neopixel
  pixels.setBrightness(150); 

  pinMode(SD_CS, OUTPUT);      
  digitalWrite(SD_CS, HIGH);
  init_sd = SD.begin(SD_CS, SPI, 50000000);
  if (init_sd)
    pixels.fill(pixels.ColorHSV(22000,255,100));
  else
    pixels.fill(pixels.ColorHSV(0,255,100));
  pixels.show();
  SD.open("/");
  delay(5000);

  audioLogger = &Serial;
  file_1 = new AudioFileSourceSD("/power-up-35839.mp3");
  file_2 = new AudioFileSourceSD("/space-radio-interference-24866.mp3");
  out->SetPinout(17, 18, 5);
  out->SetOutputModeMono(true); 
  mixer = new AudioOutputMixer(2048, out);

  stub[0] = mixer->NewInput();
  stub[0]->SetGain(0.03);

  mp3_1 = new AudioGeneratorMP3();
  start_mp3_success = mp3_1->begin(file_1, stub[0]);

  stub[1] = mixer->NewInput();
  stub[1]->SetGain(0.06);
  mp3_2 = new AudioGeneratorMP3();
  start_mp3_success = mp3_2->begin(file_2, stub[1]);

  repeater = millis();
}

void loop() {
  // neopixel
  uint16_t counter_t =  counter(); // hsv->h value rotate
  pixels.fill(pixels.ColorHSV(counter_t,255,255));
  pixels.show();

  // audio
  if (mp3_1->isRunning()) {
    if (!mp3_1->loop()){
      mp3_1->stop();

      delete file_1;
      delete mp3_1;
      delete stub[0];
      stub[0] = mixer->NewInput();
      stub[0]->SetGain(0.03);
      mp3_1 = new AudioGeneratorMP3();

      bool stop = false;
      switch (mp3_track)
      {
        case 0: file_1 = new AudioFileSourceSD("/super-specific-landing-29974.mp3"); break;
        case 1: file_1 = new AudioFileSourceSD("/nmydt.mp3"); break;
        case 2: file_1 = new AudioFileSourceSD("/intruder-alert-181439.mp3"); break;
        default: stop = true; break;
      }
      if (!stop)
        start_mp3_success = mp3_1->begin(file_1, stub[0]);
      mp3_track++;
    }
  } 

  if (mp3_2->isRunning()) 
    if (!mp3_2->loop())
      mp3_2->stop();

  delay(1);
}

uint16_t counter() {
  count+=10;
  return count;
}
lacid01 commented 4 months ago

looks like converting to wav solves this problem. Maybe the microcontroller is to slow for dual mp3 decoding.