Skip to main content

rustysynth_ext/
preset.rs

1use crate::error::SoundFontError;
2use crate::instrument::Instrument;
3use crate::preset_info::PresetInfo;
4use crate::preset_region::PresetRegion;
5use crate::zone::Zone;
6
7/// Represents a preset in the SoundFont.
8#[derive(Debug)]
9#[non_exhaustive]
10pub struct Preset {
11    pub(crate) name: String,
12    pub(crate) patch_number: i32,
13    pub(crate) bank_number: i32,
14    pub(crate) library: i32,
15    pub(crate) genre: i32,
16    pub(crate) morphology: i32,
17    pub(crate) regions: Vec<PresetRegion>,
18}
19
20impl Preset {
21    fn new(
22        info: &PresetInfo,
23        preset_id: usize,
24        zones: &[Zone],
25        instruments: &[Instrument],
26    ) -> Result<Self, SoundFontError> {
27        let name = info.name.clone();
28
29        let zone_count = info.zone_end_index - info.zone_start_index + 1;
30        if zone_count <= 0 {
31            return Err(SoundFontError::InvalidPreset(preset_id));
32        }
33
34        let span_start = info.zone_start_index as usize;
35        let span_end = span_start + zone_count as usize;
36        let zone_span = &zones[span_start..span_end];
37        let regions = PresetRegion::create(preset_id, zone_span, instruments)?;
38
39        Ok(Self {
40            name,
41            patch_number: info.patch_number,
42            bank_number: info.bank_number,
43            library: info.library,
44            genre: info.genre,
45            morphology: info.morphology,
46            regions,
47        })
48    }
49
50    pub(crate) fn create(
51        infos: &[PresetInfo],
52        zones: &[Zone],
53        instruments: &[Instrument],
54    ) -> Result<Vec<Preset>, SoundFontError> {
55        if infos.len() <= 1 {
56            return Err(SoundFontError::PresetNotFound);
57        }
58
59        // The last one is the terminator.
60        let count = infos.len() - 1;
61
62        let mut presets: Vec<Preset> = Vec::with_capacity(count);
63        for (preset_id, info) in infos.iter().take(count).enumerate() {
64            presets.push(Preset::new(info, preset_id, zones, instruments)?);
65        }
66
67        Ok(presets)
68    }
69
70    /// Gets the name of the preset.
71    pub fn get_name(&self) -> &str {
72        &self.name
73    }
74
75    /// Gets the patch number of the preset.
76    pub fn get_patch_number(&self) -> i32 {
77        self.patch_number
78    }
79
80    /// Gets the bank number of the preset.
81    pub fn get_bank_number(&self) -> i32 {
82        self.bank_number
83    }
84
85    /// Gets the library info.
86    pub fn get_library(&self) -> i32 {
87        self.library
88    }
89
90    /// Gets the genre info.
91    pub fn get_genre(&self) -> i32 {
92        self.genre
93    }
94
95    /// Gets the morphology info.
96    pub fn get_morphology(&self) -> i32 {
97        self.morphology
98    }
99
100    /// Gets the regions of the preset.
101    pub fn get_regions(&self) -> &[PresetRegion] {
102        &self.regions[..]
103    }
104}