Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original FSS Function: Snd_UpdChannel
- void Channel::Update()
- {
- // Kill active channels that aren't physically active
- if (this->state > CS_START && !this->reg.enable)
- {
- this->Kill();
- return;
- }
- bool bNotInSustain = this->state != CS_SUSTAIN;
- bool bInStart = this->state == CS_START;
- bool bPitchSweep = this->sweepPitch && this->sweepLen && this->sweepCnt <= this->sweepLen;
- bool bModulation = !!this->modDepth;
- bool bVolNeedUpdate = this->flags[CF_UPDVOL] || bNotInSustain;
- bool bPanNeedUpdate = this->flags[CF_UPDPAN] || bInStart;
- bool bTmrNeedUpdate = this->flags[CF_UPDTMR] || bInStart || bPitchSweep;
- int modParam = 0;
- switch (this->state)
- {
- case CS_NONE:
- return;
- case CS_START:
- this->reg.ClearControlRegister();
- this->reg.source = this->tempReg.SOURCE;
- this->reg.loopStart = this->tempReg.REPEAT_POINT;
- this->reg.length = this->tempReg.LENGTH;
- this->reg.totalLength = this->reg.loopStart + this->reg.length;
- this->ampl = AMPL_THRESHOLD;
- this->sharp.reset(nullptr);
- this->state = CS_ATTACK;
- // Fall down
- case CS_ATTACK:
- {
- int newAmpl = this->ampl;
- int oldAmpl = this->ampl >> 7;
- do
- newAmpl = (newAmpl * static_cast<int>(this->attackLvl)) / 256;
- while ((newAmpl >> 7) == oldAmpl);
- this->ampl = newAmpl;
- if (!this->ampl)
- this->state = CS_DECAY;
- break;
- }
- case CS_DECAY:
- {
- this->ampl -= static_cast<int>(this->decayRate);
- int sustLvl = Cnv_Sust(this->sustainLvl) << 7;
- if (this->ampl <= sustLvl)
- {
- this->ampl = sustLvl;
- this->state = CS_SUSTAIN;
- }
- break;
- }
- case CS_RELEASE:
- this->ampl -= static_cast<int>(this->releaseRate);
- if (this->ampl <= AMPL_THRESHOLD)
- {
- this->Kill();
- return;
- }
- }
- if (bModulation && this->modDelayCnt < this->modDelay)
- {
- ++this->modDelayCnt;
- bModulation = false;
- }
- if (bModulation)
- {
- switch (this->modType)
- {
- case 0:
- bTmrNeedUpdate = true;
- break;
- case 1:
- bVolNeedUpdate = true;
- break;
- case 2:
- bPanNeedUpdate = true;
- }
- // Get the current modulation parameter
- modParam = Cnv_Sine(static_cast<int32_t>(static_cast<uint32_t>(this->modCounter >> 8))) * this->modRange * this->modDepth;
- if (this->modType == 1)
- modParam = static_cast<int64_t>(modParam * 60) >> 14;
- else
- modParam >>= 8; // tmr/pan: adjust to 7.6
- // Update the modulation variables
- uint32_t counter = this->modCounter + (this->modSpeed << 6);
- counter >>= 8;
- while (counter >= 0x80)
- counter -= 0x80;
- this->modCounter += this->modSpeed << 6;
- this->modCounter &= 0xFF;
- this->modCounter |= counter << 8;
- //this->modCounter = counter;
- }
- if (bTmrNeedUpdate)
- {
- int totalAdj = this->extTune;
- if (bModulation && !this->modType)
- totalAdj += modParam;
- if (bPitchSweep)
- {
- int len = this->sweepLen;
- int cnt = this->sweepCnt;
- totalAdj += (static_cast<int64_t>(this->sweepPitch) * (len - cnt)) / len;
- if (!this->manualSweep)
- ++this->sweepCnt;
- }
- uint16_t tmr = this->tempReg.TIMER;
- if (totalAdj)
- tmr = Timer_Adjust(tmr, totalAdj);
- this->reg.timer = -tmr;
- this->reg.sampleIncrease = (ARM7_CLOCK / static_cast<double>(this->ply->sampleRate * 2)) / (0x10000 - this->reg.timer);
- //std::cout << "sample increase: " << ARM7_CLOCK << " @ " << this->ply->sampleRate << " * 2 / (" << 0x10000 << " - " << this->reg.timer << ") -> " << this->reg.sampleIncrease << std::endl;
- this->flags.reset(CF_UPDTMR);
- }
- if (bVolNeedUpdate || bPanNeedUpdate)
- {
- uint32_t cr = this->tempReg.CR;
- if (bVolNeedUpdate)
- {
- int totalVol = this->ampl >> 7;
- totalVol += this->extAmpl;
- totalVol += this->velocity;
- if (bModulation && this->modType == 1)
- totalVol += modParam;
- totalVol += AMPL_K;
- clamp(totalVol, 0, AMPL_K);
- cr &= ~(SOUND_VOL(0x7F) | SOUND_VOLDIV(3));
- cr |= SOUND_VOL(static_cast<int>(getvoltbl[totalVol]));
- if (totalVol < AMPL_K - 240)
- cr |= SOUND_VOLDIV(3);
- else if (totalVol < AMPL_K - 120)
- cr |= SOUND_VOLDIV(2);
- else if (totalVol < AMPL_K - 60)
- cr |= SOUND_VOLDIV(1);
- this->vol = ((cr & SOUND_VOL(0x7F)) << 4) >> calcVolDivShift((cr & SOUND_VOLDIV(3)) >> 8);
- this->flags.reset(CF_UPDVOL);
- }
- if (bPanNeedUpdate)
- {
- int realPan = this->pan;
- realPan += this->extPan;
- if (bModulation && this->modType == 2)
- realPan += modParam;
- realPan += 64;
- clamp(realPan, 0, 127);
- cr &= ~SOUND_PAN(0x7F);
- cr |= SOUND_PAN(realPan);
- this->flags.reset(CF_UPDPAN);
- }
- this->tempReg.CR = cr;
- this->reg.SetControlRegister(cr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement