esmp3/src/data_sources.cpp

72 lines
2.4 KiB
C++

#include "data_sources.h"
void DataSource::skip_id3_tag() {
if (peek(0)=='I' && peek(1)=='D' && peek(2)=='3') {
DEBUG("ID3 tag found\n");
// Skip ID3 tag marker
read(); read(); read();
// Skip ID3 tag version
read(); read();
byte tags = read();
bool footer_present = tags & 0x10;
DEBUG("ID3 footer found: %d\n", footer_present);
uint32_t offset = 0;
for (byte i=0; i<4; i++) {
offset <<= 7;
offset |= (0x7F & read());
}
offset += 10;
if (footer_present) offset += 10;
DEBUG("ID3 tag length is %d bytes.\n", offset);
seek(offset);
} else {
DEBUG("No ID3 tag found\n");
}
}
////////////// SDDataSource //////////////
SDDataSource::SDDataSource(String file) { _file = SD.open(file, "r"); }
SDDataSource::~SDDataSource() { if (_file) _file.close(); }
size_t SDDataSource::read(uint8_t* buf, size_t len) { return _file.read(buf, len); }
int SDDataSource::read() { return _file.read(); }
size_t SDDataSource::position() { return _file.position(); }
void SDDataSource::seek(size_t position) { _file.seek(position); }
size_t SDDataSource::size() { return _file.size(); }
void SDDataSource::close() { _file.close(); }
bool SDDataSource::usable() { return _file; }
int SDDataSource::peek(int offset) {
if (offset==0) return _file.peek();
size_t start_position = _file.position();
_file.seek(start_position + offset);
int result = _file.peek();
_file.seek(start_position);
return result;
}
////////////// HTTPSDataSource //////////////
HTTPSDataSource::HTTPSDataSource(String url, uint32_t offset) {
_url = url;
_init(url, offset);
}
void HTTPSDataSource::_init(String url, uint32_t offset) {
_url = url;
_http = new HTTPClientWrapper();
if (!_http->get(url, offset)) return;
_position = 0;
}
HTTPSDataSource::~HTTPSDataSource() {
_http->close();
delete _http;
}
bool HTTPSDataSource::usable() { return _http; }
size_t HTTPSDataSource::read(uint8_t* buf, size_t len) { size_t result = _http->read(buf, len); _position += result; return result; }
int HTTPSDataSource::read() { int b = _http->read(); if (b>=0) _position++; return b; }
size_t HTTPSDataSource::position() { return _position; }
void HTTPSDataSource::seek(size_t position) { _http->close(); delete _http; _init(_url, position); }
size_t HTTPSDataSource::size() { return _http->getSize(); }
void HTTPSDataSource::close() { _http->close(); }
int HTTPSDataSource::peek(int offset) { return _http->peek(offset); }