Skip to content

Speaker produces crackling/distorted audio with M5Unified #371

Description

@chate

Title: Speaker produces crackling/distorted audio with M5Unified

Environment:

  • Arduino IDE: 2.3.10
  • M5Stack Board: 3.3.9
  • M5Unified: 0.2.22
  • Device: M5Stack Basic
  • OS: Mac

Issue Description:
When using M5Unified 0.2.22, the speaker produces crackling/distorted audio (buzz)
when playing tones or notes. The same speaker works cleanly with M5Stack.h library,
indicating a problem with M5Unified's speaker driver implementation, not hardware.

My code

#include <M5Unified.h>

// Note frequencies
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494

#define MSEC_PER_MIN 60000
int melody[] = { G4, G4, G4, D4, E4, E4, D4, B4, B4, A4, A4, G4 };
int noteValues[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int numNotes = sizeof(melody)/sizeof(melody[0]);

int tempo = 120;  // normal tempo BPM

void setup() {
  // initialize M5Stack and speaker
  M5.begin();
  M5.Speaker.setVolume(64); // 128
  M5.Display.setTextSize(3);
  M5.Lcd.println("Press BtnA to play");
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    for (int i = 0; i < numNotes; i++) {
      int playTime = (noteValues[i]*(MSEC_PER_MIN / tempo));

      M5.Speaker.tone(melody[i], playTime);
      delay(playTime);
      delay(50);
    }
  }
}

Old Environment:

  • Arduino IDE: 2.3.10
  • M5Stack Board: 2.1.4
  • M5Stack: 0.4.6
  • Device: M5Stack Basic
  • OS: Mac

Code for old environment:

#include <M5Stack.h>

// Note frequencies
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494

#define MSEC_PER_MIN 60000
int melody[] = { G4, G4, G4, D4, E4, E4, D4, B4, B4, A4, A4, G4 };
int noteValues[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int numNotes = sizeof(melody)/sizeof(melody[0]);

int tempo = 120;  

void setup() {
  // initialize M5Stack and speaker
  M5.begin();
  M5.Speaker.setVolume(4);
  M5.Lcd.setTextSize(3);
  M5.Lcd.println("Press BtnA to play");
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    for (int i = 0; i < numNotes; i++) {
      int playTime = (noteValues[i]*(MSEC_PER_MIN / tempo));

      M5.Speaker.tone(melody[i]);
      delay(playTime);
      M5.Speaker.mute();
      delay(50);
    }
  }
}

Actual Behavior:
Audio produces crackling/distorted sound (buzz) overlaid on the tone. While using old M5Stack board 2.1.4 + M5Stack libary 0.4.6 produces clean, clear audio without any distortion.

Workaround Attempted:

  • Reducing volume with M5.Speaker.setVolume(64) - doesn't fix
  • Using M5.Speaker.playRaw() instead of tone() - still produces distortion
  • Config M5.Speaker
auto cfg = M5.Speaker.config();
cfg.magnification = 8;      
cfg.sample_rate = 44100;    
cfg.dma_buf_len = 256;      
M5.Speaker.setConfig(cfg);

doesn't fix

Please help me how to fix this problem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions