Writing audio to SD *and* writing text to SD

I received a question as to whether we have any example programs showing how to write both audio to the SD and text to the SD. In this way, they could have both the raw audio and a record of user interactions in response to the audio. It’s a good request!

Sadly, at the moment, we have no such examples. I have never tried to do this myself.

(Edit: after my longer post below, I found a little time and I did implement an example. See the post below the post below!)

OK, now the longer answer…

If we were to attempt this dual-writing goal, how might I approach it? The trick is to prevent the audio writing from colliding with the text writing. We do this by ensuring that we only create one instance of the root SD-writing class and we use that one instance for both the audio writing and the text writing.

For the audio-writing, I might look at the Tympan example: 02-Intermediate\SDWriter_wApp. For the text logging, I might look at the Arduino/Teensy examples SD\SdFat_Usage (or possibly SD\Datalogger). These examples show how to use SD for audio or SD for text, but neither one of these shows both. We need to add this idea of creating a single SD instance that is shared between the two types of writing.

What I would do is:

  1. Create a single instance of the SD-controlling class
  2. Pass this one instance to the Tympan AudioSDWriter for writing audio to the SD
  3. Use this one instance for your code that writes text to the SD

Expanding these three steps into more words, it might look like:

  1. Create one instance of the SD-controlling class. Normally the SD-controlling class is created for you in the background by the Tympan AudioSDWriter. We now need to create the class explicitly. So, I grabbed the relevant line of code from deep down in the Tympan library. I’d insert it into the global space of your program. I’d probably add the line in the global space, right after you create the “myTympan”:
#include <SD.h>  //enables us to manually control the SD card.  Add somewhere near the top

//setup the  Tympan using the default settings
Tympan  myTympan(TympanRev::E, audio_settings); 
SDClass  sdx;    // explicitly create SD card (will pass to AudioSDWriter)
  1. Pass this SD instance to the AudioSDWriter. Somewhere in your program, you create an instance of the AudioSDWriter_F32 class. We are going to modify that line to hand it the separately created SD class. Your modified line of code would look like:
AudioSDWriter_F32_UI audioSDWriter(&(sdx.sdfs), audio_settings); 
  1. Use this SD instance to write text logs. At different points in your code, you need to open a file, write to the file, and close a file. If you are not going to write very often (like, maybe, no faster than 1 per second on average), you could do all three steps together. It’s inefficient to keep opening and closing, but it is very safe for your data. If you are recording user inputs, you could open-write-close with every user input. The three commands that you might want are:
FsFile myfile = sdx.sdfs.open("datalog.txt", O_WRITE | O_CREAT | O_APPEND);
myfile.println("User Changed Volume to : " + String(newVolume_dB)); //write the text 
myfile.close();

There are additional details that might need to be added, like maybe an sd.begin line like this one (to be put in your setup() function?):

sdx.begin(SdioConfig(FIFO_SDIO));

But until it gets tested the first time, I’m not sure.

Sorry I don’t have a simple example that I can just point to!

I added a new example program to the Tympan_Library to demonstrate writing both audio and text to the SD card. It is called SDWriter_wApp_wLog.

If you update your Tympan_Library, it is under Examples->Tympan_Library->02-Intermediate->SDWriter_wApp_wLog.

It’s also available here: https://github.com/Tympan/Tympan_Library/tree/main/examples/02-Utility/SDWriter_wApp_wLog

It creates a log file on the SD card. It logs when you start and stop the audio writing. It also logs when you increase or decrease the gain. It’s these last two that are probably most relevant to you…not that you care about gain, but because they are user inputs from the phone. You can see how the user inputs are detected in the SerialManager and then cause some effect and cause a message to get written to the SD card.

Good luck!

Chip