@drincs/pixi-vn
Version:
Pixi'VN is a npm package that provides various features for creating visual novels.
501 lines (490 loc) • 19.6 kB
TypeScript
import { Player, PlayerOptions, ToneAudioNode, Param, ReverbOptions, FeedbackDelayOptions, FreeverbOptions, DelayOptions, PingPongDelayOptions, GateOptions, AutoFilterOptions, BiquadFilterOptions, OnePoleFilterOptions, FeedbackCombFilterOptions, FilterOptions, ChorusOptions, PhaserOptions, TremoloOptions, VibratoOptions, CompressorOptions, MidSideCompressorOptions, MultibandCompressorOptions, LimiterOptions, GreaterThanOptions, GreaterThanZeroOptions, DistortionOptions, BitCrusherOptions, Panner3DOptions, AutoPannerOptions, StereoWidenerOptions, ToneAudioBuffer } from 'tone';
import { C as CachedMap } from './CachedMap-DZLvJAnA.js';
import 'lru-cache';
interface MediaInterface extends Pick<Player, "blockTime" | "disposed" | "loaded" | "loop" | "loopEnd" | "loopStart" | "mute" | "now" | "playbackRate" | "reverse" | "restart" | "start" | "stop" | "chain" | "disconnect" | "volume" | "state"> {
/**
* Whether the sound is currently paused.
*/
paused: boolean;
/**
* @deprecated Use {@link mute} instead.
*/
muted: boolean;
/**
* @deprecated Use {@link playbackRate} instead.
*/
speed: number;
}
interface MediaMemory extends Partial<Omit<PlayerOptions, "url" | "volume">> {
/**
* The volume of this sound in the linear range [0, 1], where 0 is silence
* and 1 is full volume. Stored and restored in linear form; converted
* to/from Tone.js decibels internally.
*/
volume?: number;
elapsed: number | undefined;
paused: boolean;
delay?: number;
}
interface SoundOptions extends Pick<Partial<PlayerOptions>, "loop" | "autostart" | "fadeIn" | "fadeOut" | "mute" | "loopEnd" | "loopStart" | "reverse" | "playbackRate"> {
/**
* The volume of this sound in the linear range [0, 1], where 0 is silence
* and 1 is full volume. This is converted to decibels internally before
* being passed to the Tone.js Player.
*/
volume?: number;
/**
* A collection of audio filters/effects to apply to this sound.
*
* Install "tone" for the full list of available filters.
*
* @example
* ```ts
* import * as Tone from "tone";
*
* const filters = [new Tone.FeedbackDelay("8n", 0.5)];
* ```
*/
filters?: ToneAudioNode[];
/**
* @deprecated Use {@link playbackRate} instead.
*/
speed?: number;
/**
* @deprecated Use {@link mute} instead.
*/
muted?: boolean;
}
interface SoundPlayOptions extends SoundOptions {
/**
* The delay in seconds before playback starts. If specified, playback is
* scheduled to begin after the delay has elapsed rather than starting
* immediately in a paused state.
*/
delay?: number;
/**
* The offset in seconds from the start of the sound at which to begin playback.
*/
elapsed?: number;
}
interface SoundPlayOptionsWithChannel extends SoundPlayOptions {
/**
* The alias of the audio channel to play the sound on. If the channel does
* not exist it will be created automatically.
* Defaults to `SoundManagerInterface.defaultChannelAlias` ("general").
*/
channel?: string;
}
interface ChannelOptions extends Pick<SoundPlayOptions, "filters" | "muted" | "volume"> {
/**
* Whether this channel is a background channel.
* Background channels are special: media playing on them is not stopped
* when a scene changes, but continues in the background.
*/
background?: boolean;
/**
* The stereo pan position for this channel in the range [-1, 1].
* -1 is full left, 0 is centre, 1 is full right.
* Defaults to 0.
*/
pan?: number;
}
interface AudioChannelInterface {
/**
* The alias of the audio channel. This is used to reference the channel when playing sounds. The alias must be unique among all channels.
*/
readonly alias: string;
/**
* Plays a sound.
* @param alias The media and sound (asset) alias reference.
* @param options The options
* @return The sound instance,
* this cannot be reused after it is done playing. Returns a Promise if the sound
* has not yet loaded.
*/
play(alias: string, options?: SoundPlayOptions): Promise<MediaInterface>;
/**
* Plays a sound.
* @param mediaAlias The media alias reference.
* @param soundAlias The sound (asset) alias reference.
* @param options The options
* @return The sound instance,
* this cannot be reused after it is done playing. Returns a Promise if the sound
* has not yet loaded.
*/
play(mediaAlias: string, soundAlias: string, options?: SoundPlayOptions): Promise<MediaInterface>;
/**
* The stereo pan position for this channel in the range [-1, 1].
* -1 is full left, 0 is centre, 1 is full right.
*/
pan: number;
/**
* The volume of the audio channel, between 0 and 1. This is multiplied with the volume of each sound played through this channel.
*/
volume: number;
/**
* Whether the audio channel is muted. This is combined with the muted state of each sound played through this channel.
*/
muted: boolean;
/**
* The MediaInstances currently playing through this channel. This is read-only and cannot be modified directly. Use the play method to add new MediaInstances to this channel.
*/
readonly mediaInstances: MediaInterface[];
/**
* Whether this channel is a background channel.
* Background channels are special channels. Unlike normal channels, media connected to a background channel does not stop when a scene changes, but continues to play in the background.
*/
readonly background: boolean;
/**
* Stops all media currently playing through this channel.
* @return Instance for chaining.
*/
stopAll(): this;
/**
* Pauses any playing sounds.
* @return Instance for chaining.
*/
pauseAll(): this;
/**
* Resumes any sounds.
* @return Instance for chaining.
*/
resumeAll(): this;
/**
* Toggle muted property for all sounds.
* @return `true` if all sounds are muted.
*/
toggleMuteAll(): boolean;
/**
* Useful for inserting channel-wide audio effects such as reverb, delay or EQ.
*
* Install "tone" to use this method.
*
* @param nodes One or more Tone.js {@link ToneAudioNode} instances to chain in series.
* @return Instance for chaining.
*
* @example
* ```ts
* import * as Tone from "tone";
*
* const channel = sound.findChannel("music");
*
* // Create a reverb effect and wait for its impulse response to be ready.
* const reverb = new Tone.Reverb({ decay: 2.5 });
*
* // Route the channel through the reverb to the master output.
* channel.chain(reverb);
* ```
*/
chain(...nodes: ToneAudioNode[]): this;
/**
* **Advanced** — the raw `Tone.Param<"decibels">` for this channel's volume.
*
* Unlike the {@link volume} property (which uses a linear 0–1 scale), this
* Param works directly in **decibels** and exposes all Tone.js automation
* methods such as `rampTo`, `linearRampTo`, and `exponentialRampTo`.
*
* Use this when you need to smoothly automate volume over time instead of
* setting it instantly.
*
* @example
* ```ts
* const channel = sound.findChannel("music");
*
* // Fade the volume from its current level to -12 dB over 3 seconds.
* channel.volumeParam.rampTo(-12, 3);
*
* // Fade to silence over 2 seconds.
* channel.volumeParam.rampTo(-Infinity, 2);
* ```
*/
readonly volumeParam: Param<"decibels">;
/**
* **Advanced** — the raw `Tone.Param<"audioRange">` for this channel's
* stereo pan position.
*
* Unlike the {@link pan} property (which sets the value instantly), this
* Param exposes all Tone.js automation methods such as `rampTo`,
* `linearRampTo`, and `exponentialRampTo`.
*
* Use this when you need to smoothly automate panning over time instead of
* setting it instantly. Values range from -1 (full left) to 1 (full right).
*
* @example
* ```ts
* const channel = sound.findChannel("music");
*
* // Gradually pan to the left over 3 seconds.
* channel.panParam.rampTo(-1, 3);
*
* // Return to centre over 2 seconds.
* channel.panParam.rampTo(0, 2);
* ```
*/
readonly panParam: Param<"audioRange">;
}
type SoundFilterMemory = ({
filterType: "ReverbFilter";
} & Omit<Partial<ReverbOptions>, "context">) | ({
filterType: "FeedbackDelayFilter";
} & Omit<Partial<FeedbackDelayOptions>, "context">) | ({
filterType: "FreeverbFilter";
} & Omit<Partial<FreeverbOptions>, "context">) | ({
filterType: "DelayFilter";
} & Omit<Partial<DelayOptions>, "context">) | ({
filterType: "PingPongDelayFilter";
} & Omit<Partial<PingPongDelayOptions>, "context">) | ({
filterType: "GateFilter";
} & Omit<Partial<GateOptions>, "context">) | ({
filterType: "AutoFilterFilter";
} & Omit<Partial<AutoFilterOptions>, "context">) | ({
filterType: "BiquadFilterFilter";
} & Omit<Partial<BiquadFilterOptions>, "context">) | ({
filterType: "OnePoleFilterFilter";
} & Omit<Partial<OnePoleFilterOptions>, "context" | "frequency">) | ({
filterType: "FeedbackCombFilterFilter";
} & Omit<Partial<FeedbackCombFilterOptions>, "context">) | ({
filterType: "CustomFilter";
} & Omit<Partial<FilterOptions>, "context">) | ({
filterType: "ChorusFilter";
} & Omit<Partial<ChorusOptions>, "context">) | ({
filterType: "PhaserFilter";
} & Omit<Partial<PhaserOptions>, "context">) | ({
filterType: "TremoloFilter";
} & Omit<Partial<TremoloOptions>, "context">) | ({
filterType: "VibratoFilter";
} & Omit<Partial<VibratoOptions>, "context">) | ({
filterType: "CompressorFilter";
} & Omit<Partial<CompressorOptions>, "context">) | ({
filterType: "MidSideCompressorFilter";
} & Omit<Partial<MidSideCompressorOptions>, "context">) | ({
filterType: "MultibandCompressorFilter";
} & Omit<Partial<MultibandCompressorOptions>, "context">) | ({
filterType: "LimiterFilter";
} & Omit<Partial<LimiterOptions>, "context">) | ({
filterType: "GreaterThanFilter";
} & Omit<Partial<GreaterThanOptions>, "context">) | ({
filterType: "GreaterThanZeroFilter";
} & Omit<Partial<GreaterThanZeroOptions>, "context">) | ({
filterType: "DistortionFilter";
} & Omit<Partial<DistortionOptions>, "context">) | ({
filterType: "BitCrusherFilter";
} & Omit<Partial<BitCrusherOptions>, "context">) | ({
filterType: "Panner3DFilter";
} & Omit<Partial<Panner3DOptions>, "context">) | ({
filterType: "AutoPannerFilter";
} & Omit<Partial<AutoPannerOptions>, "context">) | ({
filterType: "StereoWidenerFilter";
} & Omit<Partial<StereoWidenerOptions>, "context">);
interface ExportedSound {
options: SoundOptions;
filters?: SoundFilterMemory[];
}
interface SoundPlay {
stepIndex: number;
paused: boolean;
options?: SoundPlayOptions | string;
}
interface ExportedSoundPlay extends SoundPlay {
sound: ExportedSound;
}
/**
* Interface exported sounds
*/
interface SoundGameState {
/**
* @deprecated
*/
soundsPlaying?: {
[key: string]: ExportedSoundPlay;
};
mediaInstances: {
[key: string]: {
channelAlias: string;
soundAlias: string;
stepCounter: number;
options: MediaMemory & {
filters?: SoundFilterMemory[];
delay?: number;
};
/**
* @deprecated Use options.paused instead.
*/
paused?: boolean;
};
};
}
interface SoundManagerInterface {
/** Master volume in the range [0, 1]. */
volumeAll: number;
/**
* @deprecated Global playback speed. This is not a well-supported feature and may be removed in a future release. Use individual sound speed options instead.
*/
speedAll: number;
/**
* The default channel alias used when playing a sound without specifying a
* channel. Defaults to `"general"`.
*/
defaultChannelAlias: string;
/**
* @deprecated Register sound assets directly via `PIXI.Assets` instead.
*/
add(alias: string, options: string): void;
/**
* Plays a sound.
* @param alias The media and sound (asset) alias reference.
* @param options The options.
* @returns The media instance (resolves immediately if already loaded).
*/
play(alias: string, options?: SoundPlayOptionsWithChannel): Promise<MediaInterface>;
play(mediaAlias: string, soundAlias: string, options?: SoundPlayOptionsWithChannel): Promise<MediaInterface>;
/**
* Plays a non-persistent ("transient") sound (e.g. UI / menu sounds).
* Transient playback is not tracked in save/export state.
*/
playTransient(alias: string, options?: Partial<PlayerOptions>): Promise<Player>;
/**
* Find a tracked media instance by alias.
*/
find(alias: string): MediaInterface | undefined;
/**
* Stop a tracked media instance and remove it from the manager.
*/
stop(alias: string): void;
/**
* Pause a tracked media instance.
*/
pause(alias: string): MediaInterface | undefined;
/**
* Resume a paused media instance.
*/
resume(alias: string): MediaInterface | undefined;
/** Duration in seconds of the loaded sound with the given alias. */
duration(alias: string): number;
/** Toggle mute on all sounds. Returns the new muted state. */
toggleMuteAll(): boolean;
/**
* Whether all sounds are currently muted. Note that individual channels or media instances may still be muted or unmuted; this is just the global master mute state.
*/
readonly muted: boolean;
/** Mute all sounds. */
muteAll(): this;
/** Unmute all sounds. */
unmuteAll(): this;
/** Stop all sounds. */
stopAll(): this;
/** Pause all sounds. */
pauseAll(): this;
/** Resume all sounds. */
resumeAll(): this;
/**
* Temporarily pause all currently-playing sounds (or just those in the given
* channel) without persisting the paused state. Useful for overlays / pause
* menus.
*
* Only sounds that are **actively playing** at the time of the call are paused;
* sounds that were already paused beforehand are left untouched so that they
* remain paused when {@link resumeUnsavedAll} is called later.
*
* When called without a channel argument all transient players started with
* {@link playTransient} are also stopped.
*/
pauseUnsavedAll(channel?: string): this;
/**
* Resume all sounds (or just those in the given channel) that were paused by
* the most recent call to {@link pauseUnsavedAll}. Sounds that were already
* paused before `pauseUnsavedAll` was called are **not** resumed.
*/
resumeUnsavedAll(channel?: string): this;
/**
* Stop all transient media instances started with {@link playTransient}.
*/
stopTransientAll(): this;
/** Load one or more sound assets. */
load(...alias: string[]): Promise<void>;
/** Trigger background loading of one or more sound assets. */
backgroundLoad(...alias: string[]): Promise<void>;
/** Trigger background loading of a sound bundle. */
backgroundLoadBundle(alias: string): Promise<void>;
/** Stop all sounds and clear internal state. */
clear(): void;
/**
* Add a new audio channel.
* Returns the created channel, or `undefined` if the alias already exists.
*/
addChannel(alias: string | string[], options?: ChannelOptions): AudioChannelInterface | undefined;
/**
* Find the channel for the given alias, creating it if it does not yet exist.
*/
findChannel(alias: string): AudioChannelInterface;
/** All registered audio channels. */
readonly channels: AudioChannelInterface[];
/**
* Export the current sound state, including currently playing sounds and their options, for saving or debugging purposes. This is not guaranteed to be stable across versions and may contain implementation details; it is not intended for use in general application code.
*/
export(): SoundGameState;
/**
* Restore a sound state exported by {@link export}. This will stop any currently playing sounds and replace them with the sounds specified in the exported state. This is not guaranteed to be stable across versions and may contain implementation details; it is not intended for use in general application code.
*/
restore(data: object): Promise<void>;
}
type StopTime = Parameters<Player["stop"]>[0];
type StartTime = Parameters<Player["start"]>[0];
type StartOffset = Parameters<Player["start"]>[1];
type StartDuration = Parameters<Player["start"]>[2];
declare class MediaInstance extends Player implements MediaInterface {
readonly alias: string;
readonly channelAlias: string;
readonly soundAlias: string;
readonly stepCounter: number;
readonly delay?: number | undefined;
constructor(alias: string, channelAlias: string, soundAlias: string, stepCounter: number, options?: Partial<PlayerOptions>, delay?: number | undefined);
readonly options: Partial<PlayerOptions>;
readonly filters: Set<ToneAudioNode>;
/**
* Set to `toneNow()` the moment playback is paused; cleared on resume.
* Used to compute how long the instance was paused so that
* `playStartTime` can be adjusted accordingly.
*/
private pausedAt;
/**
* Tracks the effective playback start time in Tone's clock, adjusted
* after each resume to exclude all time spent paused.
* Invariant: `toneNow() - playStartTime` equals actual playback position
* while the player is playing.
*/
private playStartTime;
get memory(): MediaMemory;
set memory(options: MediaMemory);
get paused(): boolean;
set paused(value: boolean);
get muted(): boolean;
set muted(value: boolean);
get speed(): number;
set speed(value: number);
stop(time?: StopTime): this;
start(time?: StartTime, offset?: StartOffset, duration?: StartDuration): this;
chain(...nodes: ToneAudioNode[]): this;
disconnect(node: ToneAudioNode): this;
}
/**
* SoundRegistry is a singleton namespace that holds global state for the sound system.
* **DO NOT** import this module directly; use `sound`.
*/
declare namespace SoundRegistry {
const mediaInstances: Map<string, MediaInstance>;
const channels: Map<string, AudioChannelInterface>;
const transients: Set<Player>;
/**
* Aliases of {@link mediaInstances} that were paused by `pauseUnsavedAll`.
* Used by `resumeUnsavedAll` to only resume the instances it paused, leaving
* any previously-paused instances untouched.
*/
const systemPausedAliases: Set<string>;
const bufferRegistry: CachedMap<string, ToneAudioBuffer>;
}
/**
* The singleton sound manager instance. Use this to play and manage sounds in your game.
*/
declare const sound: SoundManagerInterface;
export { type AudioChannelInterface, type ChannelOptions, type ExportedSound, type ExportedSoundPlay, type MediaInterface, type SoundGameState, type SoundOptions, type SoundPlay, type SoundPlayOptions, type SoundPlayOptionsWithChannel, SoundRegistry, sound };