## Audio Cheatsheet

### Amplitude

In general:

```Amplitude_dB = 20 log10(intensity/intensity_ref)
```

If we measure relative to a full scale signal:

```Amplitude_dB = 20 log10(intensity)
intensity    = 10^(Amplitude_dB*0.05)
```

So, for example:

```Amplitude_dB( 1.0 )       = 20 log10(1)         = 0 dB
Amplitude_dB( 0.0 )       = 20 log10(0)         = -infinity dB
Amplitude_dB( 0.5 )       = 20 log10(0.5)       ~ -6 dB
Amplitude_dB( sqrt(0.5) ) = 20 log10(sqrt(0.5)) ~ -3 dB
intensity( -6 ) = 10^(-6*0.05) ~ 0.5
```

### Mid/side Processing

From stereo til M/S:

```side = 0.5 * (left - right)
mid = 0.5 * (left + right)
```

From M/S to stereo:

``` left = mid + side
right = mid - side
```

Derivation:

``` mid + side = 0.5 * (l + r) + 0.5 * (l - r)
= 0.5 * l + 0.5 * r + 0.5 * l - 0.5 * r = l
mid - side = 0.5 * (l + r) - 0.5 * (l - r)
= 0.5 * l - 0.5 * r + 0.5 * l + 0.5 * r = r
```

Exponential / logarithmic cross-fades can be performed with this general formula, where out is the result of mixing in_a and in_b from t=0..1 with power p:

```out = in_a t^p + in_b (1-t)^p
```

Different values of p result in different fade shapes:

```p = 0.5 is an equal power fade (at t=0.5, inputs are multiplied with sqrt(0.5) ~ 0.707, ~ -3dB)
p = 1.0 is a linear fade       (at t=0.5, inputs are multiplied with 0.5, ~ -6dB)
p = 2.0 is an exponential fade (at t=0.5, inputs are multiplied with 0.25, ~ -12dB)
```

### Durations

Given that tempo is beat/minute and sample_rate is smp/s:

```beat_duration   = 60/tempo s/b
sample_duration = 1/sample_rate s
```

Examples (assumes sample_rate = 48000 smp/s):

```beat_duration(120) = 60/120 = 0.5   s/b = 24000 smp
beat_duration(140) = 60/140 ~ 0.428 s/b ~ 20571 smp
```

Examples (assumes sample_rate = 44100 smp/s):

```beat_duration(140) = 60/140 ~ 0.428 s/b = 18900 smp
```

### Relative Frequency

If we want to adjust the pitch of a sound a given number of semitones (positive or negative):

```relative_frequency = 2^(semitones/12)
```

Examples:

```relative_frequency(+12) = 2^(12/12)  = 2    # octave
relative_frequency(-12) = 2^(-12/12) = 0.5  # octave down
relative_frequency(+7)  = 2^(7/12)   ~ 1.50 # fifth
relative_frequency(+4)  = 2^(4/12)   ~ 1.26 # major third
```

### Wave length

Speed of sound:

```343.21 m/s
```

Examples:

```wavelength(50 Hz)  = 343.21 m/s /    50 per/s ~ 7 m/per
wavelength(20 KHz) = 343.21 m/s / 20000 per/s ~ 0.017 m/per = 1.7 cm/per
```