Arduino 平台下 ESP32-P4 MP3音频文件播放

ESP32-P4 开发板arduino 平台下从SD_MMC读取MP3文件播实验程序,初步验证成功。
开发板使用斑梨电子JC1060P470_P4,板载ES8311音频解码器和四线SD卡模块。
ES8311在Arduino下驱动使用了github上某国外猿的驱动代码,并搭配ESP32-AudioI2S库I2Saudio示例实现。原来想直接使用pschatzmann的arduino-drivers库,无奈此库在ESP32 3.10 ESP32 P4 Dev Module环境下编译始终无法通过。
程序环境如下、
Arduino IDE 2.3.4
Arduino IDE 中开发板使用 ESP32 P4 Dev Module
Arduino ESP32内核 3.10
ESP32-AudioI2S 版本 3.0.8(使用最新版3.0.13编译不通过)

#include "Arduino.h"
//#include "WiFiMulti.h"
#include "Audio.h"
#include "SPI.h"
#include "SD.h"
#include "SD_MMC.h"
#include "FS.h"
#include "Wire.h"
#include "esp_check.h"
#include "es8311.h"

//SD_MMC
#define SD_D0 39
#define SD_D1 40
#define SD_D2 41
#define SD_D3 42
#define SD_CMD 44
#define SD_CLK 43

Audio audio;

#define EXAMPLE_SAMPLE_RATE 16000
#define EXAMPLE_VOICE_VOLUME 60                  // 0 - 100
#define EXAMPLE_MIC_GAIN (es8311_mic_gain_t)(3)  // 0 - 7

#define I2C_NUM 0

#define SDAPIN 7  
#define SCLPIN 8

#define I2S_MCK_IO 13
#define I2S_BCK_IO 12
#define I2S_DI_IO 11
#define I2S_WS_IO 10  // Word Select (LRCK)
#define I2S_DO_IO 9

#define ES8311_PA 20  //ES8311 使能

const char *TAG = "esp32p4_i2s_es8311";

esp_err_t es8311_codec_init(void) {
  Wire.begin(SDAPIN, SCLPIN);
  es8311_handle_t es_handle = es8311_create(I2C_NUM, ES8311_ADDRRES_0);
  ESP_RETURN_ON_FALSE(es_handle, ESP_FAIL, TAG, "es8311 create failed");
  const es8311_clock_config_t es_clk = {
    .mclk_inverted = false,
    .sclk_inverted = false,
    .mclk_from_mclk_pin = true,
    .mclk_frequency = EXAMPLE_SAMPLE_RATE * 256,
    .sample_frequency = EXAMPLE_SAMPLE_RATE
  };

  ESP_ERROR_CHECK(es8311_init(es_handle, &es_clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16));
  ESP_RETURN_ON_ERROR(es8311_sample_frequency_config(es_handle, es_clk.mclk_frequency, es_clk.sample_frequency), TAG, "set es8311 sample frequency failed");
  ESP_RETURN_ON_ERROR(es8311_microphone_config(es_handle, false), TAG, "set es8311 microphone failed");

  ESP_RETURN_ON_ERROR(es8311_voice_volume_set(es_handle, EXAMPLE_VOICE_VOLUME, NULL), TAG, "set es8311 volume failed");
  ESP_RETURN_ON_ERROR(es8311_microphone_gain_set(es_handle, EXAMPLE_MIC_GAIN), TAG, "set es8311 microphone gain failed");
  return ESP_OK;
}

void scan_i2c_device(TwoWire &i2c)  //I2C 模块地址扫描函数
{
  Serial.println("Scanning for I2C devices ...");
  Serial.print("      ");
  for (int i = 0; i < 0x10; i++) {
    Serial.printf("0x%02X|", i);
  }
  uint8_t error;
  for (int j = 0; j < 0x80; j += 0x10) {
    Serial.println();
    Serial.printf("0x%02X |", j);
    for (int i = 0; i < 0x10; i++) {
      Wire.beginTransmission(i | j);
      error = Wire.endTransmission();
      if (error == 0)
        Serial.printf("0x%02X|", i | j);
      else
        Serial.print(" -- |");
    }
  }
  Serial.println();
}

bool use_1_bit_mode = true;

void setup() {
  Serial.begin(115200);

  SD_MMC.setPins(SD_CLK, SD_CMD, SD_D0, SD_D1, SD_D2, SD_D3);  

  if (!SD_MMC.begin()) {  //"/sdcard", use_1_bit_mode
    Serial.println("Card Mount Failed");
    return;
  }

  Serial.setDebugOutput(true);

  es8311_codec_init();
  pinMode(ES8311_PA, OUTPUT);
  digitalWrite(ES8311_PA, HIGH);
  scan_i2c_device(Wire);

  audio.setPinout(I2S_BCK_IO, I2S_WS_IO, I2S_DO_IO, I2S_MCK_IO);  
  audio.setVolume(21);                                            // 0...21
  audio.connecttoFS(SD_MMC, "1.mp3");
}

void loop() {
  audio.loop();
}

// optional
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_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);
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值