The library¶
lib/zspot is a Zephyr module with a plain C API. It can be used without
the player, for example in a headless speaker.
Enable CONFIG_ZSPOT=y with CONFIG_CPP=y and CONFIG_STD_CPP20=y;
app/prj.conf has a working network and TLS configuration.
Minimal example¶
#include <zspot/zspot.h>
#include <zspot/zspot_i2s_sink.h>
static size_t on_pcm(const uint8_t *pcm, size_t len, void *user)
{
return zspot_i2s_sink_write(pcm, len, user); /* 0 = try again later */
}
static void on_event(const struct zspot_event *ev, void *user)
{
if (ev->type == ZSPOT_EVENT_VOLUME) {
zspot_i2s_sink_set_volume(ev->volume);
}
}
struct zspot_config cfg = {
.device_name = "Living room",
.audio_format = ZSPOT_FORMAT_OGG_VORBIS_160,
};
zspot_init(&cfg);
zspot_i2s_sink_init(DEVICE_DT_GET(DT_ALIAS(zspot_i2s)),
DEVICE_DT_GET_OR_NULL(DT_ALIAS(zspot_codec)), 44100, 2, 16);
zspot_zeroconf_start(); /* advertise the device */
zspot_zeroconf_wait(-1); /* until the Spotify app hands over credentials */
zspot_connect(on_event, on_pcm, NULL);
Callbacks run on the library’s threads.
API overview¶
The reference is include/zspot/zspot.h and
include/zspot/zspot_i2s_sink.h.
Lifecycle and credentials¶
|
Initialises the library and generates the device identity |
|
Restore or persist Spotify’s reusable credentials, to skip the hand-over on the next boot |
|
Username and password login (rejected for most accounts nowadays) |
|
Query or forget the current credentials |
Zeroconf¶
|
Advertises the device with mDNS/DNS-SD and serves |
|
Blocks until the Spotify app delivered credentials |
|
Stops the endpoint |
Session and playback¶
|
Start or end the session; |
|
False while the library reconnects |
|
Local playback control, reported to the Spotify app |
|
Repeat off, the queue or the track; the Spotify app only knows on and off and shows both repeat modes as on |
|
Keep Spotify’s view of the playback in step with the output |
|
The play queue handed over by the controlling app |
Utilities¶
zspot_http_get() and zspot_http_request() give the application the
library’s HTTP(S) client, for cover art or the Spotify Web API.
Events¶
Event |
Meaning |
|---|---|
|
Playback paused or resumed ( |
|
Volume changed, 0 to 65535 ( |
|
Metadata of the audible track ( |
|
The PCM that follows belongs to a new track |
|
Playback starts at, or jumps to, |
|
Drop buffered audio |
|
The queue finished |
|
The controlling app replaced or edited the queue |
|
Another device took over playback |
|
The connection to Spotify dropped or is back; the library reconnects on its own |
|
The repeat mode changed, from the app or |
PCM callback¶
The PCM callback receives 16-bit signed little-endian, interleaved stereo at 44.1 kHz and returns the number of bytes it accepted. Returning 0 makes the player retry the same data shortly; that is how back-pressure and pausing work.
Configuration¶
Kconfig |
Purpose |
|---|---|
|
Name shown in the Spotify app (default |
|
Base priority of the protocol threads |
|
Stack sizes of the protocol threads |
|
Large buffers and thread stacks in external memory (PSRAM) |
|
Mbed TLS heap placed in external memory |
|
CA credential tag for server verification (-1: no verification) |
|
HTTP client settings |
|
Discovery and the credential hand-over endpoint |
|
Ready-made PCM sink using the I2S driver |
The remaining options are described in lib/zspot/Kconfig.