neser 0.1.0

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::debugging::log_info;
/// Audio output module for the NES APU
///
/// This module handles SDL2 audio initialization and manages the audio callback
/// that retrieves samples from the APU.
use crate::sdl_frontend::sdl_audio_callback::SdlAudioCallbackImpl;
use crate::sdl_frontend::sdl_audio_resampler::SdlAudioResampler;
use ringbuf::HeapRb;
use ringbuf::traits::{Producer, Split};
use sdl2::audio::{AudioDevice, AudioSpecDesired};
use std::sync::{
    Arc,
    atomic::AtomicU64,
    atomic::{AtomicU32, AtomicUsize, Ordering},
};

pub(crate) type AudioProducer = <HeapRb<f32> as Split>::Prod;
pub(crate) type AudioConsumer = <HeapRb<f32> as Split>::Cons;

/// Audio output handler that receives samples from the NES APU
pub struct SdlNesAudio {
    device: AudioDevice<SdlAudioCallbackImpl>,
    sample_producer: AudioProducer,
    volume: Arc<AtomicU32>,
    stats: Arc<AudioStats>,
    fill_level: Arc<AtomicUsize>,
    actual_sample_rate: i32,
}

#[derive(Default)]
pub(crate) struct AudioStats {
    pub(crate) received_samples: AtomicU64,
    pub(crate) dropped_samples: AtomicU64,
    pub(crate) underrun_samples: AtomicU64,
}

impl SdlNesAudio {
    /// Audio buffer size in samples
    /// At 44.1kHz, this provides ~0.5 seconds of buffering (22050 samples / 44100 Hz)
    const BUFFER_SIZE: usize = 22050;

    /// Create a new audio output handler
    ///
    /// Initializes SDL2 audio subsystem with the specified sample rate.
    /// Creates a bounded channel for sending audio samples from the emulator to the audio callback.
    ///
    /// # Arguments
    /// * `sdl_context` - The SDL2 context for audio initialization
    /// * `sample_rate` - Target sample rate in Hz (e.g., 44100, 48000)
    ///
    /// # Errors
    /// Returns an error if SDL2 audio initialization fails
    pub fn new(sdl_context: &sdl2::Sdl, sample_rate: i32) -> Result<Self, String> {
        let audio_subsystem = sdl_context.audio()?;

        let desired_spec = AudioSpecDesired {
            freq: Some(sample_rate),
            channels: Some(1),   // Mono audio
            samples: Some(1024), // Larger buffer for debug mode (less CPU pressure)
        };

        // Create bounded ring buffer for sending samples to audio callback.
        let ring_buffer = HeapRb::<f32>::new(Self::BUFFER_SIZE);
        let (producer, consumer) = ring_buffer.split();
        let fill_level = Arc::new(AtomicUsize::new(0));

        // Create shared volume control (default 75% to match tests and avoid distortion)
        let volume = Arc::new(AtomicU32::new(f32::to_bits(0.75)));
        let volume_clone = Arc::clone(&volume);

        let stats = Arc::new(AudioStats::default());
        let stats_clone = Arc::clone(&stats);
        let fill_level_clone = Arc::clone(&fill_level);

        let device =
            audio_subsystem.open_playback(None, &desired_spec, |_spec| SdlAudioCallbackImpl {
                sample_consumer: consumer,
                volume: volume_clone,
                stats: stats_clone,
                fill_level: fill_level_clone,
                resampler: SdlAudioResampler::new(Self::BUFFER_SIZE / 2),
            })?;

        let actual_rate = device.spec().freq;
        if actual_rate != sample_rate {
            log_info(format!(
                "Audio: requested {} Hz, got {} Hz from SDL device",
                sample_rate, actual_rate
            ));
        }

        Ok(Self {
            device,
            sample_producer: producer,
            volume,
            stats,
            fill_level,
            actual_sample_rate: actual_rate,
        })
    }

    /// Returns the actual sample rate of the opened SDL audio device.
    pub fn actual_sample_rate(&self) -> i32 {
        self.actual_sample_rate
    }

    /// Send an audio sample to the audio output
    ///
    /// Sends a sample to the audio callback for playback.
    /// If the buffer is full, this will block until the audio callback consumes samples.
    ///
    /// # Arguments
    /// * `sample` - Audio sample in range 0.0 to 1.0
    pub fn queue_sample(&mut self, sample: f32) {
        queue_sample_to_producer(
            &mut self.sample_producer,
            sample,
            &self.stats,
            &self.fill_level,
        );
    }

    /// Returns and resets audio stats counters.
    ///
    /// Useful for debugging pops/clicks: underruns correspond to the audio callback
    /// outputting silence because it had no queued samples available.
    pub fn take_and_reset_stats(&self) -> (u64, u64, u64) {
        let received = self.stats.received_samples.swap(0, Ordering::Relaxed);
        let dropped = self.stats.dropped_samples.swap(0, Ordering::Relaxed);
        let underrun = self.stats.underrun_samples.swap(0, Ordering::Relaxed);
        (received, dropped, underrun)
    }

