Debian Codegraph @

android/app/jni/extern/SAASound/src/SAAAmp.cpp

raw
1 // Part of SAASound copyright 1998-2018 Dave Hooper <[email protected]>
2 //
3 // SAAAmp.cpp: implementation of the CSAAAmp class.
4 // This class handles Tone/Noise mixing, Envelope application and
5 // amplification.
6 //
7 //////////////////////////////////////////////////////////////////////
8
9 #include "SAASound.h"
10 #include "types.h"
11 #include "SAANoise.h"
12 #include "SAAEnv.h"
13 #include "SAAFreq.h"
14 #include "SAAAmp.h"
15 #include "defns.h"
16
17 //////////////////////////////////////////////////////////////////////
18 // Construction/Destruction
19 //////////////////////////////////////////////////////////////////////
20
21 CSAAAmp::CSAAAmp(CSAAFreq * const ToneGenerator, const CSAANoise * const NoiseGenerator, const CSAAEnv * const EnvGenerator)
22 :
23 m_pcConnectedToneGenerator(ToneGenerator),
24 m_pcConnectedNoiseGenerator(NoiseGenerator),
25 m_pcConnectedEnvGenerator(EnvGenerator),
26 m_bUseEnvelope(EnvGenerator != NULL)
27 {
28 leftlevel = 0;
29 leftlevela0x0e = 0;
30 rightlevel = 0;
31 rightlevela0x0e = 0;
32 m_nMixMode = 0;
33 m_bMute=true;
34 m_bSync = false;
35 m_nOutputIntermediate=0;
36 last_level_byte=0;
37 SetAmpLevel(0x00);
38
39 }
40
41 CSAAAmp::~CSAAAmp()
42 {
43 // Nothing to do
44 }
45
46 void CSAAAmp::SetAmpLevel(BYTE level_byte)
47 {
48 // if level unchanged since last call then do nothing
49 if (level_byte != last_level_byte)
50 {
51 last_level_byte = level_byte;
52 leftlevel = level_byte & 0x0f;
53 leftlevela0x0e = leftlevel & 0x0e;
54
55 rightlevel = (level_byte >> 4) & 0x0f;
56 rightlevela0x0e = rightlevel & 0x0e;
57 }
58
59 }
60
61 void CSAAAmp::SetToneMixer(BYTE bEnabled)
62 {
63 if (bEnabled == 0)
64 {
65 // clear mixer bit
66 m_nMixMode &= ~(0x01);
67 }
68 else
69 {
70 // set mixer bit
71 m_nMixMode |= 0x01;
72 }
73 }
74
75 void CSAAAmp::SetNoiseMixer(BYTE bEnabled)
76 {
77 if (bEnabled == 0)
78 {
79 m_nMixMode &= ~(0x02);
80 }
81 else
82 {
83 m_nMixMode |= 0x02;
84 }
85 }
86
87 void CSAAAmp::Mute(bool bMute)
88 {
89 // m_bMute refers to the GLOBAL mute setting (register 28 bit 0)
90 // NOT the per-channel mixer settings !!
91 m_bMute = bMute;
92 }
93
94 void CSAAAmp::Sync(bool bSync)
95 {
96 // m_bSync refers to the GLOBAL sync setting (register 28 bit 1)
97 m_bSync = bSync;
98 }
99
100 void CSAAAmp::Tick(void)
101 {
102 // updates m_nOutputIntermediate to 0, 1 or 2
103 //
104
105 // connected oscillator always ticks (this isn't really connected to the amp)
106 int level = m_pcConnectedToneGenerator->Tick();
107
108 switch (m_nMixMode)
109 {
110 case 0:
111 // no tone or noise for this channel
112 m_nOutputIntermediate = 0;
113 break;
114 case 1:
115 // tone only for this channel
116 m_nOutputIntermediate = level * 2;
117 // NOTE: ConnectedToneGenerator returns either 0 or 1
118 break;
119 case 2:
120 // noise only for this channel
121 m_nOutputIntermediate = m_pcConnectedNoiseGenerator->Level() * 2;
122 // NOTE: ConnectedNoiseGenerator()->Level() returns either 0 or 1
123 break;
124 case 3:
125 // tone+noise for this channel ... mixing algorithm :
126 // tone noise output
127 // 0 0 0
128 // 1 0 2
129 // 0 1 0
130 // 1 1 1
131 // = 2 * tone - 1 * (tone & noise)
132 // = tone * (2 - noise)
133 m_nOutputIntermediate = level * (2 - m_pcConnectedNoiseGenerator->Level());
134 break;
135 }
136 // intermediate is between 0 and 2
137 }
138
139 inline int CSAAAmp::EffectiveAmplitude(int amp, int env) const
140 {
141 // Return the effective amplitude of the low-pass-filtered result of the logical
142 // AND of the amplitude PDM and envelope PDM patterns. This is a more accurate
143 // evaluation of the SAA than simply returning amp * env , based on how the SAA
144 // implements pulse-density modulation.
145 static const int pdm[16][16] = {
146 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
147 {0,0,0,0,2,2,2,2,2,2,2,2,4,4,4,4},
148 {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8},
149 {0,1,1,2,4,5,5,6,6,7,7,8,10,11,11,12},
150 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
151 {0,1,2,3,6,7,8,9,10,11,12,13,16,17,18,19},
152 {0,2,3,5,6,8,9,11,12,14,15,17,18,20,21,23},
153 {0,2,3,5,8,10,11,13,14,16,17,19,22,24,25,27},
154 {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30},
155 {0,2,4,6,10,12,14,16,18,20,22,24,28,30,32,34},
156 {0,3,5,8,10,13,15,18,20,23,25,28,30,33,35,38},
157 {0,3,5,8,12,15,17,20,22,25,27,30,34,37,39,42},
158 {0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45},
159 {0,3,6,9,14,17,20,23,26,29,32,35,40,43,46,49},
160 {0,4,7,11,14,18,21,25,28,32,35,39,42,46,49,53},
161 {0,4,7,11,16,20,23,27,30,34,37,41,46,50,53,57}
162 };
163
164 return(pdm[amp][env] * 4);
165 }
166
167 void CSAAAmp::TickAndOutputStereo(unsigned int & left, unsigned int & right)
168 {
169 // This returns a value between 0 and 480 inclusive.
170 // This represents the full dynamic range of one output mixer (tone, or noise+tone, at full volume,
171 // without envelopes enabled). Note that, with envelopes enabled, the actual dynamic range
172 // is reduced on-chip to just over 88% of this (424), so the "loudest" output requires disabling envs.
173 // NB for 6 channels at full volume, with simple additive mixing, you would see a combined
174 // output of 2880, and a multiplier of 11 (=31680) fits comfortably within 16-bit signed output range.
175
176 if (m_bSync)
177 {
178 // TODO check this
179 left = right = 0;
180 return;
181 }
182
183 // first, do the Tick:
184 Tick();
185
186 // now calculate the returned amplitude for this sample:
187 ////////////////////////////////////////////////////////
188
189 if (m_bMute)
190 {
191 left = right = 0;
192 }
193 else if (m_bUseEnvelope && m_pcConnectedEnvGenerator->IsActive())
194 {
195 left = EffectiveAmplitude(m_pcConnectedEnvGenerator->LeftLevel(), leftlevela0x0e) * (2 - m_nOutputIntermediate);
196 right = EffectiveAmplitude(m_pcConnectedEnvGenerator->RightLevel(), rightlevela0x0e) * (2 - m_nOutputIntermediate);
197 }
198 else
199 {
200 left = leftlevel * m_nOutputIntermediate * 16;
201 right = rightlevel * m_nOutputIntermediate * 16;
202 }
203 }