Skip to content

Speaker

Class: Speaker_Class (from M5Unified)
Access: M5Cardputer.Speaker

The speaker provides tone generation, WAV playback, raw audio output, and volume control.


Lifecycle

bool begin();       // Initialize I2S and start background audio task
void end();         // Stop and release I2S
bool isRunning();   // true if background task is active
bool isEnabled();   // true if speaker pin is configured

Tone Generation

bool tone(float frequency, uint32_t duration = UINT32_MAX,
          int channel = -1, bool stop_current_sound = true);

Play a sine-wave tone.

Parameter Type Default Description
frequency float Frequency in Hz
duration uint32_t UINT32_MAX Duration in ms (default: infinite)
channel int -1 Virtual channel 0–7 (-1 = auto-select)
stop_current_sound bool true Stop current sound on the selected channel first

Returns true on success.

M5Cardputer.Speaker.tone(440, 500);    // A4 for 500ms
M5Cardputer.Speaker.tone(1000);        // 1kHz, plays until stopped
M5Cardputer.Speaker.tone(220, 300, 2); // Play on channel 2

Advanced tone

For custom waveform data, use the full signature:

bool tone(float frequency, uint32_t duration, int channel, bool stop_current_sound,
          const uint8_t* raw_data, size_t array_len, bool stereo = false);


Volume

void setVolume(uint8_t master_volume);   // Master volume 0–255
uint8_t getVolume();                     // Get current master volume

void setChannelVolume(uint8_t channel, uint8_t volume);   // Per-channel 0–255
uint8_t getChannelVolume(uint8_t channel);                // Get channel volume
void setAllChannelVolume(uint8_t volume);                 // Set all 8 channels
M5Cardputer.Speaker.setVolume(128);   // Set to 50%

Stop

void stop();                    // Stop all channels
void stop(uint8_t channel);     // Stop a specific channel (0–7)

WAV Playback

bool playWav(const uint8_t* wav_data, size_t data_len = ~0u,
             uint32_t repeat = 1, int channel = -1,
             bool stop_current_sound = false);

Play audio from a WAV buffer (header included). Set data_len to ~0u for auto-detection from header.

Parameter Type Default Description
wav_data const uint8_t* Pointer to WAV data buffer
data_len size_t ~0u Buffer length (auto-detect from header if omitted)
repeat uint32_t 1 Number of repeats
channel int -1 Virtual channel 0–7
stop_current_sound bool false Stop current sound first

Raw Audio

// Signed 8-bit
bool playRaw(const int8_t* data, size_t len, uint32_t sample_rate = 44100,
             bool stereo = false, uint32_t repeat = 1,
             int channel = -1, bool stop_current_sound = false);

// Unsigned 8-bit
bool playRaw(const uint8_t* data, size_t len, uint32_t sample_rate = 44100,
             bool stereo = false, uint32_t repeat = 1,
             int channel = -1, bool stop_current_sound = false);

// Signed 16-bit
bool playRaw(const int16_t* data, size_t len, uint32_t sample_rate = 44100,
             bool stereo = false, uint32_t repeat = 1,
             int channel = -1, bool stop_current_sound = false);

Query Playback State

bool isPlaying() const;                        // true if any channel is playing
size_t isPlaying(uint8_t channel) const;       // Per-channel: 0=idle, 1=playing (room in queue), 2=playing (queue full)
size_t getPlayingChannels() const;             // Count of currently active channels

Quick Example

// Startup beep
M5Cardputer.Speaker.setVolume(200);
M5Cardputer.Speaker.tone(800, 100);
delay(100);
M5Cardputer.Speaker.tone(1000, 100);

// Play a melody
int melody[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (int i = 0; i < 8; i++) {
    M5Cardputer.Speaker.tone(melody[i], 150);
    delay(150);
}
M5Cardputer.Speaker.stop();