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
|
use std::fmt;
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;
fn map_pitch(pitch: u8) -> f32 {
const HALF_STEP: f32 = 1.059_463_1;
HALF_STEP.powf(pitch as f32 - 128.0)
}
const fn map_volume(volume: u8) -> f32 {
if volume > 100 {
1.0
} else {
(volume as f32) / 100.0
}
}
#[derive(Debug, Clone)]
pub struct Program {
ins: Vec<Instruction>,
pc: usize,
pause_cnt: usize,
pause_len: usize,
looping: bool,
playing: bool,
}
impl Program {
pub const fn new(ins: Vec<Instruction>, looping: bool) -> Self {
Self {
ins,
pc: 0,
pause_cnt: 0,
pause_len: 4,
looping,
playing: false,
}
}
pub fn parse(src: &str, looping: bool) -> parse::Result<Self> {
let ins = parse::parse(src)?;
Ok(Self::new(ins, looping))
}
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::new(res, self.looping)
}
fn exec_ins(&mut self, channels: &mut Channels, 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;
let v = map_volume(volume);
match chan_spec {
C::PulseA => channels.pulse_a.volume = v,
C::PulseB => channels.pulse_b.volume = v,
C::Triangle => channels.triangle.volume = v,
C::Noise => channels.noise.volume = v,
}
}
I::SetPitch(chan_spec, pitch) => {
// set the pitch (freq) on a given channel
use ChanSpec as C;
let p = map_pitch(pitch);
match chan_spec {
C::PulseA => channels.pulse_a.pitch = p,
C::PulseB => channels.pulse_b.pitch = p,
C::Triangle => channels.triangle.pitch = p,
C::Noise => channels.noise.pitch = p,
}
}
I::SetPulseDutyA(dc) => {
if let ChannelKind::Pulse { duty_cycle } = &mut channels.pulse_a.kind {
*duty_cycle = dc;
}
}
I::SetPulseDutyB(dc) => {
if let ChannelKind::Pulse { duty_cycle } = &mut channels.pulse_b.kind {
*duty_cycle = dc;
}
}
I::SetNoiseMode(m) => {
if let ChannelKind::Noise { mode, .. } = &mut channels.noise.kind {
*mode = m;
}
}
}
}
pub fn exec(&mut self, channels: &mut Channels) {
if !self.playing {
return;
}
if self.pause_cnt > 0 {
self.pause_cnt -= 1;
}
loop {
if self.pause_cnt > 0 {
break;
}
if self.is_finished() {
if self.looping {
self.pc = 0;
} else {
self.playing = false;
break;
}
}
let ins = self.ins[self.pc];
self.exec_ins(channels, ins);
self.pc += 1;
}
}
pub const fn is_finished(&self) -> bool {
self.pc >= self.ins.len()
}
pub const fn is_playing(&self) -> bool {
self.playing
}
pub const fn play(&mut self) {
self.playing = true;
self.pc = 0;
}
pub const fn set_looping(&mut self, looping: bool) {
self.looping = looping;
}
}
impl fmt::Display for Program {
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(())
}
}
|