AudioEffectHighPassFilter in Godot – Complete Guide

High-pass filters are like audio magicians in the world of game development, separating the wheat from the chaff, or more accurately, the treble from the bass. As developers, we often find ourselves experimenting with audio effects to create an immersive experience for players, and understanding the role of audio filters is crucial in this creative process. The AudioEffectHighPassFilter in Godot 4 is a powerful tool that allows us to fine-tune the audio in our games by allowing frequencies above a certain threshold to pass through while attenuating lower ones. This can be particularly useful when trying to highlight certain sounds or create a specific atmosphere.

What is an AudioEffectHighPassFilter?

An AudioEffectHighPassFilter is a type of audio filter essential for manipulating sound. It acts as an acoustic gatekeeper in your game’s sound engine, ensuring that only high-frequency sounds make it through to your players’ ears.

What is it Used For?

Imagine you want to create the effect of a character talking through a phone or a radio; you’d use a high-pass filter to cut out low-frequency noises and emphasize the higher frequencies that are typical for these communication devices. Or perhaps you’re designing a level with heavy winds, but you don’t want the bass rumble to overpower the environment’s subtle sounds; a high-pass filter would be your tool of choice.

Why Should I Learn It?

Understanding the AudioEffectHighPassFilter is, therefore, not just about filtering sound—it’s about crafting immersive audio landscapes that resonate with your game’s environment, story, and characters. Learning to use this tool enables you to refine your game’s audio, provide better gaming experiences, and is an essential skill for any game developer’s toolbox. Let’s dive into how to apply this powerful component in Godot 4.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Creating Your First High-Pass Filter in Godot 4

Let’s start by adding an AudioEffectHighPassFilter to an AudioStreamPlayer node in our project. This basic setup allows us to apply the filter to any sound emitted by this node.

var player = AudioStreamPlayer.new()
var high_pass = AudioEffectHighPassFilter.new()

player.add_effect(high_pass)
add_child(player)

Here we’ve created a new AudioStreamPlayer and a new AudioEffectHighPassFilter, then added the filter to the player. We then add the player as a child of the current node, so it’s ready to be used in the scene.

Setting the Cut-off Frequency

The cut-off frequency is the threshold above which frequencies are allowed to pass. We can set this property to determine the range of high frequencies we want to hear.

high_pass.cutoff_hz = 5000  # Set cut-off to 5000 Hz

In this example, only frequencies higher than 5000 Hz will be heard clearly, while those below will be significantly attenuated.

Playing a Sound with the High-Pass Filter Applied

Now that our high-pass filter is in place, we can load an audio file into our AudioStreamPlayer and play it. The filter will be applied automatically.

var sound = load("res://sounds/effects/my_sound_effect.ogg")
player.stream = sound
player.play()

This code loads an audio file and sets it as the stream for the player. When we call player.play(), the audio plays with the high-pass filter effect applied.

Adjusting the Resonance

The resonance, or Q factor, of the filter can be adjusted to emphasize frequencies at the cut-off point. A higher Q factor will result in a more pronounced peak around the cut-off frequency.

high_pass.resonance = 1.0  # Increase the resonance

This line sets the resonance of our high-pass filter to 1.0, boosting frequencies near the cut-off for a sharper effect. Experiment with different values to see how they change the sound character.

Applying the Filter to Multiple Sounds

If we have multiple sounds we want to process with the same high-pass filter, we can create a bus in Godot’s audio server and apply the filter to that bus. Every sound routed to this bus will have the filter applied.

var bus_idx = AudioServer.get_bus_index("HighPassBus")
AudioServer.add_bus_effect(bus_idx, high_pass)

# Route the player to the new bus
player.set_bus("HighPassBus")

This snippet sets up a new bus, assigns the high-pass filter to it, and routes our audio player to use the newly created bus. All audio routed through “HighPassBus” will use the high-pass filter effect.

With these code examples, you’ve now learned how to add a high-pass filter effect to individual sounds and entire audio buses. Experiment with these settings to understand their impact better and to find the perfect balance for your game’s audio experience.

Playing with Variable Cut-off Frequencies
As your game’s environment changes, you might want it to be reflected in the sound as well. For dynamic audio effects, you can modify the cut-off frequency in real-time. Here’s how you can adjust the cut-off frequency based on in-game events:

# Let's assume 'event_intensity' represents some in-game event level
var event_intensity = get_event_intensity()
var new_cutoff_freq = lerp(5000, 10000, event_intensity)
high_pass.cutoff_hz = new_cutoff_freq

This example uses a linear interpolation function (lerp) to smoothly transition the cut-off frequency based on the ‘event_intensity’ variable, which could be linked to any game dynamics such as player speed, proximity to an object, or environmental changes.

Creating an Underwater Audio Effect
Suppose your game features an underwater scene. You may want to simulate audio propagation through water with a high pass filter.

func simulate_underwater_effect():
    high_pass.cutoff_hz = 300  # under water, lower frequencies are more attenuated
    high_pass.resonance = 0.5  # less sharp resonance under water
    # Assume 'player' is already an AudioStreamPlayer set up in the game
    player.add_effect(high_pass)

Calling this function ‘simulate_underwater_effect’ will make your game’s audio sound muffled and distant, just like being underwater.

Automating Filter Adjustments Over Time
In some scenarios, you might want a filter effect to change over time without user input. To automate this, you could use a Tween node.

var tween = Tween.new() # Create a new Tween node
add_child(tween)        # Add it to the scene tree

