SD looper with overdub and lack of memory / cpu power

amii

New member
i everyone,
I’m building a simple looper on Teensy 4.1 + Audio Shield, using BUILTIN_SDCARD with the Teensy Audio Library:
  • AudioInputI2S (guitar on line-in)
  • AudioEffectFreeverb for monitoring only
  • AudioRecordQueue for recording
  • AudioPlaySdRaw for playback (MASTER + OVERDUB)
Flow:
  1. First button press: record 5 seconds MASTER to MASTER5.RAW on SD, then loop it with AudioPlaySdRaw.
  2. Second press: go to ARM_OVR – I prepare OVER5.RAW in advance (delete + SD.open()), no SD work at loop boundary.
  3. On next MASTER loop restart: start 5 seconds OVERDUB recording to OVER5.RAW while MASTER plays.
  4. After that, play MASTER + OVERDUB in sync, both looped from SD.
I log AudioProcessorUsage() and AudioMemoryUsage() every 500 ms.
Everything is stable most of the time (CPU ≈ 3–40%, mem 3–6/80), but at the exact moment overdub recording starts, I still see a very short spike:
  • cpuMax jumps above 200%
  • memMax hits 80/80 blocks
  • audible click/glitch right at overdub start
Mitigations I already tried:
  • Recording DRY only from lineIn (no reverb in the record path).
  • Preparing the overdub file (SD.remove + SD.open) before the loop boundary.
  • Limiting SD writes in loop() to a small number of blocks per iteration (e.g. max 4 blocks from AudioRecordQueue).
  • Using AUDIO_MEMORY_BLOCKS = 80 (Teensy 4.1).
Yet I still get that one nasty spike when recQueue.begin() + the first writes happen while MASTER is playing from SD.
Question:
Do you have architectural suggestions or proven patterns for simultaneous SD playback + SD recording with AudioPlaySdRaw/AudioRecordQueue on Teensy 4.1 (BUILTIN_SDCARD), to avoid this initial CPU/memory spike?
Examples of ideas I’m considering (but not sure what’s best):
  • Different way to drain AudioRecordQueue (e.g. using an ISR / dedicated task instead of loop()?).
  • Using SdFat with custom buffering instead of the built-in SD wrapper.
  • Reducing block size / sample rate / changing AudioMemory strategy.
  • Any example projects of a stable “SD looper with overdub” on Teensy 4.x?
Any pointers, code patterns, or “this is how you must structure SD I/O for this use case” would be greatly appreciated.

 
There are fundamental issues with accessing the SD card from your foreground code at the same time as using AudioPlaySdRaw, because the latter accesses the card from the Audio update interrupt. Sooner or later one access will interrupt the other, with less than hilarious consequences.
 
Back
Top