AudioClip

Namespace:

namespace Lenga\Engine\Audio;

class AudioClip

AudioClip is a script-facing reference to an imported audio asset.

You normally assign clips in the Inspector by exposing an AudioClip field on a Behaviour, then pass that clip to an AudioSource when you want to play it.

Properties

name

public string $name

Display name derived from the assigned asset.

channels

public int $channels

Number of channels in the clip.

frequency

public int $frequency

Sample rate in hertz.

length

public float $length

Clip duration in seconds.

samples

public int $samples

Number of sample frames in the clip.

loadState

public AudioDataLoadState $loadState

Whether the clip can currently be read by the runtime.

Methods

getData

public function getData(int $offsetSamples, ?int $sampleCount = null): array|false

Reads interleaved sample values from the clip, normalized from -1.0 to 1.0. Returns false if the clip data cannot be read.

Example

use Lenga\Engine\Audio\AudioClip;
use Lenga\Engine\Audio\AudioSource;
use Lenga\Engine\Core\Behaviour;

final class LevelAudio extends Behaviour
{
    public ?AudioClip $winSound = null;

    private ?AudioSource $audio = null;

    public function start(): void
    {
        $this->audio = $this->gameObject->getComponent(AudioSource::class);
    }

    public function playWinSound(): void
    {
        if ($this->winSound === null || $this->audio === null) {
            return;
        }

        $this->audio->clip = $this->winSound;
        $this->audio->play();
    }
}