YM2612 shape of envelope attack

For anything related to sound (YM2612, PSG, Z80, PCM...)

Moderator: BigEvilCorporation

Post Reply
GreenLine
Interested
Posts: 17
Joined: Tue May 10, 2011 3:42 pm

YM2612 shape of envelope attack

Post by GreenLine » Fri May 27, 2011 10:24 pm

Maybe this is already well understood but it's all new to me so I though it doesn't hurt to share.

After quite a bit of research, I've found this interesting patent:

http://www.google.com/patents?id=2S4pAA ... CCgQ6AEwAA

It explains why the "default" shape of the attack part of an envelope expressed in decibel is not desiderable, and provides two different methods to correct the problem.

The first method uses a conversion table to adjust the envelope value, whereas the second method is very clever and uses the upper bits of the envelope value itself to adjust the attack rate (the envelope starts at 1023 i.e. all 1's and goes towards 0, so it provides a good "kick" to the attack rate).

I can't test that on a real chip but I think it's very much possible that the YM2612 is using one of these two methods.

I've hacked a quick simulation in JavaScript. This simulation uses the tables published in the famous "OPLx decapsulated" post, that we know are inside the YM2612. It plots the table approach in blue and the approximated approach in green... they are very close! (The two curves are offset by 30 pixels so it's easier to compare them visually).

Sorry for bad code style and magic constants, I was too curious to try and coded that in a hurry! It also requires a browser such as Safari or FF 3.6+ that understands the "canvas" element.

http://www.ascotti.org/programming/temp/attack.html

GManiac
Very interested
Posts: 92
Joined: Thu Jan 29, 2009 2:05 am
Location: Russia

Post by GManiac » Sat May 28, 2011 12:41 pm

EG uses next formula for ATTACK RATE = 1
att += not(att) asr 4

When I tried to get template of EG attack values on hardware I used modualtion of oscillator by sum of 2 operators (algo 5, as far as I remember). Modulators were at sin256 phase (maximum), frequency = 0, TL = 0, amplitude at start = 0. Oscillator was oscillating at TL 0 at very low frequency, so I could see time when it cross for example, its null-point, on oscillogram.
Changing ATTACK of modulators from 0 to 1 and back to 0 after some time, I could increase their amplitude by 1 step of ATTACK phase, step by step. When their ampiltude increases, oscillator must shift its phase. By viewing shifts of phase, I can calculate value which amplitude of modulators was increased by.
Why I didn't ATTACK oscillator? Because of DAC we can see only 9-bit value of oscillator's output. Using "2 modulators" method I can see the whole 14-bit value of phase shift of oscillator.


Here are experimental values of sequenced shifts of oscillator. Sorry for misexaplnation, these are my old notes on YM2612. If you are familiar to EXP_TAB, SIN_TAB and know that operator is 14-bit (1 for sign), and phase shift = (modulation div 2), you should understand these tables.

Increasings of ATTACK stpes of 2 modulators at sin256, TL=0

Code: Select all

0        1-й STEP of ATTACK1
0        2nd
0
1
1
2
3
4
6
8
11
15
20
26
32
38
47
58
66
77
90
102
115
127
139
150
158
164
167
186
186
205
198
218
206
224
204
218
194
204
214
228
192
200
208
160
168
172
180
184
192
132
132
136
140
144
144
148
76
76
80
76
80
80
80
80
84
84
84
84
88
84
88        73th step
0         74th, modulators are at their maximum

Cumulative increasings (output of sum of 2 modulators):

Code: Select all

0         1st step
0
0
1
2
4
7
11
17
25
36
51
71
97
129
167
214
272
338
415
505
607
722
849
988
1138
1296
1460
1627
1813
1999
2204
2402
2620
2826
3050
3254
3472
3666
3870
4084           attenuation 256 = 0001 0000 0000
4312                      236 = 0000 1110 1100
4504                      220 = 0000 1101 1100
4704                      204
4912                      188
5072                      176
5240
5412
5592
5776
5968
6100
6232
6368
6508
6652
6796
6944
7020
7096
7176
7252
7332
7412
7492
7572
7656                      
7740                      20
7824                      16
7908                      12
7996                      8
8080                      4
8168       73th step       0
8168       74th step

