User Tools

Site Tools


en:tutorials:audio:audio_filters

Audio Filters

Audio filters are cool. They allow you add all sorts of layered effects onto your audio. Examples include notch and equalisation, delays, high and low-pass filtering. The full list of default filters can be found in the CreationTemplate.ini.

Let's try out some filters on a new project.

Setting up a new project

To help you work through this tutorial, first create a new blank project using the init script.

Assets

You can use the following audio loop for testing the filters:

Right click and save the audio above. Copy this into the data/sound folder.

Playing Audio on an object

Start by editing the default logo object, and placing the music on it:

[Object]
Graphic         = @
Texture         = logo.png
Pivot           = center
AngularVelocity = 18
SoundList       = @
Music           = FunkBitsBassDrumsLoop.mp3

Compile and run. The music will play from the object. Cool. Now we can start testing.

Introducing a Filter

Add a filter to the config for our experiments:

[EchoFilter]
Type = delay
Delay = 0.25
Decay = 0.4

Adding the filter using commands

Compile and run the program, and open the Orx Console using the backtick ` key.

Type the following to attach the filter to the object that plays the sound:

Object.FindNext Object
Object.AddFilter <press tab> EchoFilter

Nice right?

Now while you're there, remove the filter you just added with:

Object.RemoveLastFilter <press tab>

You also could have removed all filters (we only have one right now) with:

Object.RemoveAllFilters <press tab>

Adding Filters with Data Config

You can imagine how simple it is to add a filter to an object with data config. Simply by tacking the FilterList property onto the logo Object:

[Object]
Graphic         = @
Texture         = logo.png
Pivot           = center
AngularVelocity = 18
SoundList       = @
Music           = FunkBitsBassDrumsLoop.mp3
Loop            = true
FilterList      = EchoFilter

Run the program and the EchoFilter is already being processed on the audio.

Filters in code

This time, we'll do something more involved just so that you can have a little more fun really pushing the filters.

For that, we'll make three keyboard inputs, Q, W and E, that will toggle a filter or two.

Let's start by adding a couple more simple filters to the config:

[EchoFilter]
Type = delay
Delay = 0.25
Decay = 0.4
 
[TrebleFilter]
Type = highpass
Frequency = 2000; Cutoff frequency
 
[BassyFilter]
Type = lowpass;
Frequency = 1000; Cutoff frequency

Next, add an orxOBJECT pointer for the logo object at the top of the code:

orxOBJECT *logo;

Then replace the scene creation line:

  orxObject_CreateFromConfig("Scene");

with:

logo = orxObject_CreateFromConfig("Object");

That will give us easy access to add the filters to the object.

Oh and don't forget to remove the existing filter that is on the object. Should return to being:

[Object]
Graphic         = @
Texture         = logo.png
Pivot           = center
AngularVelocity = 18
SoundList       = @
Music           = FunkBitsBassDrumsLoop.mp3
Loop            = true

Now to define the keyboard inputs in the config:

[Input]
KEY_ESCAPE      = Quit
KEY_Q           = WaterKey
KEY_W           = TrebleKey
KEY_E           = BassKey

Notice the WaterKey? Why didn't I go for something like an EchoKey? You'll see in a moment, but I'll be combining two filters on one keypress. More on that soon.

Now to keep track of an active filter, we'll add a boolean variable to the top of our code:

orxOBJECT *logo;
orxBOOL hasActiveFilterOnObject;

We now need to respond to the key presses and do the filtering magic. In the Update function, add the following code:

	//clear filters if toggle any key up
	if ( (orxInput_HasBeenActivated("TrebleKey") || orxInput_HasBeenActivated("BassKey") || orxInput_HasBeenActivated("WaterKey")) && hasActiveFilterOnObject == orxTRUE){
		hasActiveFilterOnObject = orxFALSE;
		orxObject_RemoveAllFilters(logo);
		return;
	}
 
	//toggle down keys
	if (orxInput_HasBeenActivated("TrebleKey")){
		orxObject_AddFilter(logo, "TrebleFilter");
		hasActiveFilterOnObject = orxTRUE;
    }
 
	if (orxInput_HasBeenActivated("BassKey")){
		orxObject_AddFilter(logo, "BassyFilter");
		hasActiveFilterOnObject = orxTRUE;
    }
 
	if (orxInput_HasBeenActivated("WaterKey")){
		orxObject_AddFilter(logo, "EchoFilter");
		orxObject_AddFilter(logo, "TrebleFilter");
		hasActiveFilterOnObject = orxTRUE;
    }

A quick explanation of the above: if a key is pressed, but there is already a filter being played (tracked using the hasActiveFilterOnObject variable), then clear all the filters.

If a key is pressed and there are no filters, add the filter that is assigned to the keypress.

Not a perfect routine but it's short and to the point.

Notice the WaterKey test above? I've called it that because I've combined the treble (a highpass filter), and an echo (a delay filter) to give the impression of an underwater sound.

Sound Buses

Filters can be applied to sound buses as well. Not covered in this document yet.

Custom Filters

No information yet.

UseCustomParam and Events

Events can be used to update filter parameters on the fly. Filters parameters will be modifiable at runtime, similar to shader parameters, as long as a filter is defined with UseCustomParams. No further details on this yet.

Notes and rules

You can't change the order of a low/high/band pass filter on the fly nor the delay of a delay filter. Orx will warn you (in debug) if you're trying to change those parameters on the fly.

en/tutorials/audio/audio_filters.txt · Last modified: 2022/05/31 18:08 (22 months ago) by sausage