## Envelopes

An investigation into the behaviour of the Yamaha OPL3 chipset.
With a view to a more accurate emulator

### Re: Envelopes

Key Scale Rate

The actual rate of the envelope depends on more than just AR / DR and RR. It is also controlled by BLOCK, F-NUM, KSR and NTS.

The rate of the envelope increases as the frequency of the note increases. Initially a value documented as the key scale number is derived. This is taken from the BLOCK and one of the two MSB of the F-NUM. If NTS = 0 the bit 9, the MSB of F-NUM is used. If NTS = 1 the bit 8 of F-NUM is used instead. This key scale value is the BLOCK * 2 + F-NUM bit (either bit 9 or bit 8). If KSR =0 then the key scale number is then right-shifted two bits to the right to give a value between 0 and 3, if KSR=1 the key scale number remains between 0 and 15

The actual rate for an envelope phase is the envelope phase register value (e.g. AR) multiplied by 4 and added to the key scale number. This rate is clamped to between 0 and 63

the 4 MSB of the rate behave as documented in my previous posts in this topic on the Attack, decay and release rates. The 2 LSB have a subtle effect to accelerate things.

If I select a decay rate DR of 9, and low frequency leading to a key scale number of 0, the actual rate is 36 and the envelope amplitude changes by 8 every 16 samples.
if the key scale number is 1, the actual rate is 37 and the envelope amplitude changes by 8 after 16 samples, 8 after the next 16 samples, 8 after the next 16 samples, 8 after the next 8 samples and 8 again 8 samples later. So for every 64 samples we actually make 5 step transitions

if the key scale number is 2, the actual rate is 38 and the step lengths become 16, 8, 8, 16, 8, 8.
Similarly for a rate of 39 the step lengths are 16, 8, 8, 8, 8, 8, 8.

Rate Step lengths
40 8,8,8,8
41 8,8,8,4,4
42 8,4,4,8,4,4
43 8,4,4,4,4,4,4
44 4,4,4,4
45 4,4,4,2,2
Last edited by carbon14 on Sun May 27, 2012 9:50 am, edited 1 time in total.
Reason: corrected typo in first column of table

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

So far I've not pushed my KSR experiments to the faster end of the rates. The OPL-4 documentation gives actual timings for these rates and suggests that rates 60-63 are in fact identical.
This means that there need not be any decay or release steps greater than 32 in a single sample, nor any attack rate faster than the x-(x/4 + 1) algorithm previously described.

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

carbon14 wrote:If I select a decay rate DR of 9, and low frequency leading to a key scale number of 0, the actual rate is 36 and the envelope amplitude changes by 8 every 16 samples.
if the key scale number is 1, the actual rate is 37 and the envelope amplitude changes by 8 after 16 samples, 8 after the next 16 samples, 8 after the next 16 samples, 8 after the next 8 samples and 8 again 8 samples later. So for every 64 samples we actually make 5 step transitions

if the key scale number is 2, the actual rate is 38 and the step lengths become 16, 8, 8, 16, 8, 8.
Similarly for a rate of 39 the step lengths are 16, 8, 8, 8, 8, 8, 8.

Rate Step lengths
40 8,8,8,8
41 8,8,8,4,4
42 8,4,4,8,4,4
42 8,4,4,4,4,4,4
44 4,4,4,4
45 4,4,4,2,2

So, if I have understood correctly, rate 48 table (12*4+0) would look like 2,2,2,2 (increase envelope by one step (+8) at every other sample) and rate 52 table (13*4+0) would look like 1,1,1,1 (increase envelope by one step (+8) at every sample)? And with higher rates it needs to be verified how it envelope increases by two or more steps?

Basically, what is common to all those above, they could also be presented as a table which contains 8 flags, to increase by one step or not.
Rate
40 1,0,1,0,1,0,1,0 // every entry is 4 samples, your 8-8-8-8 table
41 1,0,1,0,1,0,1,1 // every entry is 4 samples, your 8-8-8-4-4 table
42 1,0,1,1,1,0,1,1 // every entry is 4 samples, your 8-4-4-8-4-4 table
43 1,0,1,1,1,1,1,1 // every entry is 4 samples, your 8-4-4-4-4-4-4 table
44 1,0,1,0,1,0,1,0 // every entry is 2 samples, your 4-4-4-4 table
45 1,0,1,0,1,0,1,1 // every entry is 2 samples, your 4-4-4-2-2 table

So in fact rates R*4+0 would use same table, but R would be used to select how many samples per step. Then the rate offset 0..3 would be used to index which of the four step tables to use.

No wonder MAME does it like this, so again, both you and MAME again do it correctly, at least for rates 1*4+0 to 13*4+0. Only thing indeterminate is what kind of timer ticks to advance to next step table, is it global so all envelopes with same rate (prescaler) are in sync, or are they local and every envelope ticks on its own. At least I know that the envelope is not in sync with phase generator, so when a sound is started, the phase generator starts running and the envelope ticks later.
opl3

Posts: 55
Joined: Sun Sep 26, 2010 8:11 pm

### Re: Envelopes

I'm confident (with no evidence at all) that the envelope advance is a global timer. Given that it's not synchronised to the phase generator or to the KON=1 event, I can see no other local timer which might be suitable, and no good reason to duplicate. There must be a timer for the VIB, so why not use that.

For faster attack, decay, release rates, where the step rate is down to stepping every sample, the attenuation change per sample increases, first from 8 to 16, and then again to 32.

Although the OPL-4 documentation specifically says that the RATE KEY SCALE number is clamped at 63, the tables for actual timings for attack, decay and release actually show the same timings for 60, 61, 62 and 63. I can see no functional difference for an emulator if we clamp it at 60. This shortens any required lookup table. ASSUMING (still on my list to test) that the same holds true for the OPL-3

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

Envelope structures for faster rates.

Decay and release rates which yield an actual rate up to 52 behave as previously described. At rate 52, however the rate of change is a attenuation increase of 8 per sample. It's not clear from what occurs at lower rates, how it behaves at rates greater than 52.

In fact it can be described by reference to a similar table of entries, each with 8 values. A repeating pattern occurs over an 8 sample period. Each sample, the attenuation is increased by an amount as shown in the table below:

Code: Select all
actual rate   attn increases

52      8  8  8  8  8  8  8  8
53      8  8  8  8  8  8  16 16
54      8  8  16 16 8  8  16 16
55      8  8  16 16 16 16 16 16
56      16 16 16 16 16 16 16 16
57      16 16 16 16 16 16 32 32
58      16 16 32 32 16 16 32 32
59      16 16 32 32 32 32 32 32
60      32 32 32 32 32 32 32 32
61      32 32 32 32 32 32 32 32
62      32 32 32 32 32 32 32 32
63      32 32 32 32 32 32 32 32

As with the OPL-4, rates 61-63 appear to behave no differently to rate 60

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

The number of samples required for a 12dB attenuation is given by the table below

Code: Select all
key scale no.   samples to reach -12dB

0      infinite
1      infinite
2      infinite
3      infinite
4      524288
5      419430
6      349525
7      299593
8      262144
9      209715
10      174763
11      149797
12      131072
13      104858
14      87381
15      74898
16      65536
17      52429
18      43691
19      37449
20      32768
21      26214
22      21845
23      18725
24      16384
25      13107
26      10923
27      9362
28      8192
29      6554
30      5461
31      4681
32      4096
33      3277
34      2731
35      2341
36      2048
37      1638
38      1365
39      1170
40      1024
41      819
42      683
43      585
44      512
45      410
46      341
47      293
48      256
49      205
50      171
51      146
52      128
53      102
54      85
55      73
56      64
57      51
58      43
59      37
60      32
61      32
62      32
63      32

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

I did an experiment with the decay rate set to 9 and the release rate set to 11.

I was expecting to see 16 samples per step for the decay, and then perhaps a glitch at the sustain level and then stabilising at 4 samples per step for the release.

What surprised me, was that I consistently saw 16 samples per step for the decay, 2 samples at the sustain level and then consistently 4 samples per step for the release.

The surprise therefore is that the stepper counter for the decay rate and that for the release rate where not synchronised.

This could be therefore that there is a stepper counter per envelope.

I think it's just possible that there is still a single clock, but that decay steps are on rising edges, and release steps are on falling edges. It should be easy for me to tell with some further experiments at slower rates. But I'm no longer particularly confident of the single clock architecture.

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

Looking at the table of decay times that I just posted, it occurred to be that I could compare that with the published decay times for the OPL-4.

I have to take these figures with a pinch of salt at present, because the OPL-4 has a sample rate of 44.1 kHz rather different from that of the OPL-3

at an actual decay rate of 4 (DR=1 BLOCK=0 etc) the time taken to fall 12dB is 10.5 seconds.
the OPL-4 documentation says that the time to decay completely is 39.28 seconds.
This all comes out to a nice 45dB limit, close to the value I surmised as a starting point for the Attack of 511 * 8 (45dB = 3840 attenuation)

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

### Re: Envelopes

Nice work!

Do you know if the table always starts from first index after attack reaches maximum, or does it start from arbitrary index, i.e. is rate 53 always
Code: Select all
8  8  8  8  8  8  16 16

or does it sometimes go for example like this?
Code: Select all
8  8  8  8  16 16  8  8

I don't know if I will confuse you more, but for example attack phase can start from any attenuation, not just from 511, so it should be possible to see other attenuation values in the attack phase instead of just those values they happen to be when starting from maximum attenuation.

I can see from your samples per 12dB attenuation table, that it is not simply divided by two every time, at least not with the rates that have sample counts other than powers of two.

This has led me to thinking, that also the envelope might be similar to phase accumulator, so only 9 most significant bits of some larger envelope accumulator is used as output, while base rate plus rate offset define what value shifted by what amount is added to envelope accumulator. There still may be a global/local timer so that at the lowest rates the envelope is not accumulated by every sample. But I have not yet figured out an algorithm that could provide same functionality as seen in your tables.
opl3

Posts: 55
Joined: Sun Sep 26, 2010 8:11 pm

### Re: Envelopes

I don't know if the table always starts from the first index.
My last set of experiments was supposed to figure that out, assuming that there was a consistent clock I figured that by synchronising a faster rate against a slower rate I could judge exactly that. Since that synchronisation didn't happen, I'm currently in the dark there.

I hadn't appreciated what you say about the starting attenuation. It's true that by starting from 508 (for example) you can end up with a different set of values on the way up. I suppose the key thing is that the sound can't decay forever, it has to be clamped somewhere and if you allow an operator to decay completely, the next attack presumably starts from this clamped value.

The 12dB table does double for every 4 values of this calculated rate (except at the extremes). The values I calculated in for the table have some decimal component which I rounded for the table. In fact where the rate does not divide by 4, the actual number of samples might vary, according to where in the table the decay starts. So for a calculated rate of 32, the decay by 12dB requires 128 steps of 32 samples each.
For the calculated rate of 33, the decay occurs with 5 steps every 128 samples, it still requires 128 steps, but the number of samples required has a little variation. It would have helped if I had clarified that the values in the table were calculations, not measurements.

carbon14

Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

PreviousNext