Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: SD.open taking long time

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    16

    SD.open taking long time

    Hi,
    I am trying to make a continuous recording device using Teensy3.5/3.6 and SPH0645 I2S mems microphone from adafruit. As per suggestions i got from this forum, i referred micro sound recorder and bat-detector project and adapted to sdfs library instead of regular sd library for faster read and write operations. I am able to record good quality audio with this setup but i am facing a issue where after recording around 15-20K 10 sec wav files on SD card, i am getting some files with 11sec timegap. When i play that audio file in media player, it plays properly but difference in timestamp (comparison with file recorder before current file) will be 11sec for some file. To debug this i did sdcard profiling and i found out that sd.open takes around 1sec if there are around 15-20k files already present in SDcard. I have tried this with high speed sdcards and results are same for all. Can anyone guide me for a solution for this?

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    What is the FAT of your microSD?
    It seems that exFAT is opening a little bit faster than FAT32. (sdfs allows exFAT)
    Also use only newly formatted disks for data logging.
    Most of these delays are due to the need of finding new and empty clusters, and updating FAT or Bitmaps.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    Yes,i format SD cards before using them. I have tried with both FAT32 and exfat, same issue with both. When i keep device recording for long duration's, i see these 11 sec time gaps (10sec wavfile) keep coming after 15-20k files recorded.

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    OK,
    what happens is, that every time you open a file, the FS must scan the whole directory to see if it already exists. After a while this will need a lot of time.
    I suggest to create for each hour a directory so you have at most 360 files per directory. This should speed up searching for files.
    If you make the files longer say 60 s then I would create a directory for every day.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    I tried creating new directory every hour and also tried with creating directory every day, but it's still having time gaps.

  6. #6
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    Hi,
    Can anyone tell me if projects like microsoud recorder and batdetector have similar issue, while recording file continuously?

  7. #7
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    @ianmb
    can you provide the complete ino file, you are using?

  8. #8
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    This is my code for creating 10 sec wav file continuously, new directory is created every day. When i compare timestamp of files, i can see gaps between 2 wav files as 9sec and 11sec when i have set recording time as 10sec and files on sdcard is less than estimated files calculated on basis of device run time. This issue appears after recording some 15-20K files on sdcard.
    Code:
    #include <Wire.h>
    #include <TimeLib.h>
    #include "RTClib.h" // RealTimeClock Library for DS1307 and DS3231
    #include "SdFs.h"
    #include "record_queue.h"
    #include "input_i2s.h"
    #include "mixer.h"
    #include "filter_biquad.h"
    #include "MAX17043.h"
    
    //Enable record only mode to use device only as recorder
    
    #define DEBUG     1                           //uncomment this to enable serial print AT command output
    
    #ifdef DEBUG //TODO: Print filename
    #define DEBUG_PRINT(x)  \
    Serial.print(x); \
    Serial.print(" : Line: "); \
    Serial.print(__LINE__);   \
    Serial.println ("");
    #else
    #define DEBUG_PRINT(x) do {} while (0)
    #endif
    
    //************defines*****************//
    #define BAUD             115200
    #define NEW_BAUDRATE     921600 
    
    #define BYTES_IN_HEADER   256
    #define BYTES_WAVHEADER   36
    #define HEADER_SIZE       264
    //***********pin_configuration**********//
    #define GSM_RTS           33
    #define GSM_CTS           34
    #define BUTTON            4
    #define LED_REC           36 // RECLED used to indicate initialization
    #define LED_UPL           37 // SDLED used for upload indication
    #define LED_NETSTAT       38 // NETSTATLED
    #define ERR_LED           39 // ERR LED used to indicate Error  
    #define PWR_PIN           20//  PWR pin of GSM
    #define NTWRK_STAT        24//  Network status pin of GSM
    #define MODE_SEL_SWITCH   32 //This slider switch is interfaced externally
    
    #define REC_TIME          10//  Length of recorded wav file
    //***********MIC constants**********//
    #define SAMPLE_RATE       16000
    #define MIC_GAIN          15
    
    #define FILE_NAME         "P"                 //Add single char for filename
    #define DEVICE_ID         "DEV1"
    #define FOLDER            "DAY"
    
    /************************************************************************************
                                      SDCARD  SETTINGS (NEW LIBRARY)                      
    ************************************************************************************/
    // SD_FAT_TYPE = 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
    #define SD_FAT_TYPE 3
    
    // SDCARD_SS_PIN is defined for the built-in SD on some boards.
    #ifndef SDCARD_SS_PIN
    const uint8_t SD_CS_PIN = SS;
    #else  // SDCARD_SS_PIN
    // Assume built-in SD is used.
    const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
    #endif  // SDCARD_SS_PIN
    
    // Try to select the best SD card configuration.
    #if HAS_SDIO_CLASS
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    #elif ENABLE_DEDICATED_SPI
    #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
    #else  // HAS_SDIO_CLASS
    #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
    #endif  // HAS_SDIO_CLASS
    
    #if SD_FAT_TYPE == 1
    SdFat sd;
    File frec;
    #elif SD_FAT_TYPE == 2
    SdExFat sd;
    ExFile file;
    #elif SD_FAT_TYPE == 3
    SdFs sd;
    FsFile frec,ftx;
    #else  // SD_FAT_TYPE
    //#error Invalid SD_FAT_TYPE
    #endif  // SD_FAT_TYPE
    //************************************************************************************//
    
    /*Change the value of recording flag to enable 4k,8k,16k write
      1 : 4K writes
      2 : 8K writes
      3 : 16K writes
      4 : 12K writes
      comment RECORDING_FLAG to select 512bytes writes
    */
    #define RECORDING_FLAG  5
    
    #if RECORDING_FLAG == 1
    byte buffer[4096] = {0};
    #elif RECORDING_FLAG == 2
    byte buffer[8192] = {0};
    #elif RECORDING_FLAG == 3
    byte buffer[16384] = {0};
    #elif RECORDING_FLAG == 4
    byte buffer[12288] = {0};
    #else
    byte buffer[512] = {0};
    #endif
    
    RTC_DS1307 RTC;
    
    AudioInputI2S            i2s1;
    AudioAmplifier           amp1;
    AudioFilterBiquad        biquad1;
    AudioRecordQueue         queue1;
    AudioConnection          patchCord1(i2s1, 0, amp1, 0);
    AudioConnection          patchCord2(amp1, biquad1);
    AudioConnection          patchCord3(biquad1, queue1);
    
    //********** recording_flags **********************************//
    bool recordInProgress = false;
    bool recording_flag = false;
    bool recording_started = false;
    bool first_file_flag = false;
    //********** recording_constants **********************************//
    
    #define SUB_CHUNK_1_SIZE  16
    #define AUDIO_FORMAT      1     //WAV FILE
    #define NUM_CHANNELS      1     //MONO:1  STEREO:2
    #define BITS_PER_SAMPLE   16    
    #define BITS_PER_BYTE     8
    #define BYTE_RATE         SAMPLE_RATE*NUM_CHANNELS*(BITS_PER_SAMPLE/BITS_PER_BYTE) // SAMPLE_RATE x channels x (BITS_PER_SAMPLE / 8)
    #define BLOCK_ALIGN       NUM_CHANNELS*(BITS_PER_SAMPLE/BITS_PER_BYTE)
    
    //********** recording_variables **********************************//
    unsigned long ChunkSize = 0L;
    unsigned long Subchunk2Size = 0L;
    unsigned long recByteSaved = 0L;
    unsigned long NumSamples = 0L;
    byte byte1, byte2, byte3, byte4;
    char const* Firm_rev = "Ver1.0";
    unsigned long sizeof_icmt_chunk = 0;
    unsigned long length_icmt_comment = 200;
    char icmt_comment[200]={0};
    byte Fdate = 0, Fmonth = 0, Fhour = 0, Fmin = 0, Fsec = 0;
    uint16_t Fyear = 0;
    char filename[50] = {0};
    char folder_buffer[30] = {0};
    uint32_t filenumber = 0;
    uint16_t folder_variable = 0;
    char folder_files[30] = {0};
    uint16_t previous_day = 0;
    static char header[264] = {0};
    //********** recording_functions **********************************//
    void startRecording(void);
    void stopRecording(void);
    void recording(void);
    void writeWavHeader(void);
    void continueRecording(void);
    void dateTime(uint16_t* date, uint16_t* time);
    void writeOutHeader(void);
    void Set_Icmt_comment(void);
    char * wavHeader(void);
    //***************************MAIN CODE************************************
    void setup()
    {
      Serial.begin(BAUD);                      //initialise UART 
      Wire.begin();
      RTC.begin();
        
      DateTime now = RTC.now();
      DateTime compiled = DateTime(__DATE__, __TIME__);
      if (now.unixtime() < compiled.unixtime()) {
      Serial.println("RTC is older than compile time! Updating");
      // following line sets the RTC to the date & time this sketch was compiled    
      RTC.adjust(DateTime(__DATE__, __TIME__));
      }
      
      pinMode_setup();
      FuelGauge.begin();                       //initialise fuel gauge
      Fhour = now.hour();Fmin=now.minute();Fsec=now.second();Fdate=now.day();Fmonth=now.month();Fyear=now.year();
      setTime(Fhour, Fmin, Fsec, Fdate, Fmonth,Fyear);
      SDcard_check();
      DEBUG_PRINT((icmt_comment));
      microphone_initialization();
      queue1.begin();
      first_file_flag = true;
    }
    void loop()
    {
      recording();  
    }
    //*****************************************************************************************************************/
    //Set wave file header with Operator, Device ID, Batt voltage and %
    void Set_Icmt_comment(void)
    {
      snprintf(icmt_comment, sizeof(icmt_comment), "Operator:%s, DeviceID:%s, Batt: %.1fV, Pct:%.1f, Signal: %s-%s, Firm_rev: %s, Timestamp:%d/%02d/%02d-%02d:%02d:%02d, Latitude:%s, Longitude:%s,Clock:%d ", "NA",DEVICE_ID, FuelGauge.voltage(), FuelGauge.percent(),"NA","NA",Firm_rev,year(),month(),day(),hour(),minute(),second(),"NA","NA",F_CPU);
      DEBUG_PRINT((icmt_comment));
    }
    
    //*****************************************************************************************************************/
    
    //initialize microphone and sdcard
    void microphone_initialization()
    {
        AudioMemory(200);
        amp1.gain(MIC_GAIN);
        biquad1.setHighpass(0, 200, 0.707);           //200 is cutoff frequency
        
        setI2SFreq(SAMPLE_RATE);
    }
    
    //*****************************************************************************************************************/
    
    void SDcard_check(void)
    {
    //  if (!(sd.begin(SD_CONFIG)))
      if (!(sd.begin(SdioConfig(FIFO_SDIO))))
      {
        DEBUG_PRINT(("SD Error!"));
        // stop here, but print a message repetitively
        digitalWrite(LED_UPL,HIGH); // SD LED set
        while (1)
        {
          DEBUG_PRINT(("Unable to access the SD card"));
          digitalWrite(ERR_LED,HIGH); // ERRLED set
          delay(500);
          digitalWrite(ERR_LED,LOW); // ERRLED set
          delay(500);
        }
      }
      previous_day = day();
      FsDateTime::callback = dateTime;    //callback function to set time on recorded files
    }
    //*************************Pin initialisation********************************************
    void pinMode_setup()
    {
      pinMode(LED_REC, OUTPUT);
      pinMode(ERR_LED,OUTPUT);
      pinMode(LED_UPL,OUTPUT);
      pinMode(GSM_RTS, OUTPUT);
      pinMode(GSM_CTS, INPUT);
      pinMode(PWR_PIN, OUTPUT);
      pinMode(NTWRK_STAT, INPUT);
      pinMode(LED_NETSTAT,OUTPUT);
      pinMode(BUTTON,INPUT_PULLUP);
      pinMode(MODE_SEL_SWITCH,INPUT);
    }
    
    //*********************************Start Recording******************************************************
    
    void startRecording(void)
    {
      if((day() > previous_day))
      {
        previous_day = day();  
        snprintf(folder_files, sizeof(folder_files), "%s%d%02d%02d_%02d",FOLDER,year(),month(),day(),hour());  
        if (!sd.mkdir(folder_files)) 
        {
          DEBUG_PRINT(("Create Folder1 failed"));
        }
      }
      else if((day() == 1) && (previous_day == 30))
      {
        previous_day = day();  
        snprintf(folder_files, sizeof(folder_files), "%s%d%02d%02d_%02d",FOLDER,year(),month(),day(),hour());  
        if (!sd.mkdir(folder_files)) 
        {
          DEBUG_PRINT(("Create Folder1 failed"));
        }    
      }
      else if((day() == 1) && (previous_day == 31))
      {
        previous_day = day();
        snprintf(folder_files, sizeof(folder_files), "%s%d%02d%02d_%02d",FOLDER,year(),month(),day(),hour());  
        if (!sd.mkdir(folder_files)) 
        {
          DEBUG_PRINT(("Create Folder1 failed"));
        }
      }
      else if(first_file_flag)
      {
        first_file_flag = false;
        snprintf(folder_files, sizeof(folder_files), "%s%d%02d%02d_%02d",FOLDER,year(),month(),day(),hour());      
        if (!sd.mkdir(folder_files)) 
        {
          DEBUG_PRINT(("Create Folder1 failed"));
        }
      }
      recordInProgress = true;
      digitalWrite(LED_UPL, LOW);
      DEBUG_PRINT(("startRecording"));
      DEBUG_PRINT(("new filename"));
      Fhour = hour();Fmin=minute();Fsec=second();Fdate=day();Fmonth=month();Fyear=year();
      snprintf(filename, sizeof(filename), "%s/%s%d%02d%02d_%02d%02d%02d.wav",folder_files,FILE_NAME,Fyear, Fmonth, Fdate, Fhour, Fmin,Fsec);
      DEBUG_PRINT((filename));
      digitalWrite(LED_REC, HIGH);
      Set_Icmt_comment();
      frec = sd.open(filename,O_CREAT | O_TRUNC |O_RDWR);
      DEBUG_PRINT(("File OPEN!"));
      recByteSaved = 0L;
      recording_started = true;
      memcpy(header,wavHeader(),264);
      frec.seek(0);
      frec.write(header,HEADER_SIZE);
    }
    
    //******************************Continue recording**********************************************
    
    void continueRecording(void)
    {
      if(recording_started)
      {
    
    #if RECORDING_FLAG == 1
      if (queue1.available() >= 16)       //accumulate 4K bytes of data in queue
      {
        for(int k=0;k<16;k++)
        {
           uint16_t buff_count = k*256; 
           memcpy(buffer+buff_count, queue1.readBuffer(), 256);
           queue1.freeBuffer();
        }
        frec.write(buffer, 4096);      // write all 4K bytes to the SD card
        recByteSaved += 4096;
      }
    
    #elif RECORDING_FLAG == 2
      if (queue1.available() >= 32)     //accumulate 8K bytes of data in queue
      {
        for(int k=0;k<32;k++)
        {
           uint16_t buff_count = k*256;
           memcpy(buffer+buff_count, queue1.readBuffer(), 256);
           queue1.freeBuffer();
        }
        frec.write(buffer, 8192);      // write all 8K bytes to the SD card
        recByteSaved += 8192;
      }
    #elif RECORDING_FLAG == 3
      if (queue1.available() >= 64)     ////accumulate 16K bytes of data in queue
      {
        for(int k=0;k<64;k++)
        {
           uint16_t buff_count = k*256; 
           memcpy(buffer+buff_count, queue1.readBuffer(), 256);
           queue1.freeBuffer();
        }
        frec.write(buffer, 16384);      // write all 16K bytes to the SD card
        recByteSaved += 16384;
      }
    #elif RECORDING_FLAG == 4
      if (queue1.available() >= 48)       //accumulate 12K bytes of data in queue
      {
        for(int k=0;k<48;k++)
        {
           uint16_t buff_count = k*256;
           memcpy(buffer+buff_count, queue1.readBuffer(), 256);
           queue1.freeBuffer();
        }
        frec.write(buffer, 12288);      // write all 12K bytes to the SD card
        recByteSaved += 12288;
      }  
    #else
      if (queue1.available() >= 2)          //accumulate 512 bytes of data in queue
      {
        memcpy(buffer, queue1.readBuffer(), 256);
        queue1.freeBuffer();
        memcpy(buffer + 256, queue1.readBuffer(), 256);
        queue1.freeBuffer();
        frec.write(buffer, 512);                // write all 512 bytes to the SD card
        recByteSaved += 512;
      }
    #endif
      }
    }
    
    //***********************************************Stop recording*********************************
    void stopRecording(void)
    {
      uint16_t eof_rec = 0;
      DEBUG_PRINT(("stopRecording"));
    //  frec.truncate();
    //  writeOutHeader();
      memcpy(header,wavHeader(),264);
      frec.seek(0);
      frec.write(header,HEADER_SIZE);
      eof_rec =(Subchunk2Size+HEADER_SIZE);
      frec.seek(eof_rec);
      frec.close();
      digitalWrite(LED_REC, LOW);
      digitalWrite(LED_UPL, HIGH);
      recording_started = false;
      recordInProgress = false;
      recording_flag = true;
      filenumber++;
    }
    
    //**************************************************************************************************
    char * wavHeader(void)
    {
      static char wavheader_buffer[264] = {0};  
    
      sizeof_icmt_chunk = (sizeof(icmt_comment)+ 12);
      ChunkSize = recByteSaved + BYTES_IN_HEADER;
      Subchunk2Size = ChunkSize - BYTES_IN_HEADER;
        
      strcpy(wavheader_buffer,"RIFF");
      strcpy(wavheader_buffer+8,"WAVE");
      strcpy(wavheader_buffer+12,"fmt ");
      strcpy(wavheader_buffer+36,"LIST");
      strcpy(wavheader_buffer+44,"INFO");
      strcpy(wavheader_buffer+48,"ICMT");
      strcpy(wavheader_buffer+56,icmt_comment);
      strcpy(wavheader_buffer+256,"data");
      *(int32_t*)(wavheader_buffer+4)=ChunkSize;
      *(int32_t*)(wavheader_buffer+16)= SUB_CHUNK_1_SIZE;// chunk_size
      *(int16_t*)(wavheader_buffer+20)= AUDIO_FORMAT; // PCM 
      *(int16_t*)(wavheader_buffer+22)=NUM_CHANNELS;// numChannels 
      *(int32_t*)(wavheader_buffer+24)= SAMPLE_RATE; // sample rate 
      *(int32_t*)(wavheader_buffer+28)= BYTE_RATE; // byte rate
      *(int16_t*)(wavheader_buffer+32)=BLOCK_ALIGN; // block align
      *(int16_t*)(wavheader_buffer+34)=BITS_PER_SAMPLE; // bits per sample 
      *(int32_t*)(wavheader_buffer+40)=sizeof_icmt_chunk;
      *(int32_t*)(wavheader_buffer+52)=length_icmt_comment;   
      *(int32_t*)(wavheader_buffer+260)=Subchunk2Size;
    
       return wavheader_buffer;
    }
    //**************************************************************************************************
    
    
    /* User provided date time callback function.
       See SdFile::dateTimeCallback() for usage.
    */
    void dateTime(uint16_t* date, uint16_t* time)
    {
    
      // DateTime now = rtc.now();
      // User gets date and time from GPS or real-time
      // clock in real callback function
    
      // return date using FAT_DATE macro to format fields
      // *date = FAT_DATE(year, month, day);
      *date = FAT_DATE(year(), month(), day());
    
      // return time using FAT_TIME macro to format fields
      //  *time = FAT_TIME(hours, minutes, seconds);
      *time = FAT_TIME(hour(), minute(), second());
    }
    //*****************************************************************************************************************/
    //Function to set sample rate
    void setI2SFreq(int freq)
    {
      typedef struct
      {
        uint8_t mult;
        uint16_t div;
      } tmclk;
    
      const int numfreqs = 14;
      const int samplefreqs[numfreqs] = { 8000, 11025, 16000, 22050, 32000, 44100, (int)44117.64706 , 48000, 88200, (int)44117.64706 * 2, 96000, 176400, (int)44117.64706 * 4, 192000};
    
    #if (F_PLL==16000000)
      const tmclk clkArr[numfreqs] = {{16, 125}, {148, 839}, {32, 125}, {145, 411}, {64, 125}, {151, 214}, {12, 17}, {96, 125}, {151, 107}, {24, 17}, {192, 125}, {127, 45}, {48, 17}, {255, 83} };
    #elif (F_PLL==72000000)
      const tmclk clkArr[numfreqs] = {{32, 1125}, {49, 1250}, {64, 1125}, {49, 625}, {128, 1125}, {98, 625}, {8, 51}, {64, 375}, {196, 625}, {16, 51}, {128, 375}, {249, 397}, {32, 51}, {185, 271} };
    #elif (F_PLL==96000000)
      const tmclk clkArr[numfreqs] = {{8, 375}, {73, 2483}, {16, 375}, {147, 2500}, {32, 375}, {147, 1250}, {2, 17}, {16, 125}, {147, 625}, {4, 17}, {32, 125}, {151, 321}, {8, 17}, {64, 125} };
    #elif (F_PLL==120000000)
      const tmclk clkArr[numfreqs] = {{32, 1875}, {89, 3784}, {64, 1875}, {147, 3125}, {128, 1875}, {205, 2179}, {8, 85}, {64, 625}, {89, 473}, {16, 85}, {128, 625}, {178, 473}, {32, 85}, {145, 354} };
    #elif (F_PLL==144000000)
      const tmclk clkArr[numfreqs] = {{16, 1125}, {49, 2500}, {32, 1125}, {49, 1250}, {64, 1125}, {49, 625}, {4, 51}, {32, 375}, {98, 625}, {8, 51}, {64, 375}, {196, 625}, {16, 51}, {128, 375} };
    #elif (F_PLL==168000000)
      const tmclk clkArr[numfreqs] = {{32, 2625}, {21, 1250}, {64, 2625}, {21, 625}, {128, 2625}, {42, 625}, {8, 119}, {64, 875}, {84, 625}, {16, 119}, {128, 875}, {168, 625}, {32, 119}, {189, 646} };
    #elif (F_PLL==180000000)
      const tmclk clkArr[numfreqs] = {{46, 4043}, {49, 3125}, {73, 3208}, {98, 3125}, {183, 4021}, {196, 3125}, {16, 255}, {128, 1875}, {107, 853}, {32, 255}, {219, 1604}, {214, 853}, {64, 255}, {219, 802} };
    #elif (F_PLL==192000000)
      const tmclk clkArr[numfreqs] = {{4, 375}, {37, 2517}, {8, 375}, {73, 2483}, {16, 375}, {147, 2500}, {1, 17}, {8, 125}, {147, 1250}, {2, 17}, {16, 125}, {147, 625}, {4, 17}, {32, 125} };
    #elif (F_PLL==216000000)
      const tmclk clkArr[numfreqs] = {{32, 3375}, {49, 3750}, {64, 3375}, {49, 1875}, {128, 3375}, {98, 1875}, {8, 153}, {64, 1125}, {196, 1875}, {16, 153}, {128, 1125}, {226, 1081}, {32, 153}, {147, 646} };
    #elif (F_PLL==240000000)
      const tmclk clkArr[numfreqs] = {{16, 1875}, {29, 2466}, {32, 1875}, {89, 3784}, {64, 1875}, {147, 3125}, {4, 85}, {32, 625}, {205, 2179}, {8, 85}, {64, 625}, {89, 473}, {16, 85}, {128, 625} };
    #endif
    
      for (int f = 0; f < numfreqs; f++) {
        if ( freq == samplefreqs[f] ) {
          while (I2S0_MCR & I2S_MCR_DUF) ;
          I2S0_MDR = I2S_MDR_FRACT((clkArr[f].mult - 1)) | I2S_MDR_DIVIDE((clkArr[f].div - 1));
          return;
        }
      }
    }
    
    //*****************************************************************************************************************/
    void recording(void)
    {
      if (!recordInProgress)
      {
        startRecording();
      }
      else if ((recByteSaved < (SAMPLE_RATE * 2 * REC_TIME)))  
      {
        continueRecording();
      }
      else if (recordInProgress && (recByteSaved >= (SAMPLE_RATE * 2 * REC_TIME)))
      {
        stopRecording();
      }
    }

  9. #9
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    The solution to your puzzle seems to be:
    the time jitter, you are observing, is real and you do not loose data.

    Reason: you are starting/stopping recording based on recByteSaved, which in incremented in multiple kB.
    Open/closing is not in sync with RTC. So, so most of the time you will close files that contain more than 10 s worth of data, but this cannot go forever, after some number of files, you are closing after 9 s and then with 11 s. I think there is a ms option in the RTC, you may use to have a better time stamp.

  10. #10
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    Do you mean its just time sync issue?, because i did perform continuity check with audio(Example: playing 1-1000 count in a loop on music player and recording on device). What i observed is, files with 9sec and 11sec gap are missing audio(0.5s-1sec of data when one file ends and other starts) and as i am doing continuous recording i am not ending queue after every file still i don't understand why i am loosing data.

  11. #11
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    Adapted code to my T3.6 configuration to get it running
    Cannot see your time jitter
    could you replace loop by
    Code:
    void loop()
    { 
      static uint32_t t0=0, t2=0, tx, ty;
      tx=micros(),
      recording();
      ty=micros()-tx;
      if((ty)>t2) t2=ty;
      if(tx-t0>1000000)
      {
        Serial.printf(" %6d %3d\n",t2, AudioMemoryUsageMax());
        t2=0;
        AudioMemoryUsageMaxReset();
        t0=tx;
      }
    }
    to see where program is hanging?

    Edit: the record_queue is limited to 53 blocks. If AudioMemoryUsageMax exceeds this value, very likely you are running out of buffers.
    In this case you may need to edit the value in the library to say 100 in record_queue.h and record_queue.cpp. better use a constant
    Last edited by WMXZ; 10-08-2019 at 03:01 PM. Reason: added record_queue limitation

  12. #12
    Junior Member
    Join Date
    Jul 2019
    Posts
    16
    I changed the loop in my code and used one you mentioned above. 11sec gap doesn't occur at the start of recording, it occur when some 5K-10k files are recorded on sdcard. I logged the results and you can see seconds shifting by 1sec in one of the timestamps in the below logs. Check timestamp after this one Timestamp:2049/07/02-23:23:50, i can also see queue value going above 53 when this time shift occurs. As you suggested, i'll try increasing the queue value from 53 and test it.

    Code:
    23:52:17.325 -> startRecording : Line: 279
    23:52:17.325 -> new filename : Line: 280
    23:52:17.325 -> FLD20490702_17/P20490702_232300.wav : Line: 283
    23:52:17.359 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:00, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:52:17.495 -> File OPEN! : Line: 297
    23:52:17.903 ->  138892  22
    23:52:18.893 ->     156   5
    23:52:19.883 ->     147   5
    23:52:20.875 ->     147   5
    23:52:21.903 ->     152   5
    23:52:22.891 ->     149   5
    23:52:23.879 ->     149   5
    23:52:24.900 ->     148   5
    23:52:25.892 ->     157   5
    23:52:26.881 ->     156   5
    23:52:27.328 -> stopRecording : Line: 379
    23:52:27.328 -> startRecording : Line: 279
    23:52:27.328 -> new filename : Line: 280
    23:52:27.328 -> FLD20490702_17/P20490702_232310.wav : Line: 283
    23:52:27.362 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:10, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:52:27.462 -> File OPEN! : Line: 297
    23:52:27.873 ->  138458  22
    23:52:28.897 ->     158   5
    23:52:29.886 ->     149   5
    23:52:30.875 ->     149   5
    23:52:31.904 ->     149   5
    23:52:32.897 ->     149   5
    23:52:33.884 ->     152   5
    23:52:34.876 ->     149   5
    23:52:35.900 ->     152   5
    23:52:36.889 ->     149   5
    23:52:37.332 -> stopRecording : Line: 379
    23:52:37.332 -> startRecording : Line: 279
    23:52:37.332 -> new filename : Line: 280
    23:52:37.332 -> FLD20490702_17/P20490702_232320.wav : Line: 283
    23:52:37.367 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:20, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:52:37.470 -> File OPEN! : Line: 297
    23:52:37.883 ->  139709  22
    23:52:38.878 ->     149   5
    23:52:39.903 ->     158   5
    23:52:40.894 ->     149   5
    23:52:41.887 ->     157   5
    23:52:42.882 ->     157   5
    23:52:43.906 ->     157   5
    23:52:44.877 ->   16891   5
    23:52:45.899 ->   14079   5
    23:52:46.889 ->     150   5
    23:52:47.332 -> stopRecording : Line: 379
    23:52:47.332 -> startRecording : Line: 279
    23:52:47.332 -> new filename : Line: 280
    23:52:47.332 -> FLD20490702_17/P20490702_232330.wav : Line: 283
    23:52:47.365 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:30, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:52:47.468 -> File OPEN! : Line: 297
    23:52:47.877 ->  139027  22
    23:52:48.903 ->     146   5
    23:52:49.896 ->     147   5
    23:52:50.887 ->     146   5
    23:52:51.879 ->     147   5
    23:52:52.903 ->     146   5
    23:52:53.885 ->     156   5
    23:52:54.877 ->     146   5
    23:52:55.899 ->     147   5
    23:52:56.887 ->     155   5
    23:52:57.329 -> stopRecording : Line: 379
    23:52:57.329 -> startRecording : Line: 279
    23:52:57.329 -> new filename : Line: 280
    23:52:57.329 -> FLD20490702_17/P20490702_232340.wav : Line: 283
    23:52:57.362 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:40, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:52:57.465 -> File OPEN! : Line: 297
    23:52:57.878 ->  108594  18
    23:52:58.902 ->     147   5
    23:52:59.897 ->     155   5
    23:53:00.884 ->     148   5
    23:53:01.877 ->     154   5
    23:53:02.905 ->     155   5
    23:53:03.894 ->     146   5
    23:53:04.884 ->     155   5
    23:53:05.875 ->     154   5
    23:53:06.899 ->     147   5
    23:53:07.309 -> stopRecording : Line: 379
    23:53:07.344 -> startRecording : Line: 279
    23:53:07.344 -> new filename : Line: 280
    23:53:07.344 -> FLD20490702_17/P20490702_232350.wav : Line: 283
    23:53:07.344 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:23:50, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:53:07.447 -> File OPEN! : Line: 297
    23:53:07.892 ->  108725  18
    23:53:08.883 ->     146   5
    23:53:09.881 ->     150   5
    23:53:10.901 ->     147   5
    23:53:11.895 ->     155   5
    23:53:12.885 ->     147   5
    23:53:13.883 ->     149   5
    23:53:14.873 ->     155   5
    23:53:15.900 ->   26393   6
    23:53:16.891 ->     155   5
    23:53:17.335 -> stopRecording : Line: 379
    23:53:17.712 -> startRecording : Line: 279
    23:53:17.712 -> new filename : Line: 280
    23:53:17.712 -> FLD20490702_17/P20490702_232401.wav : Line: 283
    23:53:17.712 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:24:01, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:53:17.919 -> File OPEN! : Line: 297
    23:53:17.988 ->  385505  56
    23:53:18.911 ->  118645  56
    23:53:19.937 ->     155   5
    23:53:20.923 ->     147   5
    23:53:21.919 ->     146   5
    23:53:22.943 ->     147   5
    23:53:23.936 ->     156   5
    23:53:24.925 ->     155   5
    23:53:25.913 ->     147   5
    23:53:26.936 ->     149   5
    23:53:27.585 -> stopRecording : Line: 379
    23:53:28.030 -> startRecording : Line: 279
    23:53:28.030 -> new filename : Line: 280
    23:53:28.030 -> FLD20490702_17/P20490702_232411.wav : Line: 283
    23:53:28.030 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:24:11, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:53:28.167 -> File OPEN! : Line: 297
    23:53:28.167 ->  437072  56
    23:53:29.020 ->    2220  54
    23:53:30.012 ->     156   5
    23:53:31.004 ->     146   5
    23:53:32.030 ->     146   5
    23:53:33.026 ->     146   5
    23:53:34.017 ->     155   5
    23:53:35.011 ->     156   5
    23:53:36.002 ->     149   5
    23:53:37.026 ->     155   5
    23:53:37.745 -> stopRecording : Line: 379
    23:53:37.812 -> startRecording : Line: 279
    23:53:37.812 -> new filename : Line: 280
    23:53:37.812 -> FLD20490702_17/P20490702_232421.wav : Line: 283
    23:53:37.846 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:24:21, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:53:37.949 -> File OPEN! : Line: 297
    23:53:38.017 ->  143538  30
    23:53:39.006 ->     147   5
    23:53:40.027 ->     146   5
    23:53:41.019 ->     146   5
    23:53:42.008 ->     147   5
    23:53:43.027 ->     147   5
    23:53:44.017 ->     147   5
    23:53:45.010 ->     155   5
    23:53:46.001 ->     155   5
    23:53:47.028 ->     147   5
    23:53:47.744 -> stopRecording : Line: 379
    23:53:47.778 -> startRecording : Line: 279
    23:53:47.778 -> new filename : Line: 280
    23:53:47.778 -> FLD20490702_17/P20490702_232431.wav : Line: 283
    23:53:47.778 -> Operator:NA, DeviceID:DEV1, Batt: 5.0V, Pct:256.0, Signal: NA-NA, Firm_rev: Ver1.0, Timestamp:2049/07/02-23:24:31, Latitude:NA, Longitude:NA,Clock:48000000  : Line: 185
    23:53:47.915 -> File OPEN! : Line: 297
    23:53:48.018 ->  136740  23
    23:53:49.014 ->   24321   6
    23:53:50.012 ->   17700   5
    23:53:51.002 ->     146   5
    23:53:52.001 ->     150   5
    23:53:53.025 ->     147   5
    23:53:54.012 ->     155   5
    23:53:55.003 ->     156   5
    23:53:56.027 ->     147   5
    23:53:57.017 ->     156   5
    23:53:57.733 -> stopRecording : Line: 379

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •