//! Render the sound of a reference run (WSG and 54xx through the filter //! network) to a 48 kHz mono 16-bit WAV for comparison with MAME's //! -wavwrite of the same run. //! Usage: galaga-audio [--rom DIR] [--inputs SCHEDULE] //! FRAMES=n overrides the run's length; WSG_ONLY=1 or NOISE_ONLY=1 isolate a path. use std::io::Write; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::sound::Sound; use emu_galaga::galaga::trace::parse_inputs; use emu_galaga::galaga::Machine; fn main() { let args: Vec = std::env::args().skip(1).collect(); let name = &args[0]; let out_path = &args[1]; let rom_dir = args .iter() .position(|a| a == "--rom") .map(|i| args[i + 1].clone()) .unwrap_or("rom/galaga".into()); let roms = load_directory(std::path::Path::new(&rom_dir)).expect("rom set"); let runs: serde_json::Value = serde_json::from_str(&std::fs::read_to_string("oracle/runs.json").unwrap()).unwrap(); let run_def = runs["runs"] .as_array() .unwrap() .iter() .find(|r| r["name"] == name.as_str()) .expect("run name"); let frames = std::env::var("FRAMES") .ok() .and_then(|f| f.parse().ok()) .unwrap_or(run_def["frames"].as_u64().unwrap()); let inputs_text = args .iter() .position(|a| a == "--inputs") .map(|i| args[i + 1].clone()) .unwrap_or_else(|| run_def["inputs"].as_str().unwrap_or("").to_string()); let inputs = parse_inputs(&inputs_text).unwrap(); let wsg_only = std::env::var("WSG_ONLY").is_ok(); let noise_only = std::env::var("NOISE_ONLY").is_ok(); let mut machine = Machine::new(&roms); let mut sound = Sound::new(&roms); if let Ok(g) = std::env::var("NOISE_GAIN") { sound.noise_gain = g.parse().expect("NOISE_GAIN"); } let mut samples: Vec = Vec::new(); let mut next_input = 0; for frame in 0..frames { machine.run_frame(); let (mut wsg_writes, mut n54_writes) = { let mut s = machine.shared.borrow_mut(); ( std::mem::take(&mut s.wsg_writes), std::mem::take(&mut s.n54_writes), ) }; if wsg_only { n54_writes.clear(); } if noise_only { wsg_writes.clear(); } sound.render_frame(frame, &wsg_writes, &n54_writes, &mut samples); while next_input < inputs.len() && inputs[next_input].frame <= frame { machine .set_control(&inputs[next_input].control, inputs[next_input].pressed) .unwrap(); next_input += 1; } } // 96 kHz -> 48 kHz by averaging pairs; 16-bit mono WAV. let pcm: Vec = samples .chunks(2) .map(|p| { ((p.iter().sum::() / p.len() as f32) * 32767.0) .round() .clamp(-32768.0, 32767.0) as i16 }) .collect(); let mut file = std::fs::File::create(out_path).unwrap(); let data_len = (pcm.len() * 2) as u32; file.write_all(b"RIFF").unwrap(); file.write_all(&(36 + data_len).to_le_bytes()).unwrap(); file.write_all(b"WAVEfmt ").unwrap(); file.write_all(&16u32.to_le_bytes()).unwrap(); file.write_all(&1u16.to_le_bytes()).unwrap(); file.write_all(&1u16.to_le_bytes()).unwrap(); file.write_all(&48_000u32.to_le_bytes()).unwrap(); file.write_all(&96_000u32.to_le_bytes()).unwrap(); file.write_all(&2u16.to_le_bytes()).unwrap(); file.write_all(&16u16.to_le_bytes()).unwrap(); file.write_all(b"data").unwrap(); file.write_all(&data_len.to_le_bytes()).unwrap(); for s in &pcm { file.write_all(&s.to_le_bytes()).unwrap(); } eprintln!( "{name}: {frames} frames, {} samples at 48 kHz -> {out_path}", pcm.len() ); }