1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#![allow(dead_code)]

use std::io::Read;

use crate::binary_reader::BinaryReader;
use crate::error::SoundFontError;
use crate::read_counter::ReadCounter;
use crate::soundfont_version::SoundFontVersion;

/// The information of a SoundFont.
#[non_exhaustive]
pub struct SoundFontInfo {
    pub(crate) version: SoundFontVersion,
    pub(crate) target_sound_engine: String,
    pub(crate) bank_name: String,
    pub(crate) rom_name: String,
    pub(crate) rom_version: SoundFontVersion,
    pub(crate) creation_date: String,
    pub(crate) author: String,
    pub(crate) target_product: String,
    pub(crate) copyright: String,
    pub(crate) comments: String,
    pub(crate) tools: String,
}

impl SoundFontInfo {
    pub(crate) fn new<R: Read>(reader: &mut R) -> Result<Self, SoundFontError> {
        let chunk_id = BinaryReader::read_four_cc(reader)?;
        if chunk_id != "LIST" {
            return Err(SoundFontError::ListChunkNotFound);
        }

        let end = BinaryReader::read_i32(reader)? as usize;
        let reader = &mut ReadCounter::new(reader);

        let list_type = BinaryReader::read_four_cc(reader)?;
        if list_type != "INFO" {
            return Err(SoundFontError::InvalidListChunkType {
                expected: "INFO",
                actual: list_type,
            });
        }

        let mut version: Option<SoundFontVersion> = None;
        let mut target_sound_engine: Option<String> = None;
        let mut bank_name: Option<String> = None;
        let mut rom_name: Option<String> = None;
        let mut rom_version: Option<SoundFontVersion> = None;
        let mut creation_date: Option<String> = None;
        let mut author: Option<String> = None;
        let mut target_product: Option<String> = None;
        let mut copyright: Option<String> = None;
        let mut comments: Option<String> = None;
        let mut tools: Option<String> = None;

        while reader.bytes_read() < end {
            let id = BinaryReader::read_four_cc(reader)?;
            let size = BinaryReader::read_i32(reader)? as usize;

            if id == "ifil" {
                version = Some(SoundFontVersion::new(reader)?);
            } else if id == "isng" {
                target_sound_engine = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "INAM" {
                bank_name = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "irom" {
                rom_name = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "iver" {
                rom_version = Some(SoundFontVersion::new(reader)?);
            } else if id == "ICRD" {
                creation_date = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "IENG" {
                author = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "IPRD" {
                target_product = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "ICOP" {
                copyright = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "ICMT" {
                comments = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else if id == "ISFT" {
                tools = Some(BinaryReader::read_fixed_length_string(reader, size)?);
            } else {
                return Err(SoundFontError::ListContainsUnknownId(id));
            }
        }

        let version = match version {
            Some(value) => value,
            None => SoundFontVersion::default(),
        };

        let target_sound_engine = match target_sound_engine {
            Some(value) => value,
            None => String::new(),
        };

        let bank_name = match bank_name {
            Some(value) => value,
            None => String::new(),
        };

        let rom_name = match rom_name {
            Some(value) => value,
            None => String::new(),
        };

        let rom_version = match rom_version {
            Some(value) => value,
            None => SoundFontVersion::default(),
        };

        let creation_date = match creation_date {
            Some(value) => value,
            None => String::new(),
        };

        let author = match author {
            Some(value) => value,
            None => String::new(),
        };

        let target_product = match target_product {
            Some(value) => value,
            None => String::new(),
        };

        let copyright = match copyright {
            Some(value) => value,
            None => String::new(),
        };

        let comments = match comments {
            Some(value) => value,
            None => String::new(),
        };

        let tools = match tools {
            Some(value) => value,
            None => String::new(),
        };

        Ok(Self {
            version,
            target_sound_engine,
            bank_name,
            rom_name,
            rom_version,
            creation_date,
            author,
            target_product,
            copyright,
            comments,
            tools,
        })
    }

    /// Gets the version of the SoundFont.
    pub fn get_version(&self) -> &SoundFontVersion {
        &self.version
    }

    /// Gets the target sound engine of the SoundFont.
    pub fn get_target_sound_engine(&self) -> &str {
        &self.target_sound_engine
    }

    /// Gets the bank name of the SoundFont.
    pub fn get_bank_name(&self) -> &str {
        &self.bank_name
    }

    /// Gets the ROM name of the SoundFont.
    pub fn get_rom_name(&self) -> &str {
        &self.rom_name
    }

    /// Gets the ROM version of the SoundFont.
    pub fn get_rom_version(&self) -> &SoundFontVersion {
        &self.rom_version
    }

    /// Gets the creation date of the SoundFont.
    pub fn get_creation_date(&self) -> &str {
        &self.creation_date
    }

    /// Gets the auther of the SoundFont.
    pub fn get_author(&self) -> &str {
        &self.author
    }

    /// Gets the target product of the SoundFont.
    pub fn get_target_product(&self) -> &str {
        &self.target_product
    }

    /// Gets the copyright message for the SoundFont.
    pub fn get_copyright(&self) -> &str {
        &self.copyright
    }

    /// Gets the comments for the SoundFont.
    pub fn get_comments(&self) -> &str {
        &self.comments
    }

    /// Gets the tools used to create the SoundFont.
    pub fn get_tools(&self) -> &str {
        &self.tools
    }
}