Table of Contents

Class SoundSynth

Namespace
FishMMO.Client
Assembly
FishMMO.Client.dll

The building blocks every synthesised sound is made from: seeded noise, filters, envelopes and the seamless loop. There are no audio files in the project; each sound is a recipe over these, run from a fixed seed.

public static class SoundSynth
Inheritance
SoundSynth
Inherited Members

Remarks

The same seed makes the same sound on every machine. Everything here is plain managed arithmetic in single precision, run in one order, with no Unity call inside: the same recipe gives the same samples on every client, and it can run off the main thread (SoundLibrary). Only ToClip(string, float[], int) touches Unity.

Filters are the RBJ cookbook biquads (Bristow-Johnson's Audio EQ Cookbook), direct form I in float.

Fields

Rate

The sample rate every loop and footstep is made at.

public const int Rate = 44100

Field Value

int

SpeedOfSound

The speed of sound in air near the ground, metres a second (15 °C).

public const float SpeedOfSound = 343

Field Value

float

Methods

AddBurst(float[], int, ref Noise, float, float, float, float, int, float)

A short burst of filtered noise added into into: an impact's click, a grain's crunch. The envelope is a fast attack and an exponential fall over seconds.

public static void AddBurst(float[] into, int start, ref SoundSynth.Noise noise, float centre, float q, float amplitude, float seconds, int rate, float attackSeconds = 0.0005)

Parameters

into float[]
start int
noise SoundSynth.Noise
centre float
q float
amplitude float
seconds float
rate int
attackSeconds float

AddChirp(float[], int, float, float, float, float, int)

A drop's or a bubble's "plink": a damped sine whose pitch rises as it rings, as a bubble's does when it shrinks at the surface (Minnaert resonance; the rise is what makes a drop on water sound wet).

public static void AddChirp(float[] into, int start, float frequency, float rise, float amplitude, float ringSeconds, int rate)

Parameters

into float[]
start int
frequency float
rise float
amplitude float
ringSeconds float
rate int

AddRing(float[], int, float, float, float, int, float)

A damped sine added into into at start: the ring of a struck body (a pebble, a board, a drop on a leaf). Decays by 60 dB over ringSeconds.

public static void AddRing(float[] into, int start, float frequency, float amplitude, float ringSeconds, int rate, float phase = 0)

Parameters

into float[]
start int
frequency float
amplitude float
ringSeconds float
rate int
phase float

FadeEnds(float[], int, int)

Short linear fades at both ends, so a one-shot never starts or stops on a click.

public static void FadeEnds(float[] data, int inSamples, int outSamples)

Parameters

data float[]
inSamples int
outSamples int

Fft(float[], float[], bool)

An in-place iterative radix-2 FFT (Cooley–Tukey) of re + i·im, whose length must be a power of two. The inverse is unscaled.

public static void Fft(float[] re, float[] im, bool inverse)

Parameters

re float[]
im float[]
inverse bool

Hash(uint, uint)

A 32-bit mix of two numbers, for seeds that must not collide.

public static uint Hash(uint a, uint b)

Parameters

a uint
b uint

Returns

uint

Limit(float[], int, float, float)

A peak limiter: the gain falls at once to hold every sample within ceiling, and recovers over releaseSeconds. A crack is held to full scale without the fizz of clipping it.

public static void Limit(float[] data, int rate, float ceiling, float releaseSeconds)

Parameters

data float[]
rate int
ceiling float
releaseSeconds float

Loop(float[], int, int)

A seamless loop of length samples from raw, which holds at least length + fade: the last fade samples made are cross-faded into the first, equal power because the two are unrelated noise, so the sample after the end continues the one before it.

public static float[] Loop(float[] raw, int length, int fade)

Parameters

raw float[]
length int
fade int

Returns

float[]

Normalize(float[], float)

Scales a sound so its loudest sample is peak.

public static void Normalize(float[] data, float peak)

Parameters

data float[]
peak float

NormalizeLoudest(float[], int, float, float)

Scales a sound by its loudest stretch rather than its loudest sample: the loudest windowSeconds is brought to rms. A sound whose energy is a long roll behind one sharp crack (thunder) would otherwise be set by the crack, a millisecond long, and the roll left at a hundredth of full level. Follow with Limit(float[], int, float, float): the crack is now above full scale.

public static void NormalizeLoudest(float[] data, int rate, float windowSeconds, float rms)

Parameters

data float[]
rate int
windowSeconds float
rms float

NormalizeRms(float[], float)

Scales a sound so its RMS is rms, then softly limits any peak above 0.98.

public static void NormalizeRms(float[] data, float rms)

Parameters

data float[]
rms float

OctaveAmplitude(float, float[], float[])

The amplitude spectrum, per hertz, of a sound whose energy in each octave is octaveDb (dB against the whole) at the centres centres: the dB joined linearly in log frequency between the centres and held beyond the ends, the energy per octave spread over the octave's width (∝ f).

public static float OctaveAmplitude(float hz, float[] centres, float[] octaveDb)

Parameters

hz float
centres float[]
octaveDb float[]

Returns

float

RemoveDc(float[])

Removes any constant offset, which only wastes headroom and thumps when a sound starts.

public static void RemoveDc(float[] data)

Parameters

data float[]

SoftClip(float)

Linear below 0.7, easing into ±1 above: keeps a rare loud peak from clipping hard.

public static float SoftClip(float x)

Parameters

x float

Returns

float

SpectralNoise(int, int, Func<float, float>, ref Noise)

Periodic noise of n samples (a power of two) whose amplitude spectrum is amplitude(Hz), made in the frequency domain: Gaussian bins (so the result is true noise, not a sum of steady tones) at random phases, transformed back. Periodic, so it loops with no seam at all; scaled to unit RMS.

public static float[] SpectralNoise(int n, int rate, Func<float, float> amplitude, ref SoundSynth.Noise noise)

Parameters

n int
rate int
amplitude Func<float, float>
noise SoundSynth.Noise

Returns

float[]

Swell(int, int, float, int, int, int, float, float, float)

A slow, seamless swell for a loop of length samples: a sum of sines each fitting the loop a whole number of times, mean 1, swinging by about depth.

public static float Swell(int i, int length, float depth, int cyclesA, int cyclesB, int cyclesC, float phaseA = 0, float phaseB = 1.3, float phaseC = 2.9)

Parameters

i int
length int
depth float
cyclesA int
cyclesB int
cyclesC int
phaseA float
phaseB float
phaseC float

Returns

float

ToClip(string, float[], int)

Makes a mono clip of the samples. The only call here that has to run on the main thread.

public static AudioClip ToClip(string name, float[] data, int rate)

Parameters

name string
data float[]
rate int

Returns

AudioClip