# Useful utility functions in Game Audio

Recently I've been spending a lot of time working in Unity, learning about how to program in it and tackling some creative problem solving in relation to game audio. I ended up creating a class of functions that have been very useful to me and I thought they'd be worth sharing here in case anyone else finds them useful.

Note: All code here is written in C# and due to the limitations of Squarespace, my website host, I suggest you copy and paste the code into your favourite text editor to enable syntax highlighting, making it easier to read.

### Non Linear Transforms

I recently watched a video from the GDC Vault entitled "Fast and Funky 1D Nonlinear Transforms" and it was incredibly informative. If you have GDC Vault membership, I highly recommend watching it (link here). These functions allow you to apply a curve to a range of values, useful when changing things like volume or frequency in real time. A couple of these functions are from that video, and the inverse ones are the same principle applied using different curves. I've put the code, followed by an image of what that curve looks like when the value used for n is 2. Increasing the input n steepens the curve.

Note: The GDC talk says that using Mathf.Pow (or similar) isn't as computational efficient as doing the multiplication by hand e.g. t * t * t * t but for the cases I'm using this for in Unity, optimal efficiency is not a priority, so the use of Mathf.Pow was fine.

```// Functions of the form x^n
public static float SmoothStart(float t, int n)
{
return Mathf.Pow(t, n);
}```
```    // Functions of the form (1 - (1-x)^n)
public static float SmoothStop(float t, int n)
{
float smoothStopOutput;
t = 1f - t;
smoothStopOutput = 1f - (Mathf.Pow(t, n));
return smoothStopOutput;
}```

The inverse functions, shown below, also take an input of 0 to 1 but output from 1 to 0. Something to be careful with when using them!

```    // Functions of the form (1-(x^n))
public static float SmoothStartInv(float t, int n)
{
t = Mathf.Pow(t, n);
return 1 - t;
}```
```    // Functions of the form (1-x)^ 1/n
public static float SmoothStopInv(float t, int n)
{
float smoothStopInvOut;
t = 1f - t;
smoothStopInvOut = Mathf.Pow(t, (1/n));
return smoothStopInvOut;
}```

### Range Transformer

The functions above are very useful, but only take an input of 0 to 1 and output 0-1/1-0. Often I've found myself wanting to transform something like velocity from 0-5 units into volume between -100 and 0, or frequency between 20Hz and 20kHz. For this, I built a range class and linear transform function, allowing me to transform a value in one range to the equivalent value in another range.

Here is the TransformLinear function. Note that it takes a Range object as input, the code for that class is below the TransformLinear function.

```    /*
*
* Make sure to include Range.cs class for this to work
*
*/
public static float TransformLinear(float value, Range inR, Range outR)
{
float transformedInputValue = inR.Clamp(value);
transformedInputValue = (transformedInputValue - inR.minimum) / inR.size;
float transformedOutputValue = (transformedInputValue * outR.size) + outR.minimum;
return transformedOutputValue;
}```

And here's the Range class that the TransformLinear function takes as input. Note that it clamps the input values to ensure unwanted output values are avoided.

```public class Range {

public float minimum;
public float maximum;
public float size;

public Range(float inMin, float inMax)
{
minimum = inMin;
maximum = inMax;
size = inMax - inMin;
}

// call this to constrain input to Min/Max values
public float Clamp (float value)
{
return Mathf.Clamp(value, minimum, maximum);
}
}```

### "Invert" function

Finally, I found myself often wanting a function to return not 0 to 1, but 1 to 0. The function shown below does exactly that.

```    // Takes a 0 to 1 range and inverts it, returning 1 to 0.
public static float InvertValues(float value)
{
return (value - 1f) * -1f;
}```