pub struct FrontendRuntime { /* private fields */ }Implementations§
Source§impl FrontendRuntime
impl FrontendRuntime
pub fn new(nes: NES) -> Self
Sourcepub fn from_rom_bytes(rom: &[u8]) -> Result<Self, CartridgeError>
pub fn from_rom_bytes(rom: &[u8]) -> Result<Self, CartridgeError>
Examples found in repository?
examples/analyze_audio.rs (line 10)
7fn main() {
8 let rom_path = env::args().nth(1).expect("Usage: analyze_audio <rom-path>");
9 let rom = fs::read(&rom_path).expect("Failed to read ROM");
10 let mut runtime = FrontendRuntime::from_rom_bytes(&rom).expect("Failed to load ROM");
11
12 let input = FrontendInput {
13 controller1: ControllerState::new(),
14 ..Default::default()
15 };
16
17 // 运行几秒收集样本
18 let mut all_samples = Vec::new();
19 for _ in 0..300 {
20 let snapshot = runtime.step(input);
21 all_samples.extend_from_slice(snapshot.audio.samples);
22 }
23
24 // 分析
25 let min = all_samples.iter().cloned().reduce(f32::min).unwrap();
26 let max = all_samples.iter().cloned().reduce(f32::max).unwrap();
27 let avg = all_samples.iter().sum::<f32>() / all_samples.len() as f32;
28
29 // 计算方差(噪声水平)
30 let variance =
31 all_samples.iter().map(|&x| (x - avg).powi(2)).sum::<f32>() / all_samples.len() as f32;
32 let std_dev = variance.sqrt();
33
34 // 统计接近静音的样本
35 let near_zero_count = all_samples.iter().filter(|&&x| x.abs() < 1e-6).count();
36 let near_zero_ratio = near_zero_count as f32 / all_samples.len() as f32;
37
38 println!("=== 音频样本分析 ===");
39 println!("样本总数: {}", all_samples.len());
40 println!("最小值: {:.6}", min);
41 println!("最大值: {:.6}", max);
42 println!("平均值: {:.6}", avg);
43 println!("标准差 (噪声水平): {:.6}", std_dev);
44 println!(
45 "接近静音的样本 (<1e-6): {} ({:.2}%)",
46 near_zero_count,
47 near_zero_ratio * 100.0
48 );
49
50 // 找出典型静音期间的噪声范围
51 let mut silent_samples: Vec<f32> = all_samples
52 .iter()
53 .filter(|&&x| x.abs() < 0.001)
54 .copied()
55 .collect();
56 silent_samples.sort_by(|a, b| a.abs().partial_cmp(&b.abs()).unwrap());
57 if silent_samples.len() > 100 {
58 let quiet_max = silent_samples[silent_samples.len() / 2 + 50]; // 取中位数附近的最大值
59 println!("静音期间典型噪声范围: ±{:.6}", quiet_max);
60 }
61
62 // 保存所有样本供可视化(默认最多保存 10 秒)
63 let sample_rate = runtime.snapshot().audio.sample_rate;
64 let max_samples = (sample_rate as usize) * 10; // 最多 10 秒
65 let wav_samples = &all_samples[..max_samples.min(all_samples.len())];
66 let wav_path = Path::new(&rom_path).with_extension("wav");
67 write_wav(&wav_path, wav_samples, sample_rate);
68 println!(
69 "已保存 {} 个样本 (约 {:.1} 秒) 到: {}",
70 wav_samples.len(),
71 wav_samples.len() as f32 / sample_rate as f32,
72 wav_path.display()
73 );
74}pub fn mode(&self) -> RunMode
pub fn nes(&self) -> &NES
pub fn nes_mut(&mut self) -> &mut NES
pub fn save_state(&self) -> Result<Vec<u8>, SaveStateError>
pub fn load_state(&mut self, bytes: &[u8]) -> Result<(), SaveStateError>
pub fn set_mode(&mut self, mode: RunMode)
Sourcepub fn snapshot(&self) -> RuntimeSnapshot<'_>
pub fn snapshot(&self) -> RuntimeSnapshot<'_>
Examples found in repository?
examples/analyze_audio.rs (line 63)
7fn main() {
8 let rom_path = env::args().nth(1).expect("Usage: analyze_audio <rom-path>");
9 let rom = fs::read(&rom_path).expect("Failed to read ROM");
10 let mut runtime = FrontendRuntime::from_rom_bytes(&rom).expect("Failed to load ROM");
11
12 let input = FrontendInput {
13 controller1: ControllerState::new(),
14 ..Default::default()
15 };
16
17 // 运行几秒收集样本
18 let mut all_samples = Vec::new();
19 for _ in 0..300 {
20 let snapshot = runtime.step(input);
21 all_samples.extend_from_slice(snapshot.audio.samples);
22 }
23
24 // 分析
25 let min = all_samples.iter().cloned().reduce(f32::min).unwrap();
26 let max = all_samples.iter().cloned().reduce(f32::max).unwrap();
27 let avg = all_samples.iter().sum::<f32>() / all_samples.len() as f32;
28
29 // 计算方差(噪声水平)
30 let variance =
31 all_samples.iter().map(|&x| (x - avg).powi(2)).sum::<f32>() / all_samples.len() as f32;
32 let std_dev = variance.sqrt();
33
34 // 统计接近静音的样本
35 let near_zero_count = all_samples.iter().filter(|&&x| x.abs() < 1e-6).count();
36 let near_zero_ratio = near_zero_count as f32 / all_samples.len() as f32;
37
38 println!("=== 音频样本分析 ===");
39 println!("样本总数: {}", all_samples.len());
40 println!("最小值: {:.6}", min);
41 println!("最大值: {:.6}", max);
42 println!("平均值: {:.6}", avg);
43 println!("标准差 (噪声水平): {:.6}", std_dev);
44 println!(
45 "接近静音的样本 (<1e-6): {} ({:.2}%)",
46 near_zero_count,
47 near_zero_ratio * 100.0
48 );
49
50 // 找出典型静音期间的噪声范围
51 let mut silent_samples: Vec<f32> = all_samples
52 .iter()
53 .filter(|&&x| x.abs() < 0.001)
54 .copied()
55 .collect();
56 silent_samples.sort_by(|a, b| a.abs().partial_cmp(&b.abs()).unwrap());
57 if silent_samples.len() > 100 {
58 let quiet_max = silent_samples[silent_samples.len() / 2 + 50]; // 取中位数附近的最大值
59 println!("静音期间典型噪声范围: ±{:.6}", quiet_max);
60 }
61
62 // 保存所有样本供可视化(默认最多保存 10 秒)
63 let sample_rate = runtime.snapshot().audio.sample_rate;
64 let max_samples = (sample_rate as usize) * 10; // 最多 10 秒
65 let wav_samples = &all_samples[..max_samples.min(all_samples.len())];
66 let wav_path = Path::new(&rom_path).with_extension("wav");
67 write_wav(&wav_path, wav_samples, sample_rate);
68 println!(
69 "已保存 {} 个样本 (约 {:.1} 秒) 到: {}",
70 wav_samples.len(),
71 wav_samples.len() as f32 / sample_rate as f32,
72 wav_path.display()
73 );
74}Sourcepub fn step(&mut self, input: FrontendInput) -> RuntimeSnapshot<'_>
pub fn step(&mut self, input: FrontendInput) -> RuntimeSnapshot<'_>
Examples found in repository?
examples/analyze_audio.rs (line 20)
7fn main() {
8 let rom_path = env::args().nth(1).expect("Usage: analyze_audio <rom-path>");
9 let rom = fs::read(&rom_path).expect("Failed to read ROM");
10 let mut runtime = FrontendRuntime::from_rom_bytes(&rom).expect("Failed to load ROM");
11
12 let input = FrontendInput {
13 controller1: ControllerState::new(),
14 ..Default::default()
15 };
16
17 // 运行几秒收集样本
18 let mut all_samples = Vec::new();
19 for _ in 0..300 {
20 let snapshot = runtime.step(input);
21 all_samples.extend_from_slice(snapshot.audio.samples);
22 }
23
24 // 分析
25 let min = all_samples.iter().cloned().reduce(f32::min).unwrap();
26 let max = all_samples.iter().cloned().reduce(f32::max).unwrap();
27 let avg = all_samples.iter().sum::<f32>() / all_samples.len() as f32;
28
29 // 计算方差(噪声水平)
30 let variance =
31 all_samples.iter().map(|&x| (x - avg).powi(2)).sum::<f32>() / all_samples.len() as f32;
32 let std_dev = variance.sqrt();
33
34 // 统计接近静音的样本
35 let near_zero_count = all_samples.iter().filter(|&&x| x.abs() < 1e-6).count();
36 let near_zero_ratio = near_zero_count as f32 / all_samples.len() as f32;
37
38 println!("=== 音频样本分析 ===");
39 println!("样本总数: {}", all_samples.len());
40 println!("最小值: {:.6}", min);
41 println!("最大值: {:.6}", max);
42 println!("平均值: {:.6}", avg);
43 println!("标准差 (噪声水平): {:.6}", std_dev);
44 println!(
45 "接近静音的样本 (<1e-6): {} ({:.2}%)",
46 near_zero_count,
47 near_zero_ratio * 100.0
48 );
49
50 // 找出典型静音期间的噪声范围
51 let mut silent_samples: Vec<f32> = all_samples
52 .iter()
53 .filter(|&&x| x.abs() < 0.001)
54 .copied()
55 .collect();
56 silent_samples.sort_by(|a, b| a.abs().partial_cmp(&b.abs()).unwrap());
57 if silent_samples.len() > 100 {
58 let quiet_max = silent_samples[silent_samples.len() / 2 + 50]; // 取中位数附近的最大值
59 println!("静音期间典型噪声范围: ±{:.6}", quiet_max);
60 }
61
62 // 保存所有样本供可视化(默认最多保存 10 秒)
63 let sample_rate = runtime.snapshot().audio.sample_rate;
64 let max_samples = (sample_rate as usize) * 10; // 最多 10 秒
65 let wav_samples = &all_samples[..max_samples.min(all_samples.len())];
66 let wav_path = Path::new(&rom_path).with_extension("wav");
67 write_wav(&wav_path, wav_samples, sample_rate);
68 println!(
69 "已保存 {} 个样本 (约 {:.1} 秒) 到: {}",
70 wav_samples.len(),
71 wav_samples.len() as f32 / sample_rate as f32,
72 wav_path.display()
73 );
74}Auto Trait Implementations§
impl !RefUnwindSafe for FrontendRuntime
impl !Send for FrontendRuntime
impl !Sync for FrontendRuntime
impl !UnwindSafe for FrontendRuntime
impl Freeze for FrontendRuntime
impl Unpin for FrontendRuntime
impl UnsafeUnpin for FrontendRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more