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
|
use std::{fmt, rc::Rc};
use crate::{
channel::{ChannelKind, Channels},
parse,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChanSpec {
PulseA,
PulseB,
Triangle,
Noise,
}
impl fmt::Display for ChanSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PulseA => f.write_str("pulsea"),
Self::PulseB => f.write_str("pulseb"),
Self::Triangle => f.write_str("triangle"),
Self::Noise => f.write_str("noise"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Instruction {
Pause(u16),
PauseLen(u16),
Jump(u16),
SetVolume(ChanSpec, u8),
SetPitch(ChanSpec, u8),
SetPulseDutyA(u8),
SetPulseDutyB(u8),
SetNoiseMode(bool),
}
impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pause(len) => write!(f, "pause {len}"),
Self::PauseLen(len) => write!(f, "pause_len {len}"),
Self::Jump(pc) => write!(f, "jump {pc}"),
Self::SetVolume(chan, v) => write!(f, "{chan} volume {v}"),
Self::SetPitch(chan, p) => write!(f, "{chan} pitch {p}"),
Self::SetPulseDutyA(dc) => write!(f, "{} duty_cycle {dc}", ChanSpec::PulseA),
Self::SetPulseDutyB(dc) => write!(f, "{} duty_cycle {dc}", ChanSpec::PulseB),
Self::SetNoiseMode(mode) => write!(f, "{} mode {mode}", ChanSpec::Noise),
}
}
}
use Instruction as I;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Track {
pub ins: Vec<Instruction>,
}
impl Track {
pub fn parse(src: &str) -> parse::Result<Self> {
let ins = parse::parse(src)?;
Ok(Self { ins })
}
#[must_use]
pub fn merge(&self, other: &Self) -> Self {
let mut res = vec![];
let mut l = 0;
let mut r = 0;
let mut l_pause = 0;
let mut r_pause = 0;
let l_ins = &self.ins;
let r_ins = &other.ins;
loop {
if l >= l_ins.len() && r >= r_ins.len() {
// were done here
break;
}
while l < l_ins.len() {
if l_pause > 0 {
break;
}
let ins = l_ins[l];
l += 1;
if let I::Pause(cnt) = ins {
l_pause = cnt;
break;
}
res.push(ins);
}
while r < r_ins.len() {
if r_pause > 0 {
break;
}
let ins = r_ins[r];
r += 1;
if let I::Pause(cnt) = ins {
r_pause = cnt;
break;
}
res.push(ins);
}
if l_pause > 0 {
if r_pause > 0 && l_pause >= r_pause {
l_pause -= r_pause;
res.push(I::Pause(r_pause));
r_pause = 0;
} else if r_pause == 0 {
res.push(I::Pause(l_pause));
l_pause = 0;
}
}
if r_pause > 0 {
if l_pause > 0 && r_pause >= l_pause {
r_pause -= l_pause;
res.push(I::Pause(l_pause));
l_pause = 0;
} else if l_pause == 0 {
res.push(I::Pause(r_pause));
r_pause = 0;
}
}
}
Self { ins: res }
}
}
impl fmt::Display for Track {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (idx, ins) in self.ins.iter().enumerate() {
if idx != 0 {
writeln!(f)?;
}
write!(f, "{ins}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Program {
track: Rc<Track>,
priority: u32,
looping: bool,
pc: usize,
pause_cnt: usize,
pause_len: usize,
channels: Channels,
}
impl Program {
pub const fn new(track: Rc<Track>, priority: u32, looping: bool) -> Self {
Self {
track,
priority,
looping,
pc: 0,
pause_cnt: 0,
pause_len: 4,
channels: Channels::new(),
}
}
const fn exec_ins(&mut self, ins: Instruction) {
use Instruction as I;
match ins {
I::Pause(cnt) => {
self.pause_cnt = self.pause_len * (cnt as usize);
}
I::PauseLen(pause_len) => {
self.pause_len = pause_len as usize;
}
I::Jump(pc) => {
self.pc = pc as usize;
}
I::SetVolume(chan_spec, volume) => {
// set the volume (amplitude) on a given channel
use ChanSpec as C;
match chan_spec {
C::PulseA => self.channels.pulse_a.volume = volume,
C::PulseB => self.channels.pulse_b.volume = volume,
C::Triangle => self.channels.triangle.volume = volume,
C::Noise => self.channels.noise.volume = volume,
}
}
I::SetPitch(chan_spec, pitch) => {
// set the pitch (freq) on a given channel
use ChanSpec as C;
match chan_spec {
C::PulseA => self.channels.pulse_a.pitch = pitch,
C::PulseB => self.channels.pulse_b.pitch = pitch,
C::Triangle => self.channels.triangle.pitch = pitch,
C::Noise => self.channels.noise.pitch = pitch,
}
}
I::SetPulseDutyA(dc) => {
if let ChannelKind::Pulse { duty_cycle } = &mut self.channels.pulse_a.kind {
*duty_cycle = dc;
}
}
I::SetPulseDutyB(dc) => {
if let ChannelKind::Pulse { duty_cycle } = &mut self.channels.pulse_b.kind {
*duty_cycle = dc;
}
}
I::SetNoiseMode(m) => {
if let ChannelKind::Noise { mode, .. } = &mut self.channels.noise.kind {
*mode = m;
}
}
}
}
pub fn exec(&mut self) -> Channels {
if self.pause_cnt > 0 {
self.pause_cnt -= 1;
}
loop {
if self.is_finished() {
if self.looping {
self.pc = 0;
} else {
break;
}
}
if self.pause_cnt > 0 {
break;
}
let ins = self.track.ins[self.pc];
self.exec_ins(ins);
self.pc += 1;
}
self.channels
}
pub fn is_finished(&self) -> bool {
self.pc >= self.track.ins.len()
}
}
impl PartialOrd for Program {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Program {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.priority.cmp(&other.priority)
}
}
|