1 | /*
|
---|
2 | Simple Audio Player
|
---|
3 |
|
---|
4 | Demonstrates the use of the Audio library for the Arduino Due
|
---|
5 |
|
---|
6 | Hardware required :
|
---|
7 | * Arduino shield with a SD card on CS4
|
---|
8 | * A sound file named "test.wav" in the root directory of the SD card
|
---|
9 | * An audio amplifier to connect to the DAC0 and ground
|
---|
10 | * A speaker to connect to the audio amplifier
|
---|
11 |
|
---|
12 | Original by Massimo Banzi September 20, 2012
|
---|
13 | Modified by Scott Fitzgerald October 19, 2012
|
---|
14 |
|
---|
15 | This example code is in the public domain
|
---|
16 |
|
---|
17 | http://arduino.cc/en/Tutorial/SimpleAudioPlayer
|
---|
18 |
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include <SD.h>
|
---|
22 | #include <SPI.h>
|
---|
23 | #include <Audio.h>
|
---|
24 |
|
---|
25 | void setup()
|
---|
26 | {
|
---|
27 | // debug output at 9600 baud
|
---|
28 | Serial.begin(9600);
|
---|
29 |
|
---|
30 | // setup SD-card
|
---|
31 | Serial.print("Initializing SD card...");
|
---|
32 | if (!SD.begin(4)) {
|
---|
33 | Serial.println(" failed!");
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | Serial.println(" done.");
|
---|
37 | // hi-speed SPI transfers
|
---|
38 | SPI.setClockDivider(4);
|
---|
39 |
|
---|
40 | // 44100Khz stereo => 88200 sample rate
|
---|
41 | // 100 mSec of prebuffering.
|
---|
42 | Audio.begin(88200, 100);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void loop()
|
---|
46 | {
|
---|
47 | int count = 0;
|
---|
48 |
|
---|
49 | // open wave file from sdcard
|
---|
50 | File myFile = SD.open("test.wav");
|
---|
51 | if (!myFile) {
|
---|
52 | // if the file didn't open, print an error and stop
|
---|
53 | Serial.println("error opening test.wav");
|
---|
54 | while (true);
|
---|
55 | }
|
---|
56 |
|
---|
57 | const int S = 1024; // Number of samples to read in block
|
---|
58 | short buffer[S];
|
---|
59 |
|
---|
60 | Serial.print("Playing");
|
---|
61 | // until the file is not finished
|
---|
62 | while (myFile.available()) {
|
---|
63 | // read from the file into buffer
|
---|
64 | myFile.read(buffer, sizeof(buffer));
|
---|
65 |
|
---|
66 | // Prepare samples
|
---|
67 | int volume = 1024;
|
---|
68 | Audio.prepare(buffer, S, volume);
|
---|
69 | // Feed samples to audio
|
---|
70 | Audio.write(buffer, S);
|
---|
71 |
|
---|
72 | // Every 100 block print a '.'
|
---|
73 | count++;
|
---|
74 | if (count == 100) {
|
---|
75 | Serial.print(".");
|
---|
76 | count = 0;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | myFile.close();
|
---|
80 |
|
---|
81 | Serial.println("End of file. Thank you for listening!");
|
---|
82 | while (true) ;
|
---|
83 | }
|
---|
84 |
|
---|