neser 0.1.1

NESER - NES Emulator in Rust - is a NES emulator written in Rust. It aims to be a high-quality, hardware-accurate emulator that is also easy to use and extend. It supports a wide range of NES games and features, including various mappers, audio processing, and input handling. NESER is designed to be modular and extensible, allowing developers to easily add new features or support for additional hardware. It can be run using one of two frontends: a native desktop application using SDL2, or a web application using WebAssembly. The desktop application provides a high-performance, feature-rich experience with support for various input devices and display options, while the web application allows users to play NES games directly in their browsers without needing to install any software in a BYOR manner (Bring Your Own Roms).
Documentation
pub(crate) struct SdlAudioResampler {
    phase: f32,
    rate: f32,
    target_fill: usize,
    current: f32,
    next: f32,
    has_current: bool,
    has_next: bool,
}

impl SdlAudioResampler {
    pub(crate) const MAX_RATE_ADJUST: f32 = 0.005;

    pub(crate) fn new(target_fill: usize) -> Self {
        Self {
            phase: 0.0,
            rate: 1.0,
            target_fill,
            current: 0.0,
            next: 0.0,
            has_current: false,
            has_next: false,
        }
    }

    pub(crate) fn update_rate(&mut self, fill_level: usize) {
        let target = self.target_fill.max(1) as f32;
        let delta = fill_level as f32 - target;
        let normalized = (delta / target).clamp(-1.0, 1.0);
        self.rate = 1.0 + normalized * Self::MAX_RATE_ADJUST;
    }

    #[cfg(test)]
    pub(crate) fn rate(&self) -> f32 {
        self.rate
    }

    #[cfg(test)]
    pub(crate) fn set_rate_for_test(&mut self, rate: f32) {
        self.rate = rate;
    }

    pub(crate) fn render_next<F>(&mut self, pop_sample: &mut F) -> Option<f32>
    where
        F: FnMut() -> Option<f32>,
    {
        if !self.has_current {
            self.current = pop_sample()?;
            self.has_current = true;
        }
        if !self.has_next {
            if let Some(next) = pop_sample() {
                self.next = next;
            } else {
                self.next = self.current;
            }
            self.has_next = true;
        }

        let sample = self.current + (self.next - self.current) * self.phase;
        self.phase += self.rate;

        while self.phase >= 1.0 {
            self.phase -= 1.0;
            self.current = self.next;
            if let Some(next) = pop_sample() {
                self.next = next;
            } else {
                self.next = self.current;
            }
            self.has_next = true;
        }

        Some(sample)
    }
}