Page 2 of 3

### Re: Envelopes

Posted: Mon May 21, 2012 9:15 pm
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

### Re: Envelopes

Posted: Mon May 21, 2012 9:22 pm
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.

### Re: Envelopes

Posted: Thu May 24, 2012 3:31 pm
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.

### Re: Envelopes

Posted: Sun May 27, 2012 9:46 am
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

### Re: Envelopes

Posted: Thu May 31, 2012 12:21 pm
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 increases52      8  8  8  8  8  8  8  853      8  8  8  8  8  8  16 1654      8  8  16 16 8  8  16 1655      8  8  16 16 16 16 16 1656      16 16 16 16 16 16 16 1657      16 16 16 16 16 16 32 3258      16 16 32 32 16 16 32 3259      16 16 32 32 32 32 32 3260      32 32 32 32 32 32 32 3261      32 32 32 32 32 32 32 3262      32 32 32 32 32 32 32 3263      32 32 32 32 32 32 32 32`

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

### Re: Envelopes

Posted: Thu May 31, 2012 12:49 pm
The number of samples required for a 12dB attenuation is given by the table below

Code: Select all
`key scale no.   samples to reach -12dB0      infinite1      infinite2      infinite3      infinite4      5242885      4194306      3495257      2995938      2621449      20971510      17476311      14979712      13107213      10485814      8738115      7489816      6553617      5242918      4369119      3744920      3276821      2621422      2184523      1872524      1638425      1310726      1092327      936228      819229      655430      546131      468132      409633      327734      273135      234136      204837      163838      136539      117040      102441      81942      68343      58544      51245      41046      34147      29348      25649      20550      17151      14652      12853      10254      8555      7356      6457      5158      4359      3760      3261      3262      3263      32`

### Re: Envelopes

Posted: Thu May 31, 2012 1:02 pm
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.

### Re: Envelopes

Posted: Thu May 31, 2012 1:11 pm
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)

### Re: Envelopes

Posted: Thu May 31, 2012 9:13 pm
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.

### Re: Envelopes

Posted: Fri Jun 01, 2012 10:26 am
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.