|
| 1 | +/* |
| 2 | + AudioFileSourceSPIFFS |
| 3 | + Input SD card "file" to be used by AudioGenerator |
| 4 | + |
| 5 | + Copyright (C) 2017 Earle F. Philhower, III |
| 6 | +
|
| 7 | + This program is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "AudioFileSourceFatFs.h" |
| 22 | + |
| 23 | +AudioFileSourceFatFs::AudioFileSourceFatFs() |
| 24 | +{ |
| 25 | + _isOpen = false; |
| 26 | +} |
| 27 | + |
| 28 | +AudioFileSourceFatFs::AudioFileSourceFatFs(const char *filename) |
| 29 | +{ |
| 30 | + _isOpen = false; |
| 31 | + open(filename); |
| 32 | +} |
| 33 | + |
| 34 | +bool AudioFileSourceFatFs::open(const char *filename) |
| 35 | +{ |
| 36 | + if (_isOpen) file.close(); |
| 37 | + _isOpen = file.open((char*)filename, FA_OPEN_EXISTING | FA_READ); |
| 38 | + return _isOpen; |
| 39 | +} |
| 40 | + |
| 41 | +AudioFileSourceFatFs::~AudioFileSourceFatFs() |
| 42 | +{ |
| 43 | + if (_isOpen) file.close(); |
| 44 | +} |
| 45 | + |
| 46 | +uint32_t AudioFileSourceFatFs::read(void *data, uint32_t len) |
| 47 | +{ |
| 48 | + return file.read(data, len); |
| 49 | +} |
| 50 | + |
| 51 | +bool AudioFileSourceFatFs::seek(int32_t pos, int dir) |
| 52 | +{ |
| 53 | + if (!_isOpen) return false; |
| 54 | + if (dir==SEEK_SET) return file.seekSet(pos); |
| 55 | + else if (dir==SEEK_CUR) return file.seekSet(file.curPosition() + pos); |
| 56 | + else if (dir==SEEK_END) return file.seekSet(file.fileSize() + pos); |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +bool AudioFileSourceFatFs::close() |
| 61 | +{ |
| 62 | + _isOpen = false; |
| 63 | + return file.close(); |
| 64 | +} |
| 65 | + |
| 66 | +bool AudioFileSourceFatFs::isOpen() |
| 67 | +{ |
| 68 | + return _isOpen; |
| 69 | +} |
| 70 | + |
| 71 | +uint32_t AudioFileSourceFatFs::getSize() |
| 72 | +{ |
| 73 | + if (!_isOpen) return 0; |
| 74 | + return file.fileSize(); |
| 75 | +} |
| 76 | + |
| 77 | +uint32_t AudioFileSourceFatFs::getPos() |
| 78 | +{ |
| 79 | + if (!_isOpen) return 0; |
| 80 | + return file.curPosition(); |
| 81 | +} |
0 commit comments