Audio#

GameKit's audio system involves two main concepts. These are playbacks and channels.

Playback#

A playback is a loaded instance of an audio asset file which can be played, paused, stopped and restarted.

Creating a Playback#

To create a playback, you must first declare the audio asset file in the corresponding scene as shows below:

import { createScene } from "gamekit-engine/core/scene";

const scene = createScene({
  id: "main-scene",
  /* ... */
  assets: [
    { src: "music.mp3", category: "audio" }, // <- Load audio assets in scene
  ],
});

After this, create a playback using the createPlayback function as shown below:

import { createPlayback } from "gamekit-engine/core/audio";

const playback = await createPlayback({
  src: string,              // Relative path to audio file from the `src/assets` directory (Required)
  loop : boolean,           // Indicates if playback should restart when it ends (Optional)
  speed : number,           // Normalized playback speed (Optional)
  volume : number,          // Normalized volume (Optional)
  autoplay : boolean,       // Indicates if the playback should start automatically (Optional)
  channelId : string,       // Id of channel to attach to the playback (Optional)
  effects : AudioEffect[],  // List of audio effects to apply to the playback (Optional)
  onend: () => void,        // Callback for when the playback ends (Optional)
});

The id returned by createPlayback can be used to control it later.

Controlling a Playback#

The following functions to control a created playback:

Function Description
startPlayback Starts an idle playback
pausePlayback Pause a started playback
resumePlayback Resumes a paused playback
stopPlayback Stops a playback
restartPlayback Restarts a playback from the beginning
disposePlayback Destroys a playback, releasing its system resources

import {
  createPlayback,
  startPlayback,
  pausePlayback,
  resumePlayback,
  stopPlayback,
  restartPlayback
  disposePlayback
} from "gamekit-engine/core/audio";

const playback = createPlayback(/* Create playback args */);

startPlayback(playback);
pausePlayback(playback);
resumePlayback(playback);
stopPlayback(playback);
restartPlayback(playback);
disposePlayback(playback);

After disposing a playback, you have to create it again to use.

Configuring a Playback#

Use the configurePlayback function to update the parameters of a playback as shown below:

import { configurePlayback } from "gamekit-engine/core/audio";

configurePlayback(
  playback,               // Playback id (Required)
  {
    volume: number,       // Normalized playback volume (Optional)
    speed: number,        // Normalized playback speed (Optional)
    pan: number,          // Stereo pan (Optional)
    mute: boolean,        // Playback mute state (Optional)
    effect: AudioEffect,  // Audio effect to configure (Optional)
  },
)
;

Channels#

Channels allow for grouping of playbacks in order to apply common settings to all of them.

The audio system provides a master channel through which all playbacks and custom channels pass through.

Playback -> Master channel -> Speakers
Playback -> Custom channel -> Master channel -> Speakers

Creating a Channel#

Create a channel using the createChannel function as shown below:

import { createChannel } from "gamekit-engine/core/audio";

createChannel(
  channelId,                // Channel id (Required)
  {
    volume: number,         // Normalized volume of the channel output (Optional)
    muted: boolean,         // Mute state of the channel output (Optional)
    effects: AudioEffect[]  // List of audio effects to apply to the channel (Optional)
  }
);

Unlike createPlayback, createPlayback requires you to explicitly provide the channel id.

The master channel has an id of master. Duplicate channel names with throw an error.

Configuring a Channel#

Use the configureChannel function to update parameters of the channel as shown below:

import { configureChannel } from "gamekit-engine/core/audio";

configureChannel(
  channelId,            // Channel id (Required)
  {
    volume: number,     // Normalized volume of the channel output (Optional)
    muted: boolean,     // Mute state of the channel output (Optional)
    effect: AudioEffect // Audio effect to update (Optional)
  }
);

Effects#

Audio effects applies custom signal processing to playbacks and channels. Supported effects are:

  • Gain
  • Delay
  • Low-pass
  • High-pass
  • Band-pass
  • Comb
  • All-pass
  • Reverb

An audio effect has the following type:

import { createPlayback, createChannel } from "gamekit-engine/core/audio";

type AudioEffect =
  | { id: string; type: "delay"; delayTime: number }
  | { id: string; type: "gain"; gain: number }
  | { id: string; type: "lowpass"; cutoff: number }
  | { id: string; type: "highpass"; cutoff: number }
  | { id: string; type: "bandpass"; minCutoff: number; maxCutoff: number }
  | { id: string; type: "reverb"; damping: number; mixRatio: number };

// Creating a playback with audio effects
const playback = await createPlayback({
  src: "path/to/audio",
  effects: [
    { id: "lp1", type: "lowpass", cutoff: 4300 }, // Lowpass filter with a cutoff frequency of 4.3kHz with id "lp1"
    { id: "hp1", type: "highpass", cutoff: 1200 }, // Highpass filter with a cutoff frequency of 1.2kHz with id "hp1"
  ],
});

// Creating a channel with audio effects. All playbacks using this channel will inherit these effects
const channel = await createChannel(
  "squeaky",
  {
    effects: [
      { id: "hp1", type: "highpass", cutoff: 5000 }, // Highpass filter with a cutoff frequency of 5kHz with id "hp1"
      { id: "ghalf", type: "gain", gain: 0.5 }, // Gain filter with half gain (I.e. half volume)
    ],
  }
);