pub fn update_estimated_rate(&mut self, level: i32, now: Duration) -> bool {
        let start = match self.window_start {
            None => {
                self.window_start = Some(now);
                return false;
            }
            Some(t) => t,
        };

        let delta = match now.checked_sub(start) {
            Some(d) => d,
            None => return false,
        };
        self.window_frames += (self.last_level - level + self.level_diff).abs() as u32;
        self.level_diff = 0;
        self.last_level = level;

        let secs = (delta.as_secs() as f64) + delta.subsec_nanos() as f64 / 1_000_000_000.0;
        self.lsq.add_sample(secs, self.window_frames as f64);
        if delta > self.window_size && self.lsq.num_samples > 1 {
            let rate = self.lsq.best_fit_slope();
            if (self.estimated_rate - rate).abs() < MAX_RATE_SKEW {
                self.estimated_rate =
                    rate * (1.0 - self.smooth_factor) + self.estimated_rate * self.smooth_factor;
            }
            self.lsq = LeastSquares::new();
            self.window_start = Some(now);
            self.window_frames = 0;
            return true;
        }
        false
    }
/// Check the timestamp and buffer level difference since last check time,
/// and use them as a new sample to update the estimated rate.
///
/// # Arguments
///    * `level` - The current buffer level of audio device.
///    * `now` - The time at which this function is called.
///
/// # Returns
///    True if the estimated rate is updated and window is reset,
///    otherwise false.
Check the timestamp and buffer level difference since last check time,
and use them as a new sample to update the estimated rate.
Check the timestamp and buffer level difference since last check time and use them as a new sample to update the estimated rate .
pub fn update_estimated_rate(&mut self, level: i32, now: Duration) -> bool {
        let start = match self.window_start {
            None => {
                self.window_start = Some(now);
                return false;
            }
            Some(t) => t,
        };
        let delta = match now.checked_sub(start) {
            Some(d) => d,
            None => return false,
        };
        self.window_frames += (self.last_level - level + self.level_diff).abs() as u32;
        self.level_diff = 0;
        self.last_level = level;
        let secs = (delta.as_secs() as f64) + delta.subsec_nanos() as f64 / 1_000_000_000.0;
        self.lsq.add_sample(secs, self.window_frames as f64);
        if delta > self.window_size && self.lsq.num_samples > 1 {
            let rate = self.lsq.best_fit_slope();
            if (self.estimated_rate - rate).abs() < MAX_RATE_SKEW {
                self.estimated_rate =
                    rate * (1.0 - self.smooth_factor) + self.estimated_rate * self.smooth_factor;
            }
            self.lsq = LeastSquares::new();
            self.window_start = Some(now);
            self.window_frames = 0;
            return true;
        }
        false
    }
pub fn update_estimated_rate ( & mut self , level : i32 , now : Duration ) -> bool { let start = match self . window_start { None => { self . window_start = Some ( now ) ; return false ; } Some ( t ) => t , } ; let delta = match now . checked_sub ( start ) { Some ( d ) => d , None => return false , } ; self . window_frames += ( self . last_level - level + self . level_diff ) . abs ( ) as u32 ; self . level_diff = 0 ; self . last_level = level ; let secs = ( delta . as_secs ( ) as f64 ) + delta . subsec_nanos ( ) as f64 / 1_000_000_000.0 ; self . lsq . add_sample ( secs , self . window_frames as f64 ) ; if delta > self . window_size && self . lsq . num_samples > 1 { let rate = self . lsq . best_fit_slope ( ) ; if ( self . estimated_rate - rate ) . abs ( ) < MAX_RATE_SKEW { self . estimated_rate = rate * ( 1.0 - self . smooth_factor ) + self . estimated_rate * self . smooth_factor ; } self . lsq = LeastSquares :: new ( ) ; self . window_start = Some ( now ) ; self . window_frames = 0 ; return true ; } false }
Check the timestamp and buffer level difference since last check time,
and use them as a new sample to update the estimated rate.
Check the timestamp and buffer level difference since last check time and use them as a new sample to update the estimated rate .
&mut self
level
i32
now
Duration
True if the estimated rate is updated and window is reset,
otherwise false.
True if the estimated rate is updated and window is reset otherwise false .
&mut self
level
i32
now
Duration