Audio is interrupted when saving preset

I devised a simple method of saving the currently selected track for the current preset each time the user presses a button to advance to the next track in the list. However, when I added this to the code, it causes a noticeable split-second pause in all audio playback. Here is the relevant code:

  void NextTrack() {
    if (num_tracks_ > 0) {
      if (track_player_->isPlaying()) track_player_->Stop();
      if (track_player_) track_player_.Free();
      char next_track[128];
      next_track[0] = 0;
      if (!RunCommandAndFindNextSortedLine<128>("list_current_tracks", nullptr, current_track_, next_track, false)) {
        RunCommandAndFindNextSortedLine<128>("list_current_tracks", nullptr, nullptr, next_track, false);
      }
      strcpy(current_track_, next_track);
      PlayTrack();
    } else {
      StartOrStopTrack();
      StartOrStopTrack();
    }
  }

  bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) override {
    switch (EVENTID(button, event, modifiers)) {
// Next Preset/Track
    case EVENTID(BUTTON_AUX, EVENT_FIRST_HELD_MEDIUM, MODE_OFF):
        if (SFX_mselect) {
          hybrid_font.PlayPolyphonic(&SFX_mselect);
        }
        NextTrack();
#ifdef CAIWYN_SAVE_TRACKS
        current_preset_.track = mkstr(current_track_);
        current_preset_.Save();
#endif
        return true;

If CAIWYN_SAVE_TRACKS is not defined then the audio plays as expected. If it’s defined, however, there’s an audible dropout in the audio. Is there something I’m missing that needs to be added to make this work more smoothly?

Yes, anytime you save to SD it will interrupt the audio because we lock the SD temporarily to write to it. Why not just save when the player is ended instead of each change?

I can do that, I just wanted to see if there was another option.

99% sure there’s no way around that based on my work on Edit Mode. @profezzorn would obviously be the definitive answer but pretty sure I asked the same thing during OS6 development, that’s why all “Save” actions are done upon exiting or turning off.

1 Like

The current APIs make it very nearly impossible to access the SD card without any audio interruptions unfortunately. I should probably write a POD explainer about how the audio stack works, but for now, just know that if you can do the saving when the audio isn’t running, that’s usually the way to do it.

1 Like

Thanks for the info. I rewote the NextTrack() function so that the save happens between stopping the previous track and starting the next one. Seems to resolve the issue.