skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries Audio Play Memory

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library

Audio Play Memory

This page is obsolete.

Documentation for specific audio library features has moved to the Design Tool (right side panel).

Converting WAV Files to Audio Data Arrays

The wav2sketch program can convert WAV files to the data arrays needed by AudioPlayMemory.

Only WAV files at 44.1, 22.05 or 11.025 kHz rates can be used. By default, wav2sketch will convert to 8 bit µ-law compressed data to save space. To preserve full 16 bit data, run with "wav2sketch -16".

All the WAV files in the current directory are converted to pairs of .cpp and .h files, which you can copy into your Arduino sketch's folder. Restart Arduino, or close and reopen your sketch for Arduino to recognize the added files.

Data Format

AudioPlayMemory reads sound data from Teensy's internal program memory. The data is stored in an array of 32 bit numbers. Normally these arrays are created automatically by wav2sketch.

The first 32 bit number encodes the data format and the file length. The lowest 24 bits are the length of the following data, in bytes. The upper 8 bits tell AudioPlayMemory how the data is encoded.

Data formats:

  • 0x01 = 44100 Hz, 8 bit µ-law compressed
  • 0x81 = 44100 Hz, 16 bit uncompressed PCM
  • 0x02 = 22050 Hz, 8 bit µ-law compressed
  • 0x82 = 22050 Hz, 16 bit uncompressed PCM
  • 0x03 = 11025 Hz, 8 bit µ-law compressed
  • 0x83 = 11025 Hz, 16 bit uncompressed PCM

8 bit µ-law compression is a very simple data translation that encodes each 16 bit sample into only 8 bits using a non-linear formula. It allows more dynamic range than simple 8 bit encoding, but the trade-off is added distortion. Many audio clips sound fine with µ-law, or at least acceptable for the increase in storage, but others may need uncompressed 16 bit playback. It's easy run wav2sketch on 2 copies of the data and simply copy different .cpp and .h files to your sketch folder to try the different encoding.

The following 32 bit numbers are the actual audio data, packed LSB first. For 8 bit µ-law encoding, each number contains 4 audio samples. For 16 bit PWM, each has 2 samples.

AudioPlayMemory automatically decodes the µ-law compression and converts the slower sample rates to 44100 Hz using linear interpolation between samples.