Advertisement
Kurausukun

original

Aug 1st, 2024
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. // Original FSS Function: Snd_UpdChannel
  2. void Channel::Update()
  3. {
  4.   // Kill active channels that aren't physically active
  5.   if (this->state > CS_START && !this->reg.enable)
  6.   {
  7.     this->Kill();
  8.     return;
  9.   }
  10.  
  11.   bool bNotInSustain = this->state != CS_SUSTAIN;
  12.   bool bInStart = this->state == CS_START;
  13.   bool bPitchSweep = this->sweepPitch && this->sweepLen && this->sweepCnt <= this->sweepLen;
  14.   bool bModulation = !!this->modDepth;
  15.   bool bVolNeedUpdate = this->flags[CF_UPDVOL] || bNotInSustain;
  16.   bool bPanNeedUpdate = this->flags[CF_UPDPAN] || bInStart;
  17.   bool bTmrNeedUpdate = this->flags[CF_UPDTMR] || bInStart || bPitchSweep;
  18.   int modParam = 0;
  19.  
  20.   switch (this->state)
  21.   {
  22.     case CS_NONE:
  23.       return;
  24.     case CS_START:
  25.       this->reg.ClearControlRegister();
  26.       this->reg.source = this->tempReg.SOURCE;
  27.       this->reg.loopStart = this->tempReg.REPEAT_POINT;
  28.       this->reg.length = this->tempReg.LENGTH;
  29.       this->reg.totalLength = this->reg.loopStart + this->reg.length;
  30.       this->ampl = AMPL_THRESHOLD;
  31.       this->sharp.reset(nullptr);
  32.       this->state = CS_ATTACK;
  33.       // Fall down
  34.     case CS_ATTACK:
  35.       {
  36.         int newAmpl = this->ampl;
  37.         int oldAmpl = this->ampl >> 7;
  38.         do
  39.           newAmpl = (newAmpl * static_cast<int>(this->attackLvl)) / 256;
  40.         while ((newAmpl >> 7) == oldAmpl);
  41.         this->ampl = newAmpl;
  42.         if (!this->ampl)
  43.           this->state = CS_DECAY;
  44.         break;
  45.       }
  46.     case CS_DECAY:
  47.       {
  48.         this->ampl -= static_cast<int>(this->decayRate);
  49.         int sustLvl = Cnv_Sust(this->sustainLvl) << 7;
  50.         if (this->ampl <= sustLvl)
  51.         {
  52.           this->ampl = sustLvl;
  53.           this->state = CS_SUSTAIN;
  54.         }
  55.         break;
  56.       }
  57.     case CS_RELEASE:
  58.       this->ampl -= static_cast<int>(this->releaseRate);
  59.       if (this->ampl <= AMPL_THRESHOLD)
  60.       {
  61.         this->Kill();
  62.         return;
  63.       }
  64.   }
  65.  
  66.   if (bModulation && this->modDelayCnt < this->modDelay)
  67.   {
  68.     ++this->modDelayCnt;
  69.     bModulation = false;
  70.   }
  71.  
  72.   if (bModulation)
  73.   {
  74.     switch (this->modType)
  75.     {
  76.       case 0:
  77.         bTmrNeedUpdate = true;
  78.         break;
  79.       case 1:
  80.         bVolNeedUpdate = true;
  81.         break;
  82.       case 2:
  83.         bPanNeedUpdate = true;
  84.     }
  85.  
  86.     // Get the current modulation parameter
  87.     modParam = Cnv_Sine(static_cast<int32_t>(static_cast<uint32_t>(this->modCounter >> 8))) * this->modRange * this->modDepth;
  88.  
  89.     if (this->modType == 1)
  90.       modParam = static_cast<int64_t>(modParam * 60) >> 14;
  91.     else
  92.       modParam >>= 8; // tmr/pan: adjust to 7.6
  93.  
  94.     // Update the modulation variables
  95.  
  96.     uint32_t counter = this->modCounter + (this->modSpeed << 6);
  97.     counter >>= 8;
  98.  
  99.     while (counter >= 0x80)
  100.       counter -= 0x80;
  101.     this->modCounter += this->modSpeed << 6;
  102.     this->modCounter &= 0xFF;
  103.     this->modCounter |= counter << 8;
  104.  
  105.     //this->modCounter = counter;
  106.   }
  107.  
  108.   if (bTmrNeedUpdate)
  109.   {
  110.     int totalAdj = this->extTune;
  111.     if (bModulation && !this->modType)
  112.       totalAdj += modParam;
  113.     if (bPitchSweep)
  114.     {
  115.       int len = this->sweepLen;
  116.       int cnt = this->sweepCnt;
  117.       totalAdj += (static_cast<int64_t>(this->sweepPitch) * (len - cnt)) / len;
  118.       if (!this->manualSweep)
  119.         ++this->sweepCnt;
  120.     }
  121.     uint16_t tmr = this->tempReg.TIMER;
  122.  
  123.     if (totalAdj)
  124.       tmr = Timer_Adjust(tmr, totalAdj);
  125.     this->reg.timer = -tmr;
  126.     this->reg.sampleIncrease = (ARM7_CLOCK / static_cast<double>(this->ply->sampleRate * 2)) / (0x10000 - this->reg.timer);
  127.     //std::cout << "sample increase: " << ARM7_CLOCK << " @ " << this->ply->sampleRate << " * 2 / (" << 0x10000 << " - " << this->reg.timer << ") -> " << this->reg.sampleIncrease << std::endl;
  128.     this->flags.reset(CF_UPDTMR);
  129.   }
  130.  
  131.   if (bVolNeedUpdate || bPanNeedUpdate)
  132.   {
  133.     uint32_t cr = this->tempReg.CR;
  134.     if (bVolNeedUpdate)
  135.     {
  136.       int totalVol = this->ampl >> 7;
  137.       totalVol += this->extAmpl;
  138.       totalVol += this->velocity;
  139.       if (bModulation && this->modType == 1)
  140.         totalVol += modParam;
  141.       totalVol += AMPL_K;
  142.       clamp(totalVol, 0, AMPL_K);
  143.  
  144.       cr &= ~(SOUND_VOL(0x7F) | SOUND_VOLDIV(3));
  145.       cr |= SOUND_VOL(static_cast<int>(getvoltbl[totalVol]));
  146.  
  147.       if (totalVol < AMPL_K - 240)
  148.         cr |= SOUND_VOLDIV(3);
  149.       else if (totalVol < AMPL_K - 120)
  150.         cr |= SOUND_VOLDIV(2);
  151.       else if (totalVol < AMPL_K - 60)
  152.         cr |= SOUND_VOLDIV(1);
  153.  
  154.       this->vol = ((cr & SOUND_VOL(0x7F)) << 4) >> calcVolDivShift((cr & SOUND_VOLDIV(3)) >> 8);
  155.  
  156.       this->flags.reset(CF_UPDVOL);
  157.     }
  158.  
  159.     if (bPanNeedUpdate)
  160.     {
  161.       int realPan = this->pan;
  162.       realPan += this->extPan;
  163.       if (bModulation && this->modType == 2)
  164.         realPan += modParam;
  165.       realPan += 64;
  166.       clamp(realPan, 0, 127);
  167.  
  168.       cr &= ~SOUND_PAN(0x7F);
  169.       cr |= SOUND_PAN(realPan);
  170.       this->flags.reset(CF_UPDPAN);
  171.     }
  172.  
  173.     this->tempReg.CR = cr;
  174.     this->reg.SetControlRegister(cr);
  175.   }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement