Skip to content

Commit 5d29709

Browse files
committed
streams-audiokit-sd-audiokit.ino
1 parent a4c34a6 commit 5d29709

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @file streams-audiokit-sd-audiokit.ino
3+
* @author Phil Schatzmann
4+
* @brief We record the input from the microphone to a file and constantly repeat to play the file
5+
* @version 0.1
6+
* @date 2022-09-01
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
#include "AudioTools.h"
12+
#include "AudioLibs/AudioKit.h"
13+
#include <SPI.h>
14+
#include <SD.h>
15+
16+
const char *file_name = "/rec.raw";
17+
uint16_t sample_rate = 16000;
18+
uint8_t channels = 1; // The stream will have 2 channels
19+
AudioKitStream kit;
20+
File file; // final output stream
21+
StreamCopy copier; // copies data
22+
bool recording = false; // flag to make sure that close is only executed one
23+
uint64_t end_time; // trigger to call endRecord
24+
25+
26+
void startRecord() {
27+
Serial.println("Recording...");
28+
// open the output file
29+
file = SD.open(file_name, FILE_WRITE);
30+
copier.begin(file, kit);
31+
recording = true;
32+
}
33+
34+
void endRecord() {
35+
if (recording == true){
36+
Serial.println("Playing...");
37+
file.close();
38+
recording = false;
39+
file = SD.open(file_name); // reopen in read mode
40+
copier.begin(kit, file); // start playback
41+
}
42+
}
43+
44+
void setup(){
45+
Serial.begin(115200);
46+
while(!Serial); // wait for serial to be ready
47+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
48+
49+
// Open SD drive
50+
if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) {
51+
Serial.println("initialization failed!");
52+
while (1); // stop
53+
}
54+
Serial.println("initialization done.");
55+
56+
// setup input and output
57+
auto cfg = kit.defaultConfig(RXTX_MODE);
58+
cfg.sd_active = true;
59+
cfg.sample_rate = sample_rate;
60+
cfg.channels = channels;
61+
cfg.input_device = AUDIO_HAL_ADC_INPUT_LINE2;
62+
kit.begin(cfg);
63+
kit.setVolume(1.0);
64+
65+
startRecord();
66+
end_time = millis()+5000; // record for 5 seconds
67+
68+
}
69+
70+
void loop(){
71+
// time based stop recording
72+
if (recording && millis()>end_time){
73+
endRecord();
74+
}
75+
76+
// record or play file
77+
copier.copy();
78+
79+
// while playing: at end of file -> reposition to beginning
80+
if (file.size()>0 && file.available()==0){
81+
file.seek(0);
82+
Serial.println("Replay...");
83+
}
84+
}

0 commit comments

Comments
 (0)