Generating LUTs for Cosine and Exponential Volume Fades

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Generating LUTs for Cosine and Exponential Volume Fades
by on (#69996)
I'm pretty rubbish at maths programming so I was wondering if anyone could write some C code to generate a couple of look-up tables.

I need the values from 00 to 0F plotting on both a cosine (S-Curve) and also exponential curve with a range of angles (is it?).

The LUTs should be 16 bytes each of course.

Like these;

http://transom.org/?p=7543

by on (#70005)
One odd thing about the linked page is that he looks at the fade waveforms in the linear space, even though we perceive it in an exponential space. In other words, his exponential fade is really linear, getting louder uniformly, while his linear fade is really exponential, getting loud quickly, then more slowly reaching full volume.

Here's a quick C program I whipped up: gen_vol_fade_curve.c

It graphs the output, for easy experimentation. Example of exponential curve, generating a 32-entry table:
Code:
.byte   0,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  4,  4,  4,  5,  5
.byte   6,  6,  7,  7,  8,  9,  9, 10, 10, 11, 12, 12, 13, 14, 15, 15


;  0 *
;  1    *
;  1    *
;  1    *
;  1    *
;  2        *
;  2        *
;  2        *
;  2        *
;  3             *
;  3             *
;  4                 *
;  4                 *
;  4                 *
;  5                     *
;  5                     *
;  6                          *
;  6                          *
;  7                              *
;  7                              *
;  8                                   *
;  9                                       *
;  9                                       *
; 10                                           *
; 10                                           *
; 11                                                *
; 12                                                    *
; 12                                                    *
; 13                                                        *
; 14                                                             *
; 15                                                                 *
; 15                                                                 *

by on (#70013)
Just my 2 cents :
To decrease/increase a value exponentially on the NES, you don't have to use lockup tables, you can do something like this :
Code:
  lda Value
  lsr A
  lsr A
  sta Temp
  lda Value
  sec
  sbc Temp
  sta Value        ;Value = 3/4 * Value


This will make the Value decrease exponentially, by a ratio of 3/4 every time the code is called. Of course it will not work very well for small values, and you can only use fixed ratios with this method.

Otherwise use a soft such as Open Ofice Calc to do it, and copy the values manually with .db statements.... annoying I know but about the same trouble as writing a program just for that.
I did such a thing once for a 12*32 exponential pitch table, and man it was annoying.

by on (#70018)
blargg wrote:
One odd thing about the linked page is that he looks at the fade waveforms in the linear space, even though we perceive it in an exponential space. In other words, his exponential fade is really linear, getting louder uniformly, while his linear fade is really exponential, getting loud quickly, then more slowly reaching full volume.

Here's a quick C program I whipped up: gen_vol_fade_curve.c

It graphs the output, for easy experimentation. Example of exponential curve, generating a 32-entry table:


Blargg, thank you so much. That is superb! :D

I've realised now that what I had in my head wasn't a cosine curve but possibly an inverse exponential curve (if that's the correct term). You can generate it by setting the 'adjust' parameter in your exp_curve() function to less than 1.0.