m8_files/instruments/
hypersynth.rs

1use super::common::SynthParams;
2use super::common::TranspEq;
3use super::common::COMMON_FILTER_TYPES;
4use super::dests;
5use super::CommandPack;
6use super::Version;
7use crate::reader::*;
8use crate::writer::Writer;
9
10use arr_macro::arr;
11
12#[derive(PartialEq, Debug, Clone)]
13pub struct HyperSynth {
14    pub number: u8,
15    pub name: String,
16    pub transpose: bool,
17    pub table_tick: u8,
18    pub synth_params: SynthParams,
19
20    pub scale: u8,
21    pub default_chord: [u8; 7],
22    pub shift: u8,
23    pub swarm: u8,
24    pub width: u8,
25    pub subosc: u8,
26
27    pub chords: [[u8; 6]; 0x10],
28}
29
30#[rustfmt::skip] // Keep constats with important order vertical for maintenance
31const HYPERSYNTH_COMMAND_NAMES : [&'static str; CommandPack::BASE_INSTRUMENT_COMMAND_COUNT + 2] = [
32    "VOL",
33    "PIT",
34    "FIN",
35    "CRD",
36    "SHF",
37    "SWM",
38    "WID",
39    "SUB",
40    "FLT",
41    "CUT",
42    "RES",
43    "AMP",
44    "LIM",
45    "PAN",
46    "DRY",
47    
48    "SCH",
49    "SDL",
50    "SRV",
51
52    // EXTRA
53    "CVO",
54    "SNC"
55];
56
57#[rustfmt::skip] // Keep constats with important order vertical for maintenance
58const DESTINATIONS : [&'static str; 15] = [
59    dests::OFF,
60    dests::VOLUME,
61    dests::PITCH,
62
63    "SHIFT",
64    "SWARM",
65    "WIDTH",
66    "SUBOSC",
67    dests::CUTOFF,
68    dests::RES,
69    dests::AMP,
70    dests::PAN,
71    dests::MOD_AMT,
72    dests::MOD_RATE,
73    dests::MOD_BOTH,
74    dests::MOD_BINV,
75];
76
77impl HyperSynth {
78    const MOD_OFFSET: usize = 23;
79
80    pub fn command_name(&self, _ver: Version) -> &'static [&'static str] {
81        &HYPERSYNTH_COMMAND_NAMES
82    }
83
84    pub fn destination_names(&self, _ver: Version) -> &'static [&'static str] {
85        &DESTINATIONS
86    }
87
88    pub fn human_readable_filter(&self) -> &'static str {
89        COMMON_FILTER_TYPES[self.synth_params.filter_type as usize]
90    }
91
92    pub fn write(&self, ver: Version, w: &mut Writer) {
93        w.write_string(&self.name, 12);
94        w.write(TranspEq::from(ver, self.transpose, self.synth_params.associated_eq).into());
95        w.write(self.table_tick);
96        w.write(self.synth_params.volume);
97        w.write(self.synth_params.pitch);
98        w.write(self.synth_params.fine_tune);
99
100        for c in self.default_chord {
101            w.write(c);
102        }
103
104        w.write(self.scale);
105        w.write(self.shift);
106        w.write(self.swarm);
107        w.write(self.width);
108        w.write(self.subosc);
109
110        self.synth_params.write(ver, w, HyperSynth::MOD_OFFSET);
111
112        for chd in self.chords {
113            w.write(0xFF);
114            for k in chd {
115                w.write(k);
116            }
117        }
118    }
119
120    fn load_chord(reader: &mut Reader) -> [u8; 6] {
121        // padding
122        let _ = reader.read();
123        arr![reader.read(); 6]
124    }
125
126    pub fn from_reader(ver: Version, reader: &mut Reader, number: u8) -> M8Result<Self> {
127        let name = reader.read_string(12);
128        let transp_eq = TranspEq::from_version(ver, reader.read());
129        let table_tick = reader.read();
130        let volume = reader.read();
131        let pitch = reader.read();
132        let fine_tune = reader.read();
133
134        let default_chord = arr![reader.read(); 7];
135        let scale = reader.read();
136        let shift = reader.read();
137        let swarm = reader.read();
138        let width = reader.read();
139        let subosc = reader.read();
140        let synth_params = SynthParams::from_reader3(
141            ver,
142            reader,
143            volume,
144            pitch,
145            fine_tune,
146            transp_eq.eq,
147            HyperSynth::MOD_OFFSET,
148        )?;
149
150        let chords = arr![HyperSynth::load_chord(reader); 0x10];
151
152        Ok(HyperSynth {
153            number,
154            name,
155            transpose: transp_eq.transpose,
156            table_tick,
157            synth_params,
158
159            scale,
160            default_chord,
161            shift,
162            swarm,
163            width,
164            subosc,
165            chords,
166        })
167    }
168}