@@ -19,7 +19,7 @@ ESPNowStream *ESPNowStreamSelf = nullptr;
19
19
*/
20
20
21
21
class ESPNowStream : public AudioStreamX {
22
- public:
22
+ public:
23
23
ESPNowStream () { ESPNowStreamSelf = this ; };
24
24
25
25
// / Adds a peer to which we can send info or from which we can receive info
@@ -32,8 +32,7 @@ class ESPNowStream : public AudioStreamX {
32
32
}
33
33
34
34
// / Adds an array of
35
- template <size_t size>
36
- bool addPeers (const char *(&array)[size]) {
35
+ template <size_t size> bool addPeers (const char *(&array)[size]) {
37
36
bool result = true ;
38
37
for (int j = 0 ; j < size; j++) {
39
38
if (!addPeer (array[j])) {
@@ -133,7 +132,7 @@ class ESPNowStream : public AudioStreamX {
133
132
// / Returns the mac address of the current ESP32
134
133
const char *address () { return WiFi.macAddress ().c_str (); }
135
134
136
- protected:
135
+ protected:
137
136
RingBuffer<uint8_t > buffer{1024 * 5 };
138
137
esp_now_recv_cb_t receive = default_recv_cb;
139
138
esp_now_send_cb_t send = default_send_cb;
@@ -168,7 +167,7 @@ class ESPNowStream : public AudioStreamX {
168
167
*/
169
168
170
169
class UDPStream : public WiFiUDP {
171
- public:
170
+ public:
172
171
/* *
173
172
* Provides the available size of the current package and if this is used up
174
173
* of the next package
@@ -213,9 +212,53 @@ class UDPStream : public WiFiUDP {
213
212
return result;
214
213
}
215
214
216
- protected:
215
+ protected:
217
216
uint16_t remote_port_ext;
218
217
IPAddress remote_address_ext;
219
218
};
220
219
221
- } // namespace audio_tools
220
+
221
+ struct ThrottleConfig : public AudioBaseInfo {
222
+ ThrottleConfig (){
223
+ sample_rate = 44100 ;
224
+ bits_per_sample = 16 ;
225
+ channels = 2 ;
226
+ }
227
+ int correction_ms = 0 ;
228
+ }
229
+
230
+ /* *
231
+ * @brief Throttle Sending to follow the the indicated sample rate
232
+ *
233
+ */
234
+ class Throttle {
235
+ public:
236
+ Throttle () = default ;
237
+
238
+ void begin (ThrottleConfig info) {
239
+ this ->info = info;
240
+ bytesPerSample = info.bits_per_sample / 8 * info.channels ;
241
+ }
242
+
243
+ // starts the timing
244
+ void start () { start_time = millis (); }
245
+
246
+ // delay
247
+ void throttle (size_t bytes) { throttleSamples (bytes / bytesPerSample); }
248
+
249
+ // delay
250
+ void throttleSamples (size_t samples) {
251
+ int durationMsEff = millis () - start_time;
252
+ int durationToBe = (samples * 1000 ) / info.sample_rate ;
253
+ int waitMs = durationToBe - durationMsEff + correction_ms;
254
+ if (waitMs > 0 ) {
255
+ delay (waitMs);
256
+ }
257
+ }
258
+
259
+ protected:
260
+ unsigned long start_time;
261
+ AudioBaseInfo info;
262
+ int bytesPerSample;
263
+
264
+ } // namespace audio_tools
0 commit comments