Last table shows OUTPUT of modulators. Knowing output, we can calculate attenuation, used to get it. For this we must use EXP_TAB. For example, attenuation of 0 gives us output of 8168. Attenuation of 256 ( 256 hex cents ) gives division of output by 2.00 -> output of 4084. And so on. Low output values give us range of attenuations with which they should by get.


As you can see, first 2 bits of attenuation are always 00, because 1 EG step = 4 cents.
And sequence of 256, 236, 220, 204, 188, 176, etc. is really sequency of 64, 59, 55, 51, 47, 44, etc.
And they are suit to fomula.

GreenLine
Interested
Posts: 17
Joined: Tue May 10, 2011 3:42 pm

Post by GreenLine » Sat May 28, 2011 10:40 pm

Thanks a lot for this data, it's very precious to me as I have no way (and not enough knowledge) to collect it with such precision! It will now take me a while to digest it and see how it fits with the formula described in the patent. :)
Your explanation was very clear. If by any chance you have collected the output of the DAC (at its lower bit resolution) in the experiment with a single oscillator, that would also be great to see!

GreenLine
Interested
Posts: 17
Joined: Tue May 10, 2011 3:42 pm

Post by GreenLine » Sun May 29, 2011 12:50 am

GManiac wrote:EG uses next formula for ATTACK RATE = 1
att += not(att) asr 4
Amazingly, the formula derived by the patent provides the same values for attack rate = 1:
att -= 1 + (att >> 4);

However, they produce different sequences when attack rate > 1.

GManiac
Very interested
Posts: 92
Joined: Thu Jan 29, 2009 2:05 am
Location: Russia

Post by GManiac » Sun May 29, 2011 10:42 am

For values > 1 the formula should be
att += template_step * (not (att)) >> 4
Maybe it's a bit incorrect. It's hard to test increasings when template_step is 2 or more, because of pretty high values of AR to get it and pretty short periods between ATTACK steps.

template_step is taken from EG_TEMPLATES, it can be 0 or 1 or 2, etc. Look at tables in MAME Source. There is complex dependency on current AR value, value of EG timer (my tests show it has 4095 values, not 4096) and template_step.

For example, I use next code in my emulator (in Delphi):

Code: Select all

Att_Incr := s16( not( EG_Att ) * template_step ) shr 4;
inc( EG_Att, Att_Incr );
s16 = type of signed 16 bit
EG_Att = current attenuation delivered by EG ("att" in formula)

GreenLine
Interested
Posts: 17
Joined: Tue May 10, 2011 3:42 pm

Post by GreenLine » Mon May 30, 2011 10:20 am

I've modified MAME to use the formula:
att -= template_step + (att >> 4)
It works at a first glance but will take me some time to do better tests.

Eke
Very interested
Posts: 885
Joined: Wed Feb 28, 2007 2:57 pm
Contact:

Post by Eke » Mon May 30, 2011 1:31 pm

Have you read this thread ?

viewtopic.php?t=386&postdays=0&postorder=asc&start=106 (original post, with some inaccuracies, see below)

viewtopic.php?t=386&postdays=0&postorder=asc&start=417 (fixes inaccuracies of previous post)

It explains how the enveloppe generator works on YM2612, and afaik MAME core is already using the correct formula.

GreenLine
Interested
Posts: 17
Joined: Tue May 10, 2011 3:42 pm

Post by GreenLine » Mon May 30, 2011 2:23 pm

I have read the thread, several times actually... it's legendary! :-)

Since reading that thread, I've been trying to understand the reasons behind the important design choices done with the YM2612 and similar chips. That's basically what keeps me interested in emulation. Mind you, I'm still happy when I can just get the job done no questions asked but I feel even better if I can learn something in the process.

I've found that several points are explained in patents filed by Yamaha in those years. Between the answers, I also found some new questions.

I think the formula for the attack rate does not blend perfectly with the rest of the chip design because it uses multiplication (even though by power of two) and assumes that not(att) becomes a negative number in two's complement.

That's far from me saying that it's not correct, I'm just saying that is enough to trigger my curiosity and wish to understand more.

I plan to play with the formula suggested by the patent and see what comes out of it. If as expected it will ultimately fail to work then I will happily adopt MAME's formula together with a big acknowledgement in my code! :-D

Post Reply