Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ////////////////////////////////////////////////////////////////////////
  2. // ADSR func
  3. ////////////////////////////////////////////////////////////////////////
  4.  
  5. unsigned long RateTable[160];
  6.  
  7. void InitADSR(void)                                    // INIT ADSR
  8. {
  9.  unsigned long r,rs,rd;int i;
  10.  
  11.  memset(RateTable,0,sizeof(unsigned long)*160);        // build the rate table according to Neill's rules (see at bottom of file)
  12.  
  13.  r=3;rs=1;rd=0;
  14.  
  15.  for(i=32;i<160;i++)                                   // we start at pos 32 with the real values... everything before is 0
  16.   {
  17.    if(r<0x3FFFFFFF)
  18.     {
  19.      r+=rs;
  20.      rd++;if(rd==5) {rd=1;rs*=2;}
  21.     }
  22.    if(r>0x3FFFFFFF) r=0x3FFFFFFF;
  23.  
  24.    RateTable[i]=r;
  25.   }
  26. }
  27.  
  28. INLINE int MixADSR(int ch)                             // MIX ADSR
  29. {    
  30.  if(s_chan[ch].bStop)                                  // should be stopped:
  31.   {                                                    // do release
  32.    if(s_chan[ch].ADSRX.ReleaseModeExp)
  33.     {
  34.      switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)
  35.       {
  36.        case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +0 + 32]; break;
  37.        case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +4 + 32]; break;
  38.        case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +6 + 32]; break;
  39.        case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +8 + 32]; break;
  40.        case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +9 + 32]; break;
  41.        case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +10+ 32]; break;
  42.        case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +11+ 32]; break;
  43.        case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +12+ 32]; break;
  44.       }
  45.     }
  46.    else
  47.     {
  48.      s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x0C + 32];
  49.     }
  50.  
  51.    if(s_chan[ch].ADSRX.EnvelopeVol<0)
  52.     {
  53.      s_chan[ch].ADSRX.EnvelopeVol=0;
  54.      s_chan[ch].bOn=0;
  55.      //s_chan[ch].bReverb=0;
  56.      //s_chan[ch].bNoise=0;
  57.     }
  58.  
  59.    s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;
  60.    return s_chan[ch].ADSRX.lVolume;
  61.   }
  62.  else                                                  // not stopped yet?
  63.   {
  64.    if(s_chan[ch].ADSRX.State==0)                       // -> attack
  65.     {
  66.      if(s_chan[ch].ADSRX.AttackModeExp)
  67.       {
  68.        if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000)
  69.         s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32];
  70.        else
  71.         s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x18 + 32];
  72.       }
  73.      else
  74.       {
  75.        s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32];
  76.       }
  77.  
  78.      if(s_chan[ch].ADSRX.EnvelopeVol<0)
  79.       {
  80.        s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;
  81.        s_chan[ch].ADSRX.State=1;
  82.       }
  83.  
  84.      s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;
  85.      return s_chan[ch].ADSRX.lVolume;
  86.     }
  87.    //--------------------------------------------------//
  88.    if(s_chan[ch].ADSRX.State==1)                       // -> decay
  89.     {
  90.      switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)
  91.       {
  92.        case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+0 + 32]; break;
  93.        case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+4 + 32]; break;
  94.        case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+6 + 32]; break;
  95.        case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+8 + 32]; break;
  96.        case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+9 + 32]; break;
  97.        case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+10+ 32]; break;
  98.        case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+11+ 32]; break;
  99.        case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+12+ 32]; break;
  100.       }
  101.  
  102.      if(s_chan[ch].ADSRX.EnvelopeVol<0) s_chan[ch].ADSRX.EnvelopeVol=0;
  103.      if(((s_chan[ch].ADSRX.EnvelopeVol>>27)&0xF) <= s_chan[ch].ADSRX.SustainLevel)
  104.       {
  105.        s_chan[ch].ADSRX.State=2;
  106.       }
  107.  
  108.      s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;
  109.      return s_chan[ch].ADSRX.lVolume;
  110.     }
  111.    //--------------------------------------------------//
  112.    if(s_chan[ch].ADSRX.State==2)                       // -> sustain
  113.     {
  114.      if(s_chan[ch].ADSRX.SustainIncrease)
  115.       {
  116.        if(s_chan[ch].ADSRX.SustainModeExp)
  117.         {
  118.          if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000)
  119.           s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32];
  120.          else
  121.           s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x18 + 32];
  122.         }
  123.        else
  124.         {
  125.          s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32];
  126.         }
  127.  
  128.        if(s_chan[ch].ADSRX.EnvelopeVol<0)
  129.         {
  130.          s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;
  131.         }
  132.       }
  133.      else
  134.       {
  135.        if(s_chan[ch].ADSRX.SustainModeExp)
  136.         {
  137.          switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7)
  138.           {
  139.            case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +0 + 32];break;
  140.            case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +4 + 32];break;
  141.            case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +6 + 32];break;
  142.            case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +8 + 32];break;
  143.            case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +9 + 32];break;
  144.            case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +10+ 32];break;
  145.            case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +11+ 32];break;
  146.            case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +12+ 32];break;
  147.           }
  148.         }
  149.        else
  150.         {
  151.          s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x0F + 32];
  152.         }
  153.  
  154.        if(s_chan[ch].ADSRX.EnvelopeVol<0)
  155.         {
  156.          s_chan[ch].ADSRX.EnvelopeVol=0;
  157.         }
  158.       }
  159.      s_chan[ch].ADSRX.lVolume=s_chan[ch].ADSRX.EnvelopeVol>>21;
  160.      return s_chan[ch].ADSRX.lVolume;
  161.     }
  162.   }
  163.  return 0;
  164. }
  165.  
  166. /*
  167. James Higgs ADSR investigations:
  168.  
  169. PSX SPU Envelope Timings
  170. ~~~~~~~~~~~~~~~~~~~~~~~~
  171.  
  172. First, here is an extract from doomed's SPU doc, which explains the basics
  173. of the SPU "volume envelope":
  174.  
  175. *** doomed doc extract start ***
  176.  
  177. --------------------------------------------------------------------------
  178. Voices.
  179. --------------------------------------------------------------------------
  180. The SPU has 24 hardware voices. These voices can be used to reproduce sample
  181. data, noise or can be used as frequency modulator on the next voice.
  182. Each voice has it's own programmable ADSR envelope filter. The main volume
  183. can be programmed independently for left and right output.
  184.  
  185. The ADSR envelope filter works as follows:
  186. Ar = Attack rate, which specifies the speed at which the volume increases
  187.      from zero to it's maximum value, as soon as the note on is given. The
  188.      slope can be set to lineair or exponential.
  189. Dr = Decay rate specifies the speed at which the volume decreases to the
  190.      sustain level. Decay is always decreasing exponentially.
  191. Sl = Sustain level, base level from which sustain starts.
  192. Sr = Sustain rate is the rate at which the volume of the sustained note
  193.      increases or decreases. This can be either lineair or exponential.
  194. Rr = Release rate is the rate at which the volume of the note decreases
  195.      as soon as the note off is given.
  196.  
  197.      lvl |
  198.        ^ |     /\Dr     __
  199.      Sl _| _  / _ \__---  \
  200.          |   /       ---__ \ Rr
  201.          |  /Ar       Sr  \ \
  202.          | /                \\
  203.          |/___________________\________
  204.                                   ->time
  205.  
  206. The overal volume can also be set to sweep up or down lineairly or
  207. exponentially from it's current value. This can be done seperately
  208. for left and right.
  209.  
  210. Relevant SPU registers:
  211. -------------------------------------------------------------
  212. $1f801xx8         Attack/Decay/Sustain level
  213. bit  |0f|0e 0d 0c 0b 0a 09 08|07 06 05 04|03 02 01 00|
  214. desc.|Am|         Ar         |Dr         |Sl         |
  215.  
  216. Am       0        Attack mode Linear
  217.          1                    Exponential
  218.  
  219. Ar       0-7f     attack rate
  220. Dr       0-f      decay rate
  221. Sl       0-f      sustain level
  222. -------------------------------------------------------------
  223. $1f801xxa         Sustain rate, Release Rate.
  224. bit  |0f|0e|0d|0c 0b 0a 09 08 07 06|05|04 03 02 01 00|
  225. desc.|Sm|Sd| 0|   Sr               |Rm|Rr            |
  226.  
  227. Sm       0        sustain rate mode linear
  228.          1                          exponential
  229. Sd       0        sustain rate mode increase
  230.          1                          decrease
  231. Sr       0-7f     Sustain Rate
  232. Rm       0        Linear decrease
  233.          1        Exponential decrease
  234. Rr       0-1f     Release Rate
  235.  
  236. Note: decay mode is always Expontial decrease, and thus cannot
  237. be set.
  238. -------------------------------------------------------------
  239. $1f801xxc         Current ADSR volume
  240. bit  |0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00|
  241. desc.|ADSRvol                                        |
  242.  
  243. ADSRvol           Returns the current envelope volume when
  244.                   read.
  245. -- James' Note: return range: 0 -> 32767
  246.  
  247. *** doomed doc extract end ***
  248.  
  249. By using a small PSX proggie to visualise the envelope as it was played,
  250. the following results for envelope timing were obtained:
  251.  
  252. 1. Attack rate value (linear mode)
  253.  
  254.    Attack value range: 0 -> 127
  255.  
  256.    Value  | 48 | 52 | 56 | 60 | 64 | 68 | 72 |    | 80 |
  257.    -----------------------------------------------------------------
  258.    Frames | 11 | 21 | 42 | 84 | 169| 338| 676|    |2890|
  259.  
  260.    Note: frames is no. of PAL frames to reach full volume (100%
  261.    amplitude)
  262.  
  263.    Hmm, noticing that the time taken to reach full volume doubles
  264.    every time we add 4 to our attack value, we know the equation is
  265.    of form:
  266.              frames = k * 2 ^ (value / 4)
  267.  
  268.    (You may ponder about envelope generator hardware at this point,
  269.    or maybe not... :)
  270.  
  271.    By substituting some stuff and running some checks, we get:
  272.  
  273.        k = 0.00257              (close enuf)
  274.  
  275.    therefore,
  276.              frames = 0.00257 * 2 ^ (value / 4)
  277.    If you just happen to be writing an emulator, then you can probably
  278.    use an equation like:
  279.  
  280.        %volume_increase_per_tick = 1 / frames
  281.  
  282.  
  283.    ------------------------------------
  284.    Pete:
  285.    ms=((1<<(value>>2))*514)/10000
  286.    ------------------------------------
  287.  
  288. 2. Decay rate value (only has log mode)
  289.  
  290.    Decay value range: 0 -> 15
  291.  
  292.    Value  |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
  293.    ------------------------------------------------
  294.    frames |    |    |    |    |  6 | 12 | 24 | 47 |
  295.  
  296.    Note: frames here is no. of PAL frames to decay to 50% volume.
  297.  
  298.    formula: frames = k * 2 ^ (value)
  299.  
  300.    Substituting, we get: k = 0.00146
  301.  
  302.    Further info on logarithmic nature:
  303.    frames to decay to sustain level 3  =  3 * frames to decay to
  304.    sustain level 9
  305.  
  306.    Also no. of frames to 25% volume = roughly 1.85 * no. of frames to
  307.    50% volume.
  308.  
  309.    Frag it - just use linear approx.
  310.  
  311.    ------------------------------------
  312.    Pete:
  313.    ms=((1<<value)*292)/10000
  314.    ------------------------------------
  315.  
  316.  
  317. 3. Sustain rate value (linear mode)
  318.  
  319.    Sustain rate range: 0 -> 127
  320.  
  321.    Value  | 48 | 52 | 56 | 60 | 64 | 68 | 72 |
  322.    -------------------------------------------
  323.    frames |  9 | 19 | 37 | 74 | 147| 293| 587|
  324.  
  325.    Here, frames = no. of PAL frames for volume amplitude to go from 100%
  326.    to 0% (or vice-versa).
  327.  
  328.    Same formula as for attack value, just a different value for k:
  329.  
  330.    k = 0.00225
  331.  
  332.    ie: frames = 0.00225 * 2 ^ (value / 4)
  333.  
  334.    For emulation purposes:
  335.  
  336.    %volume_increase_or_decrease_per_tick = 1 / frames
  337.  
  338.    ------------------------------------
  339.    Pete:
  340.    ms=((1<<(value>>2))*450)/10000
  341.    ------------------------------------
  342.  
  343.  
  344. 4. Release rate (linear mode)
  345.  
  346.    Release rate range: 0 -> 31
  347.  
  348.    Value  | 13 | 14 | 15 | 16 | 17 |
  349.    ---------------------------------------------------------------
  350.    frames | 18 | 36 | 73 | 146| 292|
  351.  
  352.    Here, frames = no. of PAL frames to decay from 100% vol to 0% vol
  353.    after "note-off" is triggered.
  354.  
  355.    Formula: frames = k * 2 ^ (value)
  356.  
  357.    And so: k = 0.00223
  358.  
  359.    ------------------------------------
  360.    Pete:
  361.    ms=((1<<value)*446)/10000
  362.    ------------------------------------
  363.  
  364.  
  365. Other notes:  
  366.  
  367. Log stuff not figured out. You may get some clues from the "Decay rate"
  368. stuff above. For emu purposes it may not be important - use linear
  369. approx.
  370.  
  371. To get timings in millisecs, multiply frames by 20.
  372.  
  373.  
  374.  
  375. - James Higgs 17/6/2000
  376. james7780@yahoo.com
  377.  
  378. //---------------------------------------------------------------
  379.  
  380. OLD adsr mixing according to james' rules... has to be called
  381. every one millisecond
  382.  
  383.  
  384.  long v,v2,lT,l1,l2,l3;
  385.  
  386.  if(s_chan[ch].bStop)                                  // psx wants to stop? -> release phase
  387.   {
  388.    if(s_chan[ch].ADSR.ReleaseVal!=0)                   // -> release not 0: do release (if 0: stop right now)
  389.     {
  390.      if(!s_chan[ch].ADSR.ReleaseVol)                   // --> release just started? set up the release stuff
  391.       {
  392.        s_chan[ch].ADSR.ReleaseStartTime=s_chan[ch].ADSR.lTime;
  393.        s_chan[ch].ADSR.ReleaseVol=s_chan[ch].ADSR.lVolume;
  394.        s_chan[ch].ADSR.ReleaseTime =                   // --> calc how long does it take to reach the wanted sus level
  395.          (s_chan[ch].ADSR.ReleaseTime*
  396.           s_chan[ch].ADSR.ReleaseVol)/1024;
  397.       }
  398.                                                        // -> NO release exp mode used (yet)
  399.      v=s_chan[ch].ADSR.ReleaseVol;                     // -> get last volume
  400.      lT=s_chan[ch].ADSR.lTime-                         // -> how much time is past?
  401.         s_chan[ch].ADSR.ReleaseStartTime;
  402.      l1=s_chan[ch].ADSR.ReleaseTime;
  403.                                                        
  404.      if(lT<l1)                                         // -> we still have to release
  405.       {
  406.        v=v-((v*lT)/l1);                                // --> calc new volume
  407.       }
  408.      else                                              // -> release is over: now really stop that sample
  409.       {v=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;}
  410.     }
  411.    else                                                // -> release IS 0: release at once
  412.     {
  413.      v=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;
  414.     }
  415.   }
  416.  else                                              
  417.   {//--------------------------------------------------// not in release phase:
  418.    v=1024;
  419.    lT=s_chan[ch].ADSR.lTime;
  420.    l1=s_chan[ch].ADSR.AttackTime;
  421.                                                        
  422.    if(lT<l1)                                           // attack
  423.     {                                                  // no exp mode used (yet)
  424. //     if(s_chan[ch].ADSR.AttackModeExp)
  425. //      {
  426. //       v=(v*lT)/l1;
  427. //      }
  428. //     else
  429.       {
  430.        v=(v*lT)/l1;
  431.       }
  432.      if(v==0) v=1;
  433.     }
  434.    else                                                // decay
  435.     {                                                  // should be exp, but who cares? ;)
  436.      l2=s_chan[ch].ADSR.DecayTime;
  437.      v2=s_chan[ch].ADSR.SustainLevel;
  438.  
  439.      lT-=l1;
  440.      if(lT<l2)
  441.       {
  442.        v-=(((v-v2)*lT)/l2);
  443.       }
  444.      else                                              // sustain
  445.       {                                                // no exp mode used (yet)
  446.        l3=s_chan[ch].ADSR.SustainTime;
  447.        lT-=l2;
  448.        if(s_chan[ch].ADSR.SustainModeDec>0)
  449.         {
  450.          if(l3!=0) v2+=((v-v2)*lT)/l3;
  451.          else      v2=v;
  452.         }
  453.        else
  454.         {
  455.          if(l3!=0) v2-=(v2*lT)/l3;
  456.          else      v2=v;
  457.         }
  458.  
  459.        if(v2>v)  v2=v;
  460.        if(v2<=0) {v2=0;s_chan[ch].bOn=0;s_chan[ch].ADSR.ReleaseVol=0;s_chan[ch].bNoise=0;}
  461.  
  462.        v=v2;
  463.       }
  464.     }
  465.   }
  466.  
  467.  //----------------------------------------------------//
  468.  // ok, done for this channel, so increase time
  469.  
  470.  s_chan[ch].ADSR.lTime+=1;                             // 1 = 1.020408f ms;      
  471.  
  472.  if(v>1024)     v=1024;                                // adjust volume
  473.  if(v<0)        v=0;                                  
  474.  s_chan[ch].ADSR.lVolume=v;                            // store act volume
  475.  
  476.  return v;                                             // return the volume factor
  477. */
  478.  
  479.  
  480. //-----------------------------------------------------------------------------
  481. //-----------------------------------------------------------------------------
  482. //-----------------------------------------------------------------------------
  483.  
  484.  
  485. /*
  486. -----------------------------------------------------------------------------
  487. Neill Corlett
  488. Playstation SPU envelope timing notes
  489. -----------------------------------------------------------------------------
  490.  
  491. This is preliminary.  This may be wrong.  But the model described herein fits
  492. all of my experimental data, and it's just simple enough to sound right.
  493.  
  494. ADSR envelope level ranges from 0x00000000 to 0x7FFFFFFF internally.
  495. The value returned by channel reg 0xC is (envelope_level>>16).
  496.  
  497. Each sample, an increment or decrement value will be added to or
  498. subtracted from this envelope level.
  499.  
  500. Create the rate log table.  The values double every 4 entries.
  501.    entry #0 = 4
  502.  
  503.     4, 5, 6, 7,
  504.     8,10,12,14,
  505.    16,20,24,28, ...
  506.  
  507.    entry #40 = 4096...
  508.    entry #44 = 8192...
  509.    entry #48 = 16384...
  510.    entry #52 = 32768...
  511.    entry #56 = 65536...
  512.  
  513. increments and decrements are in terms of ratelogtable[n]
  514. n may exceed the table bounds (plan on n being between -32 and 127).
  515. table values are all clipped between 0x00000000 and 0x3FFFFFFF
  516.  
  517. when you "voice on", the envelope is always fully reset.
  518. (yes, it may click. the real thing does this too.)
  519.  
  520. envelope level begins at zero.
  521.  
  522. each state happens for at least 1 cycle
  523. (transitions are not instantaneous)
  524. this may result in some oddness: if the decay rate is uberfast, it will cut
  525. the envelope from full down to half in one sample, potentially skipping over
  526. the sustain level
  527.  
  528. ATTACK
  529. ------
  530. - if the envelope level has overflowed past the max, clip to 0x7FFFFFFF and
  531.   proceed to DECAY.
  532.  
  533. Linear attack mode:
  534. - line extends upward to 0x7FFFFFFF
  535. - increment per sample is ratelogtable[(Ar^0x7F)-0x10]
  536.  
  537. Logarithmic attack mode:
  538. if envelope_level < 0x60000000:
  539.   - line extends upward to 0x60000000
  540.   - increment per sample is ratelogtable[(Ar^0x7F)-0x10]
  541. else:
  542.   - line extends upward to 0x7FFFFFFF
  543.   - increment per sample is ratelogtable[(Ar^0x7F)-0x18]
  544.  
  545. DECAY
  546. -----
  547. - if ((envelope_level>>27)&0xF) <= Sl, proceed to SUSTAIN.
  548.   Do not clip to the sustain level.
  549. - current line ends at (envelope_level & 0x07FFFFFF)
  550. - decrement per sample depends on (envelope_level>>28)&0x7
  551.   0: ratelogtable[(4*(Dr^0x1F))-0x18+0]
  552.   1: ratelogtable[(4*(Dr^0x1F))-0x18+4]
  553.   2: ratelogtable[(4*(Dr^0x1F))-0x18+6]
  554.   3: ratelogtable[(4*(Dr^0x1F))-0x18+8]
  555.   4: ratelogtable[(4*(Dr^0x1F))-0x18+9]
  556.   5: ratelogtable[(4*(Dr^0x1F))-0x18+10]
  557.   6: ratelogtable[(4*(Dr^0x1F))-0x18+11]
  558.   7: ratelogtable[(4*(Dr^0x1F))-0x18+12]
  559.   (note that this is the same as the release rate formula, except that
  560.    decay rates 10-1F aren't possible... those would be slower in theory)
  561.  
  562. SUSTAIN
  563. -------
  564. - no terminating condition except for voice off
  565. - Sd=0 (increase) behavior is identical to ATTACK for both log and linear.
  566. - Sd=1 (decrease) behavior:
  567. Linear sustain decrease:
  568. - line extends to 0x00000000
  569. - decrement per sample is ratelogtable[(Sr^0x7F)-0x0F]
  570. Logarithmic sustain decrease:
  571. - current line ends at (envelope_level & 0x07FFFFFF)
  572. - decrement per sample depends on (envelope_level>>28)&0x7
  573.   0: ratelogtable[(Sr^0x7F)-0x1B+0]
  574.   1: ratelogtable[(Sr^0x7F)-0x1B+4]
  575.   2: ratelogtable[(Sr^0x7F)-0x1B+6]
  576.   3: ratelogtable[(Sr^0x7F)-0x1B+8]
  577.   4: ratelogtable[(Sr^0x7F)-0x1B+9]
  578.   5: ratelogtable[(Sr^0x7F)-0x1B+10]
  579.   6: ratelogtable[(Sr^0x7F)-0x1B+11]
  580.   7: ratelogtable[(Sr^0x7F)-0x1B+12]
  581.  
  582. RELEASE
  583. -------
  584. - if the envelope level has overflowed to negative, clip to 0 and QUIT.
  585.  
  586. Linear release mode:
  587. - line extends to 0x00000000
  588. - decrement per sample is ratelogtable[(4*(Rr^0x1F))-0x0C]
  589.  
  590. Logarithmic release mode:
  591. - line extends to (envelope_level & 0x0FFFFFFF)
  592. - decrement per sample depends on (envelope_level>>28)&0x7
  593.   0: ratelogtable[(4*(Rr^0x1F))-0x18+0]
  594.   1: ratelogtable[(4*(Rr^0x1F))-0x18+4]
  595.   2: ratelogtable[(4*(Rr^0x1F))-0x18+6]
  596.   3: ratelogtable[(4*(Rr^0x1F))-0x18+8]
  597.   4: ratelogtable[(4*(Rr^0x1F))-0x18+9]
  598.   5: ratelogtable[(4*(Rr^0x1F))-0x18+10]
  599.   6: ratelogtable[(4*(Rr^0x1F))-0x18+11]
  600.   7: ratelogtable[(4*(Rr^0x1F))-0x18+12]
  601.  
  602. -----------------------------------------------------------------------------
  603. */