Porting for ESP-32 (#33)

* support ESP-32
* make compile with ESP-32/arduino platform
* fixed: use libmad on ESP8266 as original
This commit is contained in:
tueddy authored and Earle F. Philhower, III committed 2018-01-18 08:55:03 -08:00
1 parent 0c002cd014
commit 03aa2467f0
22 files changed
+199 -42

No files matched your search

+5 -2
View File
@@ -22,8 +22,11 @@
#define _AUDIOFILESOURCEHTTPSTREAM_H
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#ifdef ESP32
#include <HTTPClient.h>
#else
#include <ESP8266HTTPClient.h>
#endif
#include "AudioFileSource.h"
class AudioFileSourceHTTPStream : public AudioFileSource
+5 -1
View File
@@ -22,7 +22,11 @@
#define _AUDIOFILESOURCEICYSTREAM_H
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#ifdef ESP32
#include <HTTPClient.h>
#else
#include <ESP8266HTTPClient.h>
#endif
#include "AudioFileSourceHTTPStream.h"
+3
View File
@@ -19,6 +19,9 @@
*/
#include "AudioFileSourceSPIFFS.h"
#ifdef ESP32
#include "SPIFFS.h"
#endif
AudioFileSourceSPIFFS::AudioFileSourceSPIFFS()
{
+11
View File
@@ -231,6 +231,16 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
#undef stack
extern "C" {
#ifdef ESP32
//TOTO - add ESP32 checks
void stack(const char *s, const char *t, int i)
{
}
int stackfree()
{
return 8192;
}
#else
#include <cont.h>
extern cont_t g_cont;
@@ -264,5 +274,6 @@ extern "C" {
int freestack = 4 * (sp - g_cont.stack);
return freestack;
}
#endif
}
+49 -1
View File
@@ -19,14 +19,48 @@
*/
#include <Arduino.h>
#include <i2s.h>
#ifdef ESP32
#include "driver/i2s.h"
#else
#include <i2s.h>
#endif
#include "AudioOutputI2SDAC.h"
bool AudioOutputI2SDAC::i2sOn = false;
#ifdef ESP32
//i2s configuration
int i2s_num = 0; // i2s port number
i2s_config_t i2s_config_dac = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 36000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64 //Interrupt level 1
};
i2s_pin_config_t pin_config_dac = {
.bck_io_num = 26, //this is BCK pin
.ws_io_num = 25, // this is LRCK pin
.data_out_num = 22, // this is DATA output pin
.data_in_num = -1 //Not used
};
#endif
AudioOutputI2SDAC::AudioOutputI2SDAC()
{
#ifdef ESP32
if (!i2sOn) {
//initialize i2s with configurations above
i2s_driver_install((i2s_port_t)i2s_num, &i2s_config_dac, 0, NULL);
i2s_set_pin((i2s_port_t)i2s_num, &pin_config_dac);
}
#else
if (!i2sOn) i2s_begin();
#endif
i2sOn = true;
mono = false;
SetGain(1.0);
@@ -34,7 +68,13 @@ AudioOutputI2SDAC::AudioOutputI2SDAC()
AudioOutputI2SDAC::~AudioOutputI2SDAC()
{
#ifdef ESP32
if (!i2sOn) {
i2s_driver_uninstall((i2s_port_t)i2s_num); //stop & destroy i2s driver
}
#else
if (i2sOn) i2s_end();
#endif
i2sOn = false;
}
@@ -42,7 +82,11 @@ bool AudioOutputI2SDAC::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)i2s_num, hz);
#else
i2s_set_rate(hz);
#endif
return true;
}
@@ -82,7 +126,11 @@ bool AudioOutputI2SDAC::ConsumeSample(int16_t sample[2])
sample[LEFTCHANNEL] = sample[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
}
uint32_t s32 = ((Amplify(sample[RIGHTCHANNEL]))<<16) | (Amplify(sample[LEFTCHANNEL]) & 0xffff);
#ifdef ESP32
return i2s_write_bytes((i2s_port_t)i2s_num, (const char *)&s32, sizeof(uint32_t), 100);
#else
return i2s_write_sample_nb(s32); // If we can't store it, return false. OTW true
#endif
}
bool AudioOutputI2SDAC::stop()
+59 -1
View File
@@ -19,14 +19,49 @@
*/
#include <Arduino.h>
#include <i2s.h>
#ifdef ESP32
#include "driver/i2s.h"
#else
#include <i2s.h>
#endif
#include "AudioOutputI2SNoDAC.h"
#ifdef ESP32
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, /* the DAC module will only take the 8bits from MSB */
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = 0
};
i2s_pin_config_t pin_config = {
.bck_io_num = 26, //this is BCK pin
.ws_io_num = 25, // this is LRCK pin
.data_out_num = 22, // this is DATA output pin
.data_in_num = -1 //Not used
};
#endif
bool AudioOutputI2SNoDAC::i2sOn = false;
AudioOutputI2SNoDAC::AudioOutputI2SNoDAC()
{
#ifdef ESP32
if (!i2sOn) {
//initialize i2s with configurations above
i2s_driver_install((i2s_port_t)0, &i2s_config, 0, NULL);
i2s_set_pin((i2s_port_t)0, &pin_config);
}
#else
if (!i2sOn) i2s_begin();
#endif
i2sOn = true;
hertz = 44100;
oversample = 32;
@@ -37,7 +72,13 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC()
AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC()
{
#ifdef ESP32
if (!i2sOn) {
i2s_driver_uninstall((i2s_port_t)0); //stop & destroy i2s driver
}
#else
if (i2sOn) i2s_end();
#endif
i2sOn = false;
}
@@ -45,7 +86,11 @@ bool AudioOutputI2SNoDAC::SetRate(int hz)
{
// TODO - what is the max hz we can request on I2S?
this->hertz = hz;
#ifdef ESP32
i2s_set_sample_rates((i2s_port_t)0, hz * (oversample / 32));
#else
i2s_set_rate(hz * (oversample / 32));
#endif
return true;
}
@@ -110,6 +155,19 @@ void AudioOutputI2SNoDAC::DeltaSigma(int16_t sample[2], uint32_t dsBuff[4])
}
}
#ifdef ESP32
/* write sample data to I2S */
int i2s_write_sample_nb(uint8_t sample){
return i2s_write_bytes((i2s_port_t)0, (const char *)&sample, sizeof(uint8_t), 100);
}
// how to do this blocking??
int i2s_write_sample(uint8_t sample){
return i2s_write_bytes((i2s_port_t)0, (const char *)&sample, sizeof(uint8_t), 100);
}
#endif
bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2])
{
MakeSampleStereo16( sample );
+3
View File
@@ -20,6 +20,9 @@
#include <Arduino.h>
#include <FS.h>
#ifdef ESP32
#include "SPIFFS.h"
#endif
#include "AudioOutputSPIFFSWAV.h"
+2 -21
View File
@@ -532,8 +532,8 @@ static FLAC__StreamDecoderInitStatus init_FILE_internal_(
* must assign the FILE pointer before any further error can occur in
* this routine.
*/
if(file == stdin)
file = get_binary_stdin_(); /* just to be safe */
// if(file == stdin)
// file = get_binary_stdin_(); /* just to be safe */
decoder->private_->file = file;
@@ -1274,26 +1274,7 @@ void set_defaults_(FLAC__StreamDecoder *decoder)
#endif
}
/*
* This will forcibly set stdin to binary mode (for OSes that require it)
*/
FILE *get_binary_stdin_(void)
{
/* if something breaks here it is probably due to the presence or
* absence of an underscore before the identifiers 'setmode',
* 'fileno', and/or 'O_BINARY'; check your system header files.
*/
#if defined _MSC_VER || defined __MINGW32__
_setmode(_fileno(stdin), _O_BINARY);
#elif defined __CYGWIN__
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
setmode(_fileno(stdin), _O_BINARY);
#elif defined __EMX__
setmode(fileno(stdin), O_BINARY);
#endif
return stdin;
}
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
{
+1
View File
@@ -28,6 +28,7 @@
# include <stdlib.h>
# include <string.h>
# include <stdint.h>
# ifdef HAVE_ASSERT_H
# include <assert.h>
+1
View File
@@ -21,6 +21,7 @@
#pragma GCC optimize ("O3")
#include <stddef.h>
#include <pgmspace.h>
# include "config.h"
+4
View File
@@ -1257,7 +1257,11 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i
void DumpF32P32(char *name, long long x) {
#ifdef ESP32
printf("%s = %08x.%08x\n", name, (int)((x>>32)&0xffffffff), (int)(x&0xffffffff));
#else
printf("%s = %08x.%08x\n", name, (int32)((x>>32)&0xffffffff), (int32)(x&0xffffffff));
#endif
}
static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuffer, int numSamples)
{