Skip to content

Communication Snapcast

pschatzmann edited this page Oct 22, 2026 · 1 revision

Snapcast is a multiroom audio system: a server (e.g. on a Raspberry Pi or PC) receives audio from any source - Spotify, MPD, an internet radio - and distributes it to many clients, which play it in sync. With arduino-snapclient (install it) an ESP32 becomes a Snapcast client: a cheap synchronized speaker for every room. See Communication for the general considerations.

Snapcast client

The SnapClient connects to the server over TCP, decodes the audio with an arduino-audio-tools decoder and writes it to an output; a time synchronization class aligns the playback with the server's clock. From snapclient-i2s.ino:

#include "AudioTools.h"
#include "SnapClient.h"  // https://github.com/pschatzmann/arduino-snapclient
#include "AudioTools/AudioCodecs/CodecOpus.h"  // https://github.com/pschatzmann/arduino-libopus

#define ARDUINO_LOOP_STACK_SIZE (10 * 1024)

OpusAudioDecoder opus;
I2SStream out;
WiFiClient wifi;
SnapTimeSyncDynamic synch(172, 10);  // optional configuration
SnapClient client(wifi, out, opus);

void setup() {
  WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);  // defined in SnapConfig.h
  while (WiFi.status() != WL_CONNECTED) delay(1000);

  auto cfg = out.defaultConfig();
  cfg.pin_bck = 14;
  cfg.pin_ws = 15;
  cfg.pin_data = 22;
  out.begin(cfg);

  // client.setServerIP(IPAddress(192, 168, 1, 38));  // or CONFIG_SNAPCAST_SERVER_HOST in SnapConfig.h
  client.begin(synch);
}

void loop() { client.doLoop(); }

Things to consider

  • Codec: the decoder must match the codec configured on the Snapcast server (codec in snapserver.conf): Opus (set codec = opus) needs the least bandwidth and is a good choice for WiFi; PCM is supported as well, and FLAC with the optional arduino-libflac.
  • Sample rate: configure the server's stream with the sample rate you want to play (e.g. 48000) - the example was tested with ffmpeg -i http://stream.srg-ssr.ch/m/rsj/mp3_128 -f s16le -ar 48000 /tmp/snapfifo as source.
  • Synchronization: the time sync classes and the buffer settings determine how exactly the rooms are aligned - see the arduino-snapclient documentation and its examples.

Clone this wiki locally