/SoundAPI

A library for working with sound.

Primary LanguageTypeScriptMIT LicenseMIT

SoundAPI

SoundAPI Dark Logo SoundAPI Light Logo

A library for working with sound.

Features

  • Auto use SoundPool or MediaPlayer
  • Support multiplayer
  • Auto stoped sync sounds
  • Effects
  • Sounds with start/end sounds

Changelog

Read Changelog.md

Usage

Init

Import library in your mod

IMPORT("SoundAPI");

And create SoundPool

const MySoundPool = new SoundAPI("MOD_ID");

Where MOD_ID is the mod string code used to distinguish mods.

Register Sound

To register a sound, the Sound.registerSound method is used.

MySoundPool.registerSound("sound_name", {
	//Path to sound file
	source: __dir__ + "path/to/file.ogg",

	// The parameters below are standard values
	// Sound hearing distance
	defaultDistance: 16,
	//Default sound volume
	defaultVolume: 1,
	//Clamp sound volume
	clampVolume: {mix:0, max:1},
	//Set default mode looping
	loop: false,
	//Type sound. Used for setting volume from game settings.
	type: SoundAPI.Type.SOUND,
	//Sync player in multiplayer
	sync: true,
	//Mote sound in solid block
	muteInSolidBlock: false
});

To register a sound with standard settings, instead of options, you can specify the path to the file.

MySoundPool.registerSound("sound_name", __dir__ + "path/to/file.ogg");

Play sound

MySoundPool.select("sound_name")
	// Set source sound in coordinates
	.at({ x:1, y:1, z:1, dimension:Player.getDimension() }) 
	// Set source sound in entity
	.at(Player.get()) 
	// Set the distance to be different from the standard distance specified during registration
	.distance(5)
	// Set the volume to be different from the standard volume specified during registration
	.volume(.5)
	// Set the loop mode to be different from the standard loop mode specified during registration
	.loop(false)
	// Disable multiplayer sync
	.sync(false)
	// Play sound
	.play()

Demo

Demo SoundAPI

Older versions: