# Envelope generator Citation forms: [start-here.md](start-here.md). Line numbers are IKAOPM 08a5b40's `IKAOPM_eg.v` unless another file is named. The attenuation level is 10 bits, 0 loudest, 1023 silent; one unit is 0.09375 dB (TL's 0.75 dB is 8 units; 1023 units is about 96 dB). DOC (AM p.10: TL resolution 0.75 dB; Fig. 2.14); DIE (:611-694). ## States and rates | Rule | Evidence | | --- | --- | | Four states per slot: attack, first decay, second decay, release; key-on starts the attack, the level reaching 0 dB starts the first decay, reaching D1L the second decay, key-off the release | DOC (AM p.9, figure); DIE (:219-319) | | The attack is exponential in the level (it slows as it nears 0 dB); decays and release are linear in the level | DOC (AM p.9) | | Rate = 2 R + Rks, limited to 63. R is AR, D1R or D2R; for RR it is 2 RR + 1. Rks is KC' (the top five pitch bits) shifted right by 3 - KS | DOC (AM p.21, Fig. 2.12); DIE (:333-405) | | With KS 0 and a raw rate of 0, Rks is 0; with any KS, a raw rate of 0 never moves the level | DIE (:363-364, :556-557); OBSERVED (Nuked-OPM f209e6e, `reference/referee-measure.txt`: AR 0, KS 3, KC 0x7E stays silent) | | D1L 0-14 is 3 dB per step (32 units); D1L 15 is 93 dB (level 992) | DOC (AM p.21, Fig. 2.13); DIE (:523-524) | | The first decay ends when the level's top six bits equal the D1L target (an equality, not a threshold) | DIE (:523-524) | | In a decay or release, a level with its top six bits all set (>= 1008) is forced to 1023 and the slot goes to release, even with the key held | DIE (:283-306, :529-530, :604, :645) | ## The envelope clock | Rule | Evidence | | --- | --- | | The envelope steps once every three samples | DIE (:66-84); OBSERVED (Nuked-OPM f209e6e: at rate 63 the level changes at samples 2, 4, 7, 10, 13 ..., every third sample after the start) | | A global counter advances on each envelope step; the position of its lowest set bit (1 to 14, 0 for none) is the step's "class", so class 1 comes every second step, class 2 every fourth, and so on | DIE (:88-161) | | A rate below 48 changes the level by 1 on a step whose class c satisfies rate + 4c in {48, 49, 50, 51, 54, 55, 57, 59}; so the top four rate bits set how often and the low two bits pick a pattern of 4 to 7 out of 8 | DIE (:420-570) | | Rates 48-63 change the level on every step by 1, 2, 4 or 8 (rate bits 5-2 = 12, 13, 14, 15), doubled on some steps by a pattern from rate bits 1-0 and counter bits 2-1 | DIE (:496-552) | | Test bit 0 makes every sample an envelope step | DIE (:77) | Closed-box checks, all in `reference/referee-measure.txt` (OBSERVED, Nuked-OPM f209e6e; the noise-slot readout shows the level in steps of 4 units, see [lfo-noise.md](lfo-noise.md)): rate 12 moves 4 units every 6,139-6,144 samples, which is 1 unit per 1,536 samples (one change per 512 envelope steps: class 9); rate 26 moves 4 units every 478-484 or 573-579 samples (about 128 samples per unit: classes 6 and 7, three changes per 128 steps); rate 63 moves 8 units every third sample. Each agrees with the die rule above. ## Level arithmetic | Rule | Evidence | | --- | --- | | Decay and release add the step size (1, 2, 4 or 8) to the level | DIE (:651-656) | | The attack adds a negative amount that depends on the level L. With a = L >> 5 and L's bits written L4, L3, L2, L1: step size 1 adds -(1 + 2a + L3); size 2 adds -(1 + 4a + 2 L3 + L1); size 4 adds -(1 + 8a + 4 L3 + 3 L2); size 8 adds -(1 + 16a + 15 L4) (the die's bit selection, rewritten as sums) | DIE (:658-667) | | Rates 62 and 63 jump to level 0 at the key-on edge; while the key is held, a slot already in attack at rate 62 or 63 does not move at all | DIE (:598-608, :645-648); OBSERVED (Nuked-OPM f209e6e: AR 31 key-on is loud one sample later; AR raised to 31 during an attack at rate 62 or 63 freezes the level, AR 30 (rate 60) finishes it) | | A key-on edge sets the state to attack in the same round; the level is carried over (no reset to 1023) except at rates 62-63 | DIE (:243-268, :606-608) | | /IC puts every slot in release at level 1023 | DIE (:263-264, :645) | ## Final attenuation | Rule | Evidence | | --- | --- | | Final = envelope level + AM + TL x 8, each sum clamped at 1023 | DIE (:709-776) | | AM: the LFO's 8-bit AM value shifted left 0, 1, 2 for AMS 1, 2, 3; AMS 0 or AMS-EN 0 gives none | DIE (:713-718; IKAOPM_reg.v:693) | | Maximum AM depth 23.9, 47.8, 95.6 dB for AMS 1, 2, 3 (255, 510, 1020 units) | DOC (AM p.21, Fig. 2.15) | | Test bit 5 forces the final attenuation to 0 | DIE (:720, :787) | | AMS on the closed box, square LFO, AMD 127: noise-slot readout swings 2044 -> 1536, 1032, 16 for AMS 1, 2, 3, i.e. about 254, 506 and the clamp | OBSERVED (Nuked-OPM f209e6e, `reference/referee-measure.txt`) | The manual's tables of attack and decay times (AM p.20, Fig. 2.11, at fM = 3.6 MHz) are a DOC cross-check for rung 1; the OCR of that page is poor, so it is to be read from the scan when used. Attack 10-90 % on the closed box: 43.8 ms at rate 30, 36.9 ms at rate 31 (OBSERVED, same file).