LCC Fusion Project 1.0
LCC Automation
Loading...
Searching...
No Matches
AudioDevice Class Reference

Class to handle audio synthesis and playback using the MAX98357A DAC. More...

#include <AudioDevice.h>

Public Member Functions

 AudioDevice (int i2sBckPin, int i2sLrclkPin, int i2sDataPin, int spiClkPin, int spiMisoPin, int spiMosiPin, int spiCsPin)
 Constructor for AudioDevice class. More...
 
bool begin ()
 Initialize the audio system and eSpeak-NG. More...
 
void setVolume (int volume)
 Set the volume for audio playback. More...
 
void setLanguage (uint8_t index)
 Set the language for text-to-speech conversion by index. More...
 
void playText (const char *text)
 Convert text to speech and send it to the DAC. More...
 
void playWav (String filename)
 Play a WAV file stored on the SD card. More...
 
void selfTestSdReader ()
 Perform a built‑in self‑test of the Micro‑SD card interface. More...
 
void sendTestTone (float freqHz=1000.0f, uint16_t durationMs=300, uint8_t amplitudePct=25)
 Play a short sine tone over I2S (self-test for speaker path). More...
 

Private Member Functions

bool _initEspeak ()
 Initialize the eSpeak speech synthesizer and associated I2S interface. More...
 
bool _initSpi ()
 Initialize the SPI bus and SD card. More...
 
void _setupI2S ()
 Configure and start the ESP32 I2S peripheral for audio output. More...
 

Static Private Member Functions

static int _synthCallback (short *wav, int numsamples, espeak_EVENT *events)
 Callback function for handling synthesized audio data. More...
 

Private Attributes

int i2sDataPin_
 pins used for I2S
 
int spiCsPin_
 pins used for SD Card Reader to read .wav files
 

Static Private Attributes

static int volume_ = 100
 Master output volume as a percentage (0–100). More...
 
static const char * language_ = AudioDevice::languages_[0]
 Pointer to the current eSpeak voice code (e.g., "en", "fr"). More...
 
static const char * languages_ [10]
 Table of supported eSpeak voice identifiers. More...
 

Detailed Description

Class to handle audio synthesis and playback using the MAX98357A DAC.

Definition at line 58 of file AudioDevice.h.

Constructor & Destructor Documentation

◆ AudioDevice()

AudioDevice::AudioDevice ( int  i2sBckPin,
int  i2sLrclkPin,
int  i2sDataPin,
int  spiClkPin,
int  spiMisoPin,
int  spiMosiPin,
int  spiCsPin 
)
inline

Constructor for AudioDevice class.

Parameters
i2sBckPinThe pin number for BCLK
i2sLrclkPinThe pin number for LRCLK
i2sDataPinThe pin number for DIN
spiClkPinThe pin number for SPI CLK
spiMisoPinThe pin number for MISO
spiMosiPinThe pin number for MSOI
spiCsPinThe pin number for CS

Definition at line 70 of file AudioDevice.h.

Member Function Documentation

◆ _initEspeak()

bool AudioDevice::_initEspeak ( )
inlineprivate

Initialize the eSpeak speech synthesizer and associated I2S interface.

Sets up the I2S peripheral, invokes espeak_Initialize() in retrieval mode, selects a default voice, and installs the synthesis callback. On failure, it reports an error via SerialIO and frees the I2S resources.

Returns
True if initialization succeeded, false otherwise.

Definition at line 322 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _initSpi()

bool AudioDevice::_initSpi ( )
inlineprivate

Initialize the SPI bus and SD card.

Starts the SPI peripheral using the pins configured in the constructor, then attempts to mount the SD filesystem. If the card initialization fails, an error is printed. The SPI bus is ended before returning.

Returns
True if the SD card was successfully initialized, false otherwise.

Definition at line 351 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _setupI2S()

void AudioDevice::_setupI2S ( )
inlineprivate

Configure and start the ESP32 I2S peripheral for audio output.

Sets the sample rate, bit depth, channel format, DMA buffer counts and lengths, and assigns the BCK, LRCLK and data pins. This must be called before any audio is transmitted via I2S. The configuration uses a mono (right channel only) stream at 22.05 kHz and 16‑bit samples.

Definition at line 375 of file AudioDevice.h.

Here is the caller graph for this function:

◆ _synthCallback()

static int AudioDevice::_synthCallback ( short *  wav,
int  numsamples,
espeak_EVENT *  events 
)
inlinestaticprivate

Callback function for handling synthesized audio data.

Parameters
wavPointer to the audio data buffer
numsamplesNumber of samples in the buffer
eventsPointer to eSpeak event data
Returns
0 to continue, 1 to end synthesis

Definition at line 408 of file AudioDevice.h.

Here is the caller graph for this function:

◆ begin()

bool AudioDevice::begin ( )
inline

Initialize the audio system and eSpeak-NG.

Returns
True if initialization is successful, otherwise false

Definition at line 77 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ playText()

void AudioDevice::playText ( const char *  text)
inline

Convert text to speech and send it to the DAC.

Parameters
textThe text to be converted to speech

Definition at line 112 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ playWav()

void AudioDevice::playWav ( String  filename)
inline

Play a WAV file stored on the SD card.

Opens a .wav file from the Micro‑SD card reader and streams its PCM audio data to the DAC via the I2S interface. The method expects the file to use the standard 44‑byte WAV header for PCM 16‑bit mono samples at 22.05 kHz. If the file cannot be opened or the file size is shorter than a valid header, an error is reported over the serial port and no audio is played.

The SPI bus is initialized for each invocation and closed on return. This method blocks while the audio is playing.

Parameters
filenameRelative path to the .wav file on the SD card (e.g., "/my_sounds/hello.wav").

Definition at line 141 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ selfTestSdReader()

void AudioDevice::selfTestSdReader ( )
inline

Perform a built‑in self‑test of the Micro‑SD card interface.

This method brings up the SPI bus on the configured pins, toggles the chip‑select line, and issues a few dummy transfers to verify basic connectivity. It then attempts to initialize the SD card. If a card is present, it reports the card type (e.g., SD, SDHC), approximate capacity in megabytes, and whether a filesystem is detected. The results are printed over the serial interface. If no card is inserted, a warning is emitted but the SPI bus is still validated.

Definition at line 196 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendTestTone()

void AudioDevice::sendTestTone ( float  freqHz = 1000.0f,
uint16_t  durationMs = 300,
uint8_t  amplitudePct = 25 
)
inline

Play a short sine tone over I2S (self-test for speaker path).

Parameters
freqHzTone frequency in Hz (e.g., 1000)
durationMsDuration in milliseconds (e.g., 300)
amplitudePct0..100, percent of full-scale (default 25%)

Definition at line 239 of file AudioDevice.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLanguage()

void AudioDevice::setLanguage ( uint8_t  index)
inline

Set the language for text-to-speech conversion by index.

Parameters
indexThe index of the language code (0 to 9)

Definition at line 101 of file AudioDevice.h.

Here is the caller graph for this function:

◆ setVolume()

void AudioDevice::setVolume ( int  volume)
inline

Set the volume for audio playback.

Parameters
volumeVolume level (0 to 100)

Definition at line 91 of file AudioDevice.h.

Here is the caller graph for this function:

The documentation for this class was generated from the following file: