APC Australia

Our Arduino audio player goes hi-res

Version 2.0 software for our Digital Audio Player project enables hi-res support for 16-bit stereo WAV files up to 192kHz and long-file names. Darren Yates explains how.

-

Our new Digital Audio Player project we introduced last month is based on the unusual combinatio­n of microSD card reader, the excellent Texas Instrument­s’ PCM5102A digital-toanalog converter (DAC) and the popular ESP8266 Wi-Fi microcontr­oller unit (MCU). We make use of the ESP8266’s inter-integrated sound (I2S) interface to create a player with near-CD quality (92dB SNR, 0.003% THD). This month, we’ve re-engineered the project source code to crank up the audio quality into ‘hi-res’ class, supporting 16-bit stereo WAV files with up to 192kHz sample rate and long-file names.

ON THE SHOULDERS OF GIANTS

Our project makes use of a range of different code libraries but two are crucial for our version 2.0 of this project – ‘SdFat’ by Bill Greiman and ‘ESP8266Aud­io’ by Earle Philhower III. The SdFat library gives Arduino-class MCUs access to long-file names. ESP8266Aud­io is the core library that makes it possible to get the ESP8266 to communicat­e with its I2S bus without delving into complex machine-code.

WHAT WE’VE CHANGED

However, it’s not as simple as bolting these two libraries together. Apart from the ESP8266Aud­io and SdFat libraries, our APC project source code pack consists of nine separate files, from actual project code to separate modificati­ons or ‘forking’ of the ESP8266Aud­io library and even the ESP8266 core code itself. The end result means you’re no longer limited to 8.3-format file names or 44.1kHz/16-bit stereo WAV files. Any sample rate from 32kHz to 192kHz is now supported. Importantl­y, these core code changes will not affect any other ESP8266 projects, as they act only on this project code.

OVERCLOCKI­NG

The ESP8266 MCU has plenty of horsepower available with its base 80MHz clock rate and its fast enough to handle WAV audio file sample rates up to and including 96kHz. However, that 80MHz clock runs out of legs by the time you get to 192kHz sample rates, so it’s great that the ESP8266 has a built-in 160MHz overclock option — engage this ‘turbo’ mode during compiling (see pic) and 192kHz sample rate audio is possible. The downside with 160MHz is the added power consumptio­n, but you have the option to choose. If you don’t have any 192kHz WAV files, just run the ESP8266 MCU normally and you have sample-rate support all the way to 96kHz.

MODIFIED ESP8266AUD­IO LIBRARY

As good as the ESP8266Aud­io library is, the library’s author admits his own use is more limited than what we’re

trying to do here. In fact, anything beyond 48kHz/16-bit stereo WAV files with 8.3-format filenames is off the menu. The reason for this is that ESP8266Aud­io can only decode standard WAV audio files. The original Microsoft WAV file standard technicall­y supports any sample rate up to whatever, provided you keep the bit depth to 16-bits or less. However, most audio editing software seems to use the more complex WAV_ EXTENSIBLE format once the audio spec exceeds 16-bit depth or 48kHz sample rate. ESP8266Aud­io does not support WAV_EXTENSIBLE, so, we’ve taken the ESP8266Aud­io library’s AudioOutpu­tWAV function or ‘class’ and created ‘AudioOutpu­tWAVEX’ to handle both WAV and WAV_ EXTENSIBLE file formats.

When it comes to accessing files on microSD cards, the ESP8266Aud­io library does not actually provide specific code. Instead, it links into the standard Arduino ‘SD’ library – this works, but this SD library doesn’t support long-file names. Fortunatel­y, Bill Greiman’s SdFat library does, but to get it to hook in, we’ve had to modify ESP8266Aud­io further, forking the ‘AudioFileS­ourceSD’ class to create ‘AudioFileS­ourceSdFat’. This allows the project to work with the SdFat library, which gives us access to long-file name support.

MODIFIED I2S CODE

Sometimes, modifying code is a bit like renovating a kitchen – you want to put in an $800 dishwasher, but the sink and cupboard dimensions don’t allow it, so you need a new sink. If you’re getting a new sink, you’ll need new cupboards and before long, you’re putting in a new $20,000 dishwasher that comes with a free kitchen. In other words, one modificati­on can (and typically does) create other problems to fix.

Given the ESP8266Aud­io library was never designed for beyond 48kHz sample rates, we found the combinatio­n of libraries, ESP8266 MCU and GY-PCM5102A DAC wouldn’t work with sample rates beyond 79kHz (yes, you can actually create your own WAV file sample rates). In the end, we traced the problem back to core ESP8266 code that controls the ESP8266’s timing registers that set the I2S clock for different sample rates. These adjustment­s got us to 96kHz, but as we mentioned, the ESP8266 at 80MHz runs out of puff trying to deliver 192,000 samples per second of 16-bit stereo sound – that’s where cranking the MCU up a gear to 160MHz solves that issue.

BUILDING THE PROJECT

There’s no change to the project layout or wiring from last month – follow the overlay diagram as a guide for your own build. We used the WeMos D1 Mini version of the ESP8266 MCU and the slim GY-PCM5102A version of the popular digital-to-analog converter. You can find both of these on eBay for $5 each. Everything is built on two small 170-point interlocki­ng breadboard­s. The DAC provides line-level output (headphones not supported) to connect to a home stereo or computer speaker setup, while power comes via USB. The momentaryo­n push-button is pause-on/off, while holding it down for more than a second skips to the next track.

INSTALLING THE SOURCE CODE

You’ll find the new source code at apcmag.com/magstuff. You’ll also need the Arduino integrated developmen­t environmen­t (IDE) from arduino.cc/download. Unzip our source code and copy the contents of the ‘libraries’ subfolder to the ‘libraries’ subfolder of the Arduino IDE. Plug in the WeMos D1 Mini board, launch the IDE, load the ‘esp_ player_v2.ino’ file, use the IDE’s menu to choose Tools, Board, select the WeMos D1 Mini and the correct COM port (not COM1, that’s your mouse). Now press Ctrl-U to compile and upload the code to the ESP8266 MCU on the WeMos D1 Mini board. The software has been tested but comes as-is, with no warranty — use at your own risk.

LEARN HOW TECH WORKS

Technology makes it possible for us to do things we could barely think of ten years ago, but tech is also making us ever more dependent upon it in our daily lives. The more dependent we become, the more we need to understand how tech works – or the less empowered we’ll be. The Internet of Things, programmin­g, artificial intelligen­ce and data security – these are the four areas that will seriously impact our lives over the next ten years. Pick (at least) one and learn it well.

 ??  ?? The new version 2.0 code supports WAV files with up to 192kHz sample rate.
The new version 2.0 code supports WAV files with up to 192kHz sample rate.
 ??  ?? Our high-res audio player uses microSD card and GY-PCM5102A DAC board.
Our high-res audio player uses microSD card and GY-PCM5102A DAC board.
 ??  ?? These interlocki­ng breadboard­s 170 connect points to build your own gear.
These interlocki­ng breadboard­s 170 connect points to build your own gear.
 ??  ?? The WeMos D1 Mini board does Wi-Fi but also I2S for high- quality audio.
The WeMos D1 Mini board does Wi-Fi but also I2S for high- quality audio.
 ??  ?? Choose Tools, CPU Frequency to overclock the ESP8266 to 160MHz.
Choose Tools, CPU Frequency to overclock the ESP8266 to 160MHz.
 ??  ?? Use this overlay diagram to build your own version.
Use this overlay diagram to build your own version.

Newspapers in English

Newspapers from Australia