Native MP3 decoding on Arduino

Similar documents
Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout

Adafruit seesaw. Created by Dean Miller. Last updated on :30:23 AM UTC

Adafruit DS3231 Precision RTC Breakout

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Adafruit 1-Wire Thermocouple Amplifier - MAX31850K

Adafruit CAP1188 Breakout

Adafruit MAX98357 I2S Class-D Mono Amp

Adafruit HMC5883L Breakout - Triple-Axis Magnetometer Compass Sensor

Adafruit PDM Microphone Breakout

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

Adafruit Mini TFT " 160x80

Introducting Itsy Bitsy 32u4

Adafruit 20W Stereo Audio Amplifier - MAX9744

2.2" TFT Display. Created by lady ada. Last updated on :19:15 PM UTC

Adafruit VS1053 MP3/AAC/Ogg/MIDI/WAV Codec Breakout Tutorial

How-To #8: Work with Smart LEDs

Proper Debugging of ATSAMD21 Processors

IR Breakbeam Sensors. Created by lady ada. Last updated on :32:59 PM UTC

Microcontroller Compatible Audio File Conversion

Adafruit 20W Stereo Audio Amplifier - MAX9744

Keyes Player Mini MP3 Module. (Red & Environmental-protection)

Adafruit 1-Wire GPIO Breakout - DS2413

DS1307 Real Time Clock Breakout Board Kit

1.5" & 2.1" Monochrome 128x64 OLED Display Module

Background. Procedure

Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout

FruitBox Sequencer: Musically Delicious Step Pattern Generator

2.3" Monochrome 128x32 OLED Display Module

Adafruit Metro Mini. Created by lady ada. Last updated on :12:28 PM UTC

Adafruit MAX31865 RTD PT100 or PT1000 Amplifier

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

2.3" Monochrome 128x32 OLED Display Module

MP3 Trigger v2 User Guide

Adafruit I2S MEMS Microphone Breakout

CORTESIA ELECTRONICCA

Spectrum Shield Hookup Guide

Adafruit HTU21D-F Temperature & Humidity Sensor

Stand-alone programming AVRs using CircuitPython

Name: Serial MP3 Player A manual

ARDUINO M0 PRO Code: A000111

09/05/2014. Engaging electronics for the new D&T curriculum. Geoff Hampson Managing Director of Kitronik. Presentation overview

Adafruit Feather nrf52840 Express

ARDUINO MINI 05 Code: A000087

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Adafruit Powerboost 1000 Basic

Adafruit PowerBoost Charger

1.5" & 2.1" Monochrome 128x64 OLED Display Module

Assembly Instructions (8/14/2014) Your kit should contain the following items. If you find a part missing, please contact NeoLoch for a replacement.

Adafruit Optical Fingerprint Sensor

ARDUINO BOARD LINE UP

Metro Minimalist Clock

Adafruit PowerBoost Charger

36mm LED Pixels. Created by Phillip Burgess. Last updated on :45:20 PM EDT

