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

Thread: long file names and play audio from SD card

  1. #1
    Member Moo's Avatar
    Join Date
    Oct 2016
    Location
    London
    Posts
    20

    long file names and play audio from SD card

    Has anybody managed to get the audio library to play from SD and read long filenames too?

    Im writing a file browser to display and allow users to select files and navigate directory. using 8.3 filenames is something I'd really like to avoid if possible.

    my issue is play_sd_audio is coupled to SD library which appears to only support 8.3 file names.

    One solution would be write a play_sdfat_audio class using SDFat library - I think this seems like my best chance..

    The other option is to see if I can port the long file name functions from SdFat to Sd libraries, im not sure if this is possible, but I think it would be a better solution...

    My code is here: https://github.com/newdigate/teensy-tft-sd-browser

    Anybody have any insight and/or opinions?

  2. #2
    Senior Member
    Join Date
    Dec 2013
    Posts
    213
    Has anybody managed to get the audio library to play from SD and read long filenames too?
    Teensy Loader 1.45v ~ Arduino 1.8.8v.
    Modified 4 files in Audio library that plays nicely with long filenames and Arduino SdFat library.
    Attached Files Attached Files

  3. #3
    Senior Member
    Join Date
    Dec 2013
    Posts
    213
    WavFilePlayerUSB
    NOTE: This player needs wav file with long name on SD Card: (SDTEST1 test long name.wav)

    Code:
    #define USE_SdFat_ // uncomment for SdFat.h, comment for SD.h lib.
    // NOTE: uncomment~comment also in play_sd_raw.cpp & play_sd_wav.cpp
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    
    #ifdef USE_SdFat_
    #include <SdFat.h>
    //SdFatSdioEX SD; // SdFatSdioEX uses extended multi-block transfers without DMA.
    SdFatSdio SD; // SdFatSdio uses a traditional DMA SDIO implementation.
    // Note the difference is speed and busy yield time.
    // Teensy 3.5 & 3.6 SDIO: 4 to 5 times the throughput using the 4-bit SDIO mode compared to the 1-bit SPI mode
    #else
    #include <SD.h>
    #endif
    #include <SerialFlash.h>
    
    File root;
    File entry;
    
    AudioPlaySdWav           playWav1;
    AudioOutputUSB           audioOutput; // must set Tools > USB Type to Audio
    AudioOutputAnalog        dac;
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    AudioConnection          patchCord3(playWav1, 0, dac, 0);
    
    // Use these with the Teensy Audio Shield
    //#define SDCARD_CS_PIN    10
    //#define SDCARD_MOSI_PIN  7
    //#define SDCARD_SCK_PIN   14
    
    ////////// For Sd.h lib. only /////////
    // Use these with the Teensy 3.5 & 3.6 SD card
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11  // not actually used
    #define SDCARD_SCK_PIN   13  // not actually used
    
    // Use these for the SD+Wiz820 or other adaptors
    //#define SDCARD_CS_PIN    4
    //#define SDCARD_MOSI_PIN  11
    //#define SDCARD_SCK_PIN   13
    
    elapsedMillis since_Update;
    int SEC = 0;
    
    void setup() {
      Serial.begin(9600);
      delay(1000);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(20);
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
    
    
    #ifdef USE_SdFat_
      if (!(SD.begin())) { //<///////  SdFat.h  /////////
        delay(500);
        Serial.println(" SdFat.h ");
        // stop here, but print a message repetitively
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    #else                 //<///////   Sd.h   /////////
      if (!(SD.begin(SDCARD_CS_PIN))) {
        // stop here, but print a message repetitively
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    #endif
    
    #if defined(SdFat_h)
      Serial.print( "SdFat version "), Serial.println( SD_FAT_VERSION ), Serial.println("");
    #elif defined(__SD_H__)
      Serial.println( "SD - a slightly more friendly wrapper for sdfatlib"), Serial.println("");
    #endif
    
    #ifdef USE_SdFat_
      char filename[255]; //
      root = SD.open("/"); // ROOT
      while (true) {
        File entry = root.openNextFile();
        entry.getName(filename, sizeof(filename));
    
        if (entry.isDirectory()) {
          Serial.print("Dir.  "), Serial.println(filename);
        } else {
          Serial.print("      "), Serial.println(filename);
        }
        if (! entry) {
          // no more files
          Serial.println(" **nomorefiles** ");
          break;
        }
      }
      entry.close();
      root.close();
    #endif
    }
    
    void playFile(const char *filename)
    {
      playWav1.play(filename);
      // A brief delay for the library read WAV info
      delay(5);
      // Simply wait for the file to finish playing.
      while (playWav1.isPlaying()) {
        if (since_Update > 999) {
          since_Update = 0;
          Serial.print(" SEC: "), Serial.println(SEC);
          SEC++;
        }
      }
      SEC = 0;
    }
    
    void loop() {
      playFile("SDTEST1 test long name.WAV");  // uppercase or lowercase 255 long max.
      delay(500);
      /*playFile("SDTEST2.WAV");
        delay(500);
        playFile("SDTEST3.WAV");
        delay(500);
        playFile("SDTEST4.WAV");
        delay(1500);*/
    }
    
    // A known problem occurs on Macintosh computers, where the Mac's driver
    // does not seem to be able to adapt and transmit horribly distorted
    // audio to Teensy after a matter of minutes.  An imperfect workaround
    // can be enabled by editing usb_audio.cpp.  Find and uncomment
    // "#define MACOSX_ADAPTIVE_LIMIT".  More detailed info is available here:
    // https://forum.pjrc.com/threads/34855-Distorted-audio-when-using-USB-input-on-Teensy-3-1?p=110392&viewfull=1#post110392

  4. #4
    Member Moo's Avatar
    Join Date
    Oct 2016
    Location
    London
    Posts
    20
    Thank you Chris! Thats awesome work! Really appreciate that...

Posting Permissions

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