Skip to content

Commit f4e9a14

Browse files
committed
added SnapTimeSyncDynamicSinceStart
1 parent 1fd8058 commit f4e9a14

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

src/api/SnapTimeSync.h

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@ class SnapTimeSync {
1919

2020
void begin(int rate) {
2121
update_count = 0;
22-
sample_rate = rate;
2322
}
2423

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;
3425

3526
/// Calculate the resampling factor: with a positive delay we play too fast
3627
/// and need to slow down
@@ -66,11 +57,10 @@ class SnapTimeSync {
6657

6758
protected:
6859
const char *TAG = "SnapTimeSync";
60+
// resampling speed
6961
uint64_t update_count = 0;
70-
float sample_rate = 48000;
7162
int interval = 10;
7263
bool active = false;
73-
Vector<SnapTimePoints> time_points;
7464
// start delay
7565
uint16_t processing_lag = 0;
7666
uint16_t message_buffer_delay_ms = 0;
@@ -79,7 +69,7 @@ class SnapTimeSync {
7969

8070
/**
8171
* @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
8373
* @author Phil Schatzmann
8474
* @version 0.1
8575
* @date 2023-10-28
@@ -91,6 +81,16 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
9181
int interval = 10)
9282
: SnapTimeSync(processingLag, interval) {}
9383

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+
9494
float getFactor() {
9595
int last_idx = time_points.size()-1;
9696
if (last_idx <=1) return 1.0;
@@ -102,8 +102,51 @@ class SnapTimeSyncDynamic : public SnapTimeSync {
102102
ESP_LOGI(TAG, "=> adjusting playback speed by factor: %f", result_factor);
103103
return result_factor;
104104
}
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;
105147
};
106148

149+
107150
/**
108151
* @brief Uses predefined fixed factor
109152
* @author Phil Schatzmann
@@ -119,6 +162,9 @@ class SnapTimeSyncFixed : public SnapTimeSync {
119162
: SnapTimeSync(processingLag, interval) {
120163
resample_factor = factor;
121164
}
165+
166+
void updateServerTime(uint32_t serverMillis) override {}
167+
122168
float getFactor() { return resample_factor; }
123169

124170
protected:

0 commit comments

Comments
 (0)