Propeller Activity Board (#32910)

OPERATING MANUAL Manual Version 1.4 (Sep 11, 2017) Tel: or

ROBOTLINKING THE POWER SUPPLY LEARNING KIT TUTORIAL

Grand Central Soundboard in Ten Minutes Created by Mike Barela. Last updated on :11:24 PM UTC

mcube Proprietary APS v1.0 1 / mcube Inc. All rights reserved.

Make your own secret locking mechanism to keep unwanted guests out of your space!

Adafruit Powerboost 1000C

Circuit Playground Express: Piano in the Key of Lime

Click-A-Tune. User Manual

Home > Tutorials > Programming the SpeakJet Shield TTS

Newhaven Display International, Inc Galvin Ct. Elgin IL, Ph: Fax:

micro:bit Lesson 2. Controlling LEDs on Breadboard

Page 1. Arrakis Systems 6604 Powell St. Loveland, CO

Adafruit USB Power Gauge Mini-Kit

Monitor your home remotely using the Arduino

Adafruit TB A DC/Stepper Motor Driver Breakout Board

EasyMP3 Shield SHIELD-EZMP3

Trinket RGB Shield Clock

Adafruit 5" and 7" 800x480 TFT HDMI Backpack

RC Tractor Guy Controller V2.1 Assembly Guide

mp3 bbox 12-24V INSTALLATION GUIDE 4.0 REALITY SYS bbox 12-24V V4.0 Installation 1

Adafruit INA219 Current Sensor Breakout

Adafruit Ethernet FeatherWing

NHD-4.3CTP-SHIELD 4.3 Arduino Shield User Guide

Adafruit BME680. Created by lady ada. Last updated on :10:23 AM UTC

Circuit Playground Firmata

Mp3 Player with Speaker. Instruction Manual FOR MODEL NO FIESTA2. Please read this instruction carefully before the operation

Accelerometer-Based Musical Instrument

Getting Started with the LPC810

TSL2561 Luminosity Sensor

Adafruit I2S Audio Bonnet for Raspberry Pi Created by lady ada. Last updated on :04:10 PM UTC

Si4703 FM Radio Receiver Hookup Guide

TLC5947 and TLC59711 PWM LED Driver Breakouts

FN-BC04 MP3 Sound Module with 10W Amplifier. User s Manual

Freeduino USB 1.0. Arduino Compatible Development Board Starter Guide. 1. Overview

Bass Preamp + D.I. ProfessionalStompboxes.

Applications Voice Annunciator Taking Clock Voice Alarm Machine Operations Failure Alarm Public Announcement System Automatic Broadcast Equipment s

How to Use an Arduino

ARDUINO MEGA 2560 REV3 Code: A000067

Building CircuitPython

Adafruit HUZZAH32 - ESP32 Feather

Make It Change: Potentiometers

RedBoard Hookup Guide

SD / USB / CD / Multi Source Player Instant Playback from SD Memory High Quality DAC - Burr Brown PCM-1796

Adafruit 3.5" 320x480 Color TFT Touchscreen Breakout

Quick Guide 2017 ZOOM CORPORATION. You must read the Usage and Safety Precautions before use.

KNOCK LOCK MAKE YOUR OWN SECRET LOCKING MECHANISM TO KEEP UNWANTED GUESTS OUT OF YOUR SPACE! Discover: input with a piezo, writing your own functions

Transcription:

Native MP3 decoding on Arduino Created by Dean Miller Last updated on 2017-11-14 06:21:16 PM UTC

Guide Contents Guide Contents Overview TODO Compatible Microcontrollers Microchip ATSAMD51 Chip & Dev Boards Teensy 3.6 without headers Teensy 3.2 + header Basis Arduino Wiring & Test Wiring Loading the SD card Download the Library Load Play from SD example Adjusting the Volume How it Works Porting to different microcontrollers Adafruit_MP3 class modifications 2 3 3 3 4 4 4 4 5 5 5 5 6 6 8 10 10 Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 2 of 10

Overview If you have an ARM Cortex M4 (or M3) based microcontroller board, and you want to rock out, this mini guide will be music to your ears. With the accompanying library, based off of Helix, you will be able to decode and play stereo MP3 files without the need for an external chip! That's right, no VLSI VS10xx chips required, you can do it on the fly! MP3 files are very popular because of their widespread availability and small size. MP3 files are compressed, and can be 75 to 95% smaller in size than their original uncompressed form. This small size makes them great for embedded audio projects where storage can be an issue - WAV files are much easier to decode 'cause there is no compression, but that also made them very big. Previously, the MP3 format was protected under patent, so anybody who wanted to sell products that used MP3 files would have to pay a licensing fee. Now those patents have expired and MP3 is freeeeee and you can embed the decoder into any project or product! Because of this, we can now show you how to decode and play back MP3 files with just a powerful microcontroller like the Adafruit Metro M4 or Feather M4, Teensy 3.1, Teensy 3.2, and Teensy 3.6! This is great news for cosplayers, halloween fans, prop-makers and all other types of DIY enthusiasts who want to add MP3 audio to their projects without the external hardware that was previously required (like the Adafruit Audio FX Sound Board). TODO A lot! This code isn't optimized, doesn't use DMA, requires two DACs at this time (doesn't support I2S)... but it is working proof-of-concept! Compatible Microcontrollers The Adafruit MP3 library is currently compatible with the Adafruit Feather / Metro M4, Teensy 3.1, Teensy 3.2, and Teensy 3.6 only. The reason we like these is because all boards have a powerful ARM Cortex M4 at the heart and 1 or 2 12 bit DAC outputs (teensy 3.6 and feather/metro M4 have 2). This combo of processing power and 2 analog outputs is perfect for stereo audio processing with decent resolution, and no additional cost. They also have plenty of RAM for buffering data. Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 3 of 10

If you have a favorite Cortex M3/M4 chip, and you'd like to add support, please submit a pull request! (We only had these two boards on-hand.) You'll need about 32KB of RAM available and about 32KB of FLASH. This code will never work on an 8-bit microcontroller such as the Arduino UNO/Mega. And the 32KB of RAM requirement shuts out many other smaller Cortex chips! Microchip ATSAMD51 Chip & Dev Boards PRODUCT ID: 3382 https://adafru.it/a5s $0.00 OUT OF STOCK Teensy 3.6 without headers PRODUCT ID: 3266 https://adafru.it/a5t $29.95 IN STOCK Teensy 3.2 + header PRODUCT ID: 2756 https://adafru.it/j7a $19.95 IN STOCK Basis The Adafruit_MP3 library is a wrapper around the excellent Helix mp3 decoder, modified for ease of use. Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 4 of 10

Arduino Wiring & Test This example will use the Teensy 3.6 and will read an MP3 file off of an SD card and play it back through the onboard 2-channel DAC. The audio output goes through an impedance matching series resistor and an RC low pass filter to remove the DC offset. Wiring DAC0- connect DAC0 on the teensy to the positive (longer) lead of a 10uF electrolytic capacitor through a 68ohm resistor. The resistor value isn't critical, try to pick something in the 47-100 ohm range. Connect the negative (shorter) lead of the electrolytic capacitor to the left lead of the headphone jack. Connect a 10k resistor from the left lead of the headphone jack to AGND on the teensy. DAC1 - connect DAC1 on the teensy to the positive (longer) lead of a 10uF electrolytic capacitor through a 68ohm resistor. Again, the resistor value isn't critical, try to pick something in the 47-100 ohm range. Connect the negative (shorter) lead of the electrolytic capacitor to the right lead of the headphone jack. Connect a 10k resistor from the right lead of the headphone jack to AGND on the teensy. Connect the ground (center) pin on the headphone jack to AGND on the teensy. Loading the SD card Rename your MP3 file test.mp3 and load it onto a standard formatted microsd card. Insert the microsd card into the SD slot on the teensy. Download the Library To begin playing files, you will need to download Adafruit_MP3 from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip Download Adafruit MP3 https://adafru.it/a5u Rename the uncompressed folder Adafruit_MP3 and check that the Adafruit_MP3 folder contains Adafruit_MP3.cpp and Adafruit_MP3.h Place the Adafruit_MP3 library folder your arduinosketchfolder/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. We also have a great tutorial on Arduino library installation at: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 5 of 10

Load Play from SD example Open File->Examples->Adafruit_MP3->play_from_SD and upload to your Teensy. Open up the serial console and make sure it is able to read the SD card. You should hear the file play through whatever is plugged into the 3.5mm jack. NOTE: if playback of your file is choppy, try a different file with a lower bitrate. Adjusting the Volume The DAC on the Teensy and Feather/Metro M4 has 12 bit resolution. This means the output can have 4096 different values ranging from 0 (ground) to 4095 (3.3V). In this simple example we can raise or lower the volume by changing the mapping of the value that is written to the DAC. Change VOLUME_MAX in the sketch to a desired value between 0 (inaudible) and 4095 (very loud). Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 6 of 10

Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 7 of 10

How it Works The theory of operation of the Adafruit MP3 library is simple. After instantiating your Adafruit_MP3 object, call player.begin() from your setup function. This does necessary timer and buffer setup. #include "Adafruit_MP3.h" Adafruit_MP3 player; void setup() { player.begin(); The library uses a timer set to fire whenever a new sample should be played. For most MP3 files, this is 44.1 khz. When this timer fires, a user-specified function is called. This function should write the samples to the DAC. This function is specified with player.setsamplereadycallback(writedacs); Where writedacs is a function: void writedacs(int16_t l, int16_t r){ /* TODO: map the 16 bit values to the DAC resolution * and write to the left and right channel DACs */ } This function is called from an interrupt, so it should be short and sweet. It's getting called 44,100 to 48,000 times per second for most MP3 files. Another callback needs to be specified with instructions for loading the MP3 data into the players input buffer. This is specified with player.setbuffercallback(getmoredata); where getmoredata is a function: int getmoredata(uint8_t *writehere, int available){ /* TODO: write up to 'available' bytes of MP3 data * to the location 'writehere'. Return the actual number of bytes * that were written */ } This function should not take too long (especially if it needs to disable interrupts on the microcontroller) to avoid choppy playback. Once these two callbacks are specified, the 'tick()' function needs to be called as fast as possible from within your 'loop()' function. Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 8 of 10

void loop() { player.tick(); } The addition of DMA support would be awesome, we have not done that yet :) Adafruit Industries https://learn.adafruit.com/native-mp3-decoding-on-arduino Page 9 of 10

Porting to different microcontrollers To port the Adafruit MP3 library to a different microcontroller, we first need to make sure the helix player can support it. Open the file mp3dec.h and if necessary add an #elif to the switch at the top of the file for your platform. Next open the file assembly.h and if necessary add an #elif with some assembly code for the required functions: static inline int MULSHIFT32(int x, int y); static inline int FASTABS(int x); static inline int CLZ(int x); static inline Word64 MADD64(Word64 sum64, int x, int y); static inline Word64 SAR64(Word64 x, int n); where Word64 is: typedef signed long long int Word64; // 64-bit signed integer. Adafruit_MP3 class modifications The Adafruit_MP3 library uses a timer to control playback. This timer should be able to fire at least 44,100 times per second. Because different microcontroller platforms implement timers differently, you will need to add an #elif to the timer functions in Adafruit_MP3.cpp with instructions on how to configure, enable, and disable timers on your microcontroller platform. These functions are: //enable the playback timer static inline void enabletimer(); //disable the playback timer static inline void disabletimer(); //configure the playback timer. Make sure to enable the interrupt. static inline void configuretimer(); //if necessary, clear the interrupt flag. static inline void acknowledgeinterrupt(); You may also need to redefine the name of the interrupt handler that the timer triggers. This is done in Adafruit_MP3.h. For example, on the SAMD51 (the chip that the Metro / Feather M4 uses), this is done with: #define MP3_Handler TC2_Handler Adafruit Industries Last Updated: 2017-11-14 06:21:16 PM UTC Page 10 of 10