| 1 |
// Part of SAASound copyright 1998-2018 Dave Hooper <[email protected]> |
| 2 |
// |
| 3 |
// SAAEnv.cpp: implementation of the CSAAEnv class. |
| 4 |
// |
| 5 |
////////////////////////////////////////////////////////////////////// |
| 6 |
|
| 7 |
#include "SAASound.h" |
| 8 |
#include "types.h" |
| 9 |
#include "SAAEnv.h" |
| 10 |
|
| 11 |
|
| 12 |
////////////////////////////////////////////////////////////////////// |
| 13 |
// Static member initialisation |
| 14 |
////////////////////////////////////////////////////////////////////// |
| 15 |
|
| 16 |
const ENVDATA CSAAEnv::cs_EnvData[8] = |
| 17 |
{ |
| 18 |
{1,false, { {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, |
| 19 |
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}}, |
| 20 |
{1,true, { {{15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15},{15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15}}, |
| 21 |
{{14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14},{14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14}}}}, |
| 22 |
{1,false, { {{15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, |
| 23 |
{{14,14,12,12,10,10,8,8,6,6,4,4,2,2,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}}, |
| 24 |
{1,true, { {{15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, |
| 25 |
{{14,14,12,12,10,10,8,8,6,6,4,4,2,2,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}}, |
| 26 |
{2,false, { {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}}, |
| 27 |
{{0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14}, {14,14,12,12,10,10,8,8,6,6,4,4,2,2,0,0}}}}, |
| 28 |
{2,true, { {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}}, |
| 29 |
{{0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14}, {14,14,12,12,10,10,8,8,6,6,4,4,2,2,0,0}}}}, |
| 30 |
{1,false, { {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, |
| 31 |
{{0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}}, |
| 32 |
{1,true, { {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, |
| 33 |
{{0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}} |
| 34 |
}; |
| 35 |
|
| 36 |
////////////////////////////////////////////////////////////////////// |
| 37 |
// Construction/Destruction |
| 38 |
////////////////////////////////////////////////////////////////////// |
| 39 |
|
| 40 |
CSAAEnv::CSAAEnv() |
| 41 |
: |
| 42 |
m_bEnabled(false), |
| 43 |
m_nPhase(0), |
| 44 |
m_nPhasePosition(0), |
| 45 |
m_bEnvelopeEnded(true), |
| 46 |
m_nResolution(1), |
| 47 |
m_bNewData(false), |
| 48 |
m_nNextData(0) |
| 49 |
{ |
| 50 |
// initialise itself with the value 'zero' |
| 51 |
SetEnvControl(0); |
| 52 |
} |
| 53 |
|
| 54 |
CSAAEnv::~CSAAEnv() |
| 55 |
{ |
| 56 |
// Nothing to do |
| 57 |
} |
| 58 |
|
| 59 |
void CSAAEnv::InternalClock(void) |
| 60 |
{ |
| 61 |
// will only do something if envelope clock mode is set to internal |
| 62 |
// and the env control is enabled |
| 63 |
if (m_bEnabled && (!m_bClockExternally)) Tick(); |
| 64 |
} |
| 65 |
|
| 66 |
void CSAAEnv::ExternalClock(void) |
| 67 |
{ |
| 68 |
// will only do something if envelope clock mode is set to external |
| 69 |
// and the env control is enabled |
| 70 |
if (m_bClockExternally && m_bEnabled) Tick(); |
| 71 |
} |
| 72 |
|
| 73 |
void CSAAEnv::SetEnvControl(int nData) |
| 74 |
{ |
| 75 |
// process immediate stuff first: |
| 76 |
// start with the Enabled flag. if env is disabled, |
| 77 |
// there's not much to do |
| 78 |
bool bEnabled = ((nData & 0x80)==0x80); |
| 79 |
if (!bEnabled && !m_bEnabled) |
| 80 |
return; |
| 81 |
m_bEnabled = bEnabled; |
| 82 |
if (!m_bEnabled) |
| 83 |
{ |
| 84 |
// env control was enabled, and now disabled |
| 85 |
// Any subsequent env control changes are immediate. |
| 86 |
m_bEnvelopeEnded = true; |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Resolution (3bit/4bit) is also immediately processed |
| 91 |
int new_resolution = ((nData & 0x10) == 0x10) ? 2 : 1; |
| 92 |
// NOTE: undocumented behaviour when changing resolution mid-waveform |
| 93 |
// Empirically, the following matches observations: |
| 94 |
// * When ticking the env generator with 4-bit resolution, the position += 1 |
| 95 |
// * When ticking the env generator with 3-bit resolution, the position += 2 |
| 96 |
// * When changing between 4-bit resolution and 3-bit resolution |
| 97 |
// without ticking the env generator, the position is unchanged |
| 98 |
// (although, effectively, the LSB is ignored. Purely as an implementation |
| 99 |
// detail, I'm implementing this as clearing the LSB ie LSB=0; see next point) |
| 100 |
// * When changing between 3-bit resolution and 4-bit resolution |
| 101 |
// without ticking the env generator, the position LSB is set to 1 |
| 102 |
// See test case: envext_34b |
| 103 |
// |
| 104 |
if (m_nResolution == 1 && new_resolution == 2) |
| 105 |
{ |
| 106 |
// change from 4-bit to 3-bit |
| 107 |
m_nPhasePosition &= 0xe; |
| 108 |
} |
| 109 |
else if (m_nResolution == 2 && new_resolution == 1) |
| 110 |
{ |
| 111 |
// change from 3-bit to 4-bit |
| 112 |
m_nPhasePosition |= 0x1; |
| 113 |
} |
| 114 |
m_nResolution = new_resolution; |
| 115 |
|
| 116 |
// now buffered stuff: but only if it's ok to, and only if the |
| 117 |
// envgenerator is not disabled. otherwise it just stays buffered until |
| 118 |
// the Tick() function sets m_bEnvelopeEnded to true and realises there is |
| 119 |
// already some new data waiting |
| 120 |
if (m_bEnvelopeEnded) |
| 121 |
{ |
| 122 |
SetNewEnvData(nData); // also does the SetLevels() call for us. |
| 123 |
m_bNewData=false; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
// since the 'next resolution' changes arrive unbuffered, we |
| 128 |
// may need to change the current level because of this: |
| 129 |
SetLevels(); |
| 130 |
|
| 131 |
// store current new data, and set the newdata flag: |
| 132 |
m_bNewData = true; |
| 133 |
m_nNextData = nData; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
int CSAAEnv::LeftLevel(void) const |
| 139 |
{ |
| 140 |
return m_nLeftLevel; |
| 141 |
} |
| 142 |
|
| 143 |
int CSAAEnv::RightLevel(void) const |
| 144 |
{ |
| 145 |
return m_nRightLevel; |
| 146 |
} |
| 147 |
|
| 148 |
inline void CSAAEnv::Tick(void) |
| 149 |
{ |
| 150 |
// if disabled, do nothing |
| 151 |
if (!m_bEnabled) // m_bEnabled is set directly, not buffered, so this is ok |
| 152 |
{ |
| 153 |
// for sanity, reset stuff: |
| 154 |
m_bEnvelopeEnded = true; |
| 155 |
m_nPhase = 0; |
| 156 |
m_nPhasePosition = 0; |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// else : m_bEnabled |
| 161 |
|
| 162 |
|
| 163 |
if (m_bEnvelopeEnded) |
| 164 |
{ |
| 165 |
// do nothing |
| 166 |
// (specifically, don't change the values of m_bEnvelopeEnded, |
| 167 |
// m_nPhase and m_nPhasePosition, as these will still be needed |
| 168 |
// by SetLevels() should it be called again) |
| 169 |
|
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
// else : !m_bEnvelopeEnded |
| 175 |
// Continue playing the same envelope ... |
| 176 |
// increments the phaseposition within an envelope. |
| 177 |
// also handles looping and resolution appropriately. |
| 178 |
// Changes the level of the envelope accordingly |
| 179 |
// through calling SetLevels() . This must be called after making |
| 180 |
// any changes that will affect the output levels of the env controller!! |
| 181 |
// SetLevels also handles left-right channel inverting |
| 182 |
|
| 183 |
// increment phase position |
| 184 |
m_nPhasePosition += m_nResolution; |
| 185 |
|
| 186 |
// if this means we've gone past 16 (the end of a phase) |
| 187 |
// then change phase, and if necessary, loop |
| 188 |
// Refer to datasheet for meanings of (3) and (4) in following text |
| 189 |
// w.r.t SAA1099 envelopes |
| 190 |
|
| 191 |
// Note that we will always reach position (3) or (4), even if we keep toggling |
| 192 |
// resolution from 4-bit to 3-bit and back, because the counter will always wrap to 0. |
| 193 |
// In fact it's quite elegant: |
| 194 |
// No matter how you increment and toggle and increment and toggle, the counter |
| 195 |
// will at some point be either 0xe (either 4-bit mode or 3-bit mode) or 0xf (4-bit mode only). |
| 196 |
// Depending on the mode, even if you change the mode, the next increment, |
| 197 |
// or the one after it, will then take it to 0. |
| 198 |
// 0xe + 2 (3bit mode) => 0x0 |
| 199 |
// 0xe + 1 (4bit mode) => 0xf |
| 200 |
// 0xf + 1 (4bit mode) => 0x0 |
| 201 |
// 0xe -> (toggle 3bit mode to 4bit mode) => 0xf |
| 202 |
// 0xe -> (toggle 4bit mode to 3bit mode) => 0xe |
| 203 |
// 0xf -> (toggle 4bit mode to 3bit mode) => 0xe |
| 204 |
// |
| 205 |
// but there is a subtlety (of course), which is that any changes at point (3) |
| 206 |
// can take place immediately you hit point (3), but changes at point (4) are actually |
| 207 |
// only acted upon when the counter transitions from 0xe (or 0xf) to 0x0 (which also |
| 208 |
// means that, for these looping envelopes, which are the ones that have a point(4), |
| 209 |
// immediately after the counter wrapping to 0x0, a write to the env data register will |
| 210 |
// NOT set the waveform and will NOT reset the phase/phaseposition (even though it |
| 211 |
// will still let you toggle the 4bit/3bit mode, which will change the phaseposition LSB!) |
| 212 |
// See test case: envext_34c |
| 213 |
|
| 214 |
bool bProcessNewDataIfAvailable = false; |
| 215 |
if (m_nPhasePosition >= 16) |
| 216 |
{ |
| 217 |
m_nPhase++; |
| 218 |
|
| 219 |
// if we should loop, then do so - and we've reached position (4) |
| 220 |
// otherwise, if we shouldn't loop, |
| 221 |
// then we've reached position (3) and so we say that |
| 222 |
// we're ok for new data. |
| 223 |
if (m_nPhase == m_nNumberOfPhases) |
| 224 |
{ |
| 225 |
// at position (3) or (4) |
| 226 |
if (!m_bLooping) |
| 227 |
{ |
| 228 |
// position (3) only |
| 229 |
// note that it seems that the sustain level is ALWAYS zero |
| 230 |
// in the case of non-looping waveforms |
| 231 |
m_bEnvelopeEnded = true; |
| 232 |
bProcessNewDataIfAvailable = true; |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
// position (4) only |
| 237 |
// note that any data already latched is ONLY acted upon |
| 238 |
// at THIS point. If (after this Tick has completed) any new |
| 239 |
// env data is written, it will NOT be acted upon, until |
| 240 |
// we get back to position (4) again. |
| 241 |
// this is why m_bEnvelopeEnded (which affects the behaviour |
| 242 |
// of the SetEnvControl method) is FALSE here. |
| 243 |
// See test case: envext_34c (as noted earlier) |
| 244 |
m_bEnvelopeEnded = false; |
| 245 |
// set phase pointer to start of envelope for loop |
| 246 |
// and reset m_nPhasePosition |
| 247 |
m_nPhase=0; |
| 248 |
m_nPhasePosition -= 16; |
| 249 |
bProcessNewDataIfAvailable = true; |
| 250 |
} |
| 251 |
} |
| 252 |
else // (m_nPhase < m_nNumberOfPhases) |
| 253 |
{ |
| 254 |
// not at position (3) or (4) ... |
| 255 |
// (i.e., we're in the middle of an envelope with |
| 256 |
// more than one phase. Specifically, we're in |
| 257 |
// the middle of envelope 4 or 5 - the |
| 258 |
// triangle envelopes - but that's not important) |
| 259 |
|
| 260 |
// any commands sent to this envelope controller |
| 261 |
// will be buffered. Set the flag to indicate this. |
| 262 |
m_bEnvelopeEnded = false; |
| 263 |
m_nPhasePosition -= 16; |
| 264 |
} |
| 265 |
} |
| 266 |
else // (m_nPhasePosition < 16) |
| 267 |
{ |
| 268 |
// still within the same phase; |
| 269 |
// but, importantly, we are no inter at the start of the phase ... |
| 270 |
// so new data cannot be acted on immediately, and must |
| 271 |
// be buffered |
| 272 |
m_bEnvelopeEnded = false; |
| 273 |
// Phase and PhasePosition have already been updated. |
| 274 |
// SetLevels() will need to be called to actually calculate |
| 275 |
// the output 'level' of this envelope controller |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
// if we have new (buffered) data, now is the time to act on it |
| 280 |
if (m_bNewData && bProcessNewDataIfAvailable) |
| 281 |
{ |
| 282 |
m_bNewData = false; |
| 283 |
SetNewEnvData(m_nNextData); |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
// ok, we didn't have any new buffered date to act on, |
| 288 |
// so we just call SetLevels() to calculate the output level |
| 289 |
// for whatever the current envelope is |
| 290 |
SetLevels(); |
| 291 |
} |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
inline void CSAAEnv::SetLevels(void) |
| 296 |
{ |
| 297 |
// sets m_nLeftLevel |
| 298 |
// Also sets m_nRightLevel in terms of m_nLeftLevel |
| 299 |
// and m_bInvertRightChannel |
| 300 |
|
| 301 |
// m_nResolution: 1 means 4-bit resolution; 2 means 3-bit resolution. Resolution of envelope waveform. |
| 302 |
|
| 303 |
// Note that this is handled 'immediately', and doesn't wait for synchronisation of |
| 304 |
// the envelope waveform (this is important, see test case EnvExt_imm) |
| 305 |
// It is therefore possible to switch between 4-bit and 3-bit resolution in the middle of |
| 306 |
// an envelope waveform. if you are at an 'odd' phase position, you would be able to hear |
| 307 |
// the difference. if you are at an 'even' phase position, the volume level for 4-bit |
| 308 |
// and 3-bit would be the same. |
| 309 |
// NOTE: additional test cases are required. |
| 310 |
|
| 311 |
switch (m_nResolution) |
| 312 |
{ |
| 313 |
case 1: // 4 bit res waveforms |
| 314 |
default: |
| 315 |
{ |
| 316 |
// special case: if envelope is not a looping one, and we're at the end |
| 317 |
// then our level should be zero (all of the non-looping waveforms have |
| 318 |
// a sustain level of zero): |
| 319 |
if (m_bEnvelopeEnded && !m_bLooping) |
| 320 |
m_nLeftLevel = 0; |
| 321 |
else |
| 322 |
m_nLeftLevel = m_pEnvData->nLevels[0][m_nPhase][m_nPhasePosition]; |
| 323 |
|
| 324 |
if (m_bInvertRightChannel) |
| 325 |
m_nRightLevel = 15-m_nLeftLevel; |
| 326 |
else |
| 327 |
m_nRightLevel = m_nLeftLevel; |
| 328 |
break; |
| 329 |
} |
| 330 |
case 2: // 3 bit res waveforms |
| 331 |
{ |
| 332 |
// special case: if envelope is not a looping one, and we're at the end |
| 333 |
// then our level should be zero (all of the non-looping waveforms have |
| 334 |
// a sustain level of zero): |
| 335 |
if (m_bEnvelopeEnded && !m_bLooping) |
| 336 |
m_nLeftLevel = 0; |
| 337 |
else |
| 338 |
m_nLeftLevel = m_pEnvData->nLevels[1][m_nPhase][m_nPhasePosition]; |
| 339 |
if (m_bInvertRightChannel) |
| 340 |
m_nRightLevel = 14-m_nLeftLevel; |
| 341 |
else |
| 342 |
m_nRightLevel = m_nLeftLevel; |
| 343 |
break; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
inline void CSAAEnv::SetNewEnvData(int nData) |
| 350 |
{ |
| 351 |
// loads envgenerator's registers according to the bits set |
| 352 |
// in nData |
| 353 |
|
| 354 |
m_nPhase = 0; |
| 355 |
m_nPhasePosition = 0; |
| 356 |
m_pEnvData = &(cs_EnvData[(nData >> 1) & 0x07]); |
| 357 |
m_bInvertRightChannel = ((nData & 0x01) == 0x01); |
| 358 |
m_bClockExternally = ((nData & 0x20) == 0x20); |
| 359 |
m_nNumberOfPhases = m_pEnvData->nNumberOfPhases; |
| 360 |
m_bLooping = m_pEnvData->bLooping; |
| 361 |
m_nResolution = (((nData & 0x10)==0x10) ? 2 : 1); |
| 362 |
m_bEnabled = ((nData & 0x80) == 0x80); |
| 363 |
if (m_bEnabled) |
| 364 |
{ |
| 365 |
m_bEnvelopeEnded = false; |
| 366 |
// is this right? |
| 367 |
// YES. See test case EnvExt_34c (setting data multiple times |
| 368 |
// when at a point (3) resets the waveform so you're no inter |
| 369 |
// at a point (3). |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
// DISABLED - so set stuff accordingly |
| 374 |
m_bEnvelopeEnded = true; |
| 375 |
m_nPhase = 0; |
| 376 |
m_nPhasePosition = 0; |
| 377 |
} |
| 378 |
|
| 379 |
SetLevels(); |
| 380 |
} |