tween.interpolate_property(high_pass, "cutoff_hz", 
                           1000, 5000, 4, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tween.start()           # Start the tweening process

By adding a Tween node and calling interpolate_property, we animate the ‘cutoff_hz’ property of the high-pass filter from 1000 Hz to 5000 Hz over 4 seconds. This can create a gradual build-up or transition effect in your audio.

Saving Preset Configurations
If you have different sound environments in your game and you’d like to switch between them, you can save configurations as presets.

var preset_1 = high_pass.dup() # Duplicate the current filter settings
preset_1.cutoff_hz = 7000     # Adjust for a new game scene

# Later in the game, when you need to switch environments
player.replace_effect(0, preset_1) # Replace current effect with the preset

Using the dup() function, you can duplicate an existing high-pass filter, modify it as needed, and later apply it to an AudioStreamPlayer node, effectively creating switchable audio presets.

By following these examples, you’re not just tinkering with sound, you’re actively creating a dynamic aural canvas for your players to explore. These techniques can significantly enhance the playing experience, making each game environment feel unique and responsive. Use the power of high-pass filters to your advantage and watch your game’s audio landscape come alive.

Integrating Filters with Animation Trees

To create even more dynamic audio effects, such as those synced with animations, you can integrate high-pass filters with Godot’s animation system. Here is an example of how you might link the cut-off frequency of the filter to an AnimationPlayer node:

var animation_player = AnimationPlayer.new()
var animation = Animation.new()

# Create a track for the cut-off frequency property
animation.track_insert_key(0, 0, 1000) # Initial key at time 0 with value 1000 Hz
animation.track_insert_key(0, 1, 8000) # Second key at time 1 with value 8000 Hz

# Add the animation to the AnimationPlayer and bind the filter property
animation_player.add_animation("HighPassAdjust", animation)
animation_player.get_animation("HighPassAdjust").track_set_path(0, @"high_pass:cutoff_hz")

In this snippet, an Animation node is created where different keys are set to represent the cut-off frequencies at different times. The animation is then added to an AnimationPlayer, which is linked to the high-pass filter’s cut-off frequency property.

Modulating Sound in Real-Time with User Input

Interactive applications often require real-time adjustments to the game’s audio based on user input. Here’s an example of modulating the cut-off frequency of a high-pass filter using player input:

func _physics_process(delta):
    var input_intensity = get_input_intensity() # A function to measure input intensity from 0 to 1
    var min_freq = 1000
    var max_freq = 8000
    var new_cutoff_freq = lerp(min_freq, max_freq, input_intensity)
    high_pass.cutoff_hz = new_cutoff_freq

In the function _physics_process, which runs every physics frame, we’re interpolating the cut-off frequency of our high-pass filter based on a custom function get_input_intensity. This function could measure anything from joystick tilt to keyboard pressure.

Adjusting Filter Parameters for Sound Effects

Sound effects in games can often benefit from variable filter parameters. Consider a scenario where you have an explosion sound that you want to sound distant or muffled:

func apply_explosion_effect():
    var explosion_sound = AudioStreamPlayer.new()
    var explosion_filter = AudioEffectHighPassFilter.new()
    explosion_filter.cutoff_hz = 2500
    explosion_sound.add_effect(explosion_filter)
    explosion_sound.stream = load("res://sounds/effects/explosion.ogg")
    add_child(explosion_sound)
    explosion_sound.play()

This function creates a new AudioStreamPlayer for your explosion sound, adds a high-pass filter to it, and sets the stream to an explosion sound file. With a higher cut-off frequency, the explosion will sound as if it happened further away.

Chain Filters Together for Complex Effects

More complex soundscapes can be achieved by chaining multiple filters together. Below is an example of how you might add both a high-pass and a low-pass filter to create a band-pass effect, which only allows a specific range of frequencies to pass:

var sound_player = AudioStreamPlayer.new()
var low_pass = AudioEffectLowPassFilter.new()
var high_pass = AudioEffectHighPassFilter.new()

# Set the filter properties
low_pass.cutoff_hz = 8000
high_pass.cutoff_hz = 2000

# Add the high-pass filter first, then the low-pass filter
sound_player.add_effect(high_pass)
sound_player.add_effect(low_pass)

# Now the sound will only contain frequencies between 2000 Hz and 8000 Hz

These code examples provide additional methods of incorporating the AudioEffectHighPassFilter into your Godot 4 projects. By thoughtfully applying and modulating high-pass filters, you can greatly enrich the auditory dimension of your games, making them more engaging and interactive for the player.

Expanding Your Game Development Skills

Having harnessed the power of the AudioEffectHighPassFilter in Godot 4, you’re on your way to creating games with robust and dynamic audio landscapes. But don’t stop there! To continue sharpening your game development skills and explore the vast potential of Godot 4, consider diving into our Godot Game Development Mini-Degree. This comprehensive collection is designed to propel you from game development fundamentals to creating your own cross-platform games, giving you hands-on experience with everything from GDScript to advanced gameplay mechanics.

Whether you’re just starting out or looking to build on existing expertise, our courses are structured to cater to all levels. Each course within the mini-degree breaks down powerful game development concepts into digestible, easy-to-follow lessons, allowing you to learn at your own pace and schedule. Additionally, for a broader range of topics, our varied catalog of Godot courses offers learning paths on different aspects of the engine that can ignite your curiosity and further your knowledge.

Embrace the journey of game development with Zenva – where your creativity knows no bounds. With our extensive library of courses covering a multitude of topics, you’ll have everything you need to turn your game ideas into reality. Start learning today and see where your newfound skills will take you!

Conclusion

Mastering audio tools like the AudioEffectHighPassFilter is only the beginning of what you can achieve in Godot 4. By infusing your games with carefully sculpted soundscapes, you’re not just entertaining players—you’re drawing them into worlds of your own making. And with resources like our Godot Game Development Mini-Degree, you can transform your passion for gaming into the remarkable skill of creating immersive gaming experiences.

Don’t let your journey end here. Continue learning, experimenting, and expanding your realm of possibilities with Zenva. Whether you’re aspiring to be a game developer or looking to add new competencies to your skillset, our courses are tailored to help you succeed. Dive into the world of Godot 4 with us, and let’s bring those gaming dreams to life!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.