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
use crate::NbsError;
#[derive(Debug, Clone, Copy)]
pub enum Instrument {
Piano,
DoubleBass,
BassDrum,
SnareDrum,
Click,
Guitar,
Flute,
Bell,
Chime,
Xylophone,
IronXylophone,
CowBell,
Didgeridoo,
Bit,
Banjo,
Pling,
Custom(i8),
}
impl Instrument {
pub fn is_custom(&self) -> bool {
match self {
Instrument::Custom(_) => true,
_ => false,
}
}
}
impl Into<i8> for Instrument {
fn into(self) -> i8 {
match self {
Instrument::Piano => 0,
Instrument::DoubleBass => 1,
Instrument::BassDrum => 2,
Instrument::SnareDrum => 3,
Instrument::Click => 4,
Instrument::Guitar => 5,
Instrument::Flute => 6,
Instrument::Bell => 7,
Instrument::Chime => 8,
Instrument::Xylophone => 9,
Instrument::IronXylophone => 10,
Instrument::CowBell => 11,
Instrument::Didgeridoo => 12,
Instrument::Bit => 13,
Instrument::Banjo => 14,
Instrument::Pling => 15,
Instrument::Custom(id) => id,
}
}
}
impl From<i8> for Instrument {
fn from(id: i8) -> Self {
match id {
0 => Instrument::Piano,
1 => Instrument::DoubleBass,
2 => Instrument::BassDrum,
3 => Instrument::SnareDrum,
4 => Instrument::Click,
5 => Instrument::Guitar,
6 => Instrument::Flute,
7 => Instrument::Bell,
8 => Instrument::Chime,
9 => Instrument::Xylophone,
10 => Instrument::IronXylophone,
11 => Instrument::CowBell,
12 => Instrument::Didgeridoo,
13 => Instrument::Bit,
14 => Instrument::Banjo,
15 => Instrument::Pling,
_ => Instrument::Custom(id),
}
}
}
pub struct CustomInstruments {
instruments: Vec<CustomInstrumentInfo>,
}
impl CustomInstruments {
pub fn new() -> Self {
CustomInstruments {
instruments: Vec::new(),
}
}
pub fn decode<R>(reader: &mut R) -> Result<CustomInstruments, NbsError>
where
R: crate::ReadStringExt,
{
let instrument_count = reader.read_i8()?;
let mut custom_instruments = CustomInstruments {
instruments: Vec::with_capacity(instrument_count as usize),
};
for id in 0..instrument_count {
let instrument = Instrument::Custom(id + 16);
let name = reader.read_string()?;
let file_name = reader.read_string()?;
let pitch = reader.read_i8()?;
let press_key = reader.read_i8()? == 1;
custom_instruments.instruments.push(CustomInstrumentInfo {
instrument,
name,
file_name,
pitch,
press_key,
})
}
Ok(custom_instruments)
}
pub fn encode<W>(&self, writer: &mut W) -> Result<(), NbsError>
where
W: crate::WriteStringExt,
{
writer.write_i8(self.instruments.len() as i8)?;
for instrument in &self.instruments {
writer.write_string(&instrument.name)?;
writer.write_string(&instrument.file_name)?;
writer.write_i8(instrument.pitch)?;
writer.write_i8(if instrument.press_key { 1 } else { 0 })?;
}
Ok(())
}
}
pub struct CustomInstrumentInfo {
pub instrument: Instrument,
pub name: String,
pub file_name: String,
pub pitch: i8,
pub press_key: bool,
}