m8_files/instruments/
mod.rs

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