Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libgambatte/src/sound/envelope_unit.cpp
2 views
1
/***************************************************************************
2
* Copyright (C) 2007 by Sindre Aamås *
3
* [email protected] *
4
* *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License version 2 as *
7
* published by the Free Software Foundation. *
8
* *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License version 2 for more details. *
13
* *
14
* You should have received a copy of the GNU General Public License *
15
* version 2 along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
19
#include "envelope_unit.h"
20
#include <algorithm>
21
22
namespace gambatte {
23
24
EnvelopeUnit::VolOnOffEvent EnvelopeUnit::nullEvent;
25
26
void EnvelopeUnit::event() {
27
const unsigned long period = nr2 & 7;
28
29
if (period) {
30
unsigned newVol = volume;
31
32
if (nr2 & 8)
33
++newVol;
34
else
35
--newVol;
36
37
if (newVol < 0x10U) {
38
volume = newVol;
39
40
if (volume < 2)
41
volOnOffEvent(counter);
42
43
counter += period << 15;
44
} else
45
counter = COUNTER_DISABLED;
46
} else
47
counter += 8ul << 15;
48
}
49
50
bool EnvelopeUnit::nr2Change(const unsigned newNr2) {
51
if (!(nr2 & 7) && counter != COUNTER_DISABLED)
52
++volume;
53
else if (!(nr2 & 8))
54
volume += 2;
55
56
if ((nr2 ^ newNr2) & 8)
57
volume = 0x10 - volume;
58
59
volume &= 0xF;
60
61
nr2 = newNr2;
62
63
return !(newNr2 & 0xF8);
64
}
65
66
bool EnvelopeUnit::nr4Init(const unsigned long cc) {
67
{
68
unsigned long period = nr2 & 7;
69
70
if (!period)
71
period = 8;
72
73
if (!(cc & 0x7000))
74
++period;
75
76
counter = cc - ((cc - 0x1000) & 0x7FFF) + period * 0x8000;
77
}
78
79
volume = nr2 >> 4;
80
81
return !(nr2 & 0xF8);
82
}
83
84
EnvelopeUnit::EnvelopeUnit(VolOnOffEvent &volOnOffEvent)
85
: volOnOffEvent(volOnOffEvent),
86
nr2(0),
87
volume(0)
88
{
89
}
90
91
void EnvelopeUnit::reset() {
92
counter = COUNTER_DISABLED;
93
}
94
95
void EnvelopeUnit::loadState(const SaveState::SPU::Env &estate, const unsigned nr2, const unsigned long cc) {
96
counter = std::max(estate.counter, cc);
97
volume = estate.volume;
98
this->nr2 = nr2;
99
}
100
101
SYNCFUNC(EnvelopeUnit)
102
{
103
NSS(counter);
104
NSS(nr2);
105
NSS(volume);
106
}
107
108
}
109
110