Giter Site home page Giter Site logo

wm8978-esp32's Introduction

wm8978-esp32

Codacy Badge

Arduino IDE library for wm8978 dac on ESP32 mcu. Tested/works with a M5Stack Node.

Example code:

Setup i2c and wm8978 in one call

#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h>  /* https://github.com/schreibfaul1/ESP32-audioI2S */

/* M5Stack Node WM8978 I2C pins */
#define I2C_SDA     21
#define I2C_SCL     22

/* M5Stack Node I2S pins */
#define I2S_BCK      5
#define I2S_WS      13
#define I2S_DOUT     2
#define I2S_DIN     34
#define I2S_MCLK     0

Audio audio;
WM8978 dac;

void setup() {
  /* Setup wm8978 I2C interface */
  if (!dac.begin(I2C_SDA, I2C_SCL)) {
    log_e("Error setting up dac. System halted");
    while (1) delay(100);
  }
  dac.setSPKvol(40); /* max 63 */
  dac.setHPvol(32, 32);

  WiFi.begin("xxx", "xxx");
  while (!WiFi.isConnected()) {
    delay(10);
  }

  /* set i2s pins */
  audio.setPinout(I2S_BCK, I2S_WS, I2S_DOUT, I2S_DIN, I2S_MCLK);

  log_i("Connected. Starting MP3...");
  audio.connecttohost("http://icecast.omroep.nl/3fm-bb-mp3");
}

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

Setup i2c and wm8978 separately

#include <Wire.h>
#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h>  /* https://github.com/schreibfaul1/ESP32-audioI2S */

/* M5Stack Node WM8978 I2C pins */
#define I2C_SDA     21
#define I2C_SCL     22

/* M5Stack Node I2S pins */
#define I2S_BCK      5
#define I2S_WS      13
#define I2S_DOUT     2
#define I2S_DIN     34
#define I2S_MCLK     0

WM8978 dac;
Audio audio;

void setup() {

  if (!Wire.begin(I2C_SDA, I2C_SCL, 400000))
    log_e("i2c setup error!");

  if (!dac.begin())
    log_e("WM8978 setup error!");

  dac.setSPKvol(40); /* max 63 */
  dac.setHPvol(32, 32);

  WiFi.begin("xxx", "xxx");
  while (!WiFi.isConnected()) {
    delay(10);
  }

  /* set i2s pins */
  audio.setPinout(I2S_BCK, I2S_WS, I2S_DOUT, I2S_DIN, I2S_MCLK);

  log_i("Connected. Starting MP3...");
  audio.connecttohost("http://icecast.omroep.nl/3fm-bb-mp3");
}

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

wm8978-esp32's People

Contributors

celliesprojects avatar codacy-badger avatar h3ndrik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wm8978-esp32's Issues

ESPHome i2s_audio compatibility

Teensyduino version 1.20 or later is required to compile the Audio library.

I'm getting this error:

platformio.ini
[env:m5stack-core-esp32]
platform = espressif32
board = m5stack-core-esp32
framework = arduino
upload_speed = 115200
monitor_speed = 115200
lib_deps =
hieromon/AutoConnect@^1.2.2
me-no-dev/ESP Async WebServer@^1.2.3
celliesprojects/wm8978-esp32@^1.0.2

