Compare commits
No files matched your search
@@ -22,7 +22,7 @@ const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
// Randomly picked URL
|
||||
const char *URL="http://streaming.shoutcast.com/80sPlanet?lang=en-US";
|
||||
const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceICYStream *file;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
AudioOutput
|
||||
Base class of an audio output player
|
||||
|
||||
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 _AUDIOOUTPUTNULLSLOW_H
|
||||
#define _AUDIOOUTPUTNULLSLOW_H
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
class AudioOutputNullSlow : public AudioOutput
|
||||
{
|
||||
public:
|
||||
AudioOutputNullSlow(int hertz) { SetRate(hertz); };
|
||||
~AudioOutputNullSlow() {};
|
||||
virtual bool begin() { samples = 0; startms = millis(); return true; }
|
||||
virtual bool ConsumeSample(int16_t sample[2]) {
|
||||
// return false (= output buffer full)
|
||||
// sometimes to let the main loop running
|
||||
constexpr int everylog2 = 10;
|
||||
if ((++samples & ((1<<everylog2)-1)) == 0) {
|
||||
delay(1000/(hertz >> everylog2));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
virtual bool stop() { endms = millis(); return true; };
|
||||
unsigned long GetMilliseconds() { return endms - startms; }
|
||||
int GetSamples() { return samples; }
|
||||
int GetFrequency() { return hertz; }
|
||||
|
||||
protected:
|
||||
unsigned long startms;
|
||||
unsigned long endms;
|
||||
int samples;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#else
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif
|
||||
#include "AudioFileSourceICYStream.h"
|
||||
#include "AudioFileSourceBuffer.h"
|
||||
#include "AudioGeneratorMP3.h"
|
||||
#include "AudioOutputNullSlow.h"
|
||||
|
||||
// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
|
||||
|
||||
// Enter your WiFi setup here:
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
// Randomly picked URL
|
||||
const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";
|
||||
//const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3";
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceICYStream *file;
|
||||
AudioFileSourceBuffer *buff;
|
||||
AudioOutputNullSlow *out;
|
||||
|
||||
// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
|
||||
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
|
||||
{
|
||||
const char *ptr = reinterpret_cast<const char *>(cbData);
|
||||
(void) isUnicode; // Punt this ball for now
|
||||
// Note that the type and string may be in PROGMEM, so copy them to RAM for printf
|
||||
char s1[32], s2[64];
|
||||
strncpy_P(s1, type, sizeof(s1));
|
||||
s1[sizeof(s1)-1]=0;
|
||||
strncpy_P(s2, string, sizeof(s2));
|
||||
s2[sizeof(s2)-1]=0;
|
||||
Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
// Called when there's a warning or error (like a buffer underflow or decode hiccup)
|
||||
void StatusCallback(void *cbData, int code, const char *string)
|
||||
{
|
||||
const char *ptr = reinterpret_cast<const char *>(cbData);
|
||||
// Note that the string may be in PROGMEM, so copy it to RAM for printf
|
||||
char s1[64];
|
||||
strncpy_P(s1, string, sizeof(s1));
|
||||
s1[sizeof(s1)-1]=0;
|
||||
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("Connecting to WiFi");
|
||||
|
||||
WiFi.disconnect();
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// Try forever
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("...Connecting to WiFi");
|
||||
delay(1000);
|
||||
}
|
||||
Serial.println("Connected");
|
||||
|
||||
audioLogger = &Serial;
|
||||
file = new AudioFileSourceICYStream(URL);
|
||||
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
|
||||
buff = new AudioFileSourceBuffer(file, 2048);
|
||||
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
|
||||
out = new AudioOutputNullSlow(44100);
|
||||
mp3 = new AudioGeneratorMP3();
|
||||
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
|
||||
mp3->begin(buff, out);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
static int lastms = 0;
|
||||
|
||||
if (mp3->isRunning()) {
|
||||
if (millis()-lastms > 1000) {
|
||||
lastms = millis();
|
||||
Serial.printf("Running for %d ms...\n", lastms);
|
||||
Serial.flush();
|
||||
}
|
||||
if (!mp3->loop()) mp3->stop();
|
||||
} else {
|
||||
Serial.printf("MP3 done\n");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/bin/bash
|
||||
|
||||
ino=${PWD##*/}
|
||||
|
||||
if [ ! -d "${ESP8266ARDUINO}/tests/host" ]; then
|
||||
echo "\${ESP8266ARDUINO} should point to ESP8266 Arduino core directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
THISLIB=$(pwd)/../..
|
||||
MAD=$(ls ${THISLIB}/src/libmad/*.c)
|
||||
|
||||
cd ${ESP8266ARDUINO}/tests/host
|
||||
|
||||
if [ "$1" = "clean" ]; then
|
||||
make clean
|
||||
cd ${THISLIB}
|
||||
rm -f src/*.o src/libmad/*.o
|
||||
exit 0
|
||||
elif [ "$1" = diff ]; then
|
||||
cd ${THISLIB}/examples
|
||||
diff -u StreamMP3FromHTTP/StreamMP3FromHTTP.ino ${ino}/${ino}.ino
|
||||
exit 0
|
||||
else
|
||||
echo ""
|
||||
echo "usage:"
|
||||
echo " $0"
|
||||
echo " $0 clean"
|
||||
echo " $0 diff"
|
||||
echo " FORCE32=0 $0 (run with valgrind)"
|
||||
echo " FORCE32=1 $0 (run in 32 bits)"
|
||||
echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory"
|
||||
echo ""
|
||||
[ "$1" = "-h" ] && exit 0
|
||||
sleep 1
|
||||
fi
|
||||
|
||||
[ -z "${FORCE32}" ] && FORCE32=0
|
||||
|
||||
if [ "${FORCE32}" = 0 ]; then
|
||||
run=valgrind
|
||||
else
|
||||
run=
|
||||
fi
|
||||
|
||||
eval make FORCE32=${FORCE32} -j \
|
||||
USERCSOURCES=\"${MAD}\" \
|
||||
USERCXXSOURCES=\"${THISLIB}/src/AudioFileSourceBuffer.cpp ${THISLIB}/src/AudioLogger.cpp ${THISLIB}/src/AudioGeneratorMP3.cpp ${THISLIB}/src/AudioFileSourceICYStream.cpp ${THISLIB}/src/AudioFileSourceHTTPStream.cpp\" \
|
||||
USERCFLAGS=\"-I${THISLIB}/src/\" \
|
||||
${THISLIB}/examples/${ino}/${ino}
|
||||
|
||||
set -x
|
||||
|
||||
$run ./bin/${ino}/${ino} "$@"
|
||||
stty sane
|
||||
|
||||
+5
-2
@@ -14,10 +14,13 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/earlephilhower/ESP8266Audio"
|
||||
},
|
||||
"version": "1.5.1",
|
||||
"version": "1.8",
|
||||
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
|
||||
"dependencies": {
|
||||
"SPI": "1.0"
|
||||
"SPI": "1.0",
|
||||
"SPIFFS": "1.0",
|
||||
"HTTPClient": "1.0",
|
||||
"WiFiClientSecure": "1.0"
|
||||
},
|
||||
"frameworks": "Arduino",
|
||||
"examples": [
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
name=ESP8266Audio
|
||||
version=1.5.1
|
||||
version=1.8
|
||||
author=Earle F. Philhower, III
|
||||
maintainer=Earle F. Philhower, III
|
||||
sentence=Audio file and I2S sound playing routines.
|
||||
|
||||
@@ -29,8 +29,8 @@
|
||||
class AudioFileSourceFS : public AudioFileSource
|
||||
{
|
||||
public:
|
||||
AudioFileSourceFS(FS &fs) { filesystem = &fs; }
|
||||
AudioFileSourceFS(FS &fs, const char *filename);
|
||||
AudioFileSourceFS(fs::FS &fs) { filesystem = &fs; }
|
||||
AudioFileSourceFS(fs::FS &fs, const char *filename);
|
||||
virtual ~AudioFileSourceFS() override;
|
||||
|
||||
virtual bool open(const char *filename) override;
|
||||
@@ -42,8 +42,8 @@ class AudioFileSourceFS : public AudioFileSource
|
||||
virtual uint32_t getPos() override { if (!f) return 0; else return f.position(); };
|
||||
|
||||
private:
|
||||
FS *filesystem;
|
||||
File f;
|
||||
fs::FS *filesystem;
|
||||
fs::File f;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ bool AudioFileSourceHTTPStream::open(const char *url)
|
||||
http.begin(client, url);
|
||||
http.setReuse(true);
|
||||
#ifndef ESP32
|
||||
http.setFollowRedirects(true);
|
||||
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
|
||||
#endif
|
||||
int code = http.GET();
|
||||
if (code != HTTP_CODE_OK) {
|
||||
@@ -84,7 +84,7 @@ retry:
|
||||
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
||||
http.end();
|
||||
for (int i = 0; i < reconnectTries; i++) {
|
||||
char buff[32];
|
||||
char buff[64];
|
||||
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
||||
cb.st(STATUS_RECONNECTING, buff);
|
||||
delay(reconnectDelayMs);
|
||||
|
||||
@@ -83,12 +83,16 @@ AudioFileSourceICYStream::~AudioFileSourceICYStream()
|
||||
|
||||
uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool nonBlock)
|
||||
{
|
||||
// Ensure we can't possibly read 2 ICY headers in a single go #355
|
||||
if (icyMetaInt > 1) {
|
||||
len = std::min((int)(icyMetaInt >> 1), (int)len);
|
||||
}
|
||||
retry:
|
||||
if (!http.connected()) {
|
||||
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
||||
http.end();
|
||||
for (int i = 0; i < reconnectTries; i++) {
|
||||
char buff[32];
|
||||
char buff[64];
|
||||
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
||||
cb.st(STATUS_RECONNECTING, buff);
|
||||
delay(reconnectDelayMs);
|
||||
|
||||
@@ -143,6 +143,7 @@ uint32_t AudioFileSourceID3::read(void *data, uint32_t len)
|
||||
if (ret<10) return ret;
|
||||
|
||||
if ((buff[0]!='I') || (buff[1]!='D') || (buff[2]!='3') || (buff[3]>0x04) || (buff[3]<0x02) || (buff[4]!=0)) {
|
||||
cb.md("eof", false, "id3");
|
||||
return 10 + src->read(buff+10, len-10);
|
||||
}
|
||||
|
||||
@@ -212,9 +213,9 @@ uint32_t AudioFileSourceID3::read(void *data, uint32_t len)
|
||||
|
||||
// Read the value and send to callback
|
||||
char value[64];
|
||||
uint16_t i;
|
||||
uint32_t i;
|
||||
bool isUnicode = (id3.getByte()==1) ? true : false;
|
||||
for (i=0; i<framesize-1; i++) {
|
||||
for (i=0; i<(uint32_t)framesize-1; i++) {
|
||||
if (i<sizeof(value)-1) value[i] = id3.getByte();
|
||||
else (void)id3.getByte();
|
||||
}
|
||||
@@ -231,10 +232,24 @@ uint32_t AudioFileSourceID3::read(void *data, uint32_t len)
|
||||
} else if ( (frameid[0]=='T' && frameid[1]=='Y' && frameid[2]=='E' && frameid[3] == 'R') ||
|
||||
(frameid[0]=='T' && frameid[1]=='Y' && frameid[2]=='E' && rev==2) ) {
|
||||
cb.md("Year", isUnicode, value);
|
||||
} else if ( (frameid[0]=='T' && frameid[1]=='R' && frameid[2]=='C' && frameid[3] == 'K') ||
|
||||
(frameid[0]=='T' && frameid[1]=='R' && frameid[2]=='K' && rev==2) ) {
|
||||
cb.md("track", isUnicode, value);
|
||||
} else if ( (frameid[0]=='T' && frameid[1]=='P' && frameid[2]=='O' && frameid[3] == 'S') ||
|
||||
(frameid[0]=='T' && frameid[1]=='P' && frameid[2]=='A' && rev==2) ) {
|
||||
cb.md("Set", isUnicode, value);
|
||||
} else if ( (frameid[0]=='P' && frameid[1]=='O' && frameid[2]=='P' && frameid[3] == 'M') ||
|
||||
(frameid[0]=='P' && frameid[1]=='O' && frameid[2]=='P' && rev==2) ) {
|
||||
cb.md("Popularimeter", isUnicode, value);
|
||||
} else if ( (frameid[0]=='T' && frameid[1]=='C' && frameid[2]=='M' && frameid[3] == 'P') ) {
|
||||
cb.md("Compilation", isUnicode, value);
|
||||
}
|
||||
}
|
||||
} while (!id3.eof());
|
||||
|
||||
// use callback function to signal end of tags and beginning of content.
|
||||
cb.md("eof", false, "id3");
|
||||
|
||||
// All ID3 processing done, return to main caller
|
||||
return src->read(data, len);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ class AudioGenerator
|
||||
virtual bool loop() { return false; };
|
||||
virtual bool stop() { return false; };
|
||||
virtual bool isRunning() { return false;};
|
||||
virtual void desync () { };
|
||||
|
||||
public:
|
||||
virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); }
|
||||
|
||||
+72
-12
@@ -29,11 +29,23 @@ AudioGeneratorMP3::AudioGeneratorMP3()
|
||||
buff = NULL;
|
||||
nsCountMax = 1152/32;
|
||||
madInitted = false;
|
||||
preallocateSpace = NULL;
|
||||
preallocateSize = 0;
|
||||
}
|
||||
|
||||
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
|
||||
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size): preallocateSpace(space), preallocateSize(size)
|
||||
{
|
||||
running = false;
|
||||
file = NULL;
|
||||
output = NULL;
|
||||
buff = NULL;
|
||||
nsCountMax = 1152/32;
|
||||
madInitted = false;
|
||||
}
|
||||
|
||||
AudioGeneratorMP3::AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize):
|
||||
preallocateSpace(buff), preallocateSize(buffSize),
|
||||
preallocateStreamSpace(stream), preallocateStreamSize(streamSize),
|
||||
preallocateFrameSpace(frame), preallocateFrameSize(frameSize),
|
||||
preallocateSynthSpace(synth), preallocateSynthSize(synthSize)
|
||||
{
|
||||
running = false;
|
||||
file = NULL;
|
||||
@@ -41,8 +53,6 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
|
||||
buff = NULL;
|
||||
nsCountMax = 1152/32;
|
||||
madInitted = false;
|
||||
preallocateSpace = space;
|
||||
preallocateSize = size;
|
||||
}
|
||||
|
||||
AudioGeneratorMP3::~AudioGeneratorMP3()
|
||||
@@ -109,7 +119,12 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
|
||||
if (stream->next_frame) {
|
||||
unused = lastBuffLen - (stream->next_frame - buff);
|
||||
memmove(buff, stream->next_frame, unused);
|
||||
if (unused < 0) {
|
||||
desync();
|
||||
unused = 0;
|
||||
} else {
|
||||
memmove(buff, stream->next_frame, unused);
|
||||
}
|
||||
stream->next_frame = NULL;
|
||||
}
|
||||
|
||||
@@ -125,6 +140,10 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
// Can't read any from the file, and we don't have anything left. It's done....
|
||||
return MAD_FLOW_STOP;
|
||||
}
|
||||
if (len < 0) {
|
||||
desync();
|
||||
unused = 0;
|
||||
}
|
||||
|
||||
lastBuffLen = len + unused;
|
||||
mad_stream_buffer(stream, buff, lastBuffLen);
|
||||
@@ -132,6 +151,16 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
return MAD_FLOW_CONTINUE;
|
||||
}
|
||||
|
||||
void AudioGeneratorMP3::desync ()
|
||||
{
|
||||
audioLogger->printf_P(PSTR("MP3:desync\n"));
|
||||
if (stream) {
|
||||
stream->next_frame = nullptr;
|
||||
stream->this_frame = nullptr;
|
||||
stream->sync = 0;
|
||||
}
|
||||
lastBuffLen = 0;
|
||||
}
|
||||
|
||||
bool AudioGeneratorMP3::DecodeNextFrame()
|
||||
{
|
||||
@@ -196,6 +225,18 @@ retry:
|
||||
}
|
||||
|
||||
if (!DecodeNextFrame()) {
|
||||
if (stream->error == MAD_ERROR_BUFLEN) {
|
||||
// randomly seeking can lead to endless
|
||||
// and unrecoverable "MAD_ERROR_BUFLEN" loop
|
||||
audioLogger->printf_P(PSTR("MP3:ERROR_BUFLEN %d\n"), unrecoverable);
|
||||
if (++unrecoverable >= 3) {
|
||||
unrecoverable = 0;
|
||||
stop();
|
||||
return running;
|
||||
}
|
||||
} else {
|
||||
unrecoverable = 0;
|
||||
}
|
||||
goto retry;
|
||||
}
|
||||
samplePtr = 9999;
|
||||
@@ -229,6 +270,9 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
||||
return false; // Error
|
||||
}
|
||||
|
||||
// Reset error count from previous file
|
||||
unrecoverable = 0;
|
||||
|
||||
output->SetBitsPerSample(16); // Constant for MP3 decoder
|
||||
output->SetChannels(2);
|
||||
|
||||
@@ -243,16 +287,32 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
||||
lastBuffLen = 0;
|
||||
|
||||
// Allocate all large memory chunks
|
||||
if (preallocateSpace) {
|
||||
if (preallocateStreamSize + preallocateFrameSize + preallocateSynthSize) {
|
||||
if (preallocateSize >= preAllocBuffSize() &&
|
||||
preallocateStreamSize >= preAllocStreamSize() &&
|
||||
preallocateFrameSize >= preAllocFrameSize() &&
|
||||
preallocateSynthSize >= preAllocSynthSize()) {
|
||||
buff = reinterpret_cast<unsigned char *>(preallocateSpace);
|
||||
stream = reinterpret_cast<struct mad_stream *>(preallocateStreamSpace);
|
||||
frame = reinterpret_cast<struct mad_frame *>(preallocateFrameSpace);
|
||||
synth = reinterpret_cast<struct mad_synth *>(preallocateSynthSpace);
|
||||
}
|
||||
else {
|
||||
audioLogger->printf_P("OOM error in MP3: Want %d/%d/%d/%d bytes, have %d/%d/%d/%d bytes preallocated.\n",
|
||||
preAllocBuffSize(), preAllocStreamSize(), preAllocFrameSize(), preAllocSynthSize(),
|
||||
preallocateSize, preallocateStreamSize, preallocateFrameSize, preallocateSynthSize);
|
||||
return false;
|
||||
}
|
||||
} else if (preallocateSpace) {
|
||||
uint8_t *p = reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||
buff = reinterpret_cast<unsigned char *>(p);
|
||||
p += (buffLen+7) & ~7;
|
||||
p += preAllocBuffSize();
|
||||
stream = reinterpret_cast<struct mad_stream *>(p);
|
||||
p += (sizeof(struct mad_stream)+7) & ~7;
|
||||
p += preAllocStreamSize();
|
||||
frame = reinterpret_cast<struct mad_frame *>(p);
|
||||
p += (sizeof(struct mad_frame)+7) & ~7;
|
||||
p += preAllocFrameSize();
|
||||
synth = reinterpret_cast<struct mad_synth *>(p);
|
||||
p += (sizeof(struct mad_synth)+7) & ~7;
|
||||
p += preAllocSynthSize();
|
||||
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||
if (neededBytes > preallocateSize) {
|
||||
audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
|
||||
@@ -302,7 +362,7 @@ extern "C" {
|
||||
{
|
||||
return 8192;
|
||||
}
|
||||
#elif defined(ESP8266)
|
||||
#elif defined(ESP8266) && !defined(CORE_MOCK)
|
||||
#include <cont.h>
|
||||
extern cont_t g_cont;
|
||||
|
||||
|
||||
+21
-5
@@ -30,17 +30,31 @@ class AudioGeneratorMP3 : public AudioGenerator
|
||||
public:
|
||||
AudioGeneratorMP3();
|
||||
AudioGeneratorMP3(void *preallocateSpace, int preallocateSize);
|
||||
AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize);
|
||||
virtual ~AudioGeneratorMP3() override;
|
||||
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
|
||||
virtual bool loop() override;
|
||||
virtual bool stop() override;
|
||||
virtual bool isRunning() override;
|
||||
|
||||
protected:
|
||||
void *preallocateSpace;
|
||||
int preallocateSize;
|
||||
virtual void desync () override;
|
||||
|
||||
const int buffLen = 0x600; // Slightly larger than largest MP3 frame
|
||||
static constexpr int preAllocSize () { return preAllocBuffSize() + preAllocStreamSize() + preAllocFrameSize() + preAllocSynthSize(); }
|
||||
static constexpr int preAllocBuffSize () { return ((buffLen + 7) & ~7); }
|
||||
static constexpr int preAllocStreamSize () { return ((sizeof(struct mad_stream) + 7) & ~7); }
|
||||
static constexpr int preAllocFrameSize () { return (sizeof(struct mad_frame) + 7) & ~7; }
|
||||
static constexpr int preAllocSynthSize () { return (sizeof(struct mad_synth) + 7) & ~7; }
|
||||
|
||||
protected:
|
||||
void *preallocateSpace = nullptr;
|
||||
int preallocateSize = 0;
|
||||
void *preallocateStreamSpace = nullptr;
|
||||
int preallocateStreamSize = 0;
|
||||
void *preallocateFrameSpace = nullptr;
|
||||
int preallocateFrameSize = 0;
|
||||
void *preallocateSynthSpace = nullptr;
|
||||
int preallocateSynthSize = 0;
|
||||
|
||||
static constexpr int buffLen = 0x600; // Slightly larger than largest MP3 frame
|
||||
unsigned char *buff;
|
||||
int lastReadPos;
|
||||
int lastBuffLen;
|
||||
@@ -62,6 +76,8 @@ class AudioGeneratorMP3 : public AudioGenerator
|
||||
bool DecodeNextFrame();
|
||||
bool GetOneSample(int16_t sample[2]);
|
||||
|
||||
private:
|
||||
int unrecoverable = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+172
-115
@@ -35,113 +35,77 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
|
||||
output_mode = EXTERNAL_I2S;
|
||||
}
|
||||
this->output_mode = output_mode;
|
||||
#ifdef ESP32
|
||||
if (!i2sOn) {
|
||||
if (use_apll == APLL_AUTO) {
|
||||
// don't use audio pll on buggy rev0 chips
|
||||
use_apll = APLL_DISABLE;
|
||||
esp_chip_info_t out_info;
|
||||
esp_chip_info(&out_info);
|
||||
if(out_info.revision > 0) {
|
||||
use_apll = APLL_ENABLE;
|
||||
}
|
||||
}
|
||||
this->use_apll = use_apll;
|
||||
|
||||
i2s_mode_t mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_DAC_BUILT_IN);
|
||||
} else if (output_mode == INTERNAL_PDM) {
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_PDM);
|
||||
}
|
||||
|
||||
i2s_comm_format_t comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB);
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
comm_fmt = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S_MSB;
|
||||
}
|
||||
|
||||
i2s_config_t i2s_config_dac = {
|
||||
.mode = mode,
|
||||
.sample_rate = 44100,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = comm_fmt,
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = 64,
|
||||
.use_apll = use_apll // Use audio PLL
|
||||
};
|
||||
audioLogger->printf("+%d %p\n", portNo, &i2s_config_dac);
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL) != ESP_OK) {
|
||||
audioLogger->println("ERROR: Unable to install I2S drives\n");
|
||||
}
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) {
|
||||
i2s_set_pin((i2s_port_t)portNo, NULL);
|
||||
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
|
||||
} else {
|
||||
SetPinout(26, 25, 22);
|
||||
}
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
}
|
||||
#else
|
||||
(void) dma_buf_count;
|
||||
(void) use_apll;
|
||||
if (!i2sOn) {
|
||||
orig_bck = READ_PERI_REG(PERIPHS_IO_MUX_MTDO_U);
|
||||
orig_ws = READ_PERI_REG(PERIPHS_IO_MUX_GPIO2_U);
|
||||
i2s_begin();
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
//set defaults
|
||||
mono = false;
|
||||
bps = 16;
|
||||
channels = 2;
|
||||
hertz = 44100;
|
||||
bclkPin = 26;
|
||||
wclkPin = 25;
|
||||
doutPin = 22;
|
||||
SetGain(1.0);
|
||||
SetRate(44100); // Default
|
||||
}
|
||||
|
||||
AudioOutputI2S::~AudioOutputI2S()
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (i2sOn) {
|
||||
audioLogger->printf("UNINSTALL I2S\n");
|
||||
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
||||
}
|
||||
#else
|
||||
if (i2sOn) i2s_end();
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
if (i2sOn) {
|
||||
audioLogger->printf("UNINSTALL I2S\n");
|
||||
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
||||
}
|
||||
#else
|
||||
if (i2sOn)
|
||||
i2s_end();
|
||||
#endif
|
||||
i2sOn = false;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout)
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) return false; // Not allowed
|
||||
bclkPin = bclk;
|
||||
wclkPin = wclk;
|
||||
doutPin = dout;
|
||||
if (i2sOn)
|
||||
return SetPinout();
|
||||
|
||||
i2s_pin_config_t pins = {
|
||||
.bck_io_num = bclk,
|
||||
.ws_io_num = wclk,
|
||||
.data_out_num = dout,
|
||||
.data_in_num = I2S_PIN_NO_CHANGE
|
||||
};
|
||||
i2s_set_pin((i2s_port_t)portNo, &pins);
|
||||
return true;
|
||||
#else
|
||||
(void) bclk;
|
||||
(void) wclk;
|
||||
(void) dout;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetPinout()
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM)
|
||||
return false; // Not allowed
|
||||
|
||||
i2s_pin_config_t pins = {
|
||||
.bck_io_num = bclkPin,
|
||||
.ws_io_num = wclkPin,
|
||||
.data_out_num = doutPin,
|
||||
.data_in_num = I2S_PIN_NO_CHANGE};
|
||||
i2s_set_pin((i2s_port_t)portNo, &pins);
|
||||
return true;
|
||||
#else
|
||||
(void)bclkPin;
|
||||
(void)wclkPin;
|
||||
(void)doutPin;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::SetRate(int hz)
|
||||
{
|
||||
// TODO - have a list of allowable rates from constructor, check them
|
||||
this->hertz = hz;
|
||||
#ifdef ESP32
|
||||
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
||||
#else
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
#endif
|
||||
if (i2sOn)
|
||||
{
|
||||
#ifdef ESP32
|
||||
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
||||
#else
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -165,13 +129,99 @@ bool AudioOutputI2S::SetOutputModeMono(bool mono)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::begin()
|
||||
bool AudioOutputI2S::begin(bool txDAC)
|
||||
{
|
||||
#ifdef ESP32
|
||||
if (!i2sOn)
|
||||
{
|
||||
if (use_apll == APLL_AUTO)
|
||||
{
|
||||
// don't use audio pll on buggy rev0 chips
|
||||
use_apll = APLL_DISABLE;
|
||||
esp_chip_info_t out_info;
|
||||
esp_chip_info(&out_info);
|
||||
if (out_info.revision > 0)
|
||||
{
|
||||
use_apll = APLL_ENABLE;
|
||||
}
|
||||
}
|
||||
|
||||
i2s_mode_t mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
|
||||
if (output_mode == INTERNAL_DAC)
|
||||
{
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_DAC_BUILT_IN);
|
||||
}
|
||||
else if (output_mode == INTERNAL_PDM)
|
||||
{
|
||||
mode = (i2s_mode_t)(mode | I2S_MODE_PDM);
|
||||
}
|
||||
|
||||
i2s_comm_format_t comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB);
|
||||
if (output_mode == INTERNAL_DAC)
|
||||
{
|
||||
comm_fmt = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S_MSB;
|
||||
}
|
||||
|
||||
i2s_config_t i2s_config_dac = {
|
||||
.mode = mode,
|
||||
.sample_rate = 44100,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
||||
.communication_format = comm_fmt,
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = 64,
|
||||
.use_apll = use_apll // Use audio PLL
|
||||
};
|
||||
audioLogger->printf("+%d %p\n", portNo, &i2s_config_dac);
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL) != ESP_OK)
|
||||
{
|
||||
audioLogger->println("ERROR: Unable to install I2S drives\n");
|
||||
}
|
||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM)
|
||||
{
|
||||
i2s_set_pin((i2s_port_t)portNo, NULL);
|
||||
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetPinout();
|
||||
}
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
}
|
||||
#else
|
||||
(void)dma_buf_count;
|
||||
(void)use_apll;
|
||||
if (!i2sOn)
|
||||
{
|
||||
orig_bck = READ_PERI_REG(PERIPHS_IO_MUX_MTDO_U);
|
||||
orig_ws = READ_PERI_REG(PERIPHS_IO_MUX_GPIO2_U);
|
||||
#ifdef I2S_HAS_BEGIN_RXTX_DRIVE_CLOCKS
|
||||
if (!i2s_rxtxdrive_begin(false, true, false, txDAC)) {
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
if (!i2s_rxtx_begin(false, true)) {
|
||||
return false;
|
||||
}
|
||||
if (!txDAC) {
|
||||
audioLogger->printf_P(PSTR("I2SNoDAC: esp8266 arduino core should be upgraded to avoid conflicts with SPI\n"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
SetRate(hertz); // Default
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
|
||||
//return if we haven't called ::begin yet
|
||||
if (!i2sOn)
|
||||
return false;
|
||||
|
||||
int16_t ms[2];
|
||||
|
||||
ms[0] = sample[0];
|
||||
@@ -183,41 +233,48 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
||||
int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL];
|
||||
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
|
||||
}
|
||||
#ifdef ESP32
|
||||
uint32_t s32;
|
||||
if (output_mode == INTERNAL_DAC) {
|
||||
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
|
||||
int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000;
|
||||
s32 = (r<<16) | (l&0xffff);
|
||||
} else {
|
||||
s32 = ((Amplify(ms[RIGHTCHANNEL]))<<16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
}
|
||||
return i2s_write_bytes((i2s_port_t)portNo, (const char*)&s32, sizeof(uint32_t), 0);
|
||||
#else
|
||||
uint32_t s32 = ((Amplify(ms[RIGHTCHANNEL]))<<16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
return i2s_write_sample_nb(s32); // If we can't store it, return false. OTW true
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
uint32_t s32;
|
||||
if (output_mode == INTERNAL_DAC)
|
||||
{
|
||||
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
|
||||
int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000;
|
||||
s32 = (r << 16) | (l & 0xffff);
|
||||
}
|
||||
else
|
||||
{
|
||||
s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
}
|
||||
return i2s_write_bytes((i2s_port_t)portNo, (const char *)&s32, sizeof(uint32_t), 0);
|
||||
#else
|
||||
uint32_t s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||
return i2s_write_sample_nb(s32); // If we can't store it, return false. OTW true
|
||||
#endif
|
||||
}
|
||||
|
||||
void AudioOutputI2S::flush() {
|
||||
#ifdef ESP32
|
||||
// makes sure that all stored DMA samples are consumed / played
|
||||
int buffersize = 64 * this->dma_buf_count;
|
||||
int16_t samples[2] = {0x0,0x0};
|
||||
for (int i=0;i<buffersize; i++) {
|
||||
while (!ConsumeSample(samples)) {
|
||||
delay(10);
|
||||
void AudioOutputI2S::flush()
|
||||
{
|
||||
#ifdef ESP32
|
||||
// makes sure that all stored DMA samples are consumed / played
|
||||
int buffersize = 64 * this->dma_buf_count;
|
||||
int16_t samples[2] = {0x0, 0x0};
|
||||
for (int i = 0; i < buffersize; i++)
|
||||
{
|
||||
while (!ConsumeSample(samples))
|
||||
{
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AudioOutputI2S::stop()
|
||||
{
|
||||
#ifdef ESP32
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
#endif
|
||||
if (!i2sOn)
|
||||
return false;
|
||||
|
||||
#ifdef ESP32
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,26 +32,33 @@ class AudioOutputI2S : public AudioOutput
|
||||
virtual bool SetRate(int hz) override;
|
||||
virtual bool SetBitsPerSample(int bits) override;
|
||||
virtual bool SetChannels(int channels) override;
|
||||
virtual bool begin() override;
|
||||
virtual bool begin() override { return begin(true); }
|
||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||
virtual void flush() override;
|
||||
virtual bool stop() override;
|
||||
|
||||
bool begin (bool txDAC);
|
||||
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
|
||||
|
||||
enum : int { APLL_AUTO = -1, APLL_ENABLE = 1, APLL_DISABLE = 0 };
|
||||
enum : int { EXTERNAL_I2S = 0, INTERNAL_DAC = 1, INTERNAL_PDM = 2 };
|
||||
|
||||
protected:
|
||||
bool SetPinout();
|
||||
virtual int AdjustI2SRate(int hz) { return hz; }
|
||||
uint8_t portNo;
|
||||
int output_mode;
|
||||
bool mono;
|
||||
bool i2sOn;
|
||||
int dma_buf_count;
|
||||
int use_apll;
|
||||
// We can restore the old values and free up these pins when in NoDAC mode
|
||||
uint32_t orig_bck;
|
||||
uint32_t orig_ws;
|
||||
|
||||
uint8_t bclkPin;
|
||||
uint8_t wclkPin;
|
||||
uint8_t doutPin;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -28,6 +28,7 @@ class AudioOutputI2SNoDAC : public AudioOutputI2S
|
||||
public:
|
||||
AudioOutputI2SNoDAC(int port = 0);
|
||||
virtual ~AudioOutputI2SNoDAC() override;
|
||||
virtual bool begin() override { return AudioOutputI2S::begin(false); }
|
||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||
|
||||
bool SetOversampling(int os);
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
AudioOutputULP
|
||||
Outputs to ESP32 DAC through the ULP, freeing I2S for other uses
|
||||
|
||||
Copyright (C) 2020 Martin Laclaustra, based on bitluni's code
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
#include "AudioOutputULP.h"
|
||||
#include <esp32/ulp.h>
|
||||
#include <driver/rtc_io.h>
|
||||
#include <driver/dac.h>
|
||||
#include <soc/rtc.h>
|
||||
#include <math.h>
|
||||
|
||||
uint32_t create_I_WR_REG(uint32_t reg, uint32_t low_bit, uint32_t high_bit, uint32_t val){
|
||||
typedef union {ulp_insn_t ulp_ins; uint32_t ulp_bin;} ulp_union;
|
||||
const ulp_insn_t singleinstruction[] = {I_WR_REG(reg, low_bit, high_bit, val)};
|
||||
ulp_union recover_ins;
|
||||
recover_ins.ulp_ins=singleinstruction[0];
|
||||
return (uint32_t)(recover_ins.ulp_bin);
|
||||
}
|
||||
|
||||
uint32_t create_I_BXI(uint32_t imm_pc){
|
||||
typedef union {ulp_insn_t ulp_ins; uint32_t ulp_bin;} ulp_union;
|
||||
const ulp_insn_t singleinstruction[] = {I_BXI(imm_pc)};
|
||||
ulp_union recover_ins;
|
||||
recover_ins.ulp_ins=singleinstruction[0];
|
||||
return (uint32_t)(recover_ins.ulp_bin);
|
||||
}
|
||||
|
||||
bool AudioOutputULP::begin()
|
||||
{
|
||||
if(!stereoOutput){
|
||||
waitingOddSample = false;
|
||||
//totalSampleWords += 512;
|
||||
//dacTableStart2 = dacTableStart1;
|
||||
}
|
||||
|
||||
//calculate the actual ULP clock
|
||||
unsigned long rtc_8md256_period = rtc_clk_cal(RTC_CAL_8MD256, 1000);
|
||||
unsigned long rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
|
||||
|
||||
//initialize DACs
|
||||
if(activeDACs & 1){
|
||||
dac_output_enable(DAC_CHANNEL_1);
|
||||
dac_output_voltage(DAC_CHANNEL_1, 128);
|
||||
}
|
||||
if(activeDACs & 2){
|
||||
dac_output_enable(DAC_CHANNEL_2);
|
||||
dac_output_voltage(DAC_CHANNEL_2, 128);
|
||||
}
|
||||
|
||||
int retAddress1 = 9;
|
||||
int retAddress2 = 14;
|
||||
|
||||
int loopCycles = 134;
|
||||
int loopHalfCycles1 = 90;
|
||||
int loopHalfCycles2 = 44;
|
||||
|
||||
Serial.print("Real RTC clock: ");
|
||||
Serial.println(rtc_fast_freq_hz);
|
||||
|
||||
uint32_t dt = (rtc_fast_freq_hz / hertz) - loopCycles;
|
||||
uint32_t dt2 = 0;
|
||||
if(!stereoOutput){
|
||||
dt = (rtc_fast_freq_hz / hertz) - loopHalfCycles1;
|
||||
dt2 = (rtc_fast_freq_hz / hertz) - loopHalfCycles2;
|
||||
}
|
||||
|
||||
Serial.print("dt: ");
|
||||
Serial.println(dt);
|
||||
|
||||
Serial.print("dt2: ");
|
||||
Serial.println(dt2);
|
||||
|
||||
const ulp_insn_t stereo[] = {
|
||||
//reset offset register
|
||||
I_MOVI(R3, 0),
|
||||
//delay to get the right sampling rate
|
||||
I_DELAY(dt), // 6 + dt
|
||||
//reset sample index
|
||||
I_MOVI(R0, 0), // 6
|
||||
//write the index back to memory for the main cpu
|
||||
I_ST(R0, R3, indexAddress), // 8
|
||||
//load the samples
|
||||
I_LD(R1, R0, bufferStart), // 8
|
||||
//mask the lower 8 bits
|
||||
I_ANDI(R2, R1, 0x00ff), // 6
|
||||
//multiply by 2
|
||||
I_LSHI(R2, R2, 1), // 6
|
||||
//add start position
|
||||
I_ADDI(R2, R2, dacTableStart1),// 6
|
||||
//jump to the dac opcode
|
||||
I_BXR(R2), // 4
|
||||
//back from first dac
|
||||
//delay between the two samples in mono rendering
|
||||
I_DELAY(dt2), // 6 + dt2
|
||||
//mask the upper 8 bits
|
||||
I_ANDI(R2, R1, 0xff00), // 6
|
||||
//shift the upper bits to right and multiply by 2
|
||||
I_RSHI(R2, R2, 8 - 1), // 6
|
||||
//add start position of second dac table
|
||||
I_ADDI(R2, R2, dacTableStart2),// 6
|
||||
//jump to the dac opcode
|
||||
I_BXR(R2), // 4
|
||||
//here we get back from writing the second sample
|
||||
//load 0x8080 as sample
|
||||
I_MOVI(R1, 0x8080), // 6
|
||||
//write 0x8080 in the sample buffer
|
||||
I_ST(R1, R0, indexAddress), // 8
|
||||
//increment the sample index
|
||||
I_ADDI(R0, R0, 1), // 6
|
||||
//if reached end of the buffer, jump relative to index reset
|
||||
I_BGE(-16, totalSampleWords), // 4
|
||||
//wait to get the right sample rate (2 cycles more to compensate the index reset)
|
||||
I_DELAY((unsigned int)dt + 2), // 8 + dt
|
||||
//if not, jump absolute to where index is written to memory
|
||||
I_BXI(3) // 4
|
||||
};
|
||||
// write io and jump back another 12 + 4 + 12 + 4
|
||||
|
||||
size_t load_addr = 0;
|
||||
size_t size = sizeof(stereo)/sizeof(ulp_insn_t);
|
||||
ulp_process_macros_and_load(load_addr, stereo, &size);
|
||||
// this is how to get the opcodes
|
||||
// for(int i = 0; i < size; i++)
|
||||
// Serial.println(RTC_SLOW_MEM[i], HEX);
|
||||
|
||||
//create DAC opcode tables
|
||||
switch(activeDACs){
|
||||
case 1:
|
||||
for(int i = 0; i < 256; i++)
|
||||
{
|
||||
RTC_SLOW_MEM[dacTableStart1 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC1_REG,19,26,i); //dac1: 0x1D4C0121 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart1 + 1 + i * 2] = create_I_BXI(retAddress1); // 0x80000000 + retAddress1 * 4
|
||||
RTC_SLOW_MEM[dacTableStart2 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC1_REG,19,26,i); //dac2: 0x1D4C0122 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart2 + 1 + i * 2] = create_I_BXI(retAddress2); // 0x80000000 + retAddress2 * 4
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
for(int i = 0; i < 256; i++)
|
||||
{
|
||||
RTC_SLOW_MEM[dacTableStart1 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC2_REG,19,26,i); //dac1: 0x1D4C0121 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart1 + 1 + i * 2] = create_I_BXI(retAddress1); // 0x80000000 + retAddress1 * 4
|
||||
RTC_SLOW_MEM[dacTableStart2 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC2_REG,19,26,i); //dac2: 0x1D4C0122 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart2 + 1 + i * 2] = create_I_BXI(retAddress2); // 0x80000000 + retAddress2 * 4
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
for(int i = 0; i < 256; i++)
|
||||
{
|
||||
RTC_SLOW_MEM[dacTableStart1 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC1_REG,19,26,i); //dac1: 0x1D4C0121 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart1 + 1 + i * 2] = create_I_BXI(retAddress1); // 0x80000000 + retAddress1 * 4
|
||||
RTC_SLOW_MEM[dacTableStart2 + i * 2] = create_I_WR_REG(RTC_IO_PAD_DAC1_REG,19,26,i); //dac2: 0x1D4C0122 | (i << 10)
|
||||
RTC_SLOW_MEM[dacTableStart2 + 1 + i * 2] = create_I_BXI(retAddress2); // 0x80000000 + retAddress2 * 4
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
//set all samples to 128 (silence)
|
||||
for(int i = 0; i < totalSampleWords; i++)
|
||||
RTC_SLOW_MEM[bufferStart + i] = 0x8080;
|
||||
|
||||
//start
|
||||
RTC_SLOW_MEM[indexAddress] = 0;
|
||||
ulp_run(0);
|
||||
|
||||
//wait until ULP starts using samples and the index of output sample advances
|
||||
while(RTC_SLOW_MEM[indexAddress] == 0)
|
||||
delay(1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioOutputULP::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
int16_t ms[2];
|
||||
ms[0] = sample[0];
|
||||
ms[1] = sample[1];
|
||||
MakeSampleStereo16( ms );
|
||||
|
||||
// TODO: needs improvement (counting is different here with respect to ULP code)
|
||||
int currentSample = RTC_SLOW_MEM[indexAddress] & 0xffff;
|
||||
int currentWord = currentSample >> 1;
|
||||
|
||||
for (int i=0; i<2; i++) {
|
||||
ms[i] = ((ms[i] >> 8) + 128) & 0xff;
|
||||
}
|
||||
if(!stereoOutput) // mix both channels
|
||||
ms[0] = (uint16_t)(( (uint32_t)((int32_t)(ms[0]) + (int32_t)(ms[1])) >> 1 ) & 0xff);
|
||||
|
||||
if(waitingOddSample){ // always true for stereo because samples are consumed in pairs
|
||||
if(lastFilledWord != currentWord) // accept sample if writing index lastFilledWord has not reached index of output sample
|
||||
{
|
||||
unsigned int w;
|
||||
if(stereoOutput){
|
||||
w = ms[0];
|
||||
w |= ms[1] << 8;
|
||||
} else {
|
||||
w = bufferedOddSample;
|
||||
w |= ms[0] << 8;
|
||||
bufferedOddSample = 128;
|
||||
waitingOddSample = false;
|
||||
}
|
||||
RTC_SLOW_MEM[bufferStart + lastFilledWord] = w;
|
||||
lastFilledWord++;
|
||||
if(lastFilledWord == totalSampleWords)
|
||||
lastFilledWord = 0;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
bufferedOddSample = ms[0];
|
||||
waitingOddSample = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool AudioOutputULP::stop()
|
||||
{
|
||||
audioLogger->printf_P(PSTR("\n\n\nstop\n\n\n"));
|
||||
const ulp_insn_t stopulp[] = {
|
||||
//stop the timer
|
||||
I_END(),
|
||||
//end the program
|
||||
I_HALT()};
|
||||
|
||||
size_t load_addr = 0;
|
||||
size_t size = sizeof(stopulp)/sizeof(ulp_insn_t);
|
||||
ulp_process_macros_and_load(load_addr, stopulp, &size);
|
||||
|
||||
//start
|
||||
ulp_run(0);
|
||||
|
||||
if(activeDACs & 1){
|
||||
dac_output_voltage(DAC_CHANNEL_1, 128);
|
||||
}
|
||||
if(activeDACs & 2){
|
||||
dac_output_voltage(DAC_CHANNEL_2, 128);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
AudioOutputULP
|
||||
Outputs to ESP32 DAC through the ULP, freeing I2S for other uses
|
||||
|
||||
Copyright (C) 2020 Martin Laclaustra, based on bitluni's code
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
// Instructions:
|
||||
// AudioOutputULP out = new AudioOutputULP(); // stereo
|
||||
// Connect left channel on pin 25
|
||||
// Connect right channel on pin 26
|
||||
// OR
|
||||
// Connect mono channel on either of them (stereo samples are downmixed)
|
||||
// AudioOutputULP out = new AudioOutputULP(1); // mono, only DAC 1
|
||||
// OR
|
||||
// AudioOutputULP out = new AudioOutputULP(2); // mono, only DAC 2
|
||||
|
||||
|
||||
#ifndef _AUDIOOUTPUTULP_H
|
||||
#define _AUDIOOUTPUTULP_H
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
class AudioOutputULP : public AudioOutput
|
||||
{
|
||||
public:
|
||||
AudioOutputULP(int argActiveDACs=3) {if(argActiveDACs<1||argActiveDACs>2)argActiveDACs=3;activeDACs=argActiveDACs;stereoOutput=activeDACs==3;};
|
||||
~AudioOutputULP() {};
|
||||
virtual bool begin() override;
|
||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||
virtual bool stop() override;
|
||||
enum : int { DAC1 = 1, DAC2 = 2 };
|
||||
private:
|
||||
int lastFilledWord = 0;
|
||||
uint8_t bufferedOddSample = 128;
|
||||
bool waitingOddSample = true; // must be set to false for mono output
|
||||
int activeDACs = 3; // 1:DAC1; 2:DAC2; 3:both;
|
||||
bool stereoOutput = true;
|
||||
const int opcodeCount = 20;
|
||||
const uint32_t dacTableStart1 = 2048 - 512;
|
||||
const uint32_t dacTableStart2 = dacTableStart1 - 512;
|
||||
uint32_t totalSampleWords = 2048 - 512 - 512 - (opcodeCount + 1); // add 512 for mono
|
||||
const int totalSamples = totalSampleWords * 2;
|
||||
const uint32_t indexAddress = opcodeCount;
|
||||
const uint32_t bufferStart = indexAddress + 1;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#error Only the ESP32 supports ULP audio output
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -121,6 +121,8 @@ struct FLAC__BitReader {
|
||||
void *client_data;
|
||||
};
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
static inline void crc16_update_word_(FLAC__BitReader *br, brword word)
|
||||
{
|
||||
unsigned crc = br->read_crc16;
|
||||
@@ -149,6 +151,7 @@ static inline void crc16_update_word_(FLAC__BitReader *br, brword word)
|
||||
#endif
|
||||
br->crc16_align = 0;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
|
||||
{
|
||||
@@ -338,6 +341,8 @@ void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
|
||||
br->crc16_align = br->consumed_bits;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
|
||||
{
|
||||
FLAC__ASSERT(0 != br);
|
||||
@@ -353,6 +358,7 @@ FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
|
||||
}
|
||||
return br->read_crc16;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
|
||||
{
|
||||
|
||||
+3
-1
@@ -136,7 +136,8 @@ FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len)
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
unsigned FLAC__crc16(const FLAC__byte *data, unsigned len)
|
||||
{
|
||||
unsigned crc = 0;
|
||||
@@ -146,3 +147,4 @@ unsigned FLAC__crc16(const FLAC__byte *data, unsigned len)
|
||||
|
||||
return crc;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
@@ -2021,6 +2021,8 @@ FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
|
||||
return true;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
|
||||
{
|
||||
uint32_t channel;
|
||||
@@ -2167,6 +2169,7 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
|
||||
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
||||
return true;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
|
||||
{
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
# endif
|
||||
|
||||
# if !defined(HAVE_ASSERT_H)
|
||||
# undef assert
|
||||
# if defined(NDEBUG)
|
||||
# define assert(x) /* nothing */
|
||||
# else
|
||||
|
||||
@@ -486,16 +486,17 @@ struct tsf_stream_memory { const char* buffer; unsigned int total, pos; };
|
||||
static int tsf_stream_memory_read(struct tsf_stream_memory* m, void* ptr, unsigned int size) { if (size > m->total - m->pos) size = m->total - m->pos; TSF_MEMCPY(ptr, m->buffer+m->pos, size); m->pos += size; return size; }
|
||||
static int tsf_stream_memory_tell(struct tsf_stream_memory* m) { return m->pos; }
|
||||
static int tsf_stream_memory_size(struct tsf_stream_memory* m) { return m->total; }
|
||||
static int tsf_stream_memory_skip(struct tsf_stream_memory* m, unsigned int count) { if (m->pos + count > m->total) return 0; m->pos += count; return 1; }
|
||||
static int tsf_stream_memory_skip(struct tsf_stream_memory* m, unsigned int count) { if (m->pos + count > m->total) count = m->total - m->pos; m->pos += count; return 1; }
|
||||
static int tsf_stream_memory_seek(struct tsf_stream_memory* m, unsigned int pos) { if (pos > m->total) return 0; else m->pos = pos; return 1; }
|
||||
static int tsf_stream_memory_close(struct tsf_stream_memory* m) { (void)m; return 1; }
|
||||
static int tsf_stream_memory_close(struct tsf_stream_memory* m) { TSF_FREE(m); return 1; }
|
||||
TSFDEF tsf* tsf_load_memory(const void* buffer, int size)
|
||||
{
|
||||
struct tsf_stream stream = { TSF_NULL, (int(*)(void*,void*,unsigned int))&tsf_stream_memory_read, (int(*)(void*))&tsf_stream_memory_tell, (int(*)(void*,unsigned int))&tsf_stream_memory_skip, (int(*)(void*,unsigned int))&tsf_stream_memory_seek, (int(*)(void*))&tsf_stream_memory_close, (int(*)(void*))&tsf_stream_memory_size };
|
||||
struct tsf_stream_memory f = { 0, 0, 0 };
|
||||
f.buffer = (const char*)buffer;
|
||||
f.total = size;
|
||||
stream.data = &f;
|
||||
struct tsf_stream_memory* f = (struct tsf_stream_memory*)TSF_MALLOC(sizeof(struct tsf_stream_memory));
|
||||
f->pos = 0;
|
||||
f->buffer = (const char*)buffer;
|
||||
f->total = size;
|
||||
stream.data = f;
|
||||
return tsf_load(&stream);
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -93,9 +93,11 @@ function install_libraries()
|
||||
function install_ide()
|
||||
{
|
||||
local ide_path=$1
|
||||
wget -q -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz
|
||||
local idever='1.8.10'
|
||||
local ideurl="https://downloads.arduino.cc/arduino-$idever"
|
||||
wget -q -O arduino.tar.xz "${ideurl}-linux64.tar.xz"
|
||||
tar xf arduino.tar.xz
|
||||
mv arduino-nightly $ide_path
|
||||
mv arduino-${idever} $ide_path
|
||||
export PATH="$ide_path:$PATH"
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -73,12 +73,17 @@ libopus=../../src/libopus/opus_decoder.c ../../src/libopus/opus_projection_decod
|
||||
|
||||
opusfile=../../src/opusfile/opusfile.c ../../src/opusfile/stream.c ../../src/opusfile/internal.c ../../src/opusfile/info.c
|
||||
|
||||
libflac=../../src/libflac/md5.c ../../src/libflac/window.c ../../src/libflac/memory.c ../../src/libflac/cpu.c \
|
||||
../../src/libflac/fixed.c ../../src/libflac/format.c ../../src/libflac/lpc.c ../../src/libflac/crc.c \
|
||||
../../src/libflac/bitreader.c ../../src/libflac/bitmath.c ../../src/libflac/stream_decoder.c ../../src/libflac/float.c
|
||||
|
||||
|
||||
CCOPTS=-g -Wunused-parameter -Wall -m32 -include Arduino.h
|
||||
CPPOPTS=-g -Wunused-parameter -Wall -std=c++11 -m32 -include Arduino.h
|
||||
|
||||
.phony: all
|
||||
|
||||
all: mp3 aac wav midi opus
|
||||
all: mp3 aac wav midi opus flac
|
||||
|
||||
mp3: FORCE
|
||||
rm -f *.o
|
||||
@@ -94,6 +99,13 @@ aac: FORCE
|
||||
rm -f *.o
|
||||
echo valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./aac
|
||||
|
||||
flac: FORCE
|
||||
rm -f *.o
|
||||
gcc $(CCOPTS) -DUSE_DEFAULT_STDLIB -c $(libflac) -I ../../src/ -I ../../src/libflac -I.
|
||||
g++ $(CPPOPTS) -o flac flac.cpp Serial.cpp *.o ../../src/AudioFileSourceSTDIO.cpp ../../src/AudioOutputSTDIO.cpp ../../src/AudioFileSourceID3.cpp ../../src/AudioGeneratorFLAC.cpp ../../src/AudioLogger.cpp -I ../../src/ -I.
|
||||
rm -f *.o
|
||||
echo valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./flac
|
||||
|
||||
wav: FORCE
|
||||
rm -f *.o
|
||||
g++ $(CPPOPTS) -o wav wav.cpp Serial.cpp ../../src/AudioFileSourceSTDIO.cpp ../../src/AudioOutputSTDIO.cpp ../../src/AudioGeneratorWAV.cpp ../../src/AudioLogger.cpp -I ../../src/ -I.
|
||||
@@ -116,6 +128,6 @@ opus: FORCE
|
||||
echo valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./opus
|
||||
|
||||
clean:
|
||||
rm -f mp3 aac wav midi opus *.o
|
||||
rm -f mp3 aac wav midi opus flac *.o
|
||||
|
||||
FORCE:
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <Arduino.h>
|
||||
#include "AudioFileSourceSTDIO.h"
|
||||
#include "AudioOutputSTDIO.h"
|
||||
#include "AudioGeneratorFLAC.h"
|
||||
|
||||
#define AAC "gs-16b-2c-44100hz.flac"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
AudioFileSourceSTDIO *in = new AudioFileSourceSTDIO(AAC);
|
||||
AudioOutputSTDIO *out = new AudioOutputSTDIO();
|
||||
out->SetFilename("out.flac.wav");
|
||||
AudioGeneratorFLAC *flac = new AudioGeneratorFLAC();
|
||||
|
||||
flac->begin(in, out);
|
||||
while (flac->loop()) { /*noop*/ }
|
||||
flac->stop();
|
||||
|
||||
delete flac;
|
||||
delete out;
|
||||
delete in;
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user