Skip to content

Sound Effects

Hangman edited this page May 27, 2024 · 17 revisions

TuningFork allows to enable powerful real time special effects on sound sources. Once an effect is active on a sound source, the output signal of the effect is mixed with the original output signal of the sound source. In most cases that is exactly what you want but in case you just want the effect output, you can mute the original output via a filter. You can find a simple self-contained and runnable example here.

Attaching an Effect to a SoundSource

First you need to create an effect (we use the reverb effect in this example) and then you can attach it to a source:

SoundEffect effect = new SoundEffect(new Reverb());
soundSource.attachEffect(effect);

Note: The total number of effects you can attach depends on the device and requested number of effect slots. TuningFork defaults to 2 effect slots. If you want to change this, provide an AudioDeviceConfig (embedded in AudioConfig) to initialize the Audio class and set its effectSlots property to a reasonable number. If you attach more effects than effect slots are available, the oldest attached effect will be kicked out. Attaching an effect that is already attached to a source does nothing. Though you can attach an effect to as many sound sources as you want.

To detach an effect again:

soundSource.detachEffect(effect);

// or to detach all effects
soundSource.detachAllEffects();

Disposing Effects

SoundEffects must be disposed when they are no longer needed. You don't have to detach them before disposing, TuningFork takes care of that.

effect.dispose();

Configuring Effects

All effects have different properties you can change to your needs. For the reverb effect it could look like this:

Reverb reverb = new Reverb(); // initializes the reverb with default values
reverb.density = 0.5f;
reverb.diffusion = 0.8f;
reverb.roomRolloffFactor = 2f;

// now we're ready to create and attach the effect
SoundEffect effect = new SoundEffect(reverb);
soundSource.attachEffect(effect);

The different parameters and their ranges are documented in javadoc, so just let your IDE help you.

Environmental

There's one setting you might want to activate when using reverberation.

effect.setEnvironmental(true);

This is set to false by default. Enabling this leads to a more realistic impression of the environment by dynamical adjustment of effect properties. Usually this is only used for reverb effects that should simulate the environment surrounding the listener or a source. It makes a difference, you should definitely check it out.

Using presets

There are static factory methods to create effects based on a preset.

SoundEffect effect1 = new SoundEffect(EaxReverb.domeSaintPauls());
SoundEffect effect2 = new SoundEffect(Chorus.chore());
// etc.

Mixing

One thing that you might not expect is that the original sound is playing in parallel to the output of the effect. Surprisingly, in most cases this is what you want, hence it is the default. If you want to use only the effect path and mute the direct path, you can apply a direct filter to the sound source, like so:

source.setFilter(0f, 0f);

For more information on filters, see the wiki here.
The sound path will look like this:


sound path


An Overview of available Effects

Effect Description
AutoWah The Auto-wah effect emulates the sound of a wah-wah pedal used with an electric guitar, or a mute on a brass instrument. Such effects allow a musician to control the tone of their instrument by varying the point at which high frequencies are cut off.
Chorus The chorus effect essentially replays the input audio accompanied by another slightly delayed version of the signal, creating a doubling effect. This was originally intended to emulate the effect of several musicians playing the same notes simultaneously, to create a thicker, more satisfying sound.
Compressor The Automatic Gain Control effect performs the same task as a studio compressor – evening out the audio dynamic range of an input sound. This results in audio exhibiting smaller variation in intensity between the loudest and quietest portions. The AGC Compressor will boost quieter portions of the audio, while louder portions will stay the same or may even be reduced. The Compressor effect cannot be tweaked in depth – it can just be switched on and off.
Distortion The distortion effect simulates turning up (overdriving) the gain stage on a guitar amplifier or adding a distortion pedal to an instrument’s output. It is achieved by clipping the signal (adding more square wave-like components) and adding rich harmonics. The distortion effect could be very useful for adding extra dynamics to engine sounds in a driving simulator, or modifying samples such as vocal communications.
EaxReverb The EAX Reverb parameter set is a superset of the standard OpenAL Effects Extension environmental reverb effect. Additional parameters allow for: Closer control over the tone of the reverb, Reverb directivity using panning vectors and Reverb granularity using echo controls.
Echo The echo effect generates discrete, delayed instances of the input signal. The amount of delay and feedback is controllable. The delay is two tap – you can control the interaction between two separate instances of echoes.
Equalizer The EQ is very flexible, providing tonal control over four different adjustable frequency ranges.
Flanger The flanger effect creates a "tearing" or "whooshing" sound (like a jet flying overhead).
FrequencyShifter Applications of the frequency shifter include the creation of bizarre distortion, phaser, stereo widening and rotating speaker effects.
PitchShifter The pitch shifter applies time-invariant pitch shifting to the input signal, over a one octave range and controllable at a semi-tone and cent resolution.
Reverb The standard environmental reverberation effect.
RingModulator The ring modulator multiplies an input signal by a carrier signal in the time domain, resulting in tremolo or inharmonic effects.
VocalMorpher The vocal morpher consists of a pair of 4-band formant filters, used to impose vocal tract effects upon the input signal. If the input signal is a broadband sound such as pink noise or a car engine, the vocal morpher can provide a wide variety of filtering effects. A low-frequency oscillator can be used to morph the filtering effect between two different phonemes. The vocal morpher is not necessarily intended for use on voice signals; it is primarily intended for pitched noise effects, vocal-like wind effects, etc.

Pitch Shifter

The PitchShifter allows to shift the pitch of a sound without changing its speed at the same time. In combination with the time-variant pitch shifting ability of a sound source, it is also possible to change the playback speed of a sound without changing its pitch. Since this is not super trivial, here's an example of how to achieve this:

// set source pitch to change the speed
float pitch = 1.2f;
source.setPitch(pitch);

// apply pitch correction
effect = new SoundEffect(new PitchShifter().correctPitch(pitch));
source.attachEffect(effect);

// in order to only hear the pitch corrected sound, we must silence the original sound with a filter
source.setFilter(0f, 0f);

There's also a runnable example in the repository here: SpeedOnlyChangeTest