PnS Gain Script

Official support for: bluecataudio.com
KVRist
34 posts since 6 May, 2021

Post Tue Nov 09, 2021 2:33 am

Hello, I am just wndering why the varible double gain=0; and not 1, does it make a difference?

Code: Select all

double gain=0;

void processSample(array<double>& ioSample)
{
   for(uint channel=0;channel<audioInputsCount;channel++)
   {
      ioSample[channel]*=gain;
   }
}

void updateInputParameters()
{
   gain=inputParameters[0];
}

User avatar
KVRAF
4779 posts since 8 Sep, 2004 from Paris (France)

Post Tue Nov 09, 2021 3:31 am

There is no difference, as the gain variable will be updated by the updateInputParameters() function before processSample is called anyway.

KVRist

Topic Starter

34 posts since 6 May, 2021

Post Tue Nov 09, 2021 4:28 am

Ok thanks so could I just write

Code: Select all

double gain; 
and if so why does the factory default to 0?

KVRer
25 posts since 17 May, 2008

Post Tue Nov 09, 2021 4:39 am

It is good programming practice to assign a value to a variable when it is declared, even if that value will be changed almost immediately. In small programmes like this, where a function like updateInputParameters() will be immediately called, it doesn't really matter but, it is a good principle to follow in general programming.

It depends upon the language but when you declare a variable as:

double gain;

at that point it could have any random value because the compiler will allocate memory to store the variable but it will leave whatever value happens to be in that memory location. If your programme attempts to use that variable before it is given a value then the behaviour cannot be predicted. If that happens it is a bug in the programme, because you would not deliberately use a variable before it is given a value, but having that variable's value being potentially random every time you run the programme makes it that much harder to find that bug.

KVRist

Topic Starter

34 posts since 6 May, 2021

Post Tue Nov 09, 2021 5:01 am

Ah, that makes complete sense, many thanks.

Return to “Blue Cat Audio”