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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::collections::HashMap;
use std::io::Write;
use std::os::raw::c_float;
use enum_map::{Enum, EnumMap};
use strum::{EnumIter, EnumProperty, IntoEnumIterator};
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;
use crate::components::colours::ColourMap;
use crate::components::megaphone::Preset;
use crate::components::megaphone::Preset::{Preset1, Preset2, Preset3, Preset4, Preset5, Preset6};
#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
#[error("Expected int: {0}")]
ExpectedInt(#[from] std::num::ParseIntError),
#[error("Expected float: {0}")]
ExpectedFloat(#[from] std::num::ParseFloatError),
#[error("Expected enum: {0}")]
ExpectedEnum(#[from] strum::ParseError),
#[error("Invalid colours: {0}")]
InvalidColours(#[from] crate::components::colours::ParseError),
}
#[derive(Debug)]
pub struct PitchEncoderBase {
colour_map: ColourMap,
preset_map: EnumMap<Preset, PitchEncoder>,
active_set: u8,
}
impl PitchEncoderBase {
pub fn new(element_name: String) -> Self {
let colour_map = element_name;
Self {
colour_map: ColourMap::new(colour_map),
preset_map: EnumMap::default(),
active_set: 0,
}
}
pub fn parse_pitch_root(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
for attr in attributes {
if attr.name.local_name == "active_set" {
self.active_set = attr.value.parse()?;
continue;
}
if !self.colour_map.read_colours(attr)? {
println!("[PitchEncoder] Unparsed Attribute: {}", attr.name);
}
}
Ok(())
}
pub fn parse_pitch_preset(
&mut self,
id: u8,
attributes: &[OwnedAttribute],
) -> Result<(), ParseError> {
let mut preset = PitchEncoder::new();
for attr in attributes {
if attr.name.local_name == "PITCH_STYLE" {
for style in PitchStyle::iter() {
if style.get_str("uiIndex").unwrap() == attr.value {
preset.style = style;
break;
}
}
continue;
}
if attr.name.local_name == "PITCH_KNOB_POSITION" {
preset.knob_position = attr.value.parse::<c_float>()? as i8;
continue;
}
if attr.name.local_name == "PITCH_RANGE" {
preset.range = attr.value.parse::<c_float>()? as u8;
continue;
}
if attr.name.local_name == "PITCH_SHIFT_THRESHOLD" {
preset.threshold = attr.value.parse::<c_float>()? as i8;
continue;
}
if attr.name.local_name == "PITCH_SHIFT_INST_RATIO" {
preset.inst_ratio = Option::Some(attr.value.parse::<c_float>()? as u8);
continue;
}
println!(
"[PitchEncoder] Unparsed Child Attribute: {}",
&attr.name.local_name
);
}
if id == 1 {
self.preset_map[Preset1] = preset;
} else if id == 2 {
self.preset_map[Preset2] = preset;
} else if id == 3 {
self.preset_map[Preset3] = preset;
} else if id == 4 {
self.preset_map[Preset4] = preset;
} else if id == 5 {
self.preset_map[Preset5] = preset;
} else if id == 6 {
self.preset_map[Preset6] = preset;
}
Ok(())
}
pub fn write_pitch<W: Write>(
&self,
writer: &mut EventWriter<&mut W>,
) -> Result<(), xml::writer::Error> {
let mut element: StartElementBuilder = XmlWriterEvent::start_element("pitchEncoder");
let mut attributes: HashMap<String, String> = HashMap::default();
attributes.insert("active_set".to_string(), format!("{}", self.active_set));
self.colour_map.write_colours(&mut attributes);
for (key, value) in &attributes {
element = element.attr(key.as_str(), value.as_str());
}
writer.write(element)?;
for (key, value) in &self.preset_map {
let mut sub_attributes: HashMap<String, String> = HashMap::default();
let tag_name = format!("pitchEncoder{}", key.get_str("tagSuffix").unwrap());
let mut sub_element: StartElementBuilder =
XmlWriterEvent::start_element(tag_name.as_str());
sub_attributes.insert(
"PITCH_KNOB_POSITION".to_string(),
format!("{}", value.knob_position),
);
sub_attributes.insert(
"PITCH_STYLE".to_string(),
value.style.get_str("uiIndex").unwrap().to_string(),
);
sub_attributes.insert("PITCH_RANGE".to_string(), format!("{}", value.range));
sub_attributes.insert(
"PITCH_SHIFT_THRESHOLD".to_string(),
format!("{}", value.threshold),
);
if let Some(inst_ratio) = value.inst_ratio {
sub_attributes.insert(
"PITCH_SHIFT_INST_RATIO".to_string(),
format!("{}", inst_ratio),
);
}
for (key, value) in &sub_attributes {
sub_element = sub_element.attr(key.as_str(), value.as_str());
}
writer.write(sub_element)?;
writer.write(XmlWriterEvent::end_element())?;
}
writer.write(XmlWriterEvent::end_element())?;
Ok(())
}
pub fn colour_map(&self) -> &ColourMap {
&self.colour_map
}
pub fn colour_map_mut(&mut self) -> &mut ColourMap {
&mut self.colour_map
}
pub fn get_preset(&self, preset: Preset) -> &PitchEncoder {
&self.preset_map[preset]
}
pub fn get_preset_mut(&mut self, preset: Preset) -> &mut PitchEncoder {
&mut self.preset_map[preset]
}
}
#[derive(Debug, Default)]
pub struct PitchEncoder {
knob_position: i8,
style: PitchStyle,
range: u8,
threshold: i8,
inst_ratio: Option<u8>,
}
impl PitchEncoder {
pub fn new() -> Self {
Self {
knob_position: 0,
style: PitchStyle::Narrow,
range: 0,
threshold: 0,
inst_ratio: None,
}
}
pub fn knob_position(&self) -> i8 {
self.knob_position
}
pub fn set_knob_position(&mut self, knob_position: i8) {
self.knob_position = knob_position;
}
pub fn style(&self) -> &PitchStyle {
&self.style
}
pub fn range(&self) -> u8 {
self.range
}
pub fn threshold(&self) -> i8 {
self.threshold
}
pub fn inst_ratio(&self) -> Option<u8> {
self.inst_ratio
}
pub fn inst_ratio_value(&self) -> u8 {
if let Some(value) = self.inst_ratio {
return value;
}
return 0;
}
}
#[derive(Debug, PartialEq, EnumIter, Enum, EnumProperty, Copy, Clone)]
pub enum PitchStyle {
#[strum(props(uiIndex = "0"))]
Narrow,
#[strum(props(uiIndex = "1"))]
Wide,
}
impl Default for PitchStyle {
fn default() -> Self {
PitchStyle::Narrow
}
}