Hi all,
I am modifying the "Record and Stream" example on Nicla Voice to work with SD Card.
So far, it's able to record to the SD Card. However,the audio quality is choppy, and there's quite a bit of loss, even though the encoder writes to the SD Card File directly.
Here's my code (shortened version of the longer one to keep the relevant parts), and was wondering if anyone is experienced to understand why when I stream the encoder into the Serial, it's very clear. But when I stream the encoder into the file, it's not as clear.
I tried to encoder.setOutputStream(myFile) too, and results are similar.
#include "Arduino.h"
#include "NDP.h"
#include "AudioTools.h"
#include "AudioCodecs/CodecG722.h"
#include <Nicla_System.h>
#include <SPI.h>
#include <SD.h>
G722Encoder encoder;
const bool lowestPower = false;
// Recording Related
uint8_t data[2048];
// File Related
File myFile;
String FILE_PATH = "DATA/FILE.TXT";
const int chipSelect = 6;
class CustomPrint : public Print {
public:
size_t write(uint8_t d) override {
return myFile.write(d);
}
};
CustomPrint intermediaryPrint; // Create an instance of the custom print class
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
nicla::begin();
// nicla::disableLDO();
nicla::leds.begin();
// SD Card Initialization
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
AudioBaseInfo bi;
bi.channels = 1;
bi.sample_rate = 16000;
encoder.setOptions(0);
encoder.begin(bi);
myFile = SD.open(FILE_PATH, FILE_WRITE);
encoder.setOutputStream(myFile);
Serial.println("Loading synpackages");
NDP.begin("mcu_fw_120_v91.synpkg");
NDP.load("dsp_firmware_v91.synpkg");
NDP.load("ei_model.synpkg");
Serial.println("packages loaded");
NDP.getInfo();
Serial.println("Configure mic");
NDP.turnOnMicrophone();
int chunk_size = NDP.getAudioChunkSize();
if (chunk_size >= sizeof(data)) {
for(;;);
}
}
void loop() {
unsigned int len = 0;
NDP.extractData(data, &len);
encoder.write(data, len);
myFile.flush();
myFile.close();
myFile = SD.open(FILE_PATH, FILE_WRITE);
}
Help is much appreciated! I guess my understanding of writing into buffer/ streaming is not very strong yet especially in regards to embedded systems.