### Amplifiers and External DAC ESP32 Dev Boards have their own Digital Audio Converter, so they have the capabaility to be hooked up directly to a amplifier and play a sound. However, the audio quality that the ESP32 can get on its own is very choppy. (If you want to try this method, which uses the cheap amplifiers we have in the lab, check out <a href='https://circuitdigest.com/microcontroller-projects/esp32-based-audio-player'>this tutorial</a>.) The Arduino Uno can only play a very, very small WAV, [and at poor quality](https://github.com/charliegerard/dev-notes/blob/master/arduino/wavFilesWithoutSdCard.md). This is where EXTERNAL DACs come in! You can offload the task of converting the digital signal to audio to another board, and get much better sound quality. We use the MAX98357A which allows you to play mp3s with you esp32. In order to hook up the MAX98357A you will want to hook up the MAX98357A to these pins on your esp32: MAX98357A | esp32 ------------- | ------------- LRC | 14 BCLK| 15 DIN| 22 GAIN| GND GND | GND VCC | 3.3V Then attach the speaker wires to the screw terminals. However, mp3 files are quite large, you will not be able to store them directly on your device. So you have two options you can stream them from the internet or you can stream them from a Micro SD card ###Streaming from the Internet To stream from the internet you will nedd to use the [Arduino Audio Tools Library](https://github.com/pschatzmann/arduino-audio-tools) You will just need to download the ZIP file from the github and then go to Sketch -> Include Library -> Add .ZIP Library and click on the file you just downloaded Once you have the code downloaded it should be as simple as running the code below ``` /** * @file streams-url_mp3-i2s.ino * @author Phil Schatzmann * @brief decode MP3 stream from url and output it on I2S * @version 0.1 * @date 2021-96-25 * * @copyright Copyright (c) 2021 */ // install https://github.com/pschatzmann/arduino-libhelix.git #include "AudioTools.h" #include "AudioCodecs/CodecMP3Helix.h" URLStream url("WIFI_USERNAME","WIFI_PASSWORD"); I2SStream i2s; // final output of decoded stream VolumeStream volume(i2s); EncodedAudioStream dec(&i2s, new MP3DecoderHelix()); // Decoding stream StreamCopy copier(dec, url); // copy url to decoder void setup(){ Serial.begin(115200); // AudioLogger::instance().begin(Serial, AudioLogger::Info); // setup i2s auto config = i2s.defaultConfig(TX_MODE); // you could define e.g your pins and change other settings config.pin_ws = 14; config.pin_bck = 15; config.pin_data = 22; // config.mode = I2S_STD_FORMAT; i2s.begin(config); // setup I2S based on sampling rate provided by decoder dec.setNotifyAudioChange(i2s); dec.begin(); // set initial volume volume.begin(config); // we need to provide the bits_per_sample and channels volume.setVolume(0); // mp3 radio url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3"); // url.begin("https://streamsquid.com/#/artist/calm%20singing%20birds%20zone","audio/mp3"); } void loop(){ copier.copy(); } ``` ###Streaming from Micro SD Card To stream from an SD Card we will using Arduino Audio Tools. Then once you have that completed that you will need to hook up the Micro SD to your ESP32 using pins SD Card Reader | esp32 ------------- | ------------- CS | 5 MOSI| 23 MISO| 19 SCK| 18 GND | GND VCC | 3.3V Once it also hooked up you can try the sample code below ``` #include "AudioTools.h" #include "AudioLibs/AudioSourceSD.h" #include "AudioCodecs/CodecMP3Helix.h" const char *startFilePath="/"; const char* ext="mp3"; AudioSourceSD source(startFilePath, ext); I2SStream i2s; MP3DecoderHelix decoder; AudioPlayer player(source, i2s, decoder); void printMetaData(MetaDataType type, const char* str, int len){ Serial.print("==> "); Serial.print(toStr(type)); Serial.print(": "); Serial.println(str); } void setup() { Serial.begin(115200); AudioLogger::instance().begin(Serial, AudioLogger::Info); // setup output auto cfg = i2s.defaultConfig(TX_MODE); cfg.pin_ws = 14; cfg.pin_bck = 15; cfg.pin_data = 22; i2s.begin(cfg); // setup player player.setMetadataCallback(printMetaData); player.setVolume(0.5) player.begin(); } void loop() { player.copy(); } ``` When you run this code it will play all the mp3s in the folder you specified with *startFilePath="/"; You change the mp3 file you are playing with the player.setIndex() function which takes in an integer and will set the current mp3 to the mp3 that is at that index. For example player.setIndex(0) with be the file at index 0. You can set volume by using player.setVolume() and plugging in any value between 0 and 1