m8_file_parser/instruments/
mod.rs

1use crate::eq::Equ;
2use crate::reader::*;
3use crate::version::*;
4use crate::writer::Writer;
5use crate::ReferenceTemplating;
6use crate::Table;
7use crate::TableView;
8use crate::V4_1_OFFSETS;
9
10mod common;
11mod external_inst;
12mod fmsynth;
13mod hypersynth;
14mod macrosynth;
15mod midi;
16mod modulator;
17mod sampler;
18mod wavsynth;
19
20pub use common::{LimitType, SynthParams};
21
22pub use external_inst::*;
23pub use fmsynth::*;
24pub use hypersynth::*;
25pub use macrosynth::*;
26pub use midi::*;
27pub use modulator::*;
28pub use sampler::*;
29pub use wavsynth::*;
30
31#[derive(PartialEq, Debug, Clone, Default)]
32pub enum Instrument {
33    WavSynth(WavSynth),
34    MacroSynth(MacroSynth),
35    Sampler(Sampler),
36    MIDIOut(MIDIOut),
37    FMSynth(FMSynth),
38    HyperSynth(HyperSynth),
39    External(ExternalInst),
40    #[default]
41    None,
42}
43
44/// Various constants for common parameters, to avoid nasty typos everywhere
45pub mod params {
46    pub const NAME: &'static str = "NAME";
47    pub const TRANSPOSE: &'static str = "TRANSPOSE";
48    pub const TBLTIC: &'static str = "TBL. TIC";
49    pub const EQ: &'static str = "EQ";
50    pub const SCALE: &'static str = "SCALE";
51
52    pub const CCA: &'static str = "CCA";
53    pub const CCB: &'static str = "CCB";
54    pub const CCC: &'static str = "CCC";
55    pub const CCD: &'static str = "CCD";
56
57    pub const DEST: &'static str = "DEST";
58    pub const AMOUNT: &'static str = "AMT";
59    pub const ATTACK: &'static str = "ATK";
60    pub const DECAY: &'static str = "DEC";
61    pub const HOLD: &'static str = "HOLD";
62    pub const SUSTAIN: &'static str = "SUS";
63    pub const RELEASE: &'static str = "REL";
64    pub const PEAK: &'static str = "PEAK";
65    pub const BODY: &'static str = "BODY";
66    pub const FREQ: &'static str = "FREQ";
67    pub const TRIGGER: &'static str = "TRIG";
68    pub const LFOSHAPE: &'static str = "OSC";
69    pub const SOURCE: &'static str = "SRC";
70}
71
72/// Various constants for modulation destinations, to avoid nasty typos everywhere
73pub mod dests {
74    pub const OFF: &'static str = "OFF";
75    pub const VOLUME: &'static str = "VOLUME";
76    pub const PITCH: &'static str = "PITCH";
77    pub const CUTOFF: &'static str = "CUTOFF";
78    pub const RES: &'static str = "RES";
79    pub const AMP: &'static str = "AMP";
80    pub const PAN: &'static str = "PAN";
81    pub const DEGRADE: &'static str = "DEGRADE";
82    pub const MOD_AMT: &'static str = "MOD AMT";
83    pub const MOD_RATE: &'static str = "MOD RATE";
84    pub const MOD_BOTH: &'static str = "MOD BOTH";
85    pub const MOD_BINV: &'static str = "MOD BINV";
86}
87
88#[rustfmt::skip] // Keep constants with important order vertical for maintenance
89pub(crate) const SEND_COMMAND_NAMES : [&'static str; 3] = [
90    "SCH",
91    "SDL",
92    "SRV"
93];
94
95#[rustfmt::skip] // Keep constants with important order vertical for maintenance
96pub(crate) const SEND_COMMAND_NAMES_6_2 : [&'static str; 3] = [
97    "SMX",
98    "SDL",
99    "SRV"
100];
101
102/// This structure will aggregate for every instrument and its
103/// modulator the name of the commands associated to it.
104#[derive(Clone, Copy)]
105pub struct CommandPack {
106    /// Instruments command
107    pub instr: &'static [&'static str],
108
109    /// For all the modulators, their respective
110    /// command names
111    pub mod_commands: [&'static [&'static str]; SynthParams::MODULATOR_COUNT],
112}
113
114impl Default for CommandPack {
115    fn default() -> Self {
116        Self {
117            instr: Default::default(),
118            mod_commands: Default::default(),
119        }
120    }
121}
122
123impl CommandPack {
124    /// Instrument specific command start at 0x80
125    pub const INSTRUMENT_COMMAND_OFFSET: usize = 0x80;
126
127    /// If we are below INSTRUMENT_COMMAND_OFFSET + this number, we will access to
128    /// CommandPack::instr array, for instrument specific command.
129    pub const BASE_INSTRUMENT_COMMAND_COUNT: usize = 18;
130
131    /// Last base instrument command index.
132    pub const BASE_INSTRUMENT_COMMAND_END: usize = CommandPack::INSTRUMENT_COMMAND_OFFSET
133        + Mod::COMMAND_PER_MOD * SynthParams::MODULATOR_COUNT;
134
135    /// Does this command pack can render properly a given command.
136    pub fn accepts(self, cmd: u8) -> bool {
137        let cmd = cmd as usize;
138        CommandPack::INSTRUMENT_COMMAND_OFFSET <= cmd
139            && cmd <= (CommandPack::BASE_INSTRUMENT_COMMAND_END + self.instr.len())
140    }
141
142    pub fn try_render(self, cmd: u8) -> Option<&'static str> {
143        if self.instr.len() == 0 {
144            return None;
145        }
146        if (cmd as usize) < CommandPack::INSTRUMENT_COMMAND_OFFSET {
147            return None;
148        }
149
150        let cmd = cmd as usize - CommandPack::INSTRUMENT_COMMAND_OFFSET;
151
152        if cmd < CommandPack::BASE_INSTRUMENT_COMMAND_COUNT {
153            if cmd < self.instr.len() {
154                return Some(self.instr[cmd]);
155            } else {
156                return None;
157            }
158        }
159
160        let mod_cmd = cmd - CommandPack::BASE_INSTRUMENT_COMMAND_COUNT;
161        let mod_ix = mod_cmd / Mod::COMMAND_PER_MOD;
162
163        if mod_ix < self.mod_commands.len() {
164            let ix = mod_cmd - Mod::COMMAND_PER_MOD * mod_ix;
165            return Some(self.mod_commands[mod_ix][ix]);
166        }
167
168        let extra_cmd = cmd - (Mod::COMMAND_PER_MOD * SynthParams::MODULATOR_COUNT);
169        if extra_cmd < self.instr.len() {
170            return Some(self.instr[extra_cmd]);
171        }
172
173        None
174    }
175}
176
177/// Firmware 4.1 introduce files with an instrument definition and an
178/// EQ. This structure represent the result of parsing such insturment
179/// with an optional EQ.
180pub struct InstrumentWithEq {
181    /// The parsed instrument
182    pub instrument: Instrument,
183
184    /// Table associated with the instrument.
185    pub table: Table,
186
187    /// If the instrument was referencing an EQ, the effectively
188    /// parsed EQ.
189    pub eq: Option<Equ>,
190
191    /// Instrument file version
192    pub version: Version
193}
194
195impl InstrumentWithEq {
196    pub fn table_view(&self) -> TableView<'_> {
197        TableView {
198            table: &self.table,
199            table_index: 0,
200            instrument: self.instrument.instr_command_text(self.version),
201            templates: ReferenceTemplating::default()
202        }
203    }
204}
205
206impl Instrument {
207    pub const INSTRUMENT_MEMORY_SIZE: usize = 215;
208    pub const V4_SIZE: usize = Self::INSTRUMENT_MEMORY_SIZE;
209
210    pub fn is_empty(&self) -> bool {
211        match self {
212            Instrument::None => true,
213            _ => false,
214        }
215    }
216
217    pub fn instr_command_text(&self, ver: Version) -> CommandPack {
218        let (commands, mods) = match self {
219            Instrument::WavSynth(ws) => (ws.command_name(ver), &ws.synth_params.mods),
220            Instrument::MacroSynth(ms) => (ms.command_name(ver), &ms.synth_params.mods),
221            Instrument::Sampler(s) => (s.command_name(ver), &s.synth_params.mods),
222            Instrument::MIDIOut(mo) => (mo.command_name(ver), &mo.mods.mods),
223            Instrument::FMSynth(fs) => (fs.command_name(ver), &fs.synth_params.mods),
224            Instrument::HyperSynth(hs) => (hs.command_name(ver), &hs.synth_params.mods),
225            Instrument::External(ex) => (ex.command_name(ver), &ex.synth_params.mods),
226            Instrument::None => return CommandPack::default(),
227        };
228
229        CommandPack {
230            instr: commands,
231            mod_commands: [
232                mods[0].command_name(ver, 0),
233                mods[1].command_name(ver, 1),
234                mods[2].command_name(ver, 2),
235                mods[3].command_name(ver, 3),
236            ],
237        }
238    }
239
240    pub fn write(&self, ver: Version, w: &mut Writer) {
241        match self {
242            Instrument::WavSynth(ws) => {
243                w.write(0);
244                ws.write(ver, w);
245            }
246            Instrument::MacroSynth(ms) => {
247                w.write(1);
248                ms.write(ver, w);
249            }
250            Instrument::Sampler(s) => {
251                w.write(2);
252                s.write(ver, w);
253            }
254            Instrument::MIDIOut(mo) => {
255                w.write(3);
256                mo.write(ver, w);
257            }
258            Instrument::FMSynth(fs) => {
259                w.write(4);
260                fs.write(ver, w);
261            }
262            Instrument::HyperSynth(hs) => {
263                w.write(5);
264                hs.write(ver, w);
265            }
266            Instrument::External(ex) => {
267                w.write(6);
268                ex.write(ver, w);
269            }
270            Instrument::None => w.write(0xFF),
271        }
272    }
273
274    pub fn name(&self) -> Option<&str> {
275        match self {
276            Instrument::WavSynth(ws) => Some(&ws.name),
277            Instrument::MacroSynth(ms) => Some(&ms.name),
278            Instrument::Sampler(s) => Some(&s.name),
279            Instrument::MIDIOut(_) => None,
280            Instrument::FMSynth(fs) => Some(&fs.name),
281            Instrument::HyperSynth(hs) => Some(&hs.name),
282            Instrument::External(ex) => Some(&ex.name),
283            Instrument::None => None,
284        }
285    }
286
287    pub fn set_name(&mut self, name: String) {
288        match self {
289            Instrument::WavSynth(ws) => ws.name = name,
290            Instrument::MacroSynth(ms) => ms.name = name,
291            Instrument::Sampler(s) => s.name = name,
292            Instrument::MIDIOut(mo) => mo.name = name,
293            Instrument::FMSynth(fs) => fs.name = name,
294            Instrument::HyperSynth(hs) => hs.name = name,
295            Instrument::External(ex) => ex.name = name,
296            Instrument::None => {}
297        }
298    }
299
300    pub fn equ(&self) -> Option<u8> {
301        match self {
302            Instrument::WavSynth(ws) => Some(ws.synth_params.associated_eq),
303            Instrument::MacroSynth(ms) => Some(ms.synth_params.associated_eq),
304            Instrument::Sampler(s) => Some(s.synth_params.associated_eq),
305            Instrument::MIDIOut(_) => None,
306            Instrument::FMSynth(fs) => Some(fs.synth_params.associated_eq),
307            Instrument::HyperSynth(hs) => Some(hs.synth_params.associated_eq),
308            Instrument::External(ex) => Some(ex.synth_params.associated_eq),
309            Instrument::None => None,
310        }
311    }
312
313    pub fn set_eq(&mut self, eq_ix: u8) {
314        match self {
315            Instrument::WavSynth(ws) => ws.synth_params.set_eq(eq_ix),
316            Instrument::MacroSynth(ms) => ms.synth_params.set_eq(eq_ix),
317            Instrument::Sampler(s) => s.synth_params.set_eq(eq_ix),
318            Instrument::MIDIOut(_) => {}
319            Instrument::FMSynth(fs) => fs.synth_params.set_eq(eq_ix),
320            Instrument::HyperSynth(hs) => hs.synth_params.set_eq(eq_ix),
321            Instrument::External(ex) => ex.synth_params.set_eq(eq_ix),
322            Instrument::None => {}
323        }
324    }
325
326    /// Read an in-memory instrument file along with its optional eq
327    pub fn read_from_reader(reader: &mut Reader) -> M8Result<InstrumentWithEq> {
328        let instrument_end_offset = Instrument::INSTRUMENT_MEMORY_SIZE + Version::SIZE;
329        if reader.len() < instrument_end_offset {
330            return Err(ParseError(
331                "File is not long enough to be a M8 Instrument".to_string(),
332            ));
333        }
334
335        let version = Version::from_reader(reader)?;
336        let instrument = Self::from_reader(reader, 0, version)?;
337        let table = Table::from_reader(reader, version)?;
338
339        let eq = match V4_1_OFFSETS.instrument_file_eq_offset {
340            None => None,
341            Some(ofs) if version.at_least(4, 0) => {
342                if reader.len() >= ofs + Equ::V4_SIZE {
343                    reader.set_pos(ofs);
344                    Some(Equ::from_reader(reader))
345                } else {
346                    None
347                }
348            }
349            Some(_) => None,
350        };
351
352        Ok(InstrumentWithEq { instrument, eq, table, version })
353    }
354
355    /// Read a M8 instrument file along with its optional Eq definition.
356    pub fn read(reader: &mut impl std::io::Read) -> M8Result<InstrumentWithEq> {
357        let mut buf: Vec<u8> = vec![];
358        reader.read_to_end(&mut buf).unwrap();
359        let mut reader = Reader::new(buf);
360
361        Self::read_from_reader(&mut reader)
362    }
363
364    pub fn from_reader(reader: &mut Reader, number: u8, version: Version) -> M8Result<Self> {
365        let start_pos = reader.pos();
366        let kind = reader.read();
367
368        let instr = match kind {
369            0x00 => Self::WavSynth(WavSynth::from_reader(version, reader, number, version)?),
370            0x01 => Self::MacroSynth(MacroSynth::from_reader(version, reader, number, version)?),
371            0x02 => Self::Sampler(Sampler::from_reader(
372                version, reader, start_pos, number, version,
373            )?),
374            0x03 => Self::MIDIOut(MIDIOut::from_reader(version, reader, number, version)?),
375            0x04 => Self::FMSynth(FMSynth::from_reader(version, reader, number, version)?),
376            0x05 if version.at_least(3, 0) => {
377                Self::HyperSynth(HyperSynth::from_reader(version, reader, number)?)
378            }
379            0x06 if version.at_least(3, 0) => {
380                Self::External(ExternalInst::from_reader(version, reader, number)?)
381            }
382            0xFF => Self::None,
383            _ => {
384                return Err(ParseError(format!(
385                    "Instrument type {} not supported",
386                    kind
387                )))
388            }
389        };
390
391        reader.set_pos(start_pos + Instrument::INSTRUMENT_MEMORY_SIZE);
392
393        Ok(instr)
394    }
395}