2
2
* @file streams-audiokit-sd-audiokit.ino
3
3
* @author Phil Schatzmann
4
4
* @brief We record the input from the microphone to a file and constantly repeat to play the file
5
+ * The input is triggered by pressing key 1. Recording stops when key 1 is released!
5
6
* @version 0.1
6
7
* @date 2022-09-01
7
8
*
@@ -23,15 +24,15 @@ bool recording = false; // flag to make sure that close is only executed one
23
24
uint64_t end_time; // trigger to call endRecord
24
25
25
26
26
- void startRecord () {
27
+ void record_start ( bool pinStatus, int pin, void * ref) {
27
28
Serial.println (" Recording..." );
28
29
// open the output file
29
30
file = SD.open (file_name, FILE_WRITE);
30
31
copier.begin (file, kit);
31
32
recording = true ;
32
33
}
33
34
34
- void endRecord () {
35
+ void record_end ( bool pinStatus, int pin, void * ref) {
35
36
if (recording == true ){
36
37
Serial.println (" Playing..." );
37
38
file.close ();
@@ -48,10 +49,10 @@ void setup(){
48
49
49
50
// Open SD drive
50
51
if (!SD.begin (PIN_AUDIO_KIT_SD_CARD_CS)) {
51
- Serial.println (" initialization failed!" );
52
+ Serial.println (" Initialization failed!" );
52
53
while (1 ); // stop
53
54
}
54
- Serial.println (" initialization done." );
55
+ Serial.println (" Initialization done." );
55
56
56
57
// setup input and output
57
58
auto cfg = kit.defaultConfig (RXTX_MODE);
@@ -62,23 +63,24 @@ void setup(){
62
63
kit.begin (cfg);
63
64
kit.setVolume (1.0 );
64
65
65
- startRecord ();
66
- end_time = millis ()+5000 ; // record for 5 seconds
66
+ // record when key 1 is pressed
67
+ actions.add (PIN_KEY1, record_start, record_end);
68
+ Serial.println (" Press Key 1 to record" );
67
69
68
70
}
69
71
70
72
void loop (){
71
- // time based stop recording
72
- if (recording && millis ()>end_time){
73
- endRecord ();
74
- }
75
73
76
74
// record or play file
77
75
copier.copy ();
78
76
79
77
// while playing: at end of file -> reposition to beginning
80
- if (file.size ()>0 && file.available ()==0 ){
78
+ if (!recording && file.size ()>0 && file.available ()==0 ){
81
79
file.seek (0 );
82
80
Serial.println (" Replay..." );
83
81
}
82
+
83
+ // Process keys
84
+ kit.processActions ();
85
+
84
86
}
0 commit comments