The Teensy Audio library supports audio file playback in a number of ways. One way is to convert an existing .wav file using a computer utility called wav2sketch. This utility will output two files - one with an extension .cpp and the other with .h. These two files contain the sample data from the .wav file. Both the .cpp and .h files can be moved to an Arduino sketch folder, where they can be referenced by name in the Arduino sketch. A PlayMem object from the Audio library can then be used to play back the file through a DAC object.
The tip of an audio plug is connected to DAC0. The sleeve of an audio plug is connected to ground. The audio plug is connected to a mixer, sound card or powered speakers. The pinout can be found here.
A wave file must be prepared using the wav2sketch utility. The wav2sketch utility can be found here as source code and Windows .exe application, and here (which is part of this thread). Download the wav2sketch utility and move it to a convenient location.
If using a Mac, it is recommended to create a folder named "wav2sketch" in the current user's home directory, and place the utility there.
Place a .wav audio file in the wav2sketch folder - in this case, example.wav. This is a 44.1KHz, 16-bit mono audio file.
Launch the MacOS Terminal in Applications > Utilities > Terminal.
In Terminal, type "cd wav2sketch" and press enter. This will navigate to the wav2sketch folder.
In Terminal, type "./wav2sketch" and press enter. This will launch the wav2sketch utility, and process any .wav files in the wav2sketch folder.
The wav2sketch utility will generate a .h and a .cpp file. In this case, these files are named "AudioSampleExample.cpp" and "AudioSampleExample.h". These files are located in the wav2sketch folder.
Open the Arduino IDE, create a new sketch.
Save the sketch in the wav2sketch folder. In this example, the sketch is named "Audio_Playback".
The .cpp and .h files should be moved into the Arduino sketch folder.
Quit and re-open the Arduino IDE. Open the Arduino sketch previously created. The .cpp and .h files will now appear as additional tabs of the Arduino sketch. Any .ino, .h or .cpp file added to an Arduino sketch folder will automatically appear as an additional tab, and can be referenced from the main sketch.
Open the Audio System Design Tool, and create a PlayMem object connected to a DAC object.
Export and copy the code.
Paste the code at the top of the Arduino sketch.
Include the .h file in the sketch. In this example, the relevant line of code #include "AudioSampleExample.h" was added below the Audio library code, and above the setup function.
The wav file can now be used in the Arduino sketch.
Example 1 - Playback Just Once
In this example, an audio file is played back once. The file is stored in internal program memory, where the maximum size is approximately 1MB. The PlayMem object is used to output the audio via a DAC object. The PlayMem object references an array that contains the sample data. The PlayMem object uses the play function during the setup function to begin playback.
Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Audio_Playback_Example_1/
Example 2 - Playback When Button is Pressed
In this example, a push button is added to the hardware setup. This push button begins playback with every push.
Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Audio_Playback_Example_2/
The Teensy can play back an audio file from internal memory using the PlayMem object. The audio file has to be prepared using the wav2sketch folder.