sf2/
sf2.rs

1use soundfont::{raw::RawSoundFontData, SoundFont2};
2
3fn main() {
4    let mut file = std::fs::File::open("./testdata/sin.sf2").unwrap();
5
6    // Load from memory
7    // use std::io::Cursor;
8    // let mut file = Cursor::new(include_bytes!("../testdata/sin.sf2"));
9
10    let data = RawSoundFontData::load(&mut file).unwrap();
11    let sf2 = SoundFont2::from_raw(data);
12
13    for p in sf2.presets.iter() {
14        println!("====== Preset =======");
15        println!("Name: {}", p.header.name);
16        let instruments: Vec<_> = p
17            .zones
18            .iter()
19            .filter_map(|z| {
20                z.instrument().map(|id| {
21                    let instrument = &sf2.instruments[*id as usize];
22
23                    let mut samples = Vec::new();
24                    for z in instrument.zones.iter() {
25                        if let Some(sample_id) = z.sample() {
26                            samples.push(sf2.sample_headers[*sample_id as usize].clone());
27                        }
28                    }
29
30                    (instrument.header.name.clone(), samples.len())
31                })
32            })
33            .collect();
34        println!("Instruments: {:?}", instruments);
35        println!();
36    }
37}