-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChannel.cpp
More file actions
executable file
·201 lines (187 loc) · 3.86 KB
/
Channel.cpp
File metadata and controls
executable file
·201 lines (187 loc) · 3.86 KB
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
#include "Channel.h"
//constructor
Channel::Channel(int type, int index)
{
this->type = type;
this->index = index;
note = 0;
vol = 0;
envelope = 0xFF;
lastHigh = 0xFF;
dutyCycle = 0;
changed = false;
decaying = false;
decayTick = 0;
sweepNote = 0;
sweepTarget = 0;
sweepFrom = 0;
sweepTo = 0;
sweepTick = 0;
pitchBend = 64;
}
//Called once every main loop to adjust various attributes of the channel.
void Channel::update()
{
if (sweepTo != 0) //sweep
{
float percent = (float)sweepTick / sweepPeriod;
sweepNote = sweepFrom + floor(percent * (sweepTo - sweepFrom));
changed = true;
sweepTick++;
if (sweepTick == sweepPeriod) //sweep is complete
{
note = sweepTarget; //lock target
sweepNote = 0;
sweepTarget = 0;
sweepFrom = 0;
sweepTo = 0;
sweepTick = 0;
}
}
if (decaying) //decrease decaying volume
{
if (vol > 0x00)
{
decayTick++;
if (decayTick == decayPeriod)
{
vol--;
decayTick = 0;
changed = true;
}
}
if (vol == 0x00) //note has fully decayed, silence channel completely
{
note = 0;
decaying = false;
}
}
}
//Constructs a packet of addresses and data to be sent to the APU.
void Channel::setPacket()
{
//clear packet
for (int i = 0; i < 6; i++)
packet[i] = 0xFF;
int period = getPeriod(note);
byte finalVol = floor((envelope / (float)255) * vol);
if (sweepNote != 0) //replace with interpolated period if a sweep is occurring
period = sweepNote;
if (type == 0) //triangle wave
{
packet[0] = 0x08;
packet[1] = 0x81;
packet[2] = 0x0A;
packet[3] = period;
packet[4] = 0x0B;
packet[5] = period>>8;
if (finalVol == 0) //if channel muted
packet[1] = 0x80; //mute it
}
else if (type == 1) //pulse wave
{
packet[0] = 0x03;
packet[1] = period>>8;
packet[2] = 0x02;
packet[3] = period;
packet[4] = 0x00;
packet[5] = (dutyCycle<<6)|finalVol|0x30;
if (index == 1) //second pulse wave
{
packet[0] = 0x07;
packet[2] = 0x06;
packet[4] = 0x04;
}
if (!dirtyPulse && period>>8 == lastHigh) //high freq byte has not changed, do not resend
{
packet[0] = 0xFF;
packet[1] = 0xFF;
}
lastHigh = period>>8;
}
else if (type == 2) //noise wave
{
packet[0] = 0x0F;
if (finalVol > 0) //if note on
packet[1] = 0xFF;
else
packet[1] = 0x00;
packet[2] = 0x0C;
packet[3] = finalVol | 0x30;
packet[4] = 0x0E;
packet[5] = 0x0F - (note & 0x0F);
packet[5] |= tonalNoise << 7;
}
else if (type == 3) //DMC
{
packet[0] = 0x11;
packet[1] = period;
}
changed = false;
}
int Channel::getPeriod(int note)
{
int index = note;
if (type == 0)
index = min(127, note + 12);
if (pitchBend == 64)
return noteTable[index];
//calculate bent period
int var = pitchBend - 64;
int index2;
if (var < 0)
index2 = max(0, index - bendRange);
else
index2 = min(127, index + bendRange);
var = abs(var);
float percent = (float)var / 64;
return noteTable[index] + floor(percent * (noteTable[index2] - noteTable[index]));
}
void Channel::clear()
{
note = 0;
vol = 0;
sweepTick = 0;
changed = true;
}
//static variable accessors/mutators
boolean Channel::getDirtyPulse()
{
return dirtyPulse;
}
void Channel::setDirtyPulse(boolean val)
{
dirtyPulse = val;
}
boolean Channel::getTonalNoise()
{
return tonalNoise;
}
void Channel::setTonalNoise(boolean val)
{
tonalNoise = val;
}
int Channel::getDecayPeriod()
{
return decayPeriod;
}
void Channel::setDecayPeriod(int val)
{
decayPeriod = val;
}
int Channel::getSweepPeriod()
{
return sweepPeriod;
}
void Channel::setSweepPeriod(int val)
{
sweepPeriod = val;
}
int Channel::getBendRange()
{
return bendRange;
}
void Channel::setBendRange(int val)
{
bendRange = val;
}