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
|
use std::collections::{BTreeMap, BTreeSet};
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
use crate::itfile;
use crate::utils;
use crate::midifile;
struct PlayerState
{
skip_row: u16,
skip_ord: u8,
current_ord: u8,
current_row: u16,
in_loop: bool
}
struct Player<'a>
{
h: Box<RefCell<dyn CellHandler>>,
it: &'a itfile::ITFile
}
pub struct Converter<'a>
{
instch: Option<BTreeMap<(u8, u8), usize>>,
it: &'a itfile::ITFile
}
trait CellHandler
{
fn process(&mut self, ch: u8, cell: itfile::Cell, ps: PlayerState) -> PlayerState;
fn result_data(&mut self) -> Box<dyn Any>;
}
trait EffectHandler
{
fn process(&mut self, ch: u8, cell: itfile::Cell, chmem: &mut ChannelMemory) -> Vec<midifile::TimedMidiEvent>;
fn handled_effects(&self) -> Vec<u8>;
}
struct PrePassCellHandler
{
instchmap: Option<BTreeSet<(u8, u8)>>,
chinst: [u8; 64]
}
#[derive(Default)]
struct ChannelMemory
{
note: u8,
vol: u8,
efxmem: [u8; 32],
pitch: utils::Rational
}
struct ConvertCellHandler<'a>
{
chmem: [ChannelMemory; 64],
fx_handlers: [Rc<RefCell<dyn EffectHandler + 'a>>; 32]
}
impl<'a> Player<'a>
{
fn new(handler: Box<RefCell<dyn CellHandler>>, it: &'a itfile::ITFile) -> Player<'a>
{
Player
{
h: handler,
it
}
}
fn process_pattern(&self, pat: usize, st: PlayerState) -> PlayerState
{
let skip_row = if !st.skip_row == 0 { 0 } else { st.skip_row };
let mut ret = PlayerState {
skip_row: !0,
skip_ord: !0,
current_ord: 0,
current_row: 0,
in_loop: false
};
for r in skip_row..self.it.patterns[pat].nrows
{
ret.current_row = r;
for c in 0..64
{
let cell = *self.it.patterns[pat].cell_at(r, c);
ret = (&self.h).borrow_mut().process(c as u8, cell, ret);
}
ret = (&self.h).borrow_mut().process(!0, itfile::Cell::default(), ret);
if (!ret.skip_row) != 0 || (!ret.skip_ord) != 0 { return ret; }
}
ret
}
/// Used for effects Bxx, Cxx and SBx
///
/// passing !0 to row or ord if it's unused
///
/// in_loop == true inhibits loop detection and should only be used for SBx
fn skip_to(&self, row: u16, ord: u8, in_loop: bool, ps: PlayerState) -> PlayerState
{
PlayerState {
skip_row: if !row != 0 { row } else { ps.skip_row },
skip_ord: if !ord != 0 { ord } else { ps.skip_ord },
in_loop,
..ps
}
}
fn process_orders(&self)
{
let mut ps = PlayerState {
skip_row: !0,
skip_ord: !0,
current_ord: 0,
current_row: 0,
in_loop: false
};
let mut oid = 0;
loop
{
if oid >= self.it.orders.len() { break; }
if self.it.orders[oid] == 0xff { break; }
if self.it.orders[oid] == 0xfe { continue; }
ps.current_ord = oid as u8;
ps = self.process_pattern(self.it.orders[oid].into(), ps);
if !ps.skip_ord != 0
{
if ps.skip_ord as usize <= oid && !ps.in_loop
{ println!("loop?"); }
else { oid = ps.skip_ord as usize; }
ps.skip_ord = !0;
}
else { oid += 1; }
}
}
}
impl CellHandler for PrePassCellHandler
{
fn process(&mut self, ch: u8, cell: itfile::Cell, ps: PlayerState) -> PlayerState
{
if ch == 0xff { return ps; }
let itfile::Cell{mask, mut inst, ..} = cell;
if mask & 0x22 != 0
{ self.chinst[ch as usize] = inst; }
else
{ inst = self.chinst[ch as usize]; }
if mask & 0x11 != 0
{ self.instchmap.as_mut().unwrap().insert((ch, inst)); }
ps
}
fn result_data(&mut self) -> Box<dyn Any>
{ Box::new(self.instchmap.take().unwrap()) }
}
impl<'a> ConvertCellHandler<'a>
{
fn register_fx_handler(&mut self, hndlr: Rc<RefCell<dyn EffectHandler + 'a>>)
{
let efxs = hndlr.borrow().handled_effects();
efxs.iter()
.filter(|e| **e < 32)
.for_each(|e| self.fx_handlers[*e as usize] = Rc::clone(&hndlr));
}
}
impl<'a> CellHandler for ConvertCellHandler<'a>
{
fn process(&mut self, ch: u8, cell: itfile::Cell, ps: PlayerState) -> PlayerState
{
let itfile::Cell{mask, note, inst, vol, efx, fxp} = cell;
ps
}
fn result_data(&mut self) -> Box<dyn Any>
{ Box::new(()) }
}
impl<'a> Converter<'a>
{
pub fn new(it: &itfile::ITFile) -> Converter
{
Converter{instch: None, it}
}
fn pre_pass(&'a mut self)
{
let h = PrePassCellHandler{instchmap: Some(BTreeSet::new()), chinst: [0; 64]};
let p = Player::new(Box::new(RefCell::new(h)), self.it);
p.process_orders();
let Player{h, ..} = p;
if let Ok(m) = h.borrow_mut().result_data().downcast::<BTreeSet<(u8, u8)>>()
{
let mut instch = BTreeMap::new();
m.iter().enumerate().for_each(
|(i, p)| { instch.insert(*p, i + 1); } );
self.instch = Some(instch);
}
println!("{:?}", self.instch);
}
pub fn convert(&'a mut self)
{
self.pre_pass();
}
}
|