Sound

1. The most important classes and their usage

3D game sound demo - TCastleSound
3D game sound demo - TCastleSoundSource
  • TCastleSound: The most important class, you should use this always when you want to play any sound.

    This is a non-visual component that represents a sound file with some playback parameters. The most important properties are:

    • URL — undoubtedly the most important property, set this to actually load the sound file.

    • Stream — optionally use "streaming", which is an alternative loading method best suited for longer playing sounds (like music tracks).

    • Volume — how loud the sound is. This is multiplied by volume at TCastlePlayingSound.Volume and TCastleSoundSource.Volume and by spatial calculations.

    • Pitch — sound playing speed. As with volume, the volume of TCastleSound.Pitch is multiplied by similar parameters controlled at TCastlePlayingSound.Pitch and TCastleSoundSource.Pitch.

    TCastleSound by itself doesn't manage playing the sound. You have to use SoundEngine.Play to play the sound (the simplest way to play, for non-spatial sounds) or TCastleSoundSource (for sounds that can be spatial; assign to TCastleSoundSource.Sound for looping, use TCastleSoundSource.Play for non-looping).

  • TCastleSoundSource: A way to play spatial (3D) sounds.

    This is a behavior that enhances any TCastleTransform so that it emits (possibly spatial) sounds.

    TCastleSoundSource refers to TCastleSound for an actual sound information. There are two ways to use it:

    1. Set a looping sound in TCastleSoundSource.Sound. The sound source will play it automatically. Turn it on or off using TCastleSoundSource.SoundPlaying.

    2. Play a sound calling TCastleSoundSource.Play. You can pass any TCastleSound or even your own TCastlePlayingSound to observe the playback.

    You can use both methods to play sounds. This way TCastleSoundSource can play multiple sounds at the same time.

  • TCastlePlayingSound: Optional, use if you need more control before and during the sound playback.

Both TCastleSoundSource and TCastleSound can be created, configured and linked in the CGE editor, e.g. when designing your state. You can hear the 3D sounds in the editor. You can also create and control them from code, as all CGE components.

2. Examples

Play sounds demo

3. Editor sound features

Sound editor options
  • Sound volume, and "auto-mute on play" are available in the editor settings.

  • Drag-and-drop sound files on the viewport to automatically create a spatial sound: TCastleTransform, TCastleSoundSource, TCastleSound.

4. Playing sound from Pascal code

Load a sound file as TCastleSound like this:

  1. Add CastleSoundEngine to your uses clause.

  2. Declare variable to hold it like MySound: TCastleSound;

  3. Initialize the variable and load sound file, e.g. in Application.OnInitialize:

    MySound := TCastleSound.Create(Application);
    MySound.URL := 'castle-data:/my-sound.wav';
  4. Play the sound like this:

    SoundEngine.Play(MySound);

See source code of examples/audio/simplest_play_sound/simplest_play_sound.dpr for a working simplest possible example of this.

NOTE: simplest_play_sound example is literally the simplest application that only plays a sound, without displaying anything. In a real situation, you want to use such code to play sound inside a larger CGE application, e.g. play sound when user presses some key, using our view events.

5. Using sounds collection (.castle-components file)

Sounds collection designed in editor

It is often comfortable to define a collection of sounds, which means that each sound file is assigned a simple name and configuration (e.g. priority, default volume), and all the sound files can be loaded easily from any place in code (regardless of the current view).

Do it by using a TCastleComponent as a design root and adding TCastleSound children. Save the resulting design to a file like sounds.castle-component.

See also examples/platformer for an example of this approach. In particular, important files in this example are:

6. Sound backends

FMOD

By default we use OpenAL to play sounds. It's a great full-featured open-source audio library, perfect match for our open-source game engine.

You can alternatively switch to use the FMOD sound backend. This is just an option. FMOD is proprietary (not open-source) and commercial (though free in some cases, for indie devs).

  • Main advantage of FMOD in CGE for now is Nintendo Switch compatibility.

  • Big future advantage will be integration with the FMOD Studio. The goal of FMOD Studio is to make the work of sound designer easier. The sfx person can create sound effects in FMOD Studio, in a way that is agnostic to the game engine, and the code (like your game) simply sends "events" that may cause some sound effect (playing something, stopping something, fading in/out something...).

  • See also plans about FMOD Studio and Wwise.