This commit is contained in:
andre.malo 2023-05-24 12:05:48 +02:00
parent e944023ab3
commit 6b5623c560
3 changed files with 105 additions and 102 deletions

BIN
Mozart - Lacrimosa.mp3 Normal file

Binary file not shown.

View file

@ -13,3 +13,4 @@ platform = espressif32
board = esp32-devkitlipo
framework = arduino
monitor_speed = 9600
lib_deps = esphome/ESP32-audioI2S@^2.0.7

View file

@ -8,109 +8,111 @@
https://dronebotworkshop.com
*/
#include <Arduino.h>
// Include I2S driver
#include <driver/i2s.h>
/*
Simple Internet Radio Demo
esp32-i2s-simple-radio.ino
Simple ESP32 I2S radio
Uses MAX98357 I2S Amplifier Module
Uses ESP32-audioI2S Library - https://github.com/schreibfaul1/ESP32-audioI2S
// Connections to INMP441 I2S microphone
#define I2S_WS 25
#define I2S_SD 33
#define I2S_SCK 32
DroneBot Workshop 2022
https://dronebotworkshop.com
*/
// Use I2S Processor 0
#define I2S_PORT I2S_NUM_0
// Include required libraries
#include "WiFi.h"
#include "Audio.h"
// Define input buffer length
#define bufferLen 1024
int16_t sBuffer[bufferLen];
// Define I2S connections
#define I2S_DOUT 22
#define I2S_BCLK 26
#define I2S_LRC 25
void i2s_install()
{
// Set up I2S Processor configuration
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 40000,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
.dma_buf_len = bufferLen,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0};
// Create audio object
Audio audio;
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
// Wifi Credentials
String ssid = "cohabit";
String password = "Fablab";
void i2s_setpin()
{
// Set I2S pin configuration
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD};
void setup() {
i2s_set_pin(I2S_PORT, &pin_config);
}
// Start Serial Monitor
Serial.begin(115200);
void setup()
{
// Set up Serial Monitor
Serial.begin(115200);
Serial.println(" ");
delay(1000);
// Set up I2S
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
// Setup WiFi in Station mode
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// WiFi Connected, print IP to serial monitor
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
// Connect MAX98357 I2S Amplifier Module
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
// Set thevolume (0-100)
audio.setVolume(10);
// Connect to an Internet radio station (select one as desired)
//audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3");
//audio.connecttohost("mediaserv30.live-nect MAX98357 I2S Amplifier Module
//audio.connecttohost("www.surfmusic.de/m3u/100-5-das-hitradio,4529.m3u");
//audio.connecttohost("stream.1a-webradio.de/deutsch/mp3-128/vtuner-1a");
//audio.connecttohost("www.antenne.de/webradio/antenne.m3u");
audio.connecttohost("0n-80s.radionetz.de:8000/0n-70s.mp3");
}
void loop()
{
// Run audio player
audio.loop();
// False print statements to "lock range" on serial plotter display
// Change rangelimit value to adjust "sensitivity"
int rangelimit = 3000;
Serial.print(rangelimit * -1);
Serial.print(" ");
Serial.print(rangelimit);
Serial.print(" ");
// Get I2S data and place in data buffer
size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
if (result == ESP_OK)
{
// Read I2S data buffer
int16_t samples_read = bytesIn / 8;
if (samples_read > 0)
{
float mean = 0;
for (int16_t i = 0; i < samples_read; ++i)
{
mean += (sBuffer[i]);
}
// Average the data reading
mean /= samples_read;
// Print to serial plotter
Serial.println(mean);
int recu = bytesIn / sizeof(int32_t);
for (int i = 0; i < recu; i++)
{
Serial.printf("%ld\n", sBuffer[i]);
}
// Serial.print(" - ");
Serial.println(bytesIn);
}
}
}
// Audio status functions
void audio_info(const char *info) {
Serial.print("info "); Serial.println(info);
}
void audio_id3data(const char *info) { //id3 metadata
Serial.print("id3data "); Serial.println(info);
}
void audio_eof_mp3(const char *info) { //end of file
Serial.print("eof_mp3 "); Serial.println(info);
}
void audio_showstation(const char *info) {
Serial.print("station "); Serial.println(info);
}
void audio_showstreaminfo(const char *info) {
Serial.print("streaminfo "); Serial.println(info);
}
void audio_showstreamtitle(const char *info) {
Serial.print("streamtitle "); Serial.println(info);
}
void audio_bitrate(const char *info) {
Serial.print("bitrate "); Serial.println(info);
}
void audio_commercial(const char *info) { //duration in sec
Serial.print("commercial "); Serial.println(info);
}
void audio_icyurl(const char *info) { //homepage
Serial.print("icyurl "); Serial.println(info);
}
void audio_lasthost(const char *info) { //stream URL played
Serial.print("lasthost "); Serial.println(info);
}
void audio_eof_speech(const char *info) {
Serial.print("eof_speech "); Serial.println(info);
}