ModPlug Central

OpenMPT => Development Corner => Topic started by: Paradox Uncreated on April 16, 2012, 11:52:15

Title: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 11:52:15
As you know, in the 8-bit days, one usually clipped the samples a bit, to increase SNR.

I just made a very simple and optimal softclip, using two x-(x*x*x*0.3etc), in series with a threshold. This gives the third-order distortion of vintage gear, driven mixers, and often what people like with "hot" signals, on whatever it may be, on typically analog gear.

I recently tried OpenMPT on one of my old tracks, and with the Blackman Exact interpolation, it sounded actually quite good. I did however remember driving the levels there quite hot, but with the sampler of the time, (and most of them I think) there was no softclipping. So that can be retrofitted to the samples :) (probably pr. voice, before interpolation?)

What do you think?

(Edit: having the softclippers in parallel might be even better!)
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 19:08:50
I actually tried a batch-process on the samples now, with the softclip algo.
And it definately sounds smoother. Maybe I`ll adjust it abit, because I was used to the hardclipping overtones being present. But it`s already quite low -3.6dB knee.

I tried downloading the source, but I haven`t got visual studio installed, just an old MSVC6.0. So for the time being, me doing the tweak is not going to happen.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: LPChip on April 16, 2012, 19:57:32
I'm not a developer, but this sounds very nice. :)
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 20:17:58
Yes, it is one of those things you can actually do to remedy hardclipped samples. Ofcourse it`s mostly a problem with older modules probably. Maybe someone used noisy 16bit converters and felt the need to drive the levels hot too.

It`s actually a big discussion out there, about hardclip vs softclip. And the sound of softclip. And doing the softclip I am doing, is close to some good sounding transistors. It is actually optimized for transparent clipping, and therefore even preferrable to analog, for me.

So in addition to improving the sound with Blackman Exact, and smooth level changes, you can do this too :)

Knee-adjustments might need finetuning resolution, of two decimals. For the use here, on samples knees from 0 - -6dB is probably the range we are talking about.

Also ofcourse processing in 8bit would be bad, which I doubt is happening. So conversion to the resolution used, would be required.

Peace.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: LPChip on April 16, 2012, 20:33:39
Can you provide an audio example that shows the difference between normal hardclipping and your softclipping version?
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 20:34:46
Yes, the mod only, or do you want to hear it with the mastering effects aswell?
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 21:12:22
https://www.youtube.com/watch?v=-7H2I40-3XU
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 16, 2012, 21:47:25
I am still tweaking the algo a bit.

It`s the kind of thing were two decimalplaces matter, if you know what I mean. So doing batchprocessing on that, reloading samples, setting vols/tune, is a bit tedious, to actually try and find that value in practise.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 01:08:14
Notice particulary on the 303 sound there, that some digital edgyness is gone. That was purely due to hardclip.

Maybe a better name for the usage of this particular algorithm in this context, would be Softclip/De-hardclipping-knee.

Peace.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: LPChip on April 17, 2012, 10:05:44
Cool. I'll check them out tonight when am at home.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 12:51:41
:-)

Do you have any idea if/when the developers might insert this tweak into the code?

And if they are busy maybe they could just say where in the code it would need to be inserted, and I might get someone else with the compiler installed, to do it. And maybe, if it is asm or not, maybe that complicates things.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 13:57:20
Maybe it is ratetransposer.cpp..
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: LPChip on April 17, 2012, 14:14:37
Quote from: Paradox Uncreated on April 17, 2012, 12:51:41
Do you have any idea if/when the developers might insert this tweak into the code?

And if they are busy maybe they could just say where in the code it would need to be inserted, and I might get someone else with the compiler installed, to do it. And maybe, if it is asm or not, maybe that complicates things.
Jojo is pretty active on the forums too. He'll probably answer that himself. :)
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 14:20:39
Anyone has this compiling?
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 14:48:45
I think the function would look something like this:

double kn(double val)
{
   // antihcknee (anti-hardclipping-knee) should probably be a double, and a setting in the gui.(range 0..-6dB)
   // 0 being 0dB (no knee, which can also be optimized) and 1 being -6dB.  Two digit decimals in dB is probably good. e.g: -2.51dB knee.
   double b_knam = pow(2,(((antihcknee)*-6))/ 6); // should probably be placed somewhere else..
   double b_3rdmul = 1; b_3rdmul = b_3rdmul / 3; double b_3rdnorm = 1/(1-b_3rdmul); // same
 
   if (val > 1) {val = 1;} else if (val < -1) {val = -1;} // can be optimzed out, if there is no weirdness anywhere.

   double valr = val;
   if (valr < 0) {valr = -valr;}
   if (valr > b_knam) {
    valr = valr - b_knam;
    valr = valr / (1-b_knam);

   valr = (valr - (valr * valr * valr * b_3rdmul)) * b_3rdnorm;
   valr = (valr - (valr * valr * valr * b_3rdmul)) * b_3rdnorm; // can reduce the 3rdnorm here, and a div in the next.
   valr = (valr / b_3rdnorm) / b_3rdnorm;

    valr = valr * (1-b_knam);
    valr = valr + b_knam;
    if (val < 0) {valr = -valr;}
    val = valr;
   }
   return val;
}

