# Wavefolder/waveshaper - the problem of gain compensation

**URL:** <https://discourse.flucoma.org/t/wavefolder-waveshaper-the-problem-of-gain-compensation/341>\
**Category:** Code Sharing\
**Created:** [November 1, 2019, 1:45pm UTC](https://discourse.flucoma.org/t/wavefolder-waveshaper-the-problem-of-gain-compensation/341 "2019-11-01T13:45:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigo.constanzo](https://discourse.flucoma.org/user_avatar/discourse.flucoma.org/rodrigo.constanzo/32/12_2.png) [@rodrigo.constanzo](https://discourse.flucoma.org/u/rodrigo.constanzo)\
**Post date:** [November 1, 2019, 1:45pm UTC](https://discourse.flucoma.org/t/wavefolder-waveshaper-the-problem-of-gain-compensation/341/1 "2019-11-01T13:45:40Z")

</div>

> [@HPSS/Transients-sliced "realtime" granularization](https://discourse.flucoma.org/t/hpss-transients-sliced-realtime-granularization/339/10):
>
> (I’ll probably post my waveshaper on here too, even though it doesn’t (presently!) use any FluCoMa stuff, as I’m curious as to people’s thoughts)

In the spirit of [the thread on my HPSS/Transients-sliced granulator](http://discourse.flucoma.org/t/hpss-transients-sliced-realtime-granularization/339), here is a wavefolder I’ve been working on.

The comment about not using FluCoMa tools isn’t fully accurate as it turns out. I did use `fluid.loudness~` to try to improve my gain compensation, but it’s still not quite there.

I guess it’s the nature of wavefolding/shaping that you get pretty wild changes in gain as you adjust the parameters, particularly with ones like “creasing”. The gain compensation I’ve come up with works 100% perfectly…60% of the time. I’m kind of wondering if I should incorporate some pre/post `fluid.loudness~` “gain riding” to more automatically get things in the same ball park.

Either way, open to thoughts on this.

It’s a mix of a few different approaches. Some ideas I got from a patch from @tremblap, other’s from a bit of code that Randy shared from the [Aalto](https://www.madronalabs.com/products/aalto) prototype, which is itself based on some Buchla stuff, and finally some ideas from the [Mannequins Cold Mac](https://www.whimsicalraps.com/products/cold-mac) (which is also based on Buchla stuff).

[wavefolder.zip](https://discourse.flucoma.org/uploads/short-url/3Y4a4iziS4Sh7Q9E0XG39nmRpsD.zip) (21.1 KB)

---

<div class="post-metadata">

**Author:** ![tremblap](https://discourse.flucoma.org/user_avatar/discourse.flucoma.org/tremblap/32/5_2.png) [@tremblap](https://discourse.flucoma.org/u/tremblap)\
**Post date:** [November 10, 2019, 3:23pm UTC](https://discourse.flucoma.org/t/wavefolder-waveshaper-the-problem-of-gain-compensation/341/2 "2019-11-10T15:23:22Z")

</div>

Using a perceptual matching rider (loudness before/after) is a good idea for this indeed. I’d use the ITU settings because you can 😉 You’ll have to deal with digital silence and other extreme settings, but that should give you a good idea of how to tame this very (i find) input-dependant gain acquisition.

---

<div class="post-metadata">

**Author:** ![rodrigo.constanzo](https://discourse.flucoma.org/user_avatar/discourse.flucoma.org/rodrigo.constanzo/32/12_2.png) [@rodrigo.constanzo](https://discourse.flucoma.org/u/rodrigo.constanzo)\
**Post date:** [November 10, 2019, 4:46pm UTC](https://discourse.flucoma.org/t/wavefolder-waveshaper-the-problem-of-gain-compensation/341/3 "2019-11-10T16:46:24Z")

</div>

What I tried doing before was using the measurements to figure out how much to compensate for. It’s tricky though since it’s so non-linear (particularly things like ‘Crease’).

Having an auto-gain function would be cool, but I think in the end (as in, post-FluCoMa performance), I’ll end up building an output gain stage. I was just hoping to avoid that. (I do an ok job of this in my distortion module, it’s just harder here).
