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
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
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.
TCastleSoundSource refers to
TCastleSound for an actual sound information. There are two ways to use it:
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.
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.
See all the examples in examples/audio subdirectory.
In particular open the examples/audio/game_3d_sound demo. It's a simple example of how
TCastleSound can be set up in the CGE editor.
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:
Load a sound file as
TCastleSound like this:
CastleSoundEngine to your uses clause.
Declare variable to hold it like
Initialize the variable and load sound file, e.g. in
MySound := TCastleSound.Create(Application); MySound.URL := 'castle-data:/my-sound.wav';
Play the sound like this:
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.
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
See also examples/platformer for an example of this approach. In particular, important files in this example are:
examples/platformer/data/sounds.castle-component describes all sounds. Edit this JSON file visually in CGE editor, just double-click on it in the CGE editor "Files" panel.
examples/platformer/code/gamesound.pas loads them. It defines a simple
NamedSound routine used by the rest of code to play sounds like
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...).