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
|
use crate::audio::{
Channels,
channel::{Channel, DutyCycle},
parse,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChanSpec {
PulseA,
PulseB,
Triangle,
Noise,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Instruction {
Pause,
PauseLen(u32),
Jump(usize),
SetVolume(ChanSpec, u8),
SetPitch(ChanSpec, u8),
SetPulseDutyA(DutyCycle),
SetPulseDutyB(DutyCycle),
SetNoiseMode(bool),
}
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,
looping: bool,
pause_cnt: u32,
pause_len: u32,
}
impl Program {
pub const fn new(ins: Vec<Instruction>, looping: bool) -> Self {
let pc = ins.len();
Self {
ins,
pc,
looping,
pause_cnt: 0,
pause_len: 4,
}
}
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 l_ins = self.ins;
let r_ins = other.ins;
let mut l = 0;
let mut r = 0;
loop {
if r >= r_ins.len() {
break;
}
let ins = r_ins[r];
if ins == Instruction::Pause {
// inc l until even
loop {
if l == l_ins.len() {
l_ins.push(Instruction::Pause);
break;
}
if l_ins[l] == Instruction::Pause {
l += 1;
break;
}
l += 1;
}
} else {
l_ins.insert(l, ins);
}
r += 1;
}
Self::new(l_ins, self.looping)
}
fn exec_ins(&mut self, channels: &mut Channels, ins: Instruction) {
use Instruction as I;
match ins {
I::Pause => {
self.pause_cnt = self.pause_len;
}
I::PauseLen(pause_len) => {
self.pause_len = pause_len;
}
I::Jump(pc) => {
self.pc = pc;
}
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.set_volume(v),
C::PulseB => channels.pulse_b.set_volume(v),
C::Triangle => channels.triangle.set_volume(v),
C::Noise => channels.noise.set_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.set_pitch(p),
C::PulseB => channels.pulse_b.set_pitch(p),
C::Triangle => channels.triangle.set_pitch(p),
C::Noise => channels.noise.set_pitch(p),
}
}
I::SetPulseDutyA(duty_cycle) => {
channels.pulse_a.set_duty(duty_cycle);
}
I::SetPulseDutyB(duty_cycle) => {
channels.pulse_b.set_duty(duty_cycle);
}
I::SetNoiseMode(mode) => {
channels.noise.set_mode(mode);
}
}
}
pub fn exec(&mut self, channels: &mut Channels) {
if self.pause_cnt > 0 {
self.pause_cnt -= 1;
}
loop {
if self.pause_cnt > 0 {
break;
}
if self.finished() {
if self.looping {
self.pc = 0;
} else {
break;
}
}
let ins = self.ins[self.pc];
self.exec_ins(channels, ins);
self.pc += 1;
}
}
pub const fn finished(&self) -> bool {
self.pc >= self.ins.len()
}
pub const fn play(&mut self) {
self.pc = 0;
}
pub const fn set_looping(&mut self, looping: bool) {
self.looping = looping;
}
}
|