@@ -19,18 +19,9 @@ class SnapTimeSync {
19
19
20
20
void begin (int rate) {
21
21
update_count = 0 ;
22
- sample_rate = rate;
23
22
}
24
23
25
- void updateServerTime (uint32_t serverMillis) {
26
- update_count++;
27
- active = true ;
28
- SnapTimePoints tp{serverMillis};
29
- if (time_points.size ()>=interval){
30
- time_points.pop_front ();
31
- }
32
- time_points.push_back (tp);
33
- }
24
+ virtual void updateServerTime (uint32_t serverMillis) = 0;
34
25
35
26
// / Calculate the resampling factor: with a positive delay we play too fast
36
27
// / and need to slow down
@@ -66,11 +57,10 @@ class SnapTimeSync {
66
57
67
58
protected:
68
59
const char *TAG = " SnapTimeSync" ;
60
+ // resampling speed
69
61
uint64_t update_count = 0 ;
70
- float sample_rate = 48000 ;
71
62
int interval = 10 ;
72
63
bool active = false ;
73
- Vector<SnapTimePoints> time_points;
74
64
// start delay
75
65
uint16_t processing_lag = 0 ;
76
66
uint16_t message_buffer_delay_ms = 0 ;
@@ -79,7 +69,7 @@ class SnapTimeSync {
79
69
80
70
/* *
81
71
* @brief Dynamically adjusts the effective playback sample rate based on the differences
82
- * of the local and server clock.
72
+ * of the local and server clock between the different intervals
83
73
* @author Phil Schatzmann
84
74
* @version 0.1
85
75
* @date 2023-10-28
@@ -91,6 +81,16 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
91
81
int interval = 10 )
92
82
: SnapTimeSync(processingLag, interval) {}
93
83
84
+ void updateServerTime (uint32_t serverMillis) override {
85
+ update_count++;
86
+ active = true ;
87
+ SnapTimePoints tp{serverMillis};
88
+ if (time_points.size ()>=interval){
89
+ time_points.pop_front ();
90
+ }
91
+ time_points.push_back (tp);
92
+ }
93
+
94
94
float getFactor () {
95
95
int last_idx = time_points.size ()-1 ;
96
96
if (last_idx <=1 ) return 1.0 ;
@@ -102,8 +102,51 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
102
102
ESP_LOGI (TAG, " => adjusting playback speed by factor: %f" , result_factor);
103
103
return result_factor;
104
104
}
105
+ protected:
106
+ Vector<SnapTimePoints> time_points;
107
+ };
108
+
109
+ /* *
110
+ * @brief Dynamically adjusts the effective playback sample rate based on the differences
111
+ * of the local and server clock since the start
112
+ * @author Phil Schatzmann
113
+ * @version 0.1
114
+ * @date 2023-10-28
115
+ * @copyright Copyright (c) 2023
116
+ **/
117
+ class SnapTimeSyncDynamicSinceStart : public SnapTimeSync {
118
+ public:
119
+ SnapTimeSyncDynamicSinceStart (int processingLag = CONFIG_PROCESSING_TIME_MS,
120
+ int interval = 10 )
121
+ : SnapTimeSync(processingLag, interval) {}
122
+
123
+ void updateServerTime (uint32_t serverMillis) override {
124
+ if (update_count == 0 ){
125
+ start_time = SnapTimePoints (serverMillis)
126
+ }
127
+ current_time = SnapTimePoints (serverMillis)
128
+ update_count++;
129
+ active = true ;
130
+ }
131
+
132
+ float getFactor () {
133
+ int last_idx = time_points.size ()-1 ;
134
+ if (last_idx <=1 ) return 1.0 ;
135
+ float timespan_local_ms = current_time.local_ms - start_time.local_ms ;
136
+ float timespan_server_ms = current_time.server_ms - start_time.server_ms ;
137
+ if (timespan_local_ms == 0 ) return 1.0 ;
138
+ // if server time span is smaller then local, local runs faster and needs to be slowed down
139
+ float result_factor = timespan_server_ms / timespan_local_ms;
140
+ ESP_LOGI (TAG, " => adjusting playback speed by factor: %f" , result_factor);
141
+ return result_factor;
142
+ }
143
+ protected:
144
+ Vector<SnapTimePoints> time_points;
145
+ SnapTimePoints start_time;
146
+ SnapTimePoints current_time;
105
147
};
106
148
149
+
107
150
/* *
108
151
* @brief Uses predefined fixed factor
109
152
* @author Phil Schatzmann
@@ -119,6 +162,9 @@ class SnapTimeSyncFixed : public SnapTimeSync {
119
162
: SnapTimeSync(processingLag, interval) {
120
163
resample_factor = factor;
121
164
}
165
+
166
+ void updateServerTime (uint32_t serverMillis) override {}
167
+
122
168
float getFactor () { return resample_factor; }
123
169
124
170
protected:
0 commit comments