    /// Returns the current buffered sample count in the ring buffer.
    #[cfg(test)]
    pub fn buffered_samples(&self) -> usize {
        self.fill_level.load(Ordering::Relaxed)
    }

    /// Start audio playback
    pub fn resume(&self) {
        self.device.resume();
    }

    /// Pre-fills the audio buffer with silence to avoid startup underruns.
    pub fn prime_startup(&mut self, samples: usize) {
        for _ in 0..samples {
            queue_sample_to_producer(
                &mut self.sample_producer,
                0.0,
                &self.stats,
                &self.fill_level,
            );
        }
    }

    /// Pause audio playback
    #[cfg(test)]
    pub fn pause(&self) {
        self.device.pause();
    }

    /// Set audio volume
    ///
    /// # Arguments
    /// * `volume` - Volume level from 0.0 (mute) to 1.0 (full volume)
    pub fn set_volume(&self, volume: f32) {
        let clamped = volume.clamp(0.0, 1.0);
        self.volume.store(f32::to_bits(clamped), Ordering::Relaxed);
    }

    /// Get current audio volume
    ///
    /// # Returns
    /// Current volume level from 0.0 to 1.0
    pub fn get_volume(&self) -> f32 {
        f32::from_bits(self.volume.load(Ordering::Relaxed))
    }
}

