Sounds and music
How to play, pause, resume, edit, and stop sounds and music in Pixi’VN using media, channels, and the sound manager, including fade transitions and filters, built on Tone.js.
The entire audio system has been revised in version 1.6.0/1.8.0.
The sound module is a wrapper around the Tone.js library. It provides a simple interface for playing sounds and saves the current state at each step.
ink
You can use this method with the ink syntax. See more here.
It is composed of the following elements, which will be referenced later:
sound(manager) is the controller of the entire audio system. It is used to managechannels, start and control musicalmedia, and retrieve information or modify soundassets. You can set values to adjust the overall audio level. These settings are not stored in save files and are mainly intended for game settings.channelsis a sub-level of the sound manager and can be seen as a container. It is used to contain, start, and manage musicalmedia. You can set values to affect only the contained media. These settings are not stored in save files and are mainly intended for game settings.mediaare instances of a sound that has been started. You can set values that apply only to themselves. All settings applied to them are saved in the game save. These settings are mainly intended to be modified during gameplay. They are contained within achannelin a one-to-many relationship. Each media has a uniquealias.assets(sound assets), as the name suggests, are the assets used during the game to play sounds. You can set values to influence all media that use this asset. These settings are not saved and are mainly intended to be configured during game initialization when loading assets.
import { narration, newLabel, sound } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
await sound.play("sfx_whoosh", { delay: 0.1 });
await sound.play("bgm_cheerful", { loop: true, channel: "bgm" });
narration.dialogue =
"Hello, I'm a cheerful background music that will loop forever until you stop me.";
},
() => {
sound.pause("bgm_cheerful");
narration.dialogue = "I'm paused, but I can be resumed.";
},
() => {
sound.resume("bgm_cheerful");
narration.dialogue = "I'm back!";
},
]);Initialize
As with other components that use assets, you need to define them in the assets. After that, it is recommended to load the main sounds in the background at game startup using the functions provided by sound.
import { manifest } from "@/assets";
import { AUDIO_BUNDLE_NAME } from "@/constants";
import { Assets, sound } from "@drincs/pixi-vn";
let assetsInitialized = false;
/**
* Define all the assets that will be used in the game.
* This function will be called before the game starts.
* You can read more about assets management in the documentation: https://pixi-vn.com/start/assets-management.html
*/
export async function defineAssets() {
if (!assetsInitialized) {
const origin = `${location.protocol}//${location.host}/`;
Assets.resolver.rootPath = origin;
await Assets.init({ manifest, basePath: `${origin}assets/` });
assetsInitialized = true;
}
// The audio bundle will be loaded in the background, so it will be available when needed, but it won't block the game start.
sound.backgroundLoadBundle(AUDIO_BUNDLE_NAME);
}It is also recommended to define channels immediately after game initialization. To do this, you can use the sound.channels.add function.
import { Game, sound } from "@drincs/pixi-vn";
Game.init(body, {
// ...
}).then(() => {
sound.channels.add("bgm", { background: true });
sound.channels.add("sfx");
sound.defaultChannelAlias = "sfx";
});Play
If no channel is specified during the play function, the media will be assigned to sound.defaultChannelAlias.
The basic functionality is to start a sound (asset). This can be done using the play function. The play function generates a media instance that you can interact with to control the produced sound.
channel.play starts a media directly on a specific channel, so you don't need to pass a channel option yourself.
import { sound } from "@drincs/pixi-vn";
await sound.channels.find("sfx").play("sfx_whoosh", { delay: 0.1 });
await sound.channels.find("bgm").play("bgm", "bgm_cheerful", { loop: true });sound.play starts a media from the manager. Use the channel option to assign it to a specific channel, otherwise it falls back to sound.defaultChannelAlias.
import { sound } from "@drincs/pixi-vn";
await sound.play("sfx_whoosh", { delay: 0.1 });
await sound.play("bgm", "bgm_cheerful", { loop: true, channel: "bgm" });Transitions
You can start a media with a fade transition using the fadeIn/fadeOut options of play. fadeIn only affects the start, while fadeOut is kept on the media and applied automatically both when it ends naturally and when you call stop() later.
import { sound } from "@drincs/pixi-vn";
const music = await sound.play("theme", {
loop: true,
fadeIn: 2, // 2s fade-in on start
fadeOut: 3, // 3s fade-out, applied both on manual stop() and on natural end
});
// later...
sound.stop("theme"); // automatically fades out over the 3s set abovePause and resume
There are various ways to pause individual or multiple media.
Setting media.paused pauses or resumes that single media instance directly.
import { sound } from "@drincs/pixi-vn";
const bgmMedia = sound.find("bgm_cheerful");
if (bgmMedia) bgmMedia.paused = true;
// ...
if (bgmMedia) bgmMedia.paused = false;channel.pauseAll/channel.resumeAll pause or resume every media contained in that channel.
import { sound } from "@drincs/pixi-vn";
sound.channels.find("sfx").pauseAll();
sound.channels.find("sfx").resumeAll();sound.pause/sound.resume target a single media by alias, while sound.pauseAll/sound.resumeAll affect every media across all channels.
import { sound } from "@drincs/pixi-vn";
sound.pause("bgm_cheerful");
sound.pauseAll();
sound.resume("bgm_cheerful");
sound.resumeAll();Edit
Only media settings will be saved in game save files.
It is possible to edit almost all settings at any time for different purposes.
Settings assigned directly on a media instance (like volume) only affect that instance, and are saved in the game save.
import { sound } from "@drincs/pixi-vn";
const bgmMedia = sound.find("bgm_cheerful");
if (bgmMedia) bgmMedia.volume.value = 90;Settings assigned on a channel (like volume) affect every media it contains, without being saved.
import { sound } from "@drincs/pixi-vn";
sound.channels.find("sfx").volume = 90;Settings assigned on the sound manager (like volumeAll) affect every channel/media globally, without being saved.
import { sound } from "@drincs/pixi-vn";
sound.volumeAll = 90;Stop
To stop a sound, you can use the stop function.
media.stop stops that single media instance directly.
import { sound } from "@drincs/pixi-vn";
sound.find("bgm_cheerful")?.stop();channel.stopAll stops every media contained in that channel.
import { sound } from "@drincs/pixi-vn";
sound.channels.find("sfx").stopAll();sound.stop stops a single media by alias, while sound.stopAll stops every media across all channels.
import { sound } from "@drincs/pixi-vn";
sound.stop("bgm_cheerful");
sound.stopAll();Filters
Currently, they can only be modified when starting a media.
Filters are elements that allow advanced customization of sounds.
Passing filters to sound.play applies them only to that media instance.
import { sound, filters } from "@drincs/pixi-vn";
await sound.play("sfx_whoosh", { filters: [new filters.ReverbFilter()] });Passing filters to sound.channels.add applies them to every media started on that channel.
import { sound, filters } from "@drincs/pixi-vn";
sound.channels.add("bgm", {
filters: [new filters.ReverbFilter()],
});Other features
There are also functions available for managing sounds in menus, such as in the settings screen.
The sound.unsaved namespace manages playback that is not included in game save/export state.
The sound.unsaved.pauseAll and sound.unsaved.resumeAll functions are intended to be used when opening and closing a menu, such as the settings. The first function pauses all sounds that are not already paused, while the second resumes all sounds that were paused by the first function.
sound.unsaved.playTransient plays a transient sound that is not recorded in save/export state. It can be routed through an audio channel so channel volume, mute, pan, and filters still apply. This also works for looping background ambience that should use the BGM channel without being restored by a loaded save:
import { sound } from "@drincs/pixi-vn";
const ambience = await sound.unsaved.playTransient("room-ambience", {
channel: "bgm",
loop: true,
});
// Stop this ambience when leaving the room or screen.
ambience.stop();The function returns the transient Tone.Player, so it can be stopped individually. sound.unsaved.stopTransientAll stops every transient player when individual references are not available.