This would do the monosamples anyway. It seems he has different code for stereo.. Someone would have to modify that aswell, if they want to use it on stereostuff.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 17, 2012, 14:57:00
Or maybe it is intereger code, then you aren`t working with +-1 ofcourse.. We`ll work it out.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Saga Musix on April 18, 2012, 13:55:23
Quote from: Paradox Uncreated on April 17, 2012, 14:48:45
Is there any problem with including GPL code in a BSD project btw?
It can lead to problems. I'd rather prefer to incorporate BSD code. You can actually dual-license your code if it's necessary.

Concerning an inclusion in OpenMPT, that is theoretically possible of couse, but I personally don't think it's a good idea to implement obvious colouring on mixing level (yes, EQ, Reverb and Pro Logic Surround do the same, but I didn't say that it's a good idea to have them *at all*). If it is possible to apply the algorithm globally (i.e. on the whole mix instead of individual samples), it might be more feasible to implement it as a plug-in instead.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 18, 2012, 13:59:37
Quote from: Jojo on April 18, 2012, 13:55:23
Quote from: Paradox Uncreated on April 17, 2012, 14:48:45
Is there any problem with including GPL code in a BSD project btw?
It can lead to problems. I'd rather prefer to incorporate BSD code. You can actually dual-license your code if it's necessary.

Concerning an inclusion in OpenMPT, that is theoretically possible of couse, but I personally don't think it's a good idea to implement obvious colouring on mixing level (yes, EQ, Reverb and Pro Logic Surround do the same, but I didn't say that it's a good idea to have them *at all*). If it is possible to apply the algorithm globally (i.e. on the whole mix instead of individual samples), it might be more feasible to implement it as a plug-in instead.

On mixing levels? I don`t understand that statement. It is simply to declip samples, that were hardclipped to gain a bit more SNR back in the old days. By introducing a softknee before clip, we simply change the overtones of the clipping, into something more pleasant. And as I said earlier, that is the kind of sound many people actually want. Implementing it globally would defeat this purpose. But you do as you wish with your tracker. If you do not want to include it in your upstream version, can you compile the hack for me? I would really like to listen to my old modules, fixing some of my hardclipping there. As for the DSP stuff, yes I do agree, there is not neccesarily any point in having that, but this is not DSP in that sense. This is about fixing old 8bit samples. :)

Peace.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Saga Musix on April 18, 2012, 14:05:11
QuoteOn mixing levels? I don`t understand that statement.
On the whole mixing buffer instead of individual samples. But I see that this wouldn't make much sense anyway, so forget about it.

It would also be possible to implement it as a new sample editor tool (of course with appropriate shortcuts to apply it to all samples at once). You'd still have to apply it manually then, but I feel better about that than implementing it as a mixing step at the moment.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 18, 2012, 14:06:54
A batch process in the sample editor would be fine aswell. Maybe even better.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Harbinger on April 23, 2012, 13:45:22
I agree -- this would serve better as a sample editing tool....

But if i understand this correctly: your algorithm lessens a hard-clipped sample (which would then sound badly distorted) and attempts to re-form the peaks based on the slopes of the waves?

If this is true, this could DEFINITELY be useful, especially in MPT! :D
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 23, 2012, 14:45:21
No, it`s not a declipper. It`s a dehardclippingknee.

Usually "digital clip", and a/d converters at the time of amiga/atari/acorn, usually clipped abruptly.
That means small clips, in the a/d converters, as many did then, because it was 8bit, and tried to make the most use of the headroom, will give very particular distortion, that is psychoacoustically often percieved as noise, clicks, sparks, etc. However if one simply adds a softknee after the clip, aka "gradual clip", gradually reducing the level before clip, the overtones changes, and much higher levels of clip can be tolerated psychoacoustically, before one thinks of signal degradation. Infact, some even PREFER the sound of softclip, to digital nonclipped sound, and buy analog mixers with transistors purely for that purpose.

