Wave Playback

Playback wave files from an SD card

Introduction

This example reads a wave file from an SD card and plays it using the I2S interface to a MAX98357 I2S Amp Breakout board. The wav file must be stereo signed 16 bit 44100Hz. You can export such files using free software like audacity.

Goals

  • To use an I2S interface
  • To playback wave files from an SD card

Hardware & Software Needed

Circuit

The circuit for this tutorial.
The circuit for this tutorial.

Converting Audio Files

Import your audio file:

Navigate to Import > Audio.
Navigate to Import > Audio.

Select your audio file.
Select your audio file.

Export the wav file:

Navigate to Export Audio.
Navigate to Export Audio.

Choose the WAV Format.
Choose the WAV Format.

Programming the Board

1. First, let's make sure we have correct the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits ARM Cortex M0+) and install it.

2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries... and search for ArduinoSound, SD Library and install it.

The sketch can be found in the snippet below. Upload the sketch to the board.

Code

1/*
2
3 This reads a wave file from an SD card and plays it using the I2S interface to
4
5 a MAX08357 I2S Amp Breakout board.
6
7 Circuit:
8
9 * Arduino Zero, MKRZero or MKR1000 board
10
11 * SD breakout or shield connected
12
13 * MAX08357:
14
15 * GND connected GND
16
17 * VIN connected 5V
18
19 * LRC connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
20
21 * BCLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
22
23 * DIN connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
24
25 created 15 November 2016
26
27 by Sandeep Mistry
28
29 */
30
31#include <SD.h>
32#include <ArduinoSound.h>
33
34// filename of wave file to play
35
36const char filename[] = "MUSIC.WAV";
37
38// variable representing the Wave File
39
40SDWaveFile waveFile;
41
42void setup() {
43
44 // Open serial communications and wait for port to open:
45
46 Serial.begin(9600);
47
48 while (!Serial) {
49
50 ; // wait for serial port to connect. Needed for native USB port only
51
52 }
53
54 // setup the SD card, depending on your shield of breakout board
55
56 // you may need to pass a pin number in begin for SS
57
58 Serial.print("Initializing SD card...");
59
60 if (!SD.begin()) {
61
62 Serial.println("initialization failed!");
63
64 return;
65
66 }
67
68 Serial.println("initialization done.");
69
70 // create a SDWaveFile
71
72 waveFile = SDWaveFile(filename);
73
74 // check if the WaveFile is valid
75
76 if (!waveFile) {
77
78 Serial.println("wave file is invalid!");
79
80 while (1); // do nothing
81
82 }
83
84 // print out some info. about the wave file
85
86 Serial.print("Bits per sample = ");
87
88 Serial.println(waveFile.bitsPerSample());
89
90 long channels = waveFile.channels();
91
92 Serial.print("Channels = ");
93
94 Serial.println(channels);
95
96 long sampleRate = waveFile.sampleRate();
97
98 Serial.print("Sample rate = ");
99
100 Serial.print(sampleRate);
101
102 Serial.println(" Hz");
103
104 long duration = waveFile.duration();
105
106 Serial.print("Duration = ");
107
108 Serial.print(duration);
109
110 Serial.println(" seconds");
111
112 // adjust the playback volume
113
114 AudioOutI2S.volume(5);
115
116 // check if the I2S output can play the wave file
117
118 if (!AudioOutI2S.canPlay(waveFile)) {
119
120 Serial.println("unable to play wave file using I2S!");
121
122 while (1); // do nothing
123
124 }
125
126 // start playback
127
128 Serial.println("starting playback");
129
130 AudioOutI2S.play(waveFile);
131}
132
133void loop() {
134
135 // check if playback is still going on
136
137 if (!AudioOutI2S.isPlaying()) {
138
139 // playback has stopped
140
141 Serial.println("playback stopped");
142
143 while (1); // do nothing
144
145 }
146}

Testing It Out

After you have uploaded the code, open the Serial Monitor in your IDE. You should now be able to see information about you chosen .wav file such as the duration and sample rate of the file. The playback of the audio should start with the function

AudioOutI2S.play(waveFile)
.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • The amplifier is not wired correctly.
  • You have not installed the correct drivers.
  • You have not installed the ArduinoSound Library.

Conclusion

Congratulations! We have learned how to use an Arduino with an I2S interface to play wave files. After having completed this tutorial, you can perhaps start to tinker around with sensors that can trigger the playback of the wave files!

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.