fn queue_sample_to_producer(
    producer: &mut AudioProducer,
    sample: f32,
    _stats: &AudioStats,
    fill_level: &AtomicUsize,
) {
    // Blocking push provides backpressure instead of dropping samples.
    // Dropped samples create discontinuities that can manifest as audible clicks.
    let mut pending = sample;
    loop {
        match producer.try_push(pending) {
            Ok(()) => {
                fill_level.fetch_add(1, Ordering::Relaxed);
                return;
            }
            Err(sample) => {
                pending = sample;
                std::thread::yield_now();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sdl_frontend::sdl_audio_resampler::SdlAudioResampler;
    use ringbuf::traits::{Consumer, Split};
    use serial_test::serial;
    use std::collections::VecDeque;
    use std::env;
    use std::sync::{Arc, Barrier};
    use std::time::{Duration, Instant};

    #[test]
    #[serial]
    fn test_audio_functionality() {
        // CI often runs without an audio device; force SDL to use its dummy backend.
        // Restore the previous env value after the test to avoid cross-test pollution.
        struct EnvRestore {
            key: &'static str,
            prev: Option<String>,
        }

        impl Drop for EnvRestore {
            fn drop(&mut self) {
                match &self.prev {
                    Some(value) => unsafe {
                        // SAFETY: This test is marked `#[serial]`, and this env var is only used
                        // to configure SDL audio backend selection for this test.
                        env::set_var(self.key, value)
                    },
                    None => unsafe {
                        // SAFETY: See above.
                        env::remove_var(self.key)
                    },
                }
            }
        }

        let restore = EnvRestore {
            key: "SDL_AUDIODRIVER",
            prev: env::var("SDL_AUDIODRIVER").ok(),
        };
        unsafe {
            // SAFETY: This test is marked `#[serial]`, and SDL reads this env var during init.
            env::set_var("SDL_AUDIODRIVER", "dummy");
        }

        // Test audio creation, control, and sample queueing
        // Combine into one test to avoid SDL2 thread issues
        let sdl_context = sdl2::init().expect("Failed to initialize SDL2");

        let audio = SdlNesAudio::new(&sdl_context, 44100);
        assert!(audio.is_ok(), "Audio initialization should succeed");

        let mut audio = audio.unwrap();

        // Test volume control
        audio.set_volume(0.5);
        assert_eq!(audio.get_volume(), 0.5, "Volume should be 0.5");
        audio.set_volume(2.0); // Test clamping
        assert_eq!(audio.get_volume(), 1.0, "Volume should clamp to 1.0");
        audio.set_volume(-0.5); // Test clamping
        assert_eq!(audio.get_volume(), 0.0, "Volume should clamp to 0.0");

        // Test control methods - should not panic
        audio.resume();
        audio.pause();

        // Test queueing samples - should not panic
        audio.queue_sample(0.5);
        audio.queue_sample(0.3);
        audio.queue_sample(0.8);

        drop(restore);
    }

    #[test]
    #[serial]
    fn test_prime_startup_buffers_silence() {
        struct EnvRestore {
            key: &'static str,
            prev: Option<String>,
        }

        impl Drop for EnvRestore {
            fn drop(&mut self) {
                match &self.prev {
                    Some(value) => unsafe { env::set_var(self.key, value) },
                    None => unsafe { env::remove_var(self.key) },
                }
            }
        }

        let restore = EnvRestore {
            key: "SDL_AUDIODRIVER",
            prev: env::var("SDL_AUDIODRIVER").ok(),
        };
        unsafe {
            env::set_var("SDL_AUDIODRIVER", "dummy");
        }

        let sdl_context = sdl2::init().expect("Failed to initialize SDL2");
        let mut audio = SdlNesAudio::new(&sdl_context, 44100).expect("Audio init should succeed");

        assert_eq!(audio.buffered_samples(), 0);

        audio.prime_startup(2048);

        assert!(audio.buffered_samples() >= 2048);

        drop(restore);
    }

    #[test]
    fn test_queue_sample_does_not_drop_when_buffer_full() {
        // Desired behavior: when the bounded audio buffer is full, do NOT drop samples.
        // Dropping samples introduces discontinuities that can manifest as clicks.

        let stats = Arc::new(AudioStats::default());
        let ring_buffer = HeapRb::<f32>::new(1);
        let (mut producer, mut consumer) = ring_buffer.split();
        let fill_level = Arc::new(AtomicUsize::new(0));

        // Fill the buffer.
        queue_sample_to_producer(&mut producer, 0.1, &stats, &fill_level);

        let barrier = Arc::new(Barrier::new(2));
        let barrier_consumer = Arc::clone(&barrier);
        let (result_tx, result_rx) = std::sync::mpsc::channel::<(f32, f32)>();
        let (producer_ready_tx, producer_ready_rx) = std::sync::mpsc::channel::<()>();

        let fill_level_consumer = Arc::clone(&fill_level);
        let consumer = std::thread::spawn(move || {
            // Ensure producer attempts to enqueue while the queue is full.
            barrier_consumer.wait();

            let first = {
                let start = Instant::now();
                loop {
                    if let Some(value) = consumer.try_pop() {
                        fill_level_consumer.fetch_sub(1, Ordering::Relaxed);
                        break value;
                    }
                    if start.elapsed() > Duration::from_millis(200) {
                        panic!("expected first sample");
                    }
                    std::thread::yield_now();
                }
            };

            let second = {
                let start = Instant::now();
                loop {
                    if let Some(value) = consumer.try_pop() {
                        fill_level_consumer.fetch_sub(1, Ordering::Relaxed);
                        break value;
                    }
                    if start.elapsed() > Duration::from_millis(200) {
                        panic!("expected second sample (must not be dropped)");
                    }
                    std::thread::yield_now();
                }
            };

            result_tx
                .send((first, second))
                .expect("failed to send samples to main thread");
        });

        let stats_producer = Arc::clone(&stats);
        let fill_level_producer = Arc::clone(&fill_level);
        let producer = std::thread::spawn(move || {
            // Signal that we're about to attempt enqueue while the queue is full.
            producer_ready_tx
                .send(())
                .expect("failed to signal producer readiness");
            queue_sample_to_producer(&mut producer, 0.2, &stats_producer, &fill_level_producer);
        });

        // Ensure the producer has started the enqueue attempt before letting the consumer drain.
        producer_ready_rx
            .recv_timeout(Duration::from_millis(200))
            .expect("producer did not become ready");

        barrier.wait();

        producer.join().expect("producer thread panicked");
        consumer.join().expect("consumer thread panicked");

        let (first, second) = result_rx
            .recv_timeout(Duration::from_millis(200))
            .expect("expected samples from consumer");
        assert_eq!(first, 0.1);
        assert_eq!(second, 0.2);

        let dropped = stats.dropped_samples.load(Ordering::Relaxed);
        assert_eq!(dropped, 0, "no samples should be dropped");
    }

    #[test]
    fn test_resampler_rate_clamps_to_limits() {
        let mut resampler = SdlAudioResampler::new(100);

        resampler.update_rate(100);
        assert!((resampler.rate() - 1.0).abs() < 0.00001);

        resampler.update_rate(0);
        assert!((resampler.rate() - (1.0 - SdlAudioResampler::MAX_RATE_ADJUST)).abs() < 0.00001);

        resampler.update_rate(200);
        assert!((resampler.rate() - (1.0 + SdlAudioResampler::MAX_RATE_ADJUST)).abs() < 0.00001);
    }

    #[test]
    fn test_resampler_outputs_source_sequence_at_unity_rate() {
        let mut resampler = SdlAudioResampler::new(4);
        resampler.set_rate_for_test(1.0);

        let mut samples = VecDeque::from([0.0, 1.0, 0.0, 1.0]);
        let mut pop_sample = || samples.pop_front();

        let first = resampler
            .render_next(&mut pop_sample)
            .expect("first sample");
        let second = resampler
            .render_next(&mut pop_sample)
            .expect("second sample");
        let third = resampler
            .render_next(&mut pop_sample)
            .expect("third sample");

        assert!((first - 0.0).abs() < 0.00001);
        assert!((second - 1.0).abs() < 0.00001);
        assert!((third - 0.0).abs() < 0.00001);
    }
}