Kode Dot
Altavoz
Así suena el Kode Dot y así reproduce tu app un sonido por su altavoz.
Qué es
Dentro de la carcasa hay un altavoz pequeño. Tu app puede hacer
que suene un tono con kode_audio_tone, un archivo WAV con kode_audio_play o
sus propias muestras con kode_audio_play_samples.
Todo el sonido del Kode Dot, el que sale por el altavoz y el que entra por el micrófono, pasa por un códec de audio, el ES8311. El códec convierte en sonido las muestras que le manda tu app y digitaliza lo que capta el micrófono. Trabaja en mono, a 48 kHz y 16 bits.

Por dentro
KodeOS controla el códec y el amplificador, así que tu app no abre ningún bus y llama al SDK. El P4 le da al códec los ajustes, como el volumen, por el bus I2C compartido y le manda el sonido por I2S. De los cuatro pines del I2S, GPIO38 lleva el sonido y los otros tres son el reloj, que comparten el altavoz y el micrófono. El códec pasa el sonido al amplificador, un NS4150B, que es el que mueve el altavoz.

El amplificador lo enciende el C5, el chip de la radio. Por eso el C5 tiene que
estar en marcha para que se oiga algo, aunque tu app no use el Wi-Fi ni el
Bluetooth. Si tu app tiene la radio apagada, como la plantilla con
CONFIG_KODEOS_RADIO=n en sdkconfig.defaults, KodeOS reinicia el C5 al
arrancar y lo deja en marcha para el amplificador. El circuito completo está en
el esquema de la placa principal.
Especificaciones
kode_audio_set y "volume". Con "mute" a 1 se silencia y a 0 vuelve a sonar. El ajuste dura hasta el siguiente reinicio. El 100 es lo más fuerte que aguanta el altavoz sin calentarse y queda por debajo de la escala completa del códec.kode_audio_tone(hz, ms) dura hasta 65535 ms. Lo que se oye es la más cercana de ocho frecuencias, de 1020 a 1957 Hz.kode_audio_play lee un WAV de sd/... o de flash/....kode_audio_stream_open abre el altavoz para sonido que tu app genera por trozos, kode_audio_stream_write envía cada trozo y kode_audio_stream_close lo cierra. Se cierra solo tras 10 s sin recibir nada.Las llamadas son kode_audio_tone, kode_audio_play_samples y
kode_audio_play, en kode.h. Las tres vuelven cuando el sonido ha acabado. Para que
suene de fondo y para las demás llamadas, mira
la API del SDK.
Errores comunes
CONFIG_KODEOS_RADIO=n, el amplificador tarda alrededor de 1,5 s en encenderse desde que arranca la app, así que lo que suena antes se pierde. Espera un par de segundos antes del primer sonido.kode_audio_tone usa la más cercana de sus ocho frecuencias. Para una altura exacta, genera las muestras y reprodúcelas con kode_audio_play_samples, como hace el ejemplo.kode_error(). Convierte antes el archivo.Apps que lo usan
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.Código de ejemplo
Ejecuta el programa y oirás tres notas, do, mi y sol, que se repiten cada segundo hasta que sales de la app. Como el programa genera el sonido muestra a muestra, cada nota suena exactamente a su frecuencia.
/* 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);
}
}Si prefieres compilarlo tú, sigue los pasos comunes a todos los ejemplos.
Si no oyes nada, mira por qué puede perderse el primer sonido.
En el SDK
kdrv/audio: el driver del ES8311, conktonepara los tonos ykwavpara los archivos, enkode-sdk, descrito en el driver del ES8311.examples/audio: un ejemplo del SDK para el sonido de salida y de entrada.