It`s been quite a big discussion online. Declipping would be reconstructing the signal, yes. I have heard some declippers, and it is not something I have used. Could be that there are other declippers there that are better though, I could always have a look into it. It might be possible to alter overtones more sophistically with more of a declipper algorithm.

Soundexample:

1 - hardclipped (http://paradoxuncreated.com/Projects/tests/clip_hard.wav)
2 - softclip (http://paradoxuncreated.com/Projects/tests/clip_soft.wav) -2.4 knee. One should ofcourse tweak the amount for ones liking, or more of a average sound, which is what I am trying to do with my modules, for just a little more refinement. I am inspired to have a look into declipper algorithms now though, maybe I can improve it.
3 - softclip exaggarated (http://paradoxuncreated.com/Projects/tests/clip_softexag.wav) +9dB gain (all clipped), -23.55knee

As you can hear, hardclip sounds itchy and scratchy.
With softclip it is less noticable.
Exaggarated softclip shows just how much you can drive a signal, without neccesarily it being unmusical.

The exaggarated softclip is also closer to many vintage synths level of distortion. Because they softclip, people generally tend to think they are more hifi than they are, it isn`t until recent times that actually including a good softclip algorithm in a digital softsynth, has become standard. And has also been part of feeding the "analog is better/phatter/warmer etc than digital" debate.

Everybody agrees that the hardclipped version sounds odd right?

Very typically on old modulars, levels were hot. A lot of people who have sampled them, sample them that way. They do set the osc levels that high, and that is part of the sound they like. Moogs are also widely stated to be saturated. etc.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 23, 2012, 15:24:08
Maybe just having batchprocessing with VST plugins, in the sampleeditor will be better.

This is declipped  (http://paradoxuncreated.com/Projects/tests/clip_perfectdeclipper.wav)version.

Declipped with http://www.perfectdeclipper.com/download/

Much better, if you are not looking for "analog" sound. Which I was originally looking for, but I can try this aswell.

Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 23, 2012, 15:45:09
Well I tried it on more of a mix now. The samples I am talking about will probably exibit the same, although it would probably work well on bass samples, like the one before. I have mixed some drums though, in my modules, wellknowing that the output would clip, but kindof setting the levels so it wouldn`t be so noticable. That way I could do some nonlame drumtracks, without using all the amazing 4 tracks. :)

This declipper as many declippers output much higher levels than the sample, I had to set this to a minimum of 3x original level, for decent declipping. I think that is far too much, and it changes the sound of the samples too much. It sounds very much like a typical declipper that way. Suddently you have large peaks there, that shouldn`t be there anyway.

It is possible to implement a little bit of declipping mindset into a more of an overtone based approach though, which would not need so much higher levels.

I can think a little bit about it, and whip up a plug, if something easily doable occurs to me.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Harbinger on April 26, 2012, 13:26:14
Okay, now i understand...
I think a clipping softener would be useful, as, it seems from the way you describe it, that aurally higher volumes could be more tolerated and not so drastic in the clip distortion effect.

I think this and a declipping function would be great in MPT's sample page.  8)
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on April 26, 2012, 13:33:36
A vst plugin function, so people can apply whatever they want would probably be nice. No need to hurry though, I have pretty much done what I wanted to do anyway.

Just to follow up on my last post, I did try to combine a bit of looakhead, to apply softknee to the clipped area only. My first experiments are that good. So obviously one needs to get into the whole algorithm more, to find out where one can tweak it. Which well, I have already done what I wanted to do so, I am not too interested in it. But yeah, there are many declippers out there, and people who have spent time on this, so vstplugin function would probably cover the needs of anyone who needs to apply such things.

Peace.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Saga Musix on April 27, 2012, 15:57:38
Quote from: Paradox Uncreated on April 26, 2012, 13:33:36
A vst plugin function, so people can apply whatever they want would probably be nice. No need to hurry though, I have pretty much done what I wanted to do anyway.
That has been long on the to-do list, but it's not a trivial thing to do.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: Paradox Uncreated on February 17, 2019, 21:04:05
mm ;/
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: TheRealByteraver on February 18, 2019, 20:40:49
I found a short paper about this subject, the math is (as usual) above my paycheck though  :'( . It has been the subject of quite some research in recent years apparently.

https://mathematical-coffees.github.io/slides/mc04-kowalski-additional.pdf
Title: Re: Implementing a simple softclip algo..
Post by: Paradox Uncreated on March 03, 2019, 09:17:30
Well, that was a bit more sophisticated version, that I had in mind though, and actually may add peaks to the signal one does not want. Rather a declipping knee, probabably two third order knees in parallel, with slightly different thresholds.
Title: Re: Implementing a simple softclip algo, pr voice.
Post by: TheRealByteraver on March 04, 2019, 21:10:01
Do you have an example I could listen to? Like hard versus soft declip of a sample?