From 443979673f251e3a2a1b302915236bcb1357e05d Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 18 Aug 2017 08:18:22 -0700 Subject: [PATCH] Add SD card file input class --- src/AudioFileSourceSD.cpp | 78 +++++++++++++++++++++++++++++++++++++++ src/AudioFileSourceSD.h | 49 ++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/AudioFileSourceSD.cpp create mode 100644 src/AudioFileSourceSD.h diff --git a/src/AudioFileSourceSD.cpp b/src/AudioFileSourceSD.cpp new file mode 100644 index 0000000..8e1fcfb --- /dev/null +++ b/src/AudioFileSourceSD.cpp @@ -0,0 +1,78 @@ +/* + AudioFileSourceSPIFFS + Input SD card "file" to be used by AudioGenerator + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "AudioFileSourceSD.h" + +AudioFileSourceSD::AudioFileSourceSD() +{ +} + +AudioFileSourceSD::AudioFileSourceSD(const char *filename) +{ + open(filename); +} + +bool AudioFileSourceSD::open(const char *filename) +{ + f = SD.open(filename, FILE_READ); + return f; +} + +AudioFileSourceSD::~AudioFileSourceSD() +{ + if (f) f.close(); +} + +uint32_t AudioFileSourceSD::read(void *data, uint32_t len) +{ + return f.read(reinterpret_cast(data), len); +} + +bool AudioFileSourceSD::seek(int32_t pos, int dir) +{ + if (!f) return false; + if (dir==SEEK_SET) return f.seek(pos); + else if (dir==SEEK_CUR) return f.seek(f.position() + pos); + else if (dir==SEEK_END) return f.seek(f.size() + pos); + return false; +} + +bool AudioFileSourceSD::close() +{ + f.close(); + return true; +} + +bool AudioFileSourceSD::isOpen() +{ + return f?true:false; +} + +uint32_t AudioFileSourceSD::getSize() +{ + if (!f) return 0; + return f.size(); +} + +uint32_t AudioFileSourceSD::getPos() +{ + if (!f) return 0; + return f.position(); +} diff --git a/src/AudioFileSourceSD.h b/src/AudioFileSourceSD.h new file mode 100644 index 0000000..eacd991 --- /dev/null +++ b/src/AudioFileSourceSD.h @@ -0,0 +1,49 @@ +/* + AudioFileSourceSPIFFS + Input SD card "file" to be used by AudioGenerator + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOFILESOURCESD_H +#define _AUDIOFILESOURCESD_H + +#include "AudioFileSource.h" +#include + + +class AudioFileSourceSD : public AudioFileSource +{ + public: + AudioFileSourceSD(); + AudioFileSourceSD(const char *filename); + virtual ~AudioFileSourceSD() override; + + virtual bool open(const char *filename) override; + virtual uint32_t read(void *data, uint32_t len) override; + virtual bool seek(int32_t pos, int dir) override; + virtual bool close() override; + virtual bool isOpen() override; + virtual uint32_t getSize() override; + virtual uint32_t getPos() override; + + private: + File f; +}; + + +#endif +