use std::sync::Arc; use criterion::criterion_group; use criterion::criterion_main; use criterion::Criterion; use xsynth_core::channel::ChannelAudioEvent; use xsynth_core::channel::ChannelConfigEvent; use xsynth_core::channel::ChannelEvent; use xsynth_core::channel::VoiceChannel; use xsynth_core::soundfont::SampleSoundfont; use xsynth_core::soundfont::SoundfontBase; use xsynth_core::AudioPipe; use xsynth_core::AudioStreamParams; use xsynth_core::ChannelCount; fn criterion_benchmark(c: &mut Criterion) { let Some(sfz) = std::env::var("XSYNTH_EXAMPLE_SFZ").ok() else { println!( "Usage: {} [sfz]", std::env::current_exe() .unwrap_or("example".into()) .display() ); return; }; let stream_params = AudioStreamParams::new(48000, ChannelCount::Stereo); println!("Loading soundfont..."); let soundfonts: Vec> = vec![Arc::new( SampleSoundfont::new(sfz, stream_params, Default::default()).unwrap(), )]; let make_new_channel = || { let mut channel = VoiceChannel::new(Default::default(), stream_params, None); channel.process_event(ChannelEvent::Config(ChannelConfigEvent::SetSoundfonts( soundfonts.clone(), ))); channel.process_event(ChannelEvent::Config(ChannelConfigEvent::SetLayerCount( None, ))); channel }; let mut buffer = vec![0.0; 4800]; c.bench_function("rendering", |f| { f.iter(|| { let mut channel = make_new_channel(); for _ in 0..30 { for i in 0..127 { channel.process_event(ChannelEvent::Audio(ChannelAudioEvent::NoteOn { key: i as u8, vel: 127, })); } channel.read_samples(&mut buffer); for i in 0..127 { channel.process_event(ChannelEvent::Audio(ChannelAudioEvent::NoteOff { key: i as u8, })); } } }) }); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);