Trying to make the most simple of distortion, getting errors


#1

Hi there, so bascially im just trying to make the simplest of hardclip distortion, that being:

If input >= 1
input = 1
If input >=-1
input = -1

Ive run into an error that i really have no idea about., here is the actual code

and this is what im getting in console

Can anybody help me out ? ive tried assigning the input into a variable to use that instead of inlet_in, but i couldnt work out what data type to call it, all of the ones i used pulled up even more error messages.


#2

ok, you have a few issues

a) you cant assign to an inlet, you need to create an outlet (make sure its the correct type)
b) your syntax for if statements is incorrect, for compound statements, it is

if( condition) {
statement;
}

c) audio buffers are not straight floats... you need to do some bit shifting to take them to Q27 format

some general recommendations

i) Id recommend you look at other objects for code examples
ii) there is a good guide about object development in the user-guide section, make sure you read the section on formats, see here
ii) you might want to get a book on C, reading and understanding the basics, will be much quicker, and less frustrating than trying to 'wing it' ...


#3

First off thanks for the advice!

Upon some revisions i managed to get some things working, thanks for pointing out the syntax errors!

I did some more playing around and managed to get it slighty working, in the fact the it was only spitting out the positive half of the wave (in the scope) which was weird. Looking into the factory hardclip i noticed there are 2 rectifiers in the chain, i'm wondering if this has something to do with it ?
Furthermore after a bit more fiddling with it i seem to have taken a few steps back and now it does output something, but not enough to audibly hear, and is the tiniest movement on the scope, ive got a feeling its to do with bitshifting (i've read all the stuff on here about it and watched several videos, i understand get the concept but putting that into practice is very difficult for me.

Thanks for the recommendations as well but i have already been doing the first 2 things :slight_smile:
I do have some experience in c/c++, however it was all with floating point math so working with fixed is still somewhat new to me.


#4

If you like to work in floating point it's fine, you just have to float-divide the inputs by 2^27 (this way you'll scale them to 1) and then multiply again by 2^27 before casting to int32_t for output


#5

this might be a silly question, but how would i actually go about doing this ?

would be be something simple like:

uint32_t inf = (inlet / 2^27)

int32_t out = (inf * 2^27)

??


#6

Better to type it in float format: 134217728.0f

Yep, is that simple, anyway