Envelopes

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

Envelopes

Postby carbon14 » Thu Sep 02, 2010 1:24 pm

I'm working on the envelope generator at the moment.

There are basically 4 parameters of interest:
AR - attack rate
DR - decay rate
SL - sustain level
RR - release rate.

Setting EGT to 1 will cause the envelope to pause when the decay reaches the sustain level, and it remains at that level until the key is released (KON = 0) then it drops to zero volume (-96dB) following the release rate. If EGT is 0, then it goes directly from the decay to release as soon as it reaches the sustain level.

Also involved is the KRS key rate scaling, which means that higher pitched notes (as defined either by the most significant bits of the F-number, or by the block number) have faster rates.

The OPL4 application guide gives quite a lot of detailed information about how these parameters affect the real world values of the envelope, but you need to bear in mind that the sample rate of the OPL4 differs from the OPL3 and from my investigations so far, it looks as though the OPL4 figures are correct in terms of number of samples, rather than milliseconds, so with a faster sample rate, you actually get a shorter envelope on the OPL3 than on the OPL4 (around 10% shorter)

Decay and release should be straightforward because in these stages, the envelope is linear (in the dB domain) and so a simple count up until the envelopes reaches 8192 (-96dB) would suffice. The documentation says that the envelope resolution is 0.1875 dB which I think would therefore be a value of 16 per increment.

Attack rate however has me stumped for the moment. Although the little sketch in the OPL3 datasheet shows that the attack phase is linear, this is not true, the OPL2 and OPL4 datasheets both show an exponential curve for the attack, and this is borne out by the experimental evidence. There are a maximum of 32 steps in the attack ( fewer if the attack rate is very fast ). An attack rate value of 52 (AR = 13) has 32 samples in the attack phase, an attack rate value of 48 (AR = 12) has 64 samples in the attack phase, but pairs of samples show the same envelope attenuation. I am however struggling to find a plausible algorithm to match the experimental results.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby carbon14 » Mon Nov 01, 2010 11:52 am

I've not put in more than an hours work on this for at least a month. But something intriguing did pop out of that hour's work, so I thought I should share it.

The decay is pretty simple, the attenuation during the decay phase, and presumably during the release phase as well, increases by a value of 8 at regular intervals. The interval is determined by the decay rate, and basically doubles for each successive value of DR.

The interesting thing I noticed however, is that the interval between the end of the Attack phase, and the first attenuation in the decay phase seems random. So far the only plausible explanation I have for this, is that there is 1 global clock which triggers the increment of the decay phase attenuation. I have in mind a single global clock which probably runs continuosly, and whenever a particular bit flips, those operators in the decay or release phase increase the attenuation value. The particular bit necessary would vary according to the value of DR or RR.

I hope to do experiments with multiple sounds, triggered at different times, and with different envelopes, to test whether the attenuation increments in different sounds are synchronised. Unfortunately I suspect that this hypothesis will be easy to disprove, but difficult to prove.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby opl3 » Mon Jan 16, 2012 9:25 pm

I figured out the attack envelope levels. Measuring the peak amplitude (sine or square) is not enough, since at beginning the amplitude is so low that multiple envelope values give out the same result. Thus actual waveform of sine is better to see if it matches or not.

The first envelope values that can be seen are 342,299,261,228,199,174,152. They seem to relate to each other by about 0.875 so there is a pattern.
Code: Select all
//attack envelope
val=val-val/8-1;


The minus one makes sure the envelope goes down to 0 when it is small.

It can also be explained with much simpler algorithm if one's complement is used:
Code: Select all
//attack envelope with one's complement arithmetic
val=val+((~val)>>3)


After figuring that out, that very much resembles MAME emulator code for attack phase.

What is important that the envelope starts from value 511, so the formula gives out two values that have so small amplitude that waveform is zero (or negative zero of -1). This is evident if a square sound with additive modulation is started with operators having AR=1 and AR=15. FNUM=0 so rate scaling is also 0. The faster sound turns on immediately, and the slower sound envelope period is 4096 samples. The slower sound starts to affect after more than 8192 samples, because it needs two 4096-sample periods for envelope values 447 and 391, and the rest of the 0-4096 samples are there because the phase of the clock that triggers those advancements in the slower sound is not related to key-on of the sound that turns on immediately. It means that after key-on, the attack phase has value 511 until the next bit flip of internal counter with some period.

The attack phase stops immediately when attack envelope level becomes zero, so the zero level period does not depend on attack rate but decay etc. If a sound with AR=1, DR=15, SL=15, RR=15 is started, envelope level is 1 for 4096 samples, 0 for 2 samples, and then decay (or release period?) has control.

Mind you that I have not studied the effect of rate scaling much yet, but I have seen that it results in the period to change faster sometimes. Perhaps the whole period is subdivided into several shorter periods, like 8*512 periods when AR=15 (15*4+0=60) to get period of 4096.

This also verifies the fact that trying to get identical samples out of a real chip and emulator hard if not impossible - or at least first the state of real chip (like AM LFO phase etc) must be examined by various methods and emulator configured to that state accordingly.
opl3
 
Posts: 55
Joined: Sun Sep 26, 2010 8:11 pm

Re: Envelopes

Postby carbon14 » Thu Jan 19, 2012 9:58 am

Well, I'm glad that someone here is still working.

I've been very lazy recently, and also I'm having a new bathroom fitted which has made physical access to my study rather tiresome. I promise to do some more soon.

Anyway, that sounds like an excellent analysis, I tried and tried again to come up with an algorithm that matched, and I couldn't get anywhere close. And yet when you describe it, it seems so simple.

Thank you very much indeed for that contribution.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby carbon14 » Thu Mar 15, 2012 1:43 pm

I did my own work through these figures and I came to the same conclusion that 511 is the starting value.

I believe that there are a few possibilities for this starting figure, 512 would also reduce down to 447, 391, 342 and anything up to 514 would reduce down variously to 342 which is the first value we can have any confidence with.

But obviously 511 is an aesthetically pleasing value, it requires 1 fewer bit for storage than the others and it's trivial to reset it when triggered by KON

I was also concerned that the step counter for the envelope was not reset by KON. I presume that there is just one step counter globally and individual bits trigger step changes according to the selected rate.

I know that the highest amplitude of the decay phase may not last for as many samples as the subsequent amplitudes, for the same reason that that you cite where the first amplitude of the attack phase may be shorter, it simply lasts until the next appropriate tick of the internal counter.

In fact there's no reason to suspect that this is not the same step counter used for the AM and FM(VIB) modules.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby carbon14 » Mon Mar 19, 2012 9:13 am

I've been working back through the experiments that I first did in September 2010 on the attack phase of the envelope.

These gave me confusing results, which I now know where in part due to a bug in my experimental setup. I was managing to set the KeyOn before finalising all the other parameters.

Now that we have the general algorithm for the attack, I have revisited those results and can make better sense of them.

My run through with AR set to 12 gave me good results, matching those found by opl3. With two samples per value.
My run through with AR set to 13 gave me a different set of values, but I can now see that this was due to one operator running 1 sample later than the other.
AR=15 gives an almost instantaneous attack which I would like to investigate further.
AR=14 however is most interesting. This follows a different curve. From AR values from 13 down to 1 the set of amplitudes is consistent, which each value simply repeated for more and more samples.
AR=14 however has a similar but different algorithm. The reducing function f(x) which gives the next attenuation given the current attenuation x is:

f(x) = x - floor(x/4) - 1

this compares with the more common reducing function seen from AR values of 13 down to 1:

f(x) = x - floor(x/8) - 1


it should be noted than two iterations of the second function are not equivalent to one iteration of the first. This is an entirely separate function.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby carbon14 » Tue Mar 20, 2012 11:52 am

