Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
freq_estimator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_audio/freq_estimator.h
10 //! @brief Frequency estimator.
11 
12 #ifndef ROC_AUDIO_FREQ_ESTIMATOR_H_
13 #define ROC_AUDIO_FREQ_ESTIMATOR_H_
14 
16 #include "roc_audio/sample.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_packet/units.h"
19 
20 namespace roc {
21 namespace audio {
22 
23 //! FreqEstimator paremeter preset.
25  //! Fast and responsive tuning.
26  //! Good for lower network latency and jitter.
28 
29  //! Slow and smooth tuning.
30  //! Good for higher network latency and jitter.
32 };
33 
34 //! FreqEstimator tunable parameters.
36  double P; //!< Proportional gain of PI-controller.
37  double I; //!< Integral gain of PI-controller.
38 
39  //! How much downsample input value (latency buffer size) on the first stage.
40  //! Must be less or equal to fe_decim_factor_max and must be greater than zero.
42 
43  //! How much downsample input value on the second stage. Must be less or equal
44  //! to fe_decim_factor_max. Could be zero to disable the second decimation stage.
46 
48  : P(0)
49  , I(0)
51  , decimation_factor2(0) {
52  }
53 };
54 
55 //! Evaluates sender's frequency to receivers's frequency ratio.
56 //! @remarks
57 //! We provide FreqEstimator with traget latency and periodically update it with
58 //! the actual latency. In response, FreqEstimator computes frequency coefficient,
59 //! the ratio of sender to receiver frequency. This coefficient is then set as
60 //! the scaling factor of the resampler, which in result compensates the frequency
61 //! difference and moves the latency closer to its target value.
62 class FreqEstimator : public core::NonCopyable<> {
63 public:
64  //! Initialize.
65  //!
66  //! @b Parameters
67  //! - @p profile defines configuration preset.
68  //! - @p target_latency defines latency we want to archive.
70  packet::stream_timestamp_t target_latency);
71 
72  //! Get current frequecy coefficient.
73  float freq_coeff() const;
74 
75  //! Compute new value of frequency coefficient.
76  void update(packet::stream_timestamp_t current_latency);
77 
78 private:
79  bool run_decimators_(packet::stream_timestamp_t current, double& filtered);
80  double run_controller_(double current);
81 
82  const FreqEstimatorConfig config_;
83  const double target_; // Target latency.
84 
85  double dec1_casc_buff_[fe_decim_len];
86  size_t dec1_ind_;
87 
88  double dec2_casc_buff_[fe_decim_len];
89  size_t dec2_ind_;
90 
91  size_t samples_counter_; // Input samples counter.
92  double accum_; // Integrator value.
93 
94  double coeff_; // Current frequency coefficient value.
95 };
96 
97 } // namespace audio
98 } // namespace roc
99 
100 #endif // ROC_AUDIO_FREQ_ESTIMATOR_H_
Evaluates sender's frequency to receivers's frequency ratio.
void update(packet::stream_timestamp_t current_latency)
Compute new value of frequency coefficient.
FreqEstimator(FreqEstimatorProfile profile, packet::stream_timestamp_t target_latency)
Initialize.
float freq_coeff() const
Get current frequecy coefficient.
Base class for non-copyable objects.
Definition: noncopyable.h:23
Frequency estimator config.
FreqEstimatorProfile
FreqEstimator paremeter preset.
@ FreqEstimatorProfile_Gradual
Slow and smooth tuning. Good for higher network latency and jitter.
@ FreqEstimatorProfile_Responsive
Fast and responsive tuning. Good for lower network latency and jitter.
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
Root namespace.
Non-copyable object.
Audio sample.
FreqEstimator tunable parameters.
double I
Integral gain of PI-controller.
size_t decimation_factor1
How much downsample input value (latency buffer size) on the first stage. Must be less or equal to fe...
size_t decimation_factor2
How much downsample input value on the second stage. Must be less or equal to fe_decim_factor_max....
double P
Proportional gain of PI-controller.
Various units used in packets.