Skip to content

Synchronization

Phil Schatzmann edited this page Feb 29, 2024 · 18 revisions

Synchronization is important so that the all audio is played at the same time. I am currently not sure what the best solution would be, so I am providing a a quite flexible approach where you could even provide your own custom implementation.

The following logic is currently in place:

Startup Synchronization

The server is indicating with what delay each chunk should be played. The issue is that the processing time depends on the selected codec and output device, so the best to deal with this is to let the user define this processing time delay in the sketch.

  • If the playback is lagging compared with other devices, decrease the value. Negative values are allowed.
  • If the playback is ahead, increase the value. The delay value is 0, the system is using the message_buffer_delay_ms (w/o any change) which is provided by the server. If you use the SnapProcessorRTOS, the processing time is also impacted by your selected buffer size.

Clock Synchronization

By comparing the time differences between the server and the client we can determine if both clocks have the same speed and adjust the playback speed if there are some small differences.

Available Synchronization Implementations

We define the processing time delay is ms and the resampling speed factor:

SnapTimeSyncFixed synch(delay_ms, 1.0);
void setup {
...
client.begin(synch);
...
}

Since begin will use a reference to synch, we can't use a local variable. So either use a global variable or a static one.

We define the processing time is ms and how often the value is updated. The resampling speed factor is dynamically calculated by the system:

SnapTimeSyncDynamic synch(delay_ms, 10); 

void setup(){
...
client.begin(synch);
...
}

The resampling speed is adjusted after every 10th update.

We define the processing time is ms and how often the value is updated. The resampling speed factor is dynamically calculated by the system comparing the difference since the start:

SnapTimeSyncDynamicSinceStart synch(delay_ms, 10); 

void begin(){
...
client.begin(synch);
...
}

The resampling speed is adjusted after every 10th update.