Skip to content

Commit 036d106

Browse files
committed
recording example using buffons
1 parent 5d29709 commit 036d106

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

examples/examples-audiokit/streams-audiokit-sd-audiokit/streams-audiokit-sd-audiokit.ino

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @file streams-audiokit-sd-audiokit.ino
33
* @author Phil Schatzmann
44
* @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!
56
* @version 0.1
67
* @date 2022-09-01
78
*
@@ -23,15 +24,15 @@ bool recording = false; // flag to make sure that close is only executed one
2324
uint64_t end_time; // trigger to call endRecord
2425

2526

26-
void startRecord() {
27+
void record_start(bool pinStatus, int pin, void* ref){
2728
Serial.println("Recording...");
2829
// open the output file
2930
file = SD.open(file_name, FILE_WRITE);
3031
copier.begin(file, kit);
3132
recording = true;
3233
}
3334

34-
void endRecord() {
35+
void record_end(bool pinStatus, int pin, void* ref){
3536
if (recording == true){
3637
Serial.println("Playing...");
3738
file.close();
@@ -48,10 +49,10 @@ void setup(){
4849

4950
// Open SD drive
5051
if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) {
51-
Serial.println("initialization failed!");
52+
Serial.println("Initialization failed!");
5253
while (1); // stop
5354
}
54-
Serial.println("initialization done.");
55+
Serial.println("Initialization done.");
5556

5657
// setup input and output
5758
auto cfg = kit.defaultConfig(RXTX_MODE);
@@ -62,23 +63,24 @@ void setup(){
6263
kit.begin(cfg);
6364
kit.setVolume(1.0);
6465

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");
6769

6870
}
6971

7072
void loop(){
71-
// time based stop recording
72-
if (recording && millis()>end_time){
73-
endRecord();
74-
}
7573

7674
// record or play file
7775
copier.copy();
7876

7977
// 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){
8179
file.seek(0);
8280
Serial.println("Replay...");
8381
}
82+
83+
// Process keys
84+
kit.processActions();
85+
8486
}

examples/sandbox/streams-looper/streams-looper.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void setup() {
6565
mixer.begin(cfg);
6666

6767
// record when key 1 is pressed
68-
actions.add(PIN_KEY1, record_start, record_end);
68+
kit.audioActions().add(PIN_KEY1, record_start, record_end);
6969
}
7070

7171
void loop() {

src/AudioTools/AudioCopy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class StreamCopyT {
8989
// copies the data from one channel from the source to 2 channels on the destination - the result is in bytes
9090
inline size_t copy(){
9191
LOGD(LOG_METHOD);
92+
// if not initialized we do nothing
93+
if (from==nullptr || to==nullptr) return 0;
94+
9295
size_t result = 0;
9396
size_t delayCount = 0;
9497
size_t len = available();

0 commit comments

Comments
 (0)