forked from bcmi-labs/arduino-core-stm32f4
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimpleAudioPlayer.ino
88 lines (70 loc) · 2.02 KB
/
SimpleAudioPlayer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Simple Audio Player
Demonstrates the use of the Audio library for the Arduino OTTO
Hardware required :
* SD card in the Arduino OTTO slot
* A sound file named "test.wav" in the root directory of the SD card
* Only 22kHz and 48kHz supported for now
* A speaker to connect to the audio amplifier
Original by Massimo Banzi September 20, 2012
Modified by Scott Fitzgerald October 19, 2012
Modified by Laurent Meunier 2016 - based with STM32 MCD Application examples support
Modified by Francesco Alessi October 25, 2016
This example code is in the public domain
*/
#include <SD.h>
#include <Audio.h>
void setup() {
// initialize serial communication at 9600 bits per second:
SerialUSB.begin(115200);
while(!SerialUSB);
/* Test begin() method */
while (SD.begin(SD_DETECT_PIN) != TRUE)
{
delay(10);
}
}
void loop() {
int count = 0;
WAVE_FormatTypeDef WaveFormat;
const int S = 1024; // Number of samples to read in block
uint32_t buffer[S];
int duration;
delay(1000); // delay for console
File myFile = SD.open("test.wav");
if (!myFile.available()) {
// if the file didn't open, print an error and stop
SerialUSB.println("error opening test.wav");
while (true);
} else {
SerialUSB.println("test.wav open OK");
}
myFile.read((void*) &WaveFormat, sizeof(WaveFormat));
delay(1000);
SerialUSB.println("STARTUP AUDIO\r\n");
delay(1000);
Audio.begin(WaveFormat.SampleRate, 100);
delay(1000);
// Prepare samples
int volume = 50;
Audio.prepare(NULL, S, volume);
delay(1000);
while (myFile.available()) {
// Every 100 block print a '.'
count++;
if (count == 1000) {
SerialUSB.print(".");
count = 0;
}
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
// Feed samples to audio
Audio.write(buffer, sizeof(buffer));
}
/* reaching end of file */
SerialUSB.println("End of file. Thank you for listening!");
Audio.end();
myFile.close();
delay(5000);
SerialUSB.println("Restart Playing");
}