Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c4c9e1637 | ||
|
|
1c7a2f7cb5 | ||
|
|
bbd8748f86 | ||
|
|
299833dbf9 | ||
|
|
90258c566a | ||
|
|
0ec3bc3b31 | ||
|
|
2fb8d887b1 | ||
|
|
93a540da9b | ||
|
|
1c51669729 | ||
|
|
96d036087b |
No files matched your search
@@ -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 "AudioOutputNull.h"
|
||||
|
||||
// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
|
||||
|
||||
// Enter your WiFi setup here:
|
||||
#ifndef STASSID
|
||||
#define STASSID ""
|
||||
#define STAPSK ""
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
// Randomly picked URL
|
||||
const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3";
|
||||
//const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceICYStream *file;
|
||||
AudioFileSourceBuffer *buff;
|
||||
AudioOutputNull *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, 4096);
|
||||
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
|
||||
out = new AudioOutputNull();
|
||||
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
+50
@@ -0,0 +1,50 @@
|
||||
#!/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
|
||||
fi
|
||||
|
||||
if [ "$1" = "-h" ]; then
|
||||
echo "usage:"
|
||||
echo " $0"
|
||||
echo " $0 clean"
|
||||
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"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
[ -z "${FORCE32}" ] && FORCE32=0
|
||||
|
||||
if [ "${FORCE32}" = 0 ]; then
|
||||
run=valgrind
|
||||
else
|
||||
run=
|
||||
fi
|
||||
|
||||
eval make FORCE32=${FORCE32} V=1 -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.7",
|
||||
"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.7
|
||||
author=Earle F. Philhower, III
|
||||
maintainer=Earle F. Philhower, III
|
||||
sentence=Audio file and I2S sound playing routines.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
AudioOutputULP
|
||||
Outputs to ESP32 DAC through the ULP, freeing I2S for other uses
|
||||
|
||||
v 0.0.1 (2020-10-01)
|
||||
|
||||
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 = 120;
|
||||
int loopHalfCycles1 = 76;
|
||||
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
|
||||
//increment the sample index
|
||||
I_ADDI(R0, R0, 1), // 6
|
||||
//if reached end of the buffer, jump relative to index reset
|
||||
I_BGE(-14, 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 = ms[0];
|
||||
if(stereoOutput){
|
||||
w |= ms[1] << 8;
|
||||
} else {
|
||||
w |= bufferedOddSample << 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,63 @@
|
||||
/*
|
||||
AudioOutputULP
|
||||
Outputs to ESP32 DAC through the ULP, freeing I2S for other uses
|
||||
|
||||
v 0.0.1 (2020-10-01)
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
// Connect left channel on pin 25
|
||||
// Connect right channel on pin 26
|
||||
|
||||
#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 = 18;
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+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