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

Sound driver for ESP32 not using I2S (uses ULP processor) #325

Closed Martin-Laclaustra closed 3 years ago

Martin-Laclaustra commented 3 years ago

Thank you for all your shared work! I adapted @bitluni 's code in https://github.com/bitluni/ULPSoundESP32 to work as sound output driver for this library. It allows using both DACs (GPIO25 and GPIO26) in stereo mode or select only one of them in mono mode. I2S remains free for other uses. Relevant usage notes:

#include "AudioOutputULP.h"
AudioOutputULP *out = NULL;
void setup()
{
  out = new AudioOutputULP(); // stereo
  //out = new AudioOutputULP(1); // mono, only DAC 1
  //out = new AudioOutputULP(2); // mono, only DAC 2
  ...
}

AudioOutputULP.h.txt AudioOutputULP.cpp.txt

Let me know if you are interested in adding these to the library.

earlephilhower commented 3 years ago

That's pretty neat, it's be a good addition!

Would you like to make a real pull request, or would you like me to manually add it from your attachment?

Martin-Laclaustra commented 3 years ago

I am ok with you adding it directly. In the future, if I work on more complex contributions to the library I would go the whole "regular" path. For these kind of drivers I think this is a "lighter" way of doing it.

earlephilhower commented 3 years ago

I had a couple spots where I needed int->uint32_t conversions to make GCC -Wall happy, but I don't expect it had any actual code effect. If you could give it a quick runthrough to verify it's still good, that would be helpful. Thx for the contribution!

Martin-Laclaustra commented 3 years ago

The changes look good. I will test it tomorrow. Reviewing them I discovered that current code swaps the order of the samples in mono mode.

      unsigned int w = ms[0];
      if(stereoOutput){
        w |= ms[1] << 8;
      } else {
        w |= bufferedOddSample << 8;
        bufferedOddSample = 128;
        waitingOddSample = false;
      }

This is the fix: (but I will test this code tomorrow too)

      unsigned int w = ms[0];
      if(stereoOutput){
        w |= ms[1] << 8;
      } else {
        w = bufferedOddSample;
        w |= ms[0] << 8;
        bufferedOddSample = 128;
        waitingOddSample = false;
      }
Martin-Laclaustra commented 3 years ago

I confirm that your changes and my proposed fix work as they should. The only missing thing is adding 3 lines of usage (like in the OP) to the top of the header file. Otherwise, I am afraid that the mono feature will remain hidden/unknown for most users.

Thank you for the warmth attention and the quick adoption.

roboticboyer commented 3 years ago

@earlephilhower please could you submit a new release of the library with ULP feature and an example of the webradio Thank you Gio

earlephilhower commented 3 years ago

@roboticboyer , you should be all set!