4
4
#include " SnapTime.h"
5
5
6
6
/* *
7
- * @brief Abstract (Common) Time Synchronization Logic
7
+ * @brief Abstract (Common) Time Synchronization Logic which consists of the
8
+ * startup synchronization and the local to server clock synchronization which
9
+ * adjusts the sampling rate.
8
10
* @author Phil Schatzmann
9
11
* @version 0.1
10
12
* @date 2023-10-28
@@ -17,16 +19,19 @@ class SnapTimeSync {
17
19
setProcessingLag (processingLag);
18
20
}
19
21
22
+ // / Starts the processing
20
23
void begin (int rate) {
21
24
update_count = 0 ;
22
25
}
23
26
27
+ // / Records the actual server time in millisecondes
24
28
virtual void updateServerTime (uint32_t serverMillis) = 0;
25
29
26
30
// / Calculate the resampling factor: with a positive delay we play too fast
27
31
// / and need to slow down
28
32
virtual float getFactor () = 0;
29
33
34
+ // / Returns true if a synchronization (update of the sampling rate) is needed.
30
35
bool isSync () {
31
36
bool result = active && update_count > 2 && update_count % interval == 0 ;
32
37
active = false ;
@@ -96,7 +101,10 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
96
101
if (last_idx <=1 ) return 1.0 ;
97
102
float timespan_local_ms = time_points[last_idx].local_ms - time_points[0 ].local_ms ;
98
103
float timespan_server_ms = time_points[last_idx].server_ms - time_points[0 ].server_ms ;
99
- if (timespan_local_ms == 0 ) return 1.0 ;
104
+ if (timespan_local_ms == 0.0 || timespan_server_ms == 0.0 ) {
105
+ ESP_LOGE (TAG, " Could not determine clock differences" );
106
+ return 1.0 ;
107
+ }
100
108
// if server time span is smaller then local, local runs faster and needs to be slowed down
101
109
float result_factor = timespan_server_ms / timespan_local_ms;
102
110
ESP_LOGI (TAG, " => adjusting playback speed by factor: %f" , result_factor);
@@ -130,18 +138,18 @@ class SnapTimeSyncDynamicSinceStart : public SnapTimeSync {
130
138
}
131
139
132
140
float getFactor () {
133
- int last_idx = time_points.size ()-1 ;
134
- if (last_idx <=1 ) return 1.0 ;
135
141
float timespan_local_ms = current_time.local_ms - start_time.local_ms ;
136
142
float timespan_server_ms = current_time.server_ms - start_time.server_ms ;
137
- if (timespan_local_ms == 0 ) return 1.0 ;
143
+ if (timespan_local_ms == 0.0 || timespan_server_ms == 0.0 ) {
144
+ ESP_LOGE (TAG, " Could not determine clock differences" );
145
+ return 1.0 ;
146
+ }
138
147
// if server time span is smaller then local, local runs faster and needs to be slowed down
139
148
float result_factor = timespan_server_ms / timespan_local_ms;
140
149
ESP_LOGI (TAG, " => adjusting playback speed by factor: %f" , result_factor);
141
150
return result_factor;
142
151
}
143
152
protected:
144
- Vector<SnapTimePoints> time_points;
145
153
SnapTimePoints start_time;
146
154
SnapTimePoints current_time;
147
155
};
0 commit comments