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 constants 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 constants 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    /// List of all the applyable filter types for the instrument
89    pub fn filter_types(&self, _ver: Version) -> &'static [&'static str] {
90        &super::common::COMMON_FILTER_TYPES
91    }
92
93    pub fn human_readable_filter(&self) -> &'static str {
94        COMMON_FILTER_TYPES[self.synth_params.filter_type as usize]
95    }
96
97    pub fn write(&self, ver: Version, w: &mut Writer) {
98        w.write_string(&self.name, 12);
99        w.write(TranspEq::from(ver, self.transpose, self.synth_params.associated_eq).into());
100        w.write(self.table_tick);
101        w.write(self.synth_params.volume);
102        w.write(self.synth_params.pitch);
103        w.write(self.synth_params.fine_tune);
104
105        for c in self.default_chord {
106            w.write(c);
107        }
108
109        w.write(self.scale);
110        w.write(self.shift);
111        w.write(self.swarm);
112        w.write(self.width);
113        w.write(self.subosc);
114
115        self.synth_params.write(ver, w, HyperSynth::MOD_OFFSET);
116
117        for chd in self.chords {
118            w.write(0xFF);
119            for k in chd {
120                w.write(k);
121            }
122        }
123    }
124
125    fn load_chord(reader: &mut Reader) -> [u8; 6] {
126        // padding
127        let _ = reader.read();
128        arr![reader.read(); 6]
129    }
130
131    pub fn from_reader(ver: Version, reader: &mut Reader, number: u8) -> M8Result<Self> {
132        let name = reader.read_string(12);
133        let transp_eq = TranspEq::from_version(ver, reader.read());
134        let table_tick = reader.read();
135        let volume = reader.read();
136        let pitch = reader.read();
137        let fine_tune = reader.read();
138
139        let default_chord = arr![reader.read(); 7];
140        let scale = reader.read();
141        let shift = reader.read();
142        let swarm = reader.read();
143        let width = reader.read();
144        let subosc = reader.read();
145        let synth_params = SynthParams::from_reader3(
146            ver,
147            reader,
148            volume,
149            pitch,
150            fine_tune,
151            transp_eq.eq,
152            HyperSynth::MOD_OFFSET,
153        )?;
154
155        let chords = arr![HyperSynth::load_chord(reader); 0x10];
156
157        Ok(HyperSynth {
158            number,
159            name,
160            transpose: transp_eq.transpose,
161            table_tick,
162            synth_params,
163
164            scale,
165            default_chord,
166            shift,
167            swarm,
168            width,
169            subosc,
170            chords,
171        })
172    }
173}