PnS Gain Script

Official support for: bluecataudio.com
Post Reply New Topic
RELATED
PRODUCTS

Post

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];
}

Post

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

Post

Ok thanks so could I just write

Code: Select all

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

Post

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.

Post

Ah, that makes complete sense, many thanks.

Post Reply

Return to “Blue Cat Audio”