Skip to content

Commit

Permalink
start/stop DAC and other small changes in order to reduce click noise…
Browse files Browse the repository at this point in the history
… at beginning / end of audio playback
  • Loading branch information
ChrisVeigl committed Jul 7, 2021
1 parent 6ed2604 commit 1ce70dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/AudioOutputI2S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#endif
#include "AudioOutputI2S.h"


#define WAIT_SAMPLES_BEFORE_DAC_EN 100


#if defined(ESP32) || defined(ESP8266)
AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int use_apll)
{
Expand All @@ -47,6 +51,7 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
wclkPin = 25;
doutPin = 22;
SetGain(1.0);
dac_sample_count=0;
}

bool AudioOutputI2S::SetPinout()
Expand Down Expand Up @@ -148,6 +153,7 @@ bool AudioOutputI2S::SetOutputModeMono(bool mono)

bool AudioOutputI2S::begin(bool txDAC)
{

#ifdef ESP32
if (!i2sOn)
{
Expand Down Expand Up @@ -198,14 +204,16 @@ bool AudioOutputI2S::begin(bool txDAC)
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM)
{
i2s_set_pin((i2s_port_t)portNo, NULL);
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
// i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
}
else
{
SetPinout();
}
i2s_zero_dma_buffer((i2s_port_t)portNo);
}
// i2s_zero_dma_buffer((i2s_port_t)portNo);
}
else dac_sample_count=0; // just reset counter for DAC enable!

#elif defined(ESP8266)
(void)dma_buf_count;
(void)use_apll;
Expand Down Expand Up @@ -259,6 +267,9 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2])
}
#ifdef ESP32
uint32_t s32;

if (++dac_sample_count==WAIT_SAMPLES_BEFORE_DAC_EN) i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);

if (output_mode == INTERNAL_DAC)
{
int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000;
Expand Down Expand Up @@ -302,10 +313,12 @@ bool AudioOutputI2S::stop()
return false;

#ifdef ESP32
i2s_zero_dma_buffer((i2s_port_t)portNo);
// i2s_zero_dma_buffer((i2s_port_t)portNo);
i2s_set_dac_mode(I2S_DAC_CHANNEL_DISABLE);
#elif defined(ARDUINO_ARCH_RP2040)
I2S.end();
#endif
i2sOn = false;
// i2sOn = false; // removed to avoid re-installation of I2S-driver
// when next file is played (not sure about side effects ...)
return true;
}
1 change: 1 addition & 0 deletions src/AudioOutputI2S.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AudioOutputI2S : public AudioOutput
bool mono;
bool i2sOn;
int dma_buf_count;
int dac_sample_count;
int use_apll;
// We can restore the old values and free up these pins when in NoDAC mode
uint32_t orig_bck;
Expand Down

0 comments on commit 1ce70dc

Please sign in to comment.