`In file included from src/main.cpp:5:0:
.pio/libdeps/m5stack-core-esp32/Audio/Audio.h:31:2: error: #error "Teensyduino version 1.20 or later is required to compile the Audio library."
#error "Teensyduino version 1.20 or later is required to compile the Audio library."
^
.pio/libdeps/m5stack-core-esp32/Audio/Audio.h:37:24: fatal error: DMAChannel.h: No such file or directory

Looking for DMAChannel.h dependency? Check our library registry!
CLI > platformio lib search "header:DMAChannel.h"
Web > https://platformio.org/lib/search?query=header:DMAChannel.h
compilation terminated.
Compiling .pio/build/m5stack-core-esp32/lib3bc/Audio/analyze_fft1024.cpp.o
Compiling .pio/build/m5stack-core-esp32/lib3bc/Audio/analyze_fft256.cpp.o
Compiling .pio/build/m5stack-core-esp32/lib3bc/Audio/analyze_peak.cpp.o
Compiling .pio/build/m5stack-core-esp32/lib3bc/Audio/analyze_print.cpp.o
Compiling .pio/build/m5stack-core-esp32/lib3bc/Audio/analyze_tonedetect.cpp.o
In file included from .pio/libdeps/m5stack-core-esp32/Audio/analyze_fft1024.cpp:27:0:
.pio/libdeps/m5stack-core-esp32/Audio/analyze_fft1024.h:30:25: fatal error: AudioStream.h: No such file or directory

M5.Lcd operations are blocked?

Hello,

Nice piece of work!
I got it to work on a M5-Node. I had one, and was always wondering how to get this sound-chip to work. I did not see any Arduino examples about it. The sound is really clear on an external amplied speaker. Impressed with the chip (https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/base/WM8978%20_en.pdf)

I have added some M5-functions to show something on the screen, change the volume for both internal speaker and external audio.
My question, my screen does only show a white area on the M5-Basic Version, and sometimes I see some screen-garbling on a black screen when pushing a button clearing the screen with M5.Lcd.clear();

On the M5-Grey the screen starts white and then turns black as per instruction. But no texts.

Do you have any idea where this interference comes from? Some shared I2S port or something?
I would like to integrate the sound-stream playing in my project, and there I use the screen on the M5.
If you have any wise judgement, that would be welcome.

PS: What I intended with this script is to add some buttons to control the volume. Button A to switch between Internal Speaker and Button B to lower the volume and Button C to make the volume higher in steps.

// ==========================================================
// Added by PV:
#include <Arduino.h>
#include <M5Stack.h>

// PV This script uses both the Speaker Jack on the M5Stack NODE Base and the speaker of the M5Stack

#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h>  /* https://github.com/schreibfaul1/ESP32-audioI2S */

/* M5Stack Node WM8978 I2C pins */
#define I2C_SDA     21
#define I2C_SCL     22

/* M5Stack Node I2S pins */
#define I2S_BCK      5
#define I2S_WS      13
#define I2S_DOUT     2
#define I2S_DIN     34

/* M5Stack WM8978 MCLK gpio number and frequency */
#define I2S_MCLKPIN  0
#define I2S_MFREQ  (24 * 1000 * 1000)

WM8978 dac;
Audio audio; //  PV: I believe that this line starts to disrupt the screen operation. 

char audio_stream_str[100];

void setup() {
  M5.begin(); //  M5.begin(bool LCDEnable, bool SDEnable, bool SerialEnable, bool I2CEnable)

  delay(2000);
  Serial.println(F("M5Node Test , playing a Audio Stream from Internet"));

  M5.Lcd.setTextFont(4);
  M5.Lcd.setCursor(20, 100);
  M5.Lcd.print("Music-Stream Player");

  delay(3000);
  
    
  /* Setup wm8978 I2C interface */
  if (!dac.begin(I2C_SDA, I2C_SCL)) {
    ESP_LOGE(TAG, "Error setting up dac. System halted");
    while (1) delay(100);
  }

  /* Setup wm8978 MCLK on gpio - for example M5Stack Node needs this clock on gpio 0 */
  double retval = dac.setPinMCLK(I2S_MCLKPIN, I2S_MFREQ);
  if (!retval)
    ESP_LOGE(TAG, "Could not set %.2fMHz clock signal on GPIO %i", I2S_MFREQ / (1000.0 * 1000.0), I2S_MCLKPIN);
  else
    ESP_LOGI(TAG, "Set %.2fMHz clock signal on GPIO %i", retval / (1000.0 * 1000.0), I2S_MCLKPIN);


  /* Setup wm8978 I2S interface */
  audio.setPinout(I2S_BCK, I2S_WS, I2S_DOUT, I2S_DIN);

  WiFi.begin("xxx", "xxx");
  while (!WiFi.isConnected()) {
    Serial.print(F("."));
    delay(100);
  }
  ESP_LOGI(TAG, "Connected");

  ESP_LOGI(TAG, "Starting MP3...\n");
  //audio.connecttohost("http://icecast.omroep.nl/3fm-bb-mp3");
  strcpy_P(audio_stream_str, PSTR("http://icecast.omroep.nl/3fm-bb-mp3"));
  audio.connecttohost(audio_stream_str);
  
  dac.setSPKvol(40); /* max 63 */
  //dac.setSPKvol(55);  // OK volume 
  //dac.setSPKvol(57); // Seems to be the sweetspot for the Internal Speaker
  //dac.setSPKvol(59); // 59 is a bit distorted
  //dac.setSPKvol(63); /* 63 is overmodulating */
  dac.setHPvol(32, 32); // See : https://github.com/CelliesProjects/wm8978-esp32/blob/master/src/WM8978.cpp
}  // End of setup()

void loop() {
  char my_tmp_str[100];
  static char  volume_control_device = 'O';
  static int OutputVol = 32; // Default Output-Volume (Through 3.5mm jack)
  static int SpeakerVol = 40; // Default Speaker-Volume (Through M5Stack Speaker)
  
  audio.loop();
  //delay(1);

  M5.update();

  if (M5.BtnA.wasPressed()) {
    M5.Lcd.clear();
    M5.Lcd.printf("A wasPressed \r\n");
    Serial.println("A wasPressed");
    if (volume_control_device == 'O'){
      M5.Lcd.printf("Internal Speaker Control\r\n");
      Serial.printf("Internal Speaker Control\r\n");
      volume_control_device = 'I';
    } else {
      M5.Lcd.printf("External Speaker Control\r\n");
      Serial.printf("External Speaker Control\r\n");
      volume_control_device = 'O';
    }
  }
  
  if (M5.BtnB.wasPressed()) {
    M5.Lcd.clear();
    M5.Lcd.printf("B wasPressed \r\n");
    Serial.println("B wasPressed");
    if (volume_control_device == 'O'){
      OutputVol--;
      OutputVol--;
      if (OutputVol < 0){
        OutputVol = 0;
      }
      dac.setHPvol(OutputVol, OutputVol);
      M5.Lcd.printf("Output volume %d\r\n", OutputVol); 
      Serial.printf("Output volume %d\r\n", OutputVol); 
    } else {
      SpeakerVol--;
      SpeakerVol--;
      if (SpeakerVol < 0){
        SpeakerVol = 0;
      }
      dac.setSPKvol(SpeakerVol);
      M5.Lcd.printf("Speaker volume %d\r\n", SpeakerVol); 
      Serial.printf("Speaker volume %d\r\n", SpeakerVol); 
    }
  }

  if (M5.BtnC.wasPressed()) {
    M5.Lcd.clear();
    M5.Lcd.printf("C wasPressed \r\n");
    Serial.println("C wasPressed");
    if (volume_control_device == 'O'){
      OutputVol++;
      OutputVol++;
      if (OutputVol > 63){
        OutputVol = 63;
      }
      dac.setHPvol(OutputVol, OutputVol);
      M5.Lcd.printf("Output volume %d\r\n", OutputVol); 
      Serial.printf("Output volume %d\r\n", OutputVol); 
    } else {
      SpeakerVol++;
      SpeakerVol++;
      if (SpeakerVol > 63){
        SpeakerVol = 63;
      }
      dac.setSPKvol(SpeakerVol);
      M5.Lcd.printf("Speaker volume %d\r\n", SpeakerVol); 
      Serial.printf("Speaker volume %d\r\n", SpeakerVol); 
    }
  }
} // End of loop()

High power consumption

Hi,

I am currently testing with a ttgo audio kit (wm8978) and a custom pcb design.

In both cases I notice a very high power consumption when playing mp3. About 400 mAh and the chip gets very warm.

Is this normal or am I configuring something wrong?

Wont compile.

I haven't been able to get the code to compile, I get these errors:
WM8978.cpp:321:18: error: could not convert 'Wire.TwoWire::begin(((int)sda), ((int)scl), ((uint32_t)frequency))' from 'void' to 'bool'
if (!Wire.begin(sda, scl, frequency)) {
and
WM8978.cpp:321:38: error: in argument to unary !
if (!Wire.begin(sda, scl, frequency)) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.