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

Thread: Audio files corrupt after some time !

  1. #1
    Junior Member
    Join Date
    Jan 2017
    Posts
    8

    Audio files corrupt after some time !

    Hi !

    I have a project of 11 audio players running in a small theme park. It is mounted in a boatride and it reads RFID data from the serial input and plays files on different locations. Everything is running fine except that the files (or SDHC card) will get corrupt after 3 weeks of usage. When the park's maintenance team replaces the files on the same SDHC, everything is fine again. (for about 3 weeks lol)

    I used the read-only simplified SD library from Mr. Stoffregen and i formatted all the cards using SDFormatter before putting anything on the cards. (mac)
    I also produced the sound files and it's very unlikely there's something incorrect with the encoding. I used quality Sandisk 8GB SDHC cards. I am also using a quality Meanwell 5v power supply.

    I must add that every ride cycle (about 10 minutes) the units will be disconnected for 30 secs. This is because the boat will recharge the on-board battery. The environment is very humid and warm but i couldn't imagine this is why the files become corrupt.

    Hopefully you can find something incorrect in my code.... (?)


    Code:
    #include <SD.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    
    // GUItool: begin automatically generated code
    AudioPlaySdWav           B_Player;     //xy=79,257
    AudioPlaySdWav           A_Player;       //xy=81,172
    AudioEffectFade          B_Fader_L;          //xy=264,244
    AudioEffectFade          B_Fader_R;          //xy=264,277
    AudioEffectFade          A_Fader_L;          //xy=265,142
    AudioEffectFade          A_Fader_R;          //xy=265,178
    AudioMixer4              mixer_Left ;         //xy=464,164
    AudioMixer4              mixer_Right;         //xy=468,261
    AudioOutputI2S           audioOutput;    //xy=649,211
    AudioConnection          patchCord1(B_Player, 0, B_Fader_L, 0);
    AudioConnection          patchCord2(B_Player, 1, B_Fader_R, 0);
    AudioConnection          patchCord3(A_Player, 0, A_Fader_L, 0);
    AudioConnection          patchCord4(A_Player, 1, A_Fader_R, 0);
    AudioConnection          patchCord5(B_Fader_L, 0, mixer_Left, 1);
    AudioConnection          patchCord6(B_Fader_R, 0, mixer_Right, 1);
    AudioConnection          patchCord7(A_Fader_L, 0, mixer_Left, 0);
    AudioConnection          patchCord8(A_Fader_R, 0, mixer_Right, 0);
    AudioConnection          patchCord9(mixer_Left, 0, audioOutput, 0);
    AudioConnection          patchCord10(mixer_Right, 0, audioOutput, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=94,523
    // GUItool: end automatically generated code
    
    
    // Pinout for SDCARD on audio adaptor board
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    // Program variables
    String    inMessage ;      // Holds the latest incoming serial message
    int       parseTimer = 30;    // Timeout for WAV parsing after call
    int       fadeTime = 3000;    // Global fade time
    int       cpuCurrent ;        // Holds the current cpu value when 'cpu' command has been called
    int       cpuMax;             // Holds the max cpu when 'cpu' command has been called
    int       memoryCurrent;      // Holds the current memory value when 'mem' command has been called
    int       memoryMax;          // Holds the max memory when 'mem' command has been called
    int       cpuCounter ;        // used for incrementing in do-Loop (yes i know we can do this differently)
    int       memoryCounter ;     // used for incrementing in do-Loop (yes i know we can do this differently)
    int       show1Cycles ;       // Holds the number of shows played
    int       show2Cycles ;       // Holds the number of shows played
    int       show3Cycles ;       // Holds the number of shows played
    int       show4Cycles ;       // Holds the number of shows played
    int       show5Cycles ;       // Holds the number of shows played
    int       show6Cycles ;       // Holds the number of shows played
    int       showStatus = 0;     // Keeps track of the show state, so the same show isn't triggered again
    
    void setup() {
    
      Serial.begin(9600);
      Serial1.begin(9600);
      Serial.setTimeout(50);
      Serial1.setTimeout(50);
    
      // Audio memory allocation
      AudioMemory(16);
    
      // Activate the chip and default volume settings
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    
      // Default mixer settings
      mixer_Left.gain(0, 0.6);
      mixer_Left.gain(1, 0.6);
      mixer_Right.gain(0, 0.6);
      mixer_Right.gain(1, 0.6);
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        // Stop here, but print a message repetitively
        while (1) {
          Serial.println(F("Unable to access the SD card"));
          delay(500);
        }
      }
    }
    
    void loop() {
    
      // Read from the UART serial
      if (Serial.available()) {
        inMessage = Serial.readString();
        Serial.print(F("USB : "));
        Serial.println(inMessage);
        msgEval();
      }
      // Read from the hardware port (RFID reader)
      if (Serial1.available()) {
        inMessage = Serial1.readString();
        Serial.print(F("Serial : "));
        Serial.println(inMessage);
        msgEval();
      }
    }


    Code:
    void msgEval() {
    
      // Player A
      if (inMessage == "01\r" && showStatus != 1) {
        Serial.println(F("Playing TR01"));
        Serial1.println(F("Playing TR01"));
        showStatus = 1;
        show1Cycles++ ;
        inMessage = "" ;
    
        // Mixer settings
        A_Fader_L.fadeIn(fadeTime);
        A_Fader_R.fadeIn(fadeTime);
        B_Fader_L.fadeOut(fadeTime);
        B_Fader_R.fadeOut(fadeTime);
    
        // Play the files
        A_Player.play("1.WAV"); // Player A
        delay(fadeTime + 100);
        B_Player.stop(); // Player
        delay(parseTimer);
      }
    
      // Player B
      if (inMessage == "02\r" && showStatus != 2) {
        Serial.println(F("Playing TR02"));
        showStatus = 2;
        show2Cycles++ ;
        inMessage = "" ;
    
        // Track 2
        delay(3000);
        B_Player.play("2.WAV");
        delay(parseTimer);
    
        // Mixer settings
        A_Fader_L.fadeOut(fadeTime);
        A_Fader_R.fadeOut(fadeTime);
        B_Fader_L.fadeIn(fadeTime);
        B_Fader_R.fadeIn(fadeTime);
    
        // Stop the previous track
        delay(fadeTime + 100);
        A_Player.stop();
      }
    
      // Player A
      if (inMessage == "03\r" && showStatus != 3) {
        Serial.println(F("Playing TR03"));
        delay(5000);
        showStatus = 3;
        show3Cycles++ ;
        inMessage = "" ;
    
        // Track 3
        A_Player.play("3.WAV");
        delay(parseTimer);
    
        // Mixer settings
        B_Fader_L.fadeOut(fadeTime);
        B_Fader_R.fadeOut(fadeTime);
        A_Fader_L.fadeIn(fadeTime);
        A_Fader_R.fadeIn(fadeTime);
    
        // Stop the previous track
        delay(2000);
        B_Player.stop();
      }
    
      // Player B
      if (inMessage == "04\r" && showStatus != 4) {
        // Play the new track
        Serial.println(F("Playing TR04"));
        showStatus = 4;
        show4Cycles++ ;
        inMessage = "" ;
    
        // Track04
        B_Player.play("4.WAV");
        delay(parseTimer);
    
        // Mixer settings
        A_Fader_L.fadeOut(fadeTime);
        A_Fader_R.fadeOut(fadeTime);
        B_Fader_L.fadeIn(fadeTime);
        B_Fader_R.fadeIn(fadeTime);
    
        // Stop the previous track
        delay(fadeTime + 100);
        A_Player.stop();
      }
    
      // Player A
      if (inMessage == "05\r" && showStatus != 5) {
        Serial.println(F("Playing TR05"));
        showStatus = 5;
        show5Cycles++ ;
        inMessage = "" ;
    
        // Track 05
        A_Player.play("5.WAV");
        delay(parseTimer);
    
        // Mixer settings
        B_Fader_L.fadeOut(fadeTime);
        B_Fader_R.fadeOut(fadeTime);
        A_Fader_L.fadeIn(fadeTime);
        A_Fader_R.fadeIn(fadeTime);
    
        // Stop the previous track
        delay(fadeTime + 100);
        B_Player.stop();
    
        delay(22000);
    
        // Player B
        Serial.println(F("Playing TR06"));
        showStatus = 5;
        show6Cycles++ ;
        inMessage = "" ;
    
        // Track06
        B_Player.play("6.WAV");
        delay(parseTimer);
    
        // Mixer settings
        A_Fader_L.fadeOut(fadeTime);
        A_Fader_R.fadeOut(fadeTime);
        B_Fader_L.fadeIn(fadeTime);
        B_Fader_R.fadeIn(fadeTime);
    
        // Stop the previous track
        delay(fadeTime + 100);
        A_Player.stop();
    
      }
    
    
      if (inMessage == "stop\r" && showStatus != 0) {
        Serial.println(F("Stopped !"));
        showStatus = 0;
        A_Fader_L.fadeOut(1000);
        B_Fader_L.fadeOut(1000);
        A_Fader_R.fadeOut(1000);
        B_Fader_R.fadeOut(1000);
    
        delay(1100);
        A_Player.stop();
        B_Player.stop();
    
      }
    
      if (inMessage == "?\r") {
        Serial.println(F("---Show Cycles---"));
        Serial.print(F("Show 1 : "));
        Serial.println(show1Cycles);
    
        Serial.print(F("Show 2 : "));
        Serial.println(show2Cycles);
    
        Serial.print(F("Show 3 : "));
        Serial.println(show3Cycles);
    
        Serial.print(F("Show 4 : "));
        Serial.println(show4Cycles);
    
        Serial.print(F("Show 5 : "));
        Serial.println(show5Cycles);
        Serial.println(F("-----------------"));
      }
    
      if (inMessage == "clearshows\r") {
        Serial.println(F("---Show Cycles---"));
        show1Cycles = 0;
        show2Cycles = 0;
        show3Cycles = 0;
        show4Cycles = 0;
        show5Cycles = 0;
        Serial.print(F("Show 1 : "));
        Serial.println(show1Cycles);
    
        Serial.print(F("Show 2 : "));
        Serial.println(show2Cycles);
    
        Serial.print(F("Show 3 : "));
        Serial.println(show3Cycles);
    
        Serial.print(F("Show 4 : "));
        Serial.println(show4Cycles);
    
        Serial.print(F("Show 5 : "));
        Serial.println(show5Cycles);
        Serial.println(F("-----------------"));
      }
    
      if (inMessage == "cpu\r") {
        Serial.println(F("-----CPU test started-----"));
        do  {
          cpuCurrent = AudioProcessorUsage();
          Serial.print(F("CPU : "));
          Serial.println(cpuCurrent);
          cpuCounter++ ;
          delay(75);
        } while (cpuCounter < 100);
        cpuCounter = 0;
        cpuMax = AudioProcessorUsageMax();
        Serial.println(F("-----CPU test complete !-----"));
        Serial.println(F("-----------------------------"));
        Serial.print(F("Max CPU : "));
        Serial.println(cpuMax);
        AudioProcessorUsageMaxReset();
      }
    
      if (inMessage == "mem\r") {
        Serial.println(F("-----MEMORY test started-----"));
        do  {
          memoryCurrent = AudioMemoryUsage();
          Serial.print(F("Memory : "));
          Serial.println(memoryCurrent);
          memoryCounter++ ;
          delay(75);
        } while (memoryCounter < 100);
        memoryCounter = 0;
        memoryMax = AudioMemoryUsageMax();
        Serial.println(F("-----MEMORY test complete !-----"));
        Serial.println(F("--------------------------------"));
        Serial.print(F("Max Memory : "));
        Serial.println(memoryMax);
        AudioMemoryUsageMaxReset();
      }
    }

  2. #2
    I was having this same issue. My solution was to stop using SanDisk. I've begun using ADATA brand SHDC cards. If you have access to a MicroCenter store, their branded SD cards are actually ADATA and are about half the price So far so good. No more file corruptions.

  3. #3
    Junior Member
    Join Date
    Jan 2017
    Posts
    8
    Quote Originally Posted by lerxstrulz View Post
    I was having this same issue. My solution was to stop using SanDisk. I've begun using ADATA brand SHDC cards. If you have access to a MicroCenter store, their branded SD cards are actually ADATA and are about half the price So far so good. No more file corruptions.

    Good to know there are more people having this issue. ADATA is pretty hard to get in my country (The Netherlands) would Kingston be a substitute ?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,596
    Any idea if the bad card was a genuine Sandisk? The market is flooded with counterfeits claiming to be Sandisk.

  5. #5
    Junior Member
    Join Date
    Jan 2017
    Posts
    8
    Quote Originally Posted by PaulStoffregen View Post
    Any idea if the bad card was a genuine Sandisk? The market is flooded with counterfeits claiming to be Sandisk.

    Hi Paul,

    I got the cards from 2 different vendors, at least one of them was a trusted dealer and one of the top vendors here in The Netherlands. I did some random card testing and the speed also looked good. The packaging was all original, no spelling errors or anything. Keep in mind all my 11 projects are failing. they are either all counterfeits or all original

    Have you ever seen the same issue before ?

  6. #6
    I have not tried Kingston. I do have a PNY that has also had no problems. My SanDisks I was having to reformat every couple of weeks or so.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,596
    I saw something like this just once, but at the time wrote it off as a low quality SD card (which is was).

    Maybe there is something else wrong? Hard to say.

    If I wanted to recreate your circumstances here, how would I go about replacing the RFID reader? Maybe another Teensy could send the same or similar-enough commands? Any chance you could help with that part?

Posting Permissions

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