Kode Dot
Speaker
How the Kode Dot sounds, and how your app plays a sound through its speaker.
What it is
Inside the case there is a small speaker. Your app can play a
tone through it with kode_audio_tone, a WAV file with kode_audio_play or
its own samples with kode_audio_play_samples.
All the Kode Dot's sound, out through the speaker and in through the microphone, goes through an audio codec, the ES8311. The codec turns the samples your app sends into sound and digitises what the microphone picks up. It works in mono, at 48 kHz and 16 bits.

How it is connected
KodeOS runs the codec and the amplifier, so your app opens no bus and calls the SDK instead. The P4 gives the codec its settings, such as the volume, over the shared I2C bus and sends it the sound over I2S. Of the four I2S pins, GPIO38 carries the sound and the other three are the clock, which the speaker and the microphone share. The codec passes the sound to the amplifier, an NS4150B, and the amplifier drives the speaker.

The amplifier is switched on by the C5, the radio chip. That is why the C5 has
to be running for anything to be heard, even if your app uses neither Wi-Fi nor
Bluetooth. If your app has the radio off, like the template with
CONFIG_KODEOS_RADIO=n in sdkconfig.defaults, KodeOS resets the C5 at
start-up and leaves it running for the amplifier. The full circuit is in the
main board schematic.
Specifications
kode_audio_set and "volume". "mute" at 1 silences it and at 0 brings the sound back. The setting lasts until the next reset. 100 is the loudest the speaker takes without overheating, and it sits below the codec's full scale.kode_audio_tone(hz, ms) lasts up to 65535 ms. What you hear is the nearest of eight frequencies, from 1020 to 1957 Hz.kode_audio_play reads a WAV from sd/... or from flash/....kode_audio_stream_open opens the speaker for sound your app makes a piece at a time, kode_audio_stream_write sends each piece and kode_audio_stream_close closes it. It closes by itself after 10 s without a write.The calls are kode_audio_tone, kode_audio_play_samples and
kode_audio_play, in kode.h, and they return once the sound has ended. For
background playback and everything else, see
the SDK API.
Gotchas
CONFIG_KODEOS_RADIO=n, the amplifier takes about 1.5 s from the start of the app to come on, and whatever plays before that is lost. Wait a couple of seconds before the first sound.kode_audio_tone uses the nearest of its eight frequencies. For an exact pitch, generate the samples and play them with kode_audio_play_samples, as the example does.kode_error() says what does not fit. Convert the file first.Apps that use it
RadioInternet radio in a vintage tube radio. Tune stations from the open Radio Browser directory by country or genre, keep your favorites and play them through the speaker over Wi-Fi.
Kody WahA pocket otamatone. Slide your finger along the neck and the Dot sings *wah*: the higher you go, the higher the note. - Tilt the Dot back to open the mouth and bend the sound - TOP switches between Free (every pitch) and Major (stays in tune) - No module needed, just the speaker
ArcadeSix classic games in your pocket, all offline: Tetris, 2048, Mines, Gems, Flappy and the 15 puzzle. - Swipe or use the pad to pick a game, Play to start - Your best score for each game is saved on the Dot - Retro sound effects, and haptic taps on Dots with the vibration motor - Settings page: volume, sound on/off and vibration on/off BOT always takes you back to the menu.Example code
Run the program and you hear three notes, C, E and G, again every second until you leave the app. Because the program builds the sound sample by sample, each note sounds at its exact pitch.
/* SPDX-License-Identifier: GPL-3.0-or-later */
/**
* @file speaker_chime.c
* @brief Plays three notes, C, E and G, through the speaker of the Kode Dot, over and over.
*
* It builds the chime sample by sample, so each note sounds at its exact
* pitch, and plays it once a second until you leave the app.
*
* from Kode with love
*/
#include <math.h>
#include "kode.h"
#define CHIME_RATE_HZ 48000U /**< Samples per second. */
#define CHIME_NOTE_N 7200U /**< Samples in one note: 150 ms at 48 kHz. */
#define CHIME_NOTES 3U /**< Notes in the chime. */
#define CHIME_PEAK 16000.0f /**< Height of the wave, out of 32767. */
#define CHIME_PI 3.14159265f /**< Pi, for the sine. */
#define CHIME_VOLUME 80U /**< Speaker volume, from 0 to 100. */
#define CHIME_PAUSE_MS 1000U /**< Silence between two chimes. */
/** @brief The chime, one sample per frame. */
static int16_t s_samples[CHIME_NOTES * CHIME_NOTE_N];
/** @brief Start the system, build the chime and play it forever. */
void app_main(void)
{
const float pitch_hz[CHIME_NOTES] = { 1046.5f, 1318.5f, 1568.0f }; /* C6, E6 and G6 */
kode_init();
kode_print("Running: listen to the chime");
for ( size_t i = 0U; (CHIME_NOTES * CHIME_NOTE_N) > i; ++i )
{
const float t = (float)(i % CHIME_NOTE_N) / (float)CHIME_RATE_HZ;
const float fade = 1.0f - ((float)(i % CHIME_NOTE_N) / (float)CHIME_NOTE_N); /* each note dies away */
s_samples[i] = (int16_t)(CHIME_PEAK * fade * sinf(2.0f * CHIME_PI * pitch_hz[i / CHIME_NOTE_N] * t));
}
(void)kode_audio_set("volume", CHIME_VOLUME);
for ( ;; )
{
if ( !kode_audio_play_samples(s_samples, CHIME_NOTES * CHIME_NOTE_N) )
{
kode_print("no sound: %s", kode_error());
}
kode_delay(CHIME_PAUSE_MS);
}
}If you would rather build it yourself, follow the steps shared by every example.
If you hear nothing, see why the first sound can be lost.
In the SDK
kdrv/audio: the ES8311 driver, withktonefor tones andkwavfor files, inkode-sdk, described in the ES8311 driver.examples/audio: an SDK example for sound out and in.