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
2.02k stars 432 forks source link

Small click when using mixer #655

Open ChuckMash opened 9 months ago

ChuckMash commented 9 months ago

Hello,

I am using mixer to mix 2 stubs with MP3s from an SD card, one is an long ongoing background track and the other is various sound clips played at various times.

The issue is that there is a audible click each time the second stub with the sound clip is used.

I have searched the repo and found a few other issues commenting on such clicks, but have so far been unable to implement any of the suggested fixes or mitigations while also using mixer.

Here I have a reduced version of the example code


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

#include <SD.h>13.0
#include <SPI.h>
#include <FS.h>

AudioFileSourceSD    *play_file1;
AudioFileSourceSD    *play_file2;
AudioGeneratorMP3    *mp31;
AudioGeneratorMP3    *mp32;
AudioOutputMixer     *mixer;
AudioOutputMixerStub *stub[2];
AudioOutputI2S       *out;

long lastTime = millis();

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

  SD.begin(5, SPI, 25000000); 
  //SD.begin(5, SPI, 4000000); 
  audioLogger = &Serial;

  mp31  = new AudioGeneratorMP3();
  mp32  = new AudioGeneratorMP3();

  out   = new AudioOutputI2S(0,0);
  out->SetOutputModeMono(true);

  mixer = new AudioOutputMixer(1024, out);

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

  stub[1] = mixer->NewInput();
  stub[1]->SetGain(.1);

  play_file1 = new AudioFileSourceSD("/underwater.mp3");
  play_file2 = new AudioFileSourceSD("/sonar_ping.mp3");

  mp31->begin(play_file1, stub[0]);

}

void loop() {  

  // Background Audio
  if (mp31->isRunning()) {
    if (!mp31->loop()){
      mp31->stop();
      delete play_file1;
      delete mp31;
      delete stub[0];
      stub[0] = mixer->NewInput();
      stub[0]->SetGain(.1);
      mp31 = new AudioGeneratorMP3();
    }
  }

  // Sound Clip
  if (mp32->isRunning()) {
    if (!mp32->loop()){
      mp32->stop();
      delete play_file2;
      delete mp32;
      delete stub[1];
      stub[1] = mixer->NewInput();
      stub[1]->SetGain(.1);
      mp32 = new AudioGeneratorMP3();
    }
  }

  // Background Audio
  if(!mp31->isRunning()){
    play_file1 = new AudioFileSourceSD("/underwater.mp3");
    mp31->begin(play_file1, stub[0]);
  }

  // Sound Clip
  if(millis()-lastTime > 8000){ 
    if(!mp32->isRunning()){
      play_file2 = new AudioFileSourceSD("/sonar_ping.mp3");
      mp32->begin(play_file2, stub[1]);
      lastTime = millis();
    }
  }

}

My hope is that I have overlooked something obvious

The only thing coming from audioLogger is

MP3:ERROR_BUFLEN 0

sounds.zip

ChuckMash commented 9 months ago

Some more information:

The click remains when trying with other generator file formats.

If I set stub[0] (background audio) to gain of 0, it sounds like there is no click.

If I set all stubs to gain of 0, there is a barely perceivable short duration change in the (lack of) audio, a momentary shift in the silent hiss where the click would be. I'm not sure what this is indicative of, but I found it interesting

ChuckMash commented 9 months ago

The issue seems to be from SetRate()

Found a workaround in #329

By adding custom AudioOutput layer AudioOutputFixedRates between each generator and mixer stub, the click is removed.

ChuckMash commented 9 months ago

PR #656 fixes the above issue by adding a quick check to see if rate actually needs updating or if it matches what is currently set, eliminating the issue as long as rates match.