Add callbacks for metadata and status reports
All objects can have a metadata and/or status report callback once created. They will call the source program with the parsed ID3 tag, ICY StreamTitle, or text and numeric error ID of any interesting event. It is up to the end application to figure out what to do with this. Normally, you'd just print it to the LCD or Serial port. But it is possible to detect, say, a buffer overflow and then switch to a lower bitrate stream for example.
This commit is contained in:
1 parent
1ab9348a4c
commit
9c120a777a
22 files changed
+339
-85
No files matched your search
@@ -22,6 +22,7 @@
|
||||
#define _AUDIOFILESOURCE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "AudioStatus.h"
|
||||
|
||||
class AudioFileSource
|
||||
{
|
||||
@@ -37,6 +38,13 @@ class AudioFileSource
|
||||
virtual uint32_t getSize() { return 0; };
|
||||
virtual uint32_t getPos() { return 0; };
|
||||
virtual bool loop() { return true; };
|
||||
|
||||
public:
|
||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||
virtual bool RegisterStatusCB(AudioStatus::statusCBFn fn, void *data) { return cb.RegisterStatusCB(fn, data); }
|
||||
|
||||
protected:
|
||||
AudioStatus cb;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -78,7 +78,7 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len)
|
||||
uint32_t bytes = 0;
|
||||
if (!filled) {
|
||||
// Fill up completely before returning any data at all
|
||||
Serial.println("Buffering...");
|
||||
cb.st(STATUS_FILLING, "Refilling buffer");
|
||||
length = src->read(buffer, buffSize);
|
||||
writePtr = length % buffSize;
|
||||
filled = true;
|
||||
@@ -115,6 +115,7 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len)
|
||||
writePtr = 0;
|
||||
length = 0;
|
||||
filled = false;
|
||||
cb.st(STATUS_UNDERFLOW, "Buffer underflow");
|
||||
}
|
||||
|
||||
fill();
|
||||
|
||||
@@ -38,6 +38,8 @@ class AudioFileSourceBuffer : public AudioFileSource
|
||||
virtual uint32_t getPos() override;
|
||||
virtual bool loop() override;
|
||||
|
||||
enum { STATUS_FILLING=2, STATUS_UNDERFLOW };
|
||||
|
||||
private:
|
||||
virtual void fill();
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ bool AudioFileSourceHTTPStream::open(const char *url)
|
||||
int code = http.GET();
|
||||
if (code != HTTP_CODE_OK) {
|
||||
http.end();
|
||||
cb.st(STATUS_HTTPFAIL, "Can't open HTTP request");
|
||||
return false;
|
||||
}
|
||||
size = http.getSize();
|
||||
@@ -69,21 +70,20 @@ uint32_t AudioFileSourceHTTPStream::readInternal(void *data, uint32_t len, bool
|
||||
{
|
||||
retry:
|
||||
if (!http.connected()) {
|
||||
Serial.println("Stream disconnected\n");
|
||||
Serial.flush();
|
||||
cb.st(STATUS_DISCONNECTED, "Stream disconnected");
|
||||
http.end();
|
||||
for (int i = 0; i < reconnectTries; i++) {
|
||||
Serial.printf("Attempting to reconnect, try %d\n", i);
|
||||
Serial.flush();
|
||||
char buff[32];
|
||||
sprintf(buff, "Attempting to reconnect, try %d", i);
|
||||
cb.st(STATUS_RECONNECTING, buff);
|
||||
delay(reconnectDelayMs);
|
||||
if (open(saveURL)) {
|
||||
Serial.println("Reconnected to stream");
|
||||
cb.st(STATUS_RECONNECTED, "Stream reconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!http.connected()) {
|
||||
Serial.printf("Unable to reconnect\n");
|
||||
Serial.flush();
|
||||
cb.st(STATUS_DISCONNECTED, "Unable to reconnect");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,7 @@ retry:
|
||||
|
||||
size_t avail = stream->available();
|
||||
if (!nonBlock && !avail) {
|
||||
Serial.printf("No stream data available\n");
|
||||
cb.st(STATUS_NODATA, "No stream data available");
|
||||
http.end();
|
||||
goto retry;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ class AudioFileSourceHTTPStream : public AudioFileSource
|
||||
virtual uint32_t getPos() override;
|
||||
bool SetReconnect(int tries, int delayms) { reconnectTries = tries; reconnectDelayMs = delayms; return true; }
|
||||
|
||||
enum { STATUS_HTTPFAIL=2, STATUS_DISCONNECTED, STATUS_RECONNECTING, STATUS_RECONNECTED, STATUS_NODATA };
|
||||
|
||||
private:
|
||||
virtual uint32_t readInternal(void *data, uint32_t len, bool nonBlock);
|
||||
HTTPClient http;
|
||||
|
||||
@@ -19,19 +19,19 @@
|
||||
*/
|
||||
|
||||
#include "AudioFileSourceICYStream.h"
|
||||
#include "AudioFileSourcePROGMEM.h"
|
||||
#include "AudioFileStream.h"
|
||||
|
||||
AudioFileSourceICYStream::AudioFileSourceICYStream()
|
||||
{
|
||||
pos = 0;
|
||||
reconnectTries = 0;
|
||||
saveURL = NULL;
|
||||
cb = NULL;
|
||||
}
|
||||
|
||||
AudioFileSourceICYStream::AudioFileSourceICYStream(const char *url)
|
||||
{
|
||||
saveURL = NULL;
|
||||
cb = NULL;
|
||||
reconnectTries = 0;
|
||||
open(url);
|
||||
}
|
||||
@@ -47,6 +47,7 @@ bool AudioFileSourceICYStream::open(const char *url)
|
||||
int code = http.GET();
|
||||
if (code != HTTP_CODE_OK) {
|
||||
http.end();
|
||||
cb.st(STATUS_HTTPFAIL, "Can't open HTTP request");
|
||||
return false;
|
||||
}
|
||||
if (http.hasHeader(hdr[0])) {
|
||||
@@ -73,6 +74,7 @@ class ICYMDReader {
|
||||
stream = str;
|
||||
avail = bytes;
|
||||
ptr = sizeof(buff)+1; // Cause read next time
|
||||
saved = -1;
|
||||
}
|
||||
~ICYMDReader() {
|
||||
// Get rid of any remaining bytes in the MD block
|
||||
@@ -84,6 +86,8 @@ class ICYMDReader {
|
||||
stream->readBytes(xxx, avail);
|
||||
}
|
||||
int read(uint8_t *dest, int len) {
|
||||
if (!len) return 0;
|
||||
if (saved >= 0) { *(dest++) = (uint8_t)saved; saved = -1; len--; }
|
||||
int ret = 0;
|
||||
while ((len>0) && (avail>0)) {
|
||||
// Always copy from bounce buffer first
|
||||
@@ -104,12 +108,14 @@ class ICYMDReader {
|
||||
return ret;
|
||||
}
|
||||
bool eof() { return (avail==0); }
|
||||
bool unread(uint8_t c) { if (saved>=0) return false; saved = c; return true; }
|
||||
|
||||
private:
|
||||
WiFiClient *stream;
|
||||
uint16_t avail;
|
||||
uint8_t ptr;
|
||||
uint8_t buff[16];
|
||||
int saved;
|
||||
};
|
||||
|
||||
|
||||
@@ -117,21 +123,20 @@ uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool n
|
||||
{
|
||||
retry:
|
||||
if (!http.connected()) {
|
||||
Serial.println("Stream disconnected\n");
|
||||
Serial.flush();
|
||||
cb.st(STATUS_DISCONNECTED, "Stream disconnected");
|
||||
http.end();
|
||||
for (int i = 0; i < reconnectTries; i++) {
|
||||
Serial.printf("Attempting to reconnect, try %d\n", i);
|
||||
Serial.flush();
|
||||
char buff[32];
|
||||
sprintf(buff, "Attempting to reconnect, try %d", i);
|
||||
cb.st(STATUS_RECONNECTING, buff);
|
||||
delay(reconnectDelayMs);
|
||||
if (open(saveURL)) {
|
||||
Serial.println("Reconnected to stream");
|
||||
cb.st(STATUS_RECONNECTED, "Stream reconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!http.connected()) {
|
||||
Serial.printf("Unable to reconnect\n");
|
||||
Serial.flush();
|
||||
cb.st(STATUS_DISCONNECTED, "Unable to reconnect");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -149,8 +154,7 @@ retry:
|
||||
|
||||
size_t avail = stream->available();
|
||||
if (!nonBlock && !avail) {
|
||||
Serial.printf("No stream data available\n");
|
||||
Serial.flush();
|
||||
cb.st(STATUS_NODATA, "No stream data available");
|
||||
http.end();
|
||||
goto retry;
|
||||
}
|
||||
@@ -172,44 +176,54 @@ retry:
|
||||
uint8_t mdSize;
|
||||
int mdret = stream->readBytes(&mdSize, 1);
|
||||
if ((mdret == 1) && (mdSize > 0)) {
|
||||
ICYMDReader md(stream, mdSize * 16);
|
||||
ICYMDReader mdr(stream, mdSize * 16);
|
||||
// Break out (potentially multiple) NAME='xxxx'
|
||||
char type[32];
|
||||
char value[64];
|
||||
while (!md.eof()) {
|
||||
while (!mdr.eof()) {
|
||||
memset(type, 0, sizeof(type));
|
||||
memset(value, 0, sizeof(value));
|
||||
uint8_t c;
|
||||
char *p = type;
|
||||
for (size_t i=0; i<sizeof(type)-1; i++) {
|
||||
int ret = md.read(&c, 1);
|
||||
int ret = mdr.read(&c, 1);
|
||||
if (!ret) break;
|
||||
if (c == '=') break;
|
||||
*(p++) = c;
|
||||
}
|
||||
if (c != '=') {
|
||||
// MD type was too long, read remainder and throw away
|
||||
while ((c != '=') && !md.eof()) md.read(&c, 1);
|
||||
while ((c != '=') && !mdr.eof()) mdr.read(&c, 1);
|
||||
}
|
||||
md.read(&c, 1);
|
||||
mdr.read(&c, 1);
|
||||
if (c=='\'') {
|
||||
// Got start of string value, read that, too
|
||||
p = value;
|
||||
for (size_t i=0; i<sizeof(value)-1; i++) {
|
||||
int ret = md.read(&c, 1);
|
||||
int ret = mdr.read(&c, 1);
|
||||
if (!ret) break;
|
||||
if (c == '\'') break;
|
||||
if ((c=='\n') || (c=='\r') || (c==' ') || (c=='\t') || (c==0)) continue; // Throw away any whitespace
|
||||
if (c == '\'') {
|
||||
// Need to special case as you don't escape "'", so look for '; to terminate
|
||||
uint8_t d = ';'; // In case of EOF, return end of line
|
||||
mdr.read(&d, 1);
|
||||
if (d==';') { mdr.unread(d); break; }
|
||||
else { mdr.unread(d); }
|
||||
}
|
||||
// if ((c=='\n') || (c=='\r') || (c==' ') || (c=='\t') || (c==0)) continue; // Throw away any whitespace
|
||||
*(p++) = c;
|
||||
}
|
||||
if (c != '\'') {
|
||||
// MD data was too long, read and throw away rest
|
||||
while ((c != '\'') && !md.eof()) md.read(&c, 1);
|
||||
while ((c != '\'') && !mdr.eof()) mdr.read(&c, 1);
|
||||
}
|
||||
{
|
||||
AudioFileSourcePROGMEM afsp(value, strlen(value));
|
||||
AudioFileStream afs(&afsp, strlen(value));
|
||||
cb.md(type, false, &afs);
|
||||
}
|
||||
if (cb) cb(type, value);
|
||||
do {
|
||||
ret = md.read(&c, 1);
|
||||
} while ((c !=';') && (c!=0) && !md.eof());
|
||||
ret = mdr.read(&c, 1);
|
||||
} while ((c !=';') && (c!=0) && !mdr.eof());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +35,10 @@ class AudioFileSourceICYStream : public AudioFileSourceHTTPStream
|
||||
|
||||
virtual bool open(const char *url) override;
|
||||
|
||||
typedef void (*callbackFn)(const char *type, const char *value);
|
||||
void setCallback(callbackFn f) { cb = f; }
|
||||
|
||||
private:
|
||||
virtual uint32_t readInternal(void *data, uint32_t len, bool nonBlock) override;
|
||||
int icyMetaInt;
|
||||
int icyByteCount;
|
||||
callbackFn cb;
|
||||
};
|
||||
|
||||
|
||||
|
||||
+13
-13
@@ -19,7 +19,7 @@
|
||||
*/
|
||||
|
||||
#include "AudioFileSourceID3.h"
|
||||
|
||||
#include "AudioFileStream.h"
|
||||
|
||||
// Handle unsync operation in ID3 with custom class
|
||||
class AudioFileSourceUnsync : public AudioFileSource
|
||||
@@ -113,7 +113,6 @@ AudioFileSourceID3::AudioFileSourceID3(AudioFileSource *src)
|
||||
{
|
||||
this->src = src;
|
||||
this->checked = false;
|
||||
this->cb = NULL;
|
||||
}
|
||||
|
||||
AudioFileSourceID3::~AudioFileSourceID3()
|
||||
@@ -179,17 +178,18 @@ uint32_t AudioFileSourceID3::read(void *data, uint32_t len)
|
||||
for (int j=0; j<framesize; j++)
|
||||
id3.getByte();
|
||||
}
|
||||
if (frameid[0]=='T' && frameid[1]=='A' && frameid[2]=='L' && frameid[3] == 'B') {
|
||||
if (cb) cb("Album", id3.getByte()==1, framesize-1, &id3);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='I' && frameid[2]=='T' && frameid[3] == '2') {
|
||||
if (cb) cb("Title", id3.getByte()==1, framesize-1, &id3);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='P' && frameid[2]=='E' && frameid[3] == '1') {
|
||||
if (cb) cb("Performer", id3.getByte()==1, framesize-1, &id3);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='Y' && frameid[2]=='E' && frameid[3] == 'R') {
|
||||
if (cb) cb("Year", id3.getByte()==1, framesize-1, &id3);
|
||||
} else {
|
||||
for (int j=0; j<framesize; j++)
|
||||
id3.getByte();
|
||||
{
|
||||
AudioFileStream afs(&id3, framesize);
|
||||
if (frameid[0]=='T' && frameid[1]=='A' && frameid[2]=='L' && frameid[3] == 'B') {
|
||||
cb.md("Album", afs.read()==1, &afs);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='I' && frameid[2]=='T' && frameid[3] == '2') {
|
||||
cb.md("Title", afs.read()==1, &afs);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='P' && frameid[2]=='E' && frameid[3] == '1') {
|
||||
cb.md("Performer", afs.read()==1, &afs);
|
||||
} else if (frameid[0]=='T' && frameid[1]=='Y' && frameid[2]=='E' && frameid[3] == 'R') {
|
||||
cb.md("Year", afs.read()==1, &afs);
|
||||
}
|
||||
//afs destructor here will read any unread bytes
|
||||
}
|
||||
}
|
||||
} while (!id3.eof());
|
||||
|
||||
@@ -38,13 +38,9 @@ class AudioFileSourceID3 : public AudioFileSource
|
||||
virtual uint32_t getSize() override;
|
||||
virtual uint32_t getPos() override;
|
||||
|
||||
typedef void (*callbackFn)(const char *type, bool isUnicode, int len, AudioFileSource *stream);
|
||||
void setCallback(callbackFn f) { cb = f; }
|
||||
|
||||
private:
|
||||
AudioFileSource *src;
|
||||
bool checked;
|
||||
callbackFn cb;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <Arduino.h>
|
||||
#include "AudioFileStream.h"
|
||||
|
||||
|
||||
AudioFileStream::AudioFileStream(AudioFileSource *source, int definedLen)
|
||||
{
|
||||
src = source;
|
||||
len = definedLen;
|
||||
ptr = 0;
|
||||
saved = -1;
|
||||
}
|
||||
|
||||
AudioFileStream::~AudioFileStream()
|
||||
{
|
||||
// If there's a defined len, read until we're empty
|
||||
if (len) {
|
||||
while (ptr++ < len) (void)read();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int AudioFileStream::available()
|
||||
{
|
||||
if (saved >= 0) return 1;
|
||||
else if (len) return ptr - len;
|
||||
else if (src->getSize()) return (src->getPos() - src->getSize());
|
||||
else return 1;
|
||||
}
|
||||
|
||||
int AudioFileStream::read()
|
||||
{
|
||||
uint8_t c;
|
||||
int r;
|
||||
if (ptr >= len) return -1;
|
||||
ptr++;
|
||||
if (saved >= 0) {
|
||||
c = (uint8_t)saved;
|
||||
saved = -1;
|
||||
r = 1;
|
||||
} else {
|
||||
r = src->read(&c, 1);
|
||||
}
|
||||
if (r != 1) return -1;
|
||||
return (int)c;
|
||||
}
|
||||
|
||||
int AudioFileStream::peek()
|
||||
{
|
||||
uint8_t c;
|
||||
if ((ptr+1) >= len) return -1;
|
||||
if (saved >= 0) return saved;
|
||||
int r = src->read(&c, 1);
|
||||
if (r<1) return -1;
|
||||
saved = c;
|
||||
return saved;
|
||||
}
|
||||
|
||||
void AudioFileStream::flush()
|
||||
{
|
||||
/* noop? */
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
AudioFileStream
|
||||
Convert an AudioFileSource* to a Stream*
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef AUDIOFILESTREAM_H
|
||||
#define AUDIOFILESTREAM_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "AudioFileSource.h"
|
||||
|
||||
class AudioFileStream : public Stream
|
||||
{
|
||||
public:
|
||||
AudioFileStream(AudioFileSource *source, int definedLen);
|
||||
virtual ~AudioFileStream();
|
||||
|
||||
public:
|
||||
// Stream interface - see the Arduino library documentation.
|
||||
virtual int available() override;
|
||||
virtual int read() override;
|
||||
virtual int peek() override;
|
||||
virtual void flush() override;
|
||||
virtual size_t write(uint8_t x) override { (void)x; return 0; };
|
||||
|
||||
private:
|
||||
AudioFileSource *src;
|
||||
int saved;
|
||||
int len;
|
||||
int ptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -22,6 +22,7 @@
|
||||
#define _AUDIOGENERATOR_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "AudioStatus.h"
|
||||
#include "AudioFileSource.h"
|
||||
#include "AudioOutput.h"
|
||||
|
||||
@@ -35,13 +36,18 @@ class AudioGenerator
|
||||
virtual bool stop() { return false; };
|
||||
virtual bool isRunning() { return false;};
|
||||
|
||||
public:
|
||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||
virtual bool RegisterStatusCB(AudioStatus::statusCBFn fn, void *data) { return cb.RegisterStatusCB(fn, data); }
|
||||
|
||||
protected:
|
||||
bool running;
|
||||
AudioFileSource *file;
|
||||
AudioOutput *output;
|
||||
int16_t lastSample[2];
|
||||
|
||||
protected:
|
||||
AudioStatus cb;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -108,7 +108,9 @@ bool AudioGeneratorAAC::loop()
|
||||
int ret = AACDecode(hAACDecoder, &inBuff, &bytesLeft, outSample);
|
||||
if (ret) {
|
||||
// Error, skip the frame...
|
||||
Serial.printf("AAC decode error %d\n", ret);
|
||||
char buff[48];
|
||||
sprintf(buff, "AAC decode error %d", ret);
|
||||
cb.st(ret, buff);
|
||||
} else {
|
||||
lastFrameEnd = buffValid - bytesLeft;
|
||||
AACFrameInfo fi;
|
||||
|
||||
@@ -191,6 +191,6 @@ void AudioGeneratorFLAC::metadata_cb(const FLAC__StreamDecoder *decoder, const F
|
||||
void AudioGeneratorFLAC::error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
|
||||
{
|
||||
(void) decoder;
|
||||
Serial.printf("Error:%d\n", status);
|
||||
cb.st((int)status, FLAC__StreamDecoderErrorStatusString[status]);
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@
|
||||
|
||||
void AudioGeneratorMIDI::midi_error(const char *msg, int curpos)
|
||||
{
|
||||
cb.st(curpos, msg);
|
||||
#if 0
|
||||
int ptr;
|
||||
Serial.printf("---> MIDI file error at position %04X (%d): %s\n", (uint16_t) curpos, (uint16_t) curpos, msg);
|
||||
/* print some bytes surrounding the error */
|
||||
@@ -82,6 +84,7 @@ void AudioGeneratorMIDI::midi_error(const char *msg, int curpos)
|
||||
Serial.printf((ptr + i) == curpos ? " [%02X] " : "%02X ", (int) c & 0xff);
|
||||
}
|
||||
Serial.printf("\n");
|
||||
#endif
|
||||
running = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,9 +67,11 @@ bool AudioGeneratorMP3::isRunning()
|
||||
enum mad_flow AudioGeneratorMP3::ErrorToFlow()
|
||||
{
|
||||
char err[64];
|
||||
char errLine[128];
|
||||
strcpy_P(err, mad_stream_errorstr(&stream));
|
||||
Serial.printf("Decoding error 0x%04x (%s) at byte offset %d\n", stream.error, err, (stream.this_frame - buff) + lastReadPos);
|
||||
Serial.flush();
|
||||
snprintf(errLine, sizeof(errLine), "Decoding error '%s' at byte offset %d",
|
||||
err, (stream.this_frame - buff) + lastReadPos);
|
||||
cb.st(stream.error, errLine);
|
||||
return MAD_FLOW_CONTINUE;
|
||||
}
|
||||
|
||||
@@ -193,11 +195,14 @@ done:
|
||||
|
||||
bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
||||
{
|
||||
if (!source) return false;
|
||||
if (!source) return false;
|
||||
file = source;
|
||||
if (!output) return false;
|
||||
this->output = output;
|
||||
if (!file->isOpen()) return false; // Error
|
||||
if (!file->isOpen()) {
|
||||
Serial.printf("MP3 source file not open\n");
|
||||
return false; // Error
|
||||
}
|
||||
if (!output->begin()) return false;
|
||||
|
||||
// Where we are in generating one frame's data, set to invalid so we will run loop on first getsample()
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#include "libmad/config.h"
|
||||
#include "libmad/mad.h"
|
||||
|
||||
class AudioGeneratorMP3 : AudioGenerator
|
||||
class AudioGeneratorMP3 : public AudioGenerator
|
||||
{
|
||||
public:
|
||||
AudioGeneratorMP3();
|
||||
|
||||
+10
-1
@@ -22,8 +22,9 @@
|
||||
#define _AUDIOOUTPUT_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "AudioStatus.h"
|
||||
|
||||
class AudioOutput
|
||||
class AudioOutput
|
||||
{
|
||||
public:
|
||||
AudioOutput() { };
|
||||
@@ -38,6 +39,11 @@ class AudioOutput
|
||||
virtual bool stop() { return false; };
|
||||
virtual bool loop() { return true; };
|
||||
|
||||
public:
|
||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||
virtual bool RegisterStatusCB(AudioStatus::statusCBFn fn, void *data) { return cb.RegisterStatusCB(fn, data); }
|
||||
|
||||
protected:
|
||||
void MakeSampleStereo16(int16_t sample[2]) {
|
||||
// Mono to "stereo" conversion
|
||||
if (channels == 1)
|
||||
@@ -61,6 +67,9 @@ class AudioOutput
|
||||
uint8_t bps;
|
||||
uint8_t channels;
|
||||
uint8_t gainF2P6; // Fixed point 2.6
|
||||
|
||||
protected:
|
||||
AudioStatus cb;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
AudioStatus
|
||||
Base class for Audio* status/metadata reporting
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _AUDIOSTATUS_H
|
||||
#define _AUDIOSTATUS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class AudioStatus
|
||||
{
|
||||
public:
|
||||
AudioStatus() { ClearCBs(); };
|
||||
virtual ~AudioStatus() {};
|
||||
|
||||
void ClearCBs() { mdFn = NULL; stFn = NULL; };
|
||||
|
||||
typedef void (*metadataCBFn)(void *cbData, const char *type, bool isUnicode, Stream *stream);
|
||||
bool RegisterMetadataCB(metadataCBFn f, void *cbData) { mdFn = f; mdData = cbData; return true; }
|
||||
|
||||
// Returns a unique warning/error code, varying by the object. The string may be a PSTR, use _P functions!
|
||||
typedef void (*statusCBFn)(void *cbData, int code, const char *string);
|
||||
bool RegisterStatusCB(statusCBFn f, void *cbData) { stFn = f; stData = cbData; return true; }
|
||||
|
||||
// Safely call the md function, if defined
|
||||
inline void md(const char *type, bool isUnicode, Stream *stream) { if (mdFn) mdFn(mdData, type, isUnicode, stream); }
|
||||
|
||||
// Safely call the st function, if defined
|
||||
inline void st(int code, const char *string) { if (stFn) stFn(stData, code, string); }
|
||||
|
||||
private:
|
||||
metadataCBFn mdFn;
|
||||
void *mdData;
|
||||
statusCBFn stFn;
|
||||
void *stData;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user