Skip to main content

rustysynth_ext/
soundfont_version.rs

1use std::io;
2use std::io::Read;
3
4use crate::binary_reader::BinaryReader;
5
6/// Represents the version of a SoundFont.
7#[derive(Debug)]
8#[non_exhaustive]
9pub struct SoundFontVersion {
10    pub(crate) major: i16,
11    pub(crate) minor: i16,
12}
13
14impl SoundFontVersion {
15    const SIZE: usize = 4;
16
17    pub(crate) fn default() -> Self {
18        Self { major: 0, minor: 0 }
19    }
20
21    /// Reads the two version numbers and skips whatever the chunk declares
22    /// beyond them, so that the following chunk is read at the right offset.
23    pub(crate) fn new<R: Read>(reader: &mut R, size: usize) -> Result<Self, io::Error> {
24        let major = BinaryReader::read_i16(reader)?;
25        let minor = BinaryReader::read_i16(reader)?;
26        BinaryReader::discard_data(reader, size.saturating_sub(Self::SIZE))?;
27
28        Ok(Self { major, minor })
29    }
30
31    /// Gets the major version.
32    pub fn get_major(&self) -> i32 {
33        self.major as i32
34    }
35
36    /// Gets the minor version.
37    pub fn get_minor(&self) -> i32 {
38        self.minor as i32
39    }
40}