Sounds and music
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
이 메서드는 ink 문법과 함께 사용할 수 있습니다. See more here.
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!";
},
]);초기화
First of all, you need to define the asset matrix exactly as with other components. After that, it is recommended to load sounds at the start of the game using the functions provided by sound.
import { Assets, sound } from "@drincs/pixi-vn";
import manifest from "../assets/manifest";
/**
* 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() {
await Assets.init({ manifest });
// 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");
}It is also recommended to define channels immediately after game initialization. During the definition of channels, you can configure their settings, which are all intuitive and based on PixiJS Sound, except for:
background(Optional): this is a boolean value that can be used to definechannelsintended for background sounds such as music or ambient audio. Unlike other sounds, they will not be stopped between narrativesteps.
import { Game, sound } from "@drincs/pixi-vn";
Game.init(body, {
// ...
}).then(() => {
sound.addChannel("bgm", { background: true });
sound.addChannel("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.
This function has the following parameters:
alias: The alias to identify themedia.soundUrl(Optional): The URL or path. If you have initialized the asset matrix, you can use the alias of the sound. If you don't provide the URL, then the alias is used as the URL.options(Optional): The options for themedia.delay: The delay before the sound starts, in seconds.loop: Override default loop, default to the Sound's loop setting.speed: Override default speed, default to the Sound's speed setting.volume: Override default volume, default to the Sound's volume setting.start: Start time offset in seconds.end: End time in seconds.muted: If sound instance is muted by default.filters: Filters that apply to play. Only supported with WebAudio.channel: The alias of thechannelto which themediawill be assigned. If not specified, it will be assigned tosound.defaultChannelAlias.
Pause and resume
There are various ways to pause individual or multiple media.
편집
Only media settings will be saved in game save files.
It is possible to edit almost all settings at any time for different purposes.
Stop
To stop a sound, you can use the stop function.
Filters
Currently, they can only be modified when starting a media.
Filters are elements that allow advanced customization of sounds.