This repository was archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathSimpleAudioPlayerWithDebug.ino
161 lines (131 loc) · 3.97 KB
/
SimpleAudioPlayerWithDebug.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Simple Audio Player With Debug
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 22MHz and 48MHz 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>
int i2c_status;
int error_status;
int sd_status;
int audio_status;
int cb_error;
typedef struct {
uint32_t ChunkID; /* 0 */
uint32_t FileSize; /* 4 */
uint32_t FileFormat; /* 8 */
uint32_t SubChunk1ID; /* 12 */
uint32_t SubChunk1Size; /* 16*/
uint16_t AudioFormat; /* 20 */
uint16_t NbrChannels; /* 22 */
uint32_t SampleRate; /* 24 */
uint32_t ByteRate; /* 28 */
uint16_t BlockAlign; /* 32 */
uint16_t BitPerSample; /* 34 */
uint32_t SubChunk2ID; /* 36 */
uint32_t SubChunk2Size; /* 40 */
}WAVE_FormatTypeDef;
void setup() {
i2c_status = 0;
cb_error = 0;
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
while(!Serial);
Serial.println("Starting...");
/* 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(5000); // delay for console
File myFile = SD.open("test.wav");
if (!myFile.available()) {
// if the file didn't open, print an error and stop
Serial.println("error opening test.wav");
while (true);
} else {
Serial.println("test.wav open OK");
}
myFile.read((void*) &WaveFormat, sizeof(WaveFormat));
Serial.print("Sample rate: ");
Serial.print(WaveFormat.SampleRate);
Serial.println(" Hz ");
Serial.print("Byte rate: ");
Serial.print(WaveFormat.ByteRate);
Serial.println(" Hz");
Serial.print("Bytes per sample: ");
Serial.println(WaveFormat.ByteRate / WaveFormat.SampleRate);
Serial.print("Channels number: ");
Serial.println(WaveFormat.NbrChannels);
duration = WaveFormat.FileSize / WaveFormat.ByteRate;
Serial.print("Duration: ");
Serial.print(duration);
Serial.println(" sec");
Serial.print("File Size : ");
Serial.print((int)(WaveFormat.FileSize/1024));
Serial.println(" KB");
//delay(1000);
Serial.println("STARTUP AUDIO\r\n");
//delay(1000);
audio_status = Audio.begin(WaveFormat.SampleRate, 100);
// delay(1000);
if(audio_status > 0) {
Serial.println("ERROR in AUDIO INIT");
}
Serial.println("Prepare Playing");
// Prepare samples
int volume = 100;
Audio.prepare(NULL, S, volume);
Serial.println("Playing");
Serial.println(Audio.getBuffer());
Serial.println(Audio.getHalf());
Serial.println(Audio.getLast());
Serial.println(Audio.getBufferSize());
//delay(1000);
while (myFile.available()) {
// Every 100 block print a '.'
count++;
if (count == 1000) {
Serial.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 */
Serial.println("");
Serial.println("End of file. Thank you for listening!");
Audio.end();
myFile.close();
Serial.println("Printing info");
Serial.print("i2c_status: ");
Serial.println(i2c_status);
Serial.print("error_status: ");
Serial.println(error_status);
Serial.print("playedBytes: ");
Serial.println(Audio.playedBytes());
Serial.print("receivedBytes: ");
Serial.println(Audio.receivedBytes());
Serial.print("cb_error: ");
Serial.println(cb_error);
delay(5000);//delay in between reads for stability
Serial.println("Restart Playing");
}