I re-ran the september 2010 experiments and confirmed the results that I got from last time.

Oddly I still had one run through where the two envelopes ran 1 sample out of step with one another. I'm not clear why that was, but taking that into account, the results remain consistent with the two algorithms previously described.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby opl3 » Tue Mar 20, 2012 9:11 pm

Thanks for the info on the double-speed envelope at AR=14, I just began to think that after thinking about some other things like rhythm mode and noise generator that I should figure out that as well, as it can't really run at double the speed, more like twice the step size it seems. The formula seems logical, just the bits are shifted by one position before calculating. Easy implement in hardware. To my knowledge, AR=15 really is instantaneous, it could have delay regarding the phase generator but it should go from max attenuation to 0 attenuation in one step.

Also yes you are correct when you see the off-by one sample issue - if you happen to write the key-on to the chip at a time when it has already processed the modulator but is about to process the carrier, they start from a different sample then, so carrier has one sample advance.

But about the rate, it should be noted that the rate is internally scaled by four and is offset by key rate scale value, so in fact the range is 0 to 63. So when you use attack or decay or release rate of 1, it should actually be rate 4 internally if there is no scaling offset from frequency. So it might be that when we think that at FNUM=0 and RR=1 (internally 4), one rate step is 4096 samples, but in fact the rate clock might actually run at every 1024 or 512 samples. When rate is internally 5, it might sometimes change after 4096 samples, but sometimes after some other amount of samples, and that will tell what is the base frequency and what is the increment before overflow happens and it advances to next step. I think it might be so that the step rates are 4/8, 5/8, 6/8 and 7/8.

Do you have plans for making an own forum topic for rhythm mode, its noise LFSR etc?
opl3
 
Posts: 55
Joined: Sun Sep 26, 2010 8:11 pm

Re: Envelopes

Postby carbon14 » Mon May 21, 2012 8:53 pm

Sustain level.

The sustain level register SL varies from 0 - 15 and represents the attenuation level at which the envelope is held (while KON=1 and EGT=1), or where the envelope shifts from the decay phase to the release phase (if EGT=0)

A value of 0 represents no attenuation, with each increment representing another 1.5dB attenuation, or an additional 128 feeding into the inv-log lookup
A value of 15 however represents maximum attenuation which for our purposes appears to be 48dB given the apparent starting point of the envelope at 4088.

When the envelope amplitude (in inverse log terms) rises up to meet or exceed the sustain level, the phase shifts from the decay phase to the sustain or release phase (according to EGT). Changing the sustain level during the decay phase can cause an immediate shift to the next phase. I have not yet tested the effect of increasing the sustain level during the sustain phase.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Re: Envelopes

Postby carbon14 » Mon May 21, 2012 9:01 pm

Decay rate / release rate

The decay rate and release rate registers DR and RR vary from 0 to 15.
They represent the rate at which the attenuation grows during the decay and release phases of the envelope.

bear in mind that the register values actual represent rates from 0 to 60 in steps of 4, because the final rate is calculated from DR/RR and also the BLOCK, F-NUM, NTS and KSR registers

a register value of 0 gives no decay/release at all, the amplitude remains constant.

a register value of 1 gives an attenuation increase of 32 per sample
a register value of 2 gives an attenuation increase of 16 per sample
a register value of 3 gives an attenuation increase of 8 per sample
a register value of 4 gives an attenuation increase of 8 every other sample
a register value of 5 gives an attenuation increase of 8 every 4th sample
etc.

As with the Attack, the step change occurs at a point which appears to be independant of the note start time, and is perhaps dependant on a bit change in a master counter. As a result, the first step in any given phase of the envelope may be shorter than subsequent steps.
User avatar
carbon14
 
Posts: 124
Joined: Tue Aug 05, 2008 9:11 am
Location: York, England

Next

Return to Yamaha OPL-3 research

Who is online

Users browsing this forum: No registered users and 2 guests