Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40d0d997a8 | ||
|
|
0cee4d410e | ||
|
|
b832ec2434 | ||
|
|
7cb02a6986 | ||
|
|
435e2cf028 | ||
|
|
dd8ac07e4e | ||
|
|
e65cc0fab0 | ||
|
|
1c548c8846 | ||
|
|
2dc245ff2e | ||
|
|
7f3908c239 | ||
|
|
299db3d03d | ||
|
|
0c4c9e1637 | ||
|
|
1c7a2f7cb5 | ||
|
|
bbd8748f86 |
No files matched your search
@@ -22,7 +22,7 @@ const char* ssid = STASSID;
|
|||||||
const char* password = STAPSK;
|
const char* password = STAPSK;
|
||||||
|
|
||||||
// Randomly picked URL
|
// 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;
|
AudioGeneratorMP3 *mp3;
|
||||||
AudioFileSourceICYStream *file;
|
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
|
||||||
|
|
||||||
@@ -8,27 +8,27 @@
|
|||||||
#include "AudioFileSourceICYStream.h"
|
#include "AudioFileSourceICYStream.h"
|
||||||
#include "AudioFileSourceBuffer.h"
|
#include "AudioFileSourceBuffer.h"
|
||||||
#include "AudioGeneratorMP3.h"
|
#include "AudioGeneratorMP3.h"
|
||||||
#include "AudioOutputNull.h"
|
#include "AudioOutputNullSlow.h"
|
||||||
|
|
||||||
// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
|
// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
|
||||||
|
|
||||||
// Enter your WiFi setup here:
|
// Enter your WiFi setup here:
|
||||||
#ifndef STASSID
|
#ifndef STASSID
|
||||||
#define STASSID ""
|
#define STASSID "your-ssid"
|
||||||
#define STAPSK ""
|
#define STAPSK "your-password"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char* ssid = STASSID;
|
const char* ssid = STASSID;
|
||||||
const char* password = STAPSK;
|
const char* password = STAPSK;
|
||||||
|
|
||||||
// Randomly picked URL
|
// Randomly picked URL
|
||||||
const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3";
|
const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";
|
||||||
//const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";
|
//const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3";
|
||||||
|
|
||||||
AudioGeneratorMP3 *mp3;
|
AudioGeneratorMP3 *mp3;
|
||||||
AudioFileSourceICYStream *file;
|
AudioFileSourceICYStream *file;
|
||||||
AudioFileSourceBuffer *buff;
|
AudioFileSourceBuffer *buff;
|
||||||
AudioOutputNull *out;
|
AudioOutputNullSlow *out;
|
||||||
|
|
||||||
// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
|
// 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)
|
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
|
||||||
@@ -80,9 +80,9 @@ void setup()
|
|||||||
audioLogger = &Serial;
|
audioLogger = &Serial;
|
||||||
file = new AudioFileSourceICYStream(URL);
|
file = new AudioFileSourceICYStream(URL);
|
||||||
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
|
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
|
||||||
buff = new AudioFileSourceBuffer(file, 4096);
|
buff = new AudioFileSourceBuffer(file, 2048);
|
||||||
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
|
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
|
||||||
out = new AudioOutputNull();
|
out = new AudioOutputNullSlow(44100);
|
||||||
mp3 = new AudioGeneratorMP3();
|
mp3 = new AudioGeneratorMP3();
|
||||||
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
|
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
|
||||||
mp3->begin(buff, out);
|
mp3->begin(buff, out);
|
||||||
|
|||||||
@@ -17,16 +17,22 @@ if [ "$1" = "clean" ]; then
|
|||||||
cd ${THISLIB}
|
cd ${THISLIB}
|
||||||
rm -f src/*.o src/libmad/*.o
|
rm -f src/*.o src/libmad/*.o
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
elif [ "$1" = diff ]; then
|
||||||
|
cd ${THISLIB}/examples
|
||||||
if [ "$1" = "-h" ]; then
|
diff -u StreamMP3FromHTTP/StreamMP3FromHTTP.ino ${ino}/${ino}.ino
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
echo ""
|
||||||
echo "usage:"
|
echo "usage:"
|
||||||
echo " $0"
|
echo " $0"
|
||||||
echo " $0 clean"
|
echo " $0 clean"
|
||||||
|
echo " $0 diff"
|
||||||
echo " FORCE32=0 $0 (run with valgrind)"
|
echo " FORCE32=0 $0 (run with valgrind)"
|
||||||
echo " FORCE32=1 $0 (run in 32 bits)"
|
echo " FORCE32=1 $0 (run in 32 bits)"
|
||||||
echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory"
|
echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory"
|
||||||
exit 0
|
echo ""
|
||||||
|
[ "$1" = "-h" ] && exit 0
|
||||||
|
sleep 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -z "${FORCE32}" ] && FORCE32=0
|
[ -z "${FORCE32}" ] && FORCE32=0
|
||||||
@@ -37,7 +43,7 @@ else
|
|||||||
run=
|
run=
|
||||||
fi
|
fi
|
||||||
|
|
||||||
eval make FORCE32=${FORCE32} V=1 -j \
|
eval make FORCE32=${FORCE32} -j \
|
||||||
USERCSOURCES=\"${MAD}\" \
|
USERCSOURCES=\"${MAD}\" \
|
||||||
USERCXXSOURCES=\"${THISLIB}/src/AudioFileSourceBuffer.cpp ${THISLIB}/src/AudioLogger.cpp ${THISLIB}/src/AudioGeneratorMP3.cpp ${THISLIB}/src/AudioFileSourceICYStream.cpp ${THISLIB}/src/AudioFileSourceHTTPStream.cpp\" \
|
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/\" \
|
USERCFLAGS=\"-I${THISLIB}/src/\" \
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/earlephilhower/ESP8266Audio"
|
"url": "https://github.com/earlephilhower/ESP8266Audio"
|
||||||
},
|
},
|
||||||
"version": "1.6",
|
"version": "1.8",
|
||||||
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
|
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"SPI": "1.0",
|
"SPI": "1.0",
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
name=ESP8266Audio
|
name=ESP8266Audio
|
||||||
version=1.6
|
version=1.8
|
||||||
author=Earle F. Philhower, III
|
author=Earle F. Philhower, III
|
||||||
maintainer=Earle F. Philhower, III
|
maintainer=Earle F. Philhower, III
|
||||||
sentence=Audio file and I2S sound playing routines.
|
sentence=Audio file and I2S sound playing routines.
|
||||||
|
|||||||
@@ -29,8 +29,8 @@
|
|||||||
class AudioFileSourceFS : public AudioFileSource
|
class AudioFileSourceFS : public AudioFileSource
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AudioFileSourceFS(FS &fs) { filesystem = &fs; }
|
AudioFileSourceFS(fs::FS &fs) { filesystem = &fs; }
|
||||||
AudioFileSourceFS(FS &fs, const char *filename);
|
AudioFileSourceFS(fs::FS &fs, const char *filename);
|
||||||
virtual ~AudioFileSourceFS() override;
|
virtual ~AudioFileSourceFS() override;
|
||||||
|
|
||||||
virtual bool open(const char *filename) 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(); };
|
virtual uint32_t getPos() override { if (!f) return 0; else return f.position(); };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FS *filesystem;
|
fs::FS *filesystem;
|
||||||
File f;
|
fs::File f;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ retry:
|
|||||||
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
||||||
http.end();
|
http.end();
|
||||||
for (int i = 0; i < reconnectTries; i++) {
|
for (int i = 0; i < reconnectTries; i++) {
|
||||||
char buff[32];
|
char buff[64];
|
||||||
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
||||||
cb.st(STATUS_RECONNECTING, buff);
|
cb.st(STATUS_RECONNECTING, buff);
|
||||||
delay(reconnectDelayMs);
|
delay(reconnectDelayMs);
|
||||||
|
|||||||
@@ -83,12 +83,16 @@ AudioFileSourceICYStream::~AudioFileSourceICYStream()
|
|||||||
|
|
||||||
uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool nonBlock)
|
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:
|
retry:
|
||||||
if (!http.connected()) {
|
if (!http.connected()) {
|
||||||
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
cb.st(STATUS_DISCONNECTED, PSTR("Stream disconnected"));
|
||||||
http.end();
|
http.end();
|
||||||
for (int i = 0; i < reconnectTries; i++) {
|
for (int i = 0; i < reconnectTries; i++) {
|
||||||
char buff[32];
|
char buff[64];
|
||||||
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
sprintf_P(buff, PSTR("Attempting to reconnect, try %d"), i);
|
||||||
cb.st(STATUS_RECONNECTING, buff);
|
cb.st(STATUS_RECONNECTING, buff);
|
||||||
delay(reconnectDelayMs);
|
delay(reconnectDelayMs);
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ uint32_t AudioFileSourceID3::read(void *data, uint32_t len)
|
|||||||
if (ret<10) return ret;
|
if (ret<10) return ret;
|
||||||
|
|
||||||
if ((buff[0]!='I') || (buff[1]!='D') || (buff[2]!='3') || (buff[3]>0x04) || (buff[3]<0x02) || (buff[4]!=0)) {
|
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);
|
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
|
// Read the value and send to callback
|
||||||
char value[64];
|
char value[64];
|
||||||
uint16_t i;
|
uint32_t i;
|
||||||
bool isUnicode = (id3.getByte()==1) ? true : false;
|
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();
|
if (i<sizeof(value)-1) value[i] = id3.getByte();
|
||||||
else (void)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') ||
|
} 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) ) {
|
(frameid[0]=='T' && frameid[1]=='Y' && frameid[2]=='E' && rev==2) ) {
|
||||||
cb.md("Year", isUnicode, value);
|
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());
|
} 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
|
// All ID3 processing done, return to main caller
|
||||||
return src->read(data, len);
|
return src->read(data, len);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -231,7 +231,8 @@ retry:
|
|||||||
audioLogger->printf_P(PSTR("MP3:ERROR_BUFLEN %d\n"), unrecoverable);
|
audioLogger->printf_P(PSTR("MP3:ERROR_BUFLEN %d\n"), unrecoverable);
|
||||||
if (++unrecoverable >= 3) {
|
if (++unrecoverable >= 3) {
|
||||||
unrecoverable = 0;
|
unrecoverable = 0;
|
||||||
return (running = false);
|
stop();
|
||||||
|
return running;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
unrecoverable = 0;
|
unrecoverable = 0;
|
||||||
@@ -269,6 +270,9 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
|||||||
return false; // Error
|
return false; // Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset error count from previous file
|
||||||
|
unrecoverable = 0;
|
||||||
|
|
||||||
output->SetBitsPerSample(16); // Constant for MP3 decoder
|
output->SetBitsPerSample(16); // Constant for MP3 decoder
|
||||||
output->SetChannels(2);
|
output->SetChannels(2);
|
||||||
|
|
||||||
|
|||||||
+172
-115
@@ -35,113 +35,77 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
|
|||||||
output_mode = EXTERNAL_I2S;
|
output_mode = EXTERNAL_I2S;
|
||||||
}
|
}
|
||||||
this->output_mode = output_mode;
|
this->output_mode = output_mode;
|
||||||
#ifdef ESP32
|
this->use_apll = use_apll;
|
||||||
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);
|
//set defaults
|
||||||
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;
|
|
||||||
mono = false;
|
mono = false;
|
||||||
bps = 16;
|
bps = 16;
|
||||||
channels = 2;
|
channels = 2;
|
||||||
|
hertz = 44100;
|
||||||
|
bclkPin = 26;
|
||||||
|
wclkPin = 25;
|
||||||
|
doutPin = 22;
|
||||||
SetGain(1.0);
|
SetGain(1.0);
|
||||||
SetRate(44100); // Default
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioOutputI2S::~AudioOutputI2S()
|
AudioOutputI2S::~AudioOutputI2S()
|
||||||
{
|
{
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
if (i2sOn) {
|
if (i2sOn) {
|
||||||
audioLogger->printf("UNINSTALL I2S\n");
|
audioLogger->printf("UNINSTALL I2S\n");
|
||||||
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (i2sOn) i2s_end();
|
if (i2sOn)
|
||||||
#endif
|
i2s_end();
|
||||||
|
#endif
|
||||||
i2sOn = false;
|
i2sOn = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout)
|
bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout)
|
||||||
{
|
{
|
||||||
#ifdef ESP32
|
bclkPin = bclk;
|
||||||
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) return false; // Not allowed
|
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;
|
return true;
|
||||||
#else
|
}
|
||||||
(void) bclk;
|
|
||||||
(void) wclk;
|
bool AudioOutputI2S::SetPinout()
|
||||||
(void) dout;
|
{
|
||||||
return false;
|
#ifdef ESP32
|
||||||
#endif
|
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)
|
bool AudioOutputI2S::SetRate(int hz)
|
||||||
{
|
{
|
||||||
// TODO - have a list of allowable rates from constructor, check them
|
// TODO - have a list of allowable rates from constructor, check them
|
||||||
this->hertz = hz;
|
this->hertz = hz;
|
||||||
#ifdef ESP32
|
if (i2sOn)
|
||||||
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
{
|
||||||
#else
|
#ifdef ESP32
|
||||||
i2s_set_rate(AdjustI2SRate(hz));
|
i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz));
|
||||||
#endif
|
#else
|
||||||
|
i2s_set_rate(AdjustI2SRate(hz));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,13 +129,99 @@ bool AudioOutputI2S::SetOutputModeMono(bool mono)
|
|||||||
return true;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//return if we haven't called ::begin yet
|
||||||
|
if (!i2sOn)
|
||||||
|
return false;
|
||||||
|
|
||||||
int16_t ms[2];
|
int16_t ms[2];
|
||||||
|
|
||||||
ms[0] = sample[0];
|
ms[0] = sample[0];
|
||||||
@@ -183,41 +233,48 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
|
|||||||
int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL];
|
int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL];
|
||||||
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
|
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
|
||||||
}
|
}
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
uint32_t s32;
|
uint32_t s32;
|
||||||
if (output_mode == INTERNAL_DAC) {
|
if (output_mode == INTERNAL_DAC)
|
||||||
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
|
{
|
||||||
int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000;
|
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
|
||||||
s32 = (r<<16) | (l&0xffff);
|
int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000;
|
||||||
} else {
|
s32 = (r << 16) | (l & 0xffff);
|
||||||
s32 = ((Amplify(ms[RIGHTCHANNEL]))<<16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
}
|
||||||
}
|
else
|
||||||
return i2s_write_bytes((i2s_port_t)portNo, (const char*)&s32, sizeof(uint32_t), 0);
|
{
|
||||||
#else
|
s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
|
||||||
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
|
return i2s_write_bytes((i2s_port_t)portNo, (const char *)&s32, sizeof(uint32_t), 0);
|
||||||
#endif
|
#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() {
|
void AudioOutputI2S::flush()
|
||||||
#ifdef ESP32
|
{
|
||||||
// makes sure that all stored DMA samples are consumed / played
|
#ifdef ESP32
|
||||||
int buffersize = 64 * this->dma_buf_count;
|
// makes sure that all stored DMA samples are consumed / played
|
||||||
int16_t samples[2] = {0x0,0x0};
|
int buffersize = 64 * this->dma_buf_count;
|
||||||
for (int i=0;i<buffersize; i++) {
|
int16_t samples[2] = {0x0, 0x0};
|
||||||
while (!ConsumeSample(samples)) {
|
for (int i = 0; i < buffersize; i++)
|
||||||
delay(10);
|
{
|
||||||
|
while (!ConsumeSample(samples))
|
||||||
|
{
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
#endif
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AudioOutputI2S::stop()
|
bool AudioOutputI2S::stop()
|
||||||
{
|
{
|
||||||
#ifdef ESP32
|
if (!i2sOn)
|
||||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
return false;
|
||||||
#endif
|
|
||||||
|
#ifdef ESP32
|
||||||
|
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -32,26 +32,33 @@ class AudioOutputI2S : public AudioOutput
|
|||||||
virtual bool SetRate(int hz) override;
|
virtual bool SetRate(int hz) override;
|
||||||
virtual bool SetBitsPerSample(int bits) override;
|
virtual bool SetBitsPerSample(int bits) override;
|
||||||
virtual bool SetChannels(int channels) 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 bool ConsumeSample(int16_t sample[2]) override;
|
||||||
virtual void flush() override;
|
virtual void flush() override;
|
||||||
virtual bool stop() override;
|
virtual bool stop() override;
|
||||||
|
|
||||||
|
bool begin (bool txDAC);
|
||||||
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
|
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
|
||||||
|
|
||||||
enum : int { APLL_AUTO = -1, APLL_ENABLE = 1, APLL_DISABLE = 0 };
|
enum : int { APLL_AUTO = -1, APLL_ENABLE = 1, APLL_DISABLE = 0 };
|
||||||
enum : int { EXTERNAL_I2S = 0, INTERNAL_DAC = 1, INTERNAL_PDM = 2 };
|
enum : int { EXTERNAL_I2S = 0, INTERNAL_DAC = 1, INTERNAL_PDM = 2 };
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool SetPinout();
|
||||||
virtual int AdjustI2SRate(int hz) { return hz; }
|
virtual int AdjustI2SRate(int hz) { return hz; }
|
||||||
uint8_t portNo;
|
uint8_t portNo;
|
||||||
int output_mode;
|
int output_mode;
|
||||||
bool mono;
|
bool mono;
|
||||||
bool i2sOn;
|
bool i2sOn;
|
||||||
int dma_buf_count;
|
int dma_buf_count;
|
||||||
|
int use_apll;
|
||||||
// We can restore the old values and free up these pins when in NoDAC mode
|
// We can restore the old values and free up these pins when in NoDAC mode
|
||||||
uint32_t orig_bck;
|
uint32_t orig_bck;
|
||||||
uint32_t orig_ws;
|
uint32_t orig_ws;
|
||||||
|
|
||||||
|
uint8_t bclkPin;
|
||||||
|
uint8_t wclkPin;
|
||||||
|
uint8_t doutPin;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ class AudioOutputI2SNoDAC : public AudioOutputI2S
|
|||||||
public:
|
public:
|
||||||
AudioOutputI2SNoDAC(int port = 0);
|
AudioOutputI2SNoDAC(int port = 0);
|
||||||
virtual ~AudioOutputI2SNoDAC() override;
|
virtual ~AudioOutputI2SNoDAC() override;
|
||||||
|
virtual bool begin() override { return AudioOutputI2S::begin(false); }
|
||||||
virtual bool ConsumeSample(int16_t sample[2]) override;
|
virtual bool ConsumeSample(int16_t sample[2]) override;
|
||||||
|
|
||||||
bool SetOversampling(int os);
|
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
|
||||||
@@ -48,6 +48,7 @@
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
# if !defined(HAVE_ASSERT_H)
|
# if !defined(HAVE_ASSERT_H)
|
||||||
|
# undef assert
|
||||||
# if defined(NDEBUG)
|
# if defined(NDEBUG)
|
||||||
# define assert(x) /* nothing */
|
# define assert(x) /* nothing */
|
||||||
# else
|
# else
|
||||||
|
|||||||
+4
-2
@@ -93,9 +93,11 @@ function install_libraries()
|
|||||||
function install_ide()
|
function install_ide()
|
||||||
{
|
{
|
||||||
local ide_path=$1
|
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
|
tar xf arduino.tar.xz
|
||||||
mv arduino-nightly $ide_path
|
mv arduino-${idever} $ide_path
|
||||||
export PATH="$ide_path:$PATH"
|
export PATH="$ide_path:$PATH"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user