Can a PC speaker (with its own amplifier) be directly connected to an ESP32 for audio?

Thread Starter

Adamelli

Joined Sep 14, 2020
66
1683918748641.png

Would this work? I just hear noise from the PlayMODFromPROGMEMToDAC example, which uses pin 25 DAC:
C++:
#include <Arduino.h>
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorMOD.h"
#include "AudioOutputI2S.h"
#if defined(ARDUINO_ARCH_RP2040)
    #define WIFI_OFF
    class __x { public: __x() {}; void mode() {}; };
    __x WiFi;
#elif defined(ESP32)
    #include <WiFi.h>
#else
    #include <ESP8266WiFi.h>
#endif

// enigma.mod sample from the mod archive: https://modarchive.org/index.php?request=view_by_moduleid&query=42146
#include "enigma.h"

AudioGeneratorMOD *mod;
AudioFileSourcePROGMEM *file;
AudioOutputI2S *out;

void setup()
{
  WiFi.mode(WIFI_OFF); //WiFi.forceSleepBegin();
  Serial.begin(115200);
  delay(1000);

  audioLogger = &Serial;
  file = new AudioFileSourcePROGMEM( enigma_mod, sizeof(enigma_mod) );

   /* HERE! */
   // ↓
   // ↓
   out = new AudioOutputI2S(0, 1); //Uncomment this line, comment the next one to use the internal DAC channel 1 (pin25) on ESP32
   // ↑
   // ↑
//  out = new AudioOutputI2S();
  mod = new AudioGeneratorMOD();
  mod->SetBufferSize(3*1024);
  mod->SetSampleRate(44100);
  mod->SetStereoSeparation(32);
  mod->begin(file, out);
}

void loop()
{
  if (mod->isRunning()) {
    if (!mod->loop()) mod->stop();
  } else {
    Serial.printf("MOD done\n");
    delay(1000);
  }
}
 

Thread Starter

Adamelli

Joined Sep 14, 2020
66
1683924682274.png
I changed the configuration of all three pins I have available.
I get a bit more than noise now, but it's garbage.
 

Audioguru again

Joined Oct 21, 2019
6,705
Please use details for the "garbage" sounds:
1) Clipping distortion because the output power is not enough?
2) Mains hum because a shielded audio input cable was not used?
3) No low frequencies because the computer speakers are too small?
4) Overall bad sounds because the computer speakers are too cheap?
 

bertus

Joined Apr 5, 2008
22,278
Hello,

What is the level that is coming from the DAC pins?
It could well be that the signal input for the amplifier must be lower as 1 Vpp.
A higher input voltage will lead to clipping and high distortion.
Also the use if capacitors between the DAC and audio connector may help.

Bertus
 

Thread Starter

Adamelli

Joined Sep 14, 2020
66
I tried this sketch too:

C++:
/*
  LAB Name: ESP32 Audio Music With DAC Example
  Author: Khaled Magdy
  DeepBlueMbedded 2023
  For More Info Visit: www.DeepBlueMbedded.com
*/

#include <driver/dac.h>

// Timer0 Configuration Pointer (Handle)
hw_timer_t *Timer0_Cfg = NULL;

// Variable To Control The TMR0 Auto-Reload Register Value In Order To Change The TimerInterrupt Interval
// Which Enable Us To Control The Output WaveForm's Frequency
uint64_t TMR0_ARR_Val = 10;

// Sine LookUpTable & Index Variable
uint8_t SampleIdx = 0;
const uint8_t sineLookupTable[100] = {
  128, 136, 143, 151, 159, 167, 174, 182,
  189, 196, 202, 209, 215, 220, 226, 231,
  235, 239, 243, 246, 249, 251, 253, 254,
  255, 255, 255, 254, 253, 251, 249, 246,
  243, 239, 235, 231, 226, 220, 215, 209,
  202, 196, 189, 182, 174, 167, 159, 151,
  143, 136, 128, 119, 112, 104, 96, 88,
  81, 73, 66, 59, 53, 46, 40, 35,
  29, 24, 20, 16, 12, 9, 6, 4,
  2, 1, 0, 0, 0, 1, 2, 4,
  6, 9, 12, 16, 20, 24, 29, 35,
  40, 46, 53, 59, 66, 73, 81, 88,
  96, 104, 112, 119
};

// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)
void IRAM_ATTR Timer0_ISR()
{
  // Send SineTable Values To DAC One By One
  dac_output_voltage(DAC_CHANNEL_1, sineLookupTable[SampleIdx++]);
  if (SampleIdx == 100)
  {
    SampleIdx = 0;
  }
}

void setup()
{
  // Configure Timer0 Interrupt
  Timer0_Cfg = timerBegin(0, 80, true);
  timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
  timerAlarmWrite(Timer0_Cfg, TMR0_ARR_Val, true);
  timerAlarmEnable(Timer0_Cfg);
  
  // Disable DAC1 Channel's Output
  dac_output_disable(DAC_CHANNEL_1);  // pin 25
}

void loop()
{

  TMR0_ARR_Val = 50;
  dac_output_enable(DAC_CHANNEL_1);

//  delay(1000);
//
//  TMR0_ARR_Val = 20;
//  dac_output_enable(DAC_CHANNEL_1);
//
//  delay(1000);
//
//  TMR0_ARR_Val = 10;
//  dac_output_enable(DAC_CHANNEL_1);
//  delay(1000);
//
//  TMR0_ARR_Val = 7;
//  dac_output_enable(DAC_CHANNEL_1);

  // Update The Timer Interrupt Interval According To The Desired Tone
  timerAlarmWrite(Timer0_Cfg, TMR0_ARR_Val, true);
}
https://deepbluembedded.com/esp32-dac-audio-arduino-examples/

Nothing even though the DMM reads about 0.5 V from Ch1 or pin 25.
 
Top