-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio.ino
58 lines (51 loc) · 1.54 KB
/
audio.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
#include "audio.h"
AudioGeneratorWAV *wav;
AudioFileSourceSD *fileplay;
AudioOutputI2S *out;
//for recording
const int headerSize = 44;
const int numCommunicationData = 8000;
const int numPartWavData = numCommunicationData/4;
byte header[headerSize];
char communicationData[numCommunicationData];
char partWavData[numPartWavData];
void playAudio(const char* path) {
File sd = SD.open(path);
fileplay = new AudioFileSourceSD(path);
// id3 = new AudioFileSourceID3(file);
out = new AudioOutputI2S(0, 1);
wav = new AudioGeneratorWAV();
wav->begin(fileplay, out);
sd.close();
while(1){
if (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
out->stop();
}
} else {
Serial.printf("WAV done\n");
delay(100);
break;
}
}
}
void record(const char filename[], const int record_time){
const int waveDataSize = record_time * 176000;
CreateWavHeader(header, waveDataSize);
SD.remove(filename);
File file = SD.open(filename, FILE_WRITE);
if (!file) return;
file.write(header, headerSize);
I2S_Init(I2S_MODE, I2S_BITS_PER_SAMPLE_32BIT);
for (int j = 0; j < waveDataSize/numPartWavData; ++j) {
I2S_Read(communicationData, numCommunicationData);
for (int i = 0; i < numCommunicationData/8; ++i) {
partWavData[2*i] = communicationData[8*i + 2];
partWavData[2*i + 1] = communicationData[8*i + 3];
}
file.write((const byte*)partWavData, numPartWavData);
}
file.close();
Serial.println("finish");
}