1010#define USE_SDFAT
1111#include " AudioTools/Disk/SDIndex.h"
1212
13- // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
14- // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
15- #ifndef SD_FAT_TYPE
16- #define SD_FAT_TYPE 1
17- #endif
1813// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
1914#define SPI_CLOCK SD_SCK_MHZ (50 )
20- #if SD_FAT_TYPE == 0
21- typedef SdFat AudioFs;
22- typedef File AudioFile;
23- #elif SD_FAT_TYPE == 1
24- typedef SdFat32 AudioFs;
25- typedef File32 AudioFile;
26- #elif SD_FAT_TYPE == 2
27- typedef SdExFat AudioFs;
28- typedef ExFile AudioFile;
29- #elif SD_FAT_TYPE == 3
30- typedef SdFs AudioFs;
31- typedef FsFile AudioFile;
32- #endif
3315
3416namespace audio_tools {
3517/* *
3618 * @brief ESP32 AudioSource for AudioPlayer using an SD card as data source.
3719 * An index file is used to speed up the access to the audio files by index.
3820 * This class is based on the Arduino SD implementation
3921 * For UTF8 Support change SdFatConfig.h #define USE_UTF8_LONG_NAMES 1
22+ * @param <SdFat32, File32>, <SdFs, FsFile>, <SdExFat, ExFile>, <SdFat, File>
4023 * @ingroup player
4124 * @author Phil Schatzmann
4225 * @copyright GPLv3
4326 */
27+ template <typename AudioFs = SdFat32, typename AudioFile = File32>
4428class AudioSourceIdxSDFAT : public AudioSource {
4529 public:
4630 // / Default constructor
@@ -69,6 +53,20 @@ class AudioSourceIdxSDFAT : public AudioSource {
6953 setup_index = setupIndex;
7054 }
7155
56+ // / Constructor for providing an open FS
57+ AudioSourceIdxSDFAT (AudioFs fs, const char *startFilePath=" /" , const char *ext=" " , bool setupIndex = true ){
58+ TRACED ();
59+ sd = fs;
60+ p_cfg = nullptr ;
61+ owns_cfg = false ;
62+ start_path = startFilePath;
63+ exension = ext;
64+ setup_index = setupIndex;
65+ is_sd_setup = true ;
66+ // since we expect an open fs we do not close it
67+ is_close_sd = false ;
68+ }
69+
7270 virtual ~AudioSourceIdxSDFAT () { end (); }
7371
7472 virtual void begin () override {
@@ -87,7 +85,7 @@ class AudioSourceIdxSDFAT : public AudioSource {
8785 void end () {
8886 if (is_sd_setup) {
8987#ifdef ESP32
90- sd.end ();
88+ if (is_close_sd) sd.end ();
9189#endif
9290 if (owns_cfg) delete (p_cfg);
9391 is_sd_setup = false ;
@@ -162,6 +160,7 @@ class AudioSourceIdxSDFAT : public AudioSource {
162160 const char *start_path = nullptr ;
163161 const char *file_name_pattern = " *" ;
164162 bool setup_index = true ;
163+ bool is_close_sd = true ;
165164 bool is_sd_setup = false ;
166165 int cs;
167166 bool owns_cfg = false ;
0 commit comments