Class to handle audio synthesis and playback using the MAX98357A DAC.
More...
#include <AudioDevice.h>
|
| | 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...
|
| |
|
| 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 int | _synthCallback (short *wav, int numsamples, espeak_EVENT *events) |
| | Callback function for handling synthesized audio data. More...
|
| |
|
|
int | i2sDataPin_ |
| | pins used for I2S
|
| |
|
int | spiCsPin_ |
| | pins used for SD Card Reader to read .wav files
|
| |
Class to handle audio synthesis and playback using the MAX98357A DAC.
Definition at line 58 of file AudioDevice.h.
◆ AudioDevice()
| AudioDevice::AudioDevice |
( |
int |
i2sBckPin, |
|
|
int |
i2sLrclkPin, |
|
|
int |
i2sDataPin, |
|
|
int |
spiClkPin, |
|
|
int |
spiMisoPin, |
|
|
int |
spiMosiPin, |
|
|
int |
spiCsPin |
|
) |
| |
|
inline |
Constructor for AudioDevice class.
- Parameters
-
| i2sBckPin | The pin number for BCLK |
| i2sLrclkPin | The pin number for LRCLK |
| i2sDataPin | The pin number for DIN |
| spiClkPin | The pin number for SPI CLK |
| spiMisoPin | The pin number for MISO |
| spiMosiPin | The pin number for MSOI |
| spiCsPin | The pin number for CS |
Definition at line 70 of file AudioDevice.h.
◆ _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.
◆ _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.
◆ _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.
◆ _synthCallback()
| static int AudioDevice::_synthCallback |
( |
short * |
wav, |
|
|
int |
numsamples, |
|
|
espeak_EVENT * |
events |
|
) |
| |
|
inlinestaticprivate |
Callback function for handling synthesized audio data.
- Parameters
-
| wav | Pointer to the audio data buffer |
| numsamples | Number of samples in the buffer |
| events | Pointer to eSpeak event data |
- Returns
- 0 to continue, 1 to end synthesis
Definition at line 408 of file AudioDevice.h.
◆ 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.
◆ playText()
| void AudioDevice::playText |
( |
const char * |
text | ) |
|
|
inline |
Convert text to speech and send it to the DAC.
- Parameters
-
| text | The text to be converted to speech |
Definition at line 112 of file AudioDevice.h.
◆ 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
-
| filename | Relative path to the .wav file on the SD card (e.g., "/my_sounds/hello.wav"). |
Definition at line 141 of file AudioDevice.h.
◆ 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.
◆ 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
-
| freqHz | Tone frequency in Hz (e.g., 1000) |
| durationMs | Duration in milliseconds (e.g., 300) |
| amplitudePct | 0..100, percent of full-scale (default 25%) |
Definition at line 239 of file AudioDevice.h.
◆ setLanguage()
| void AudioDevice::setLanguage |
( |
uint8_t |
index | ) |
|
|
inline |
Set the language for text-to-speech conversion by index.
- Parameters
-
| index | The index of the language code (0 to 9) |
Definition at line 101 of file AudioDevice.h.
◆ setVolume()
| void AudioDevice::setVolume |
( |
int |
volume | ) |
|
|
inline |
Set the volume for audio playback.
- Parameters
-
| volume | Volume level (0 to 100) |
Definition at line 91 of file AudioDevice.h.
The documentation for this class was generated from the following file: