Skip to main content
Version: 1.20.x

Sound Definition Generation

The sounds.json file can be generated for a mod by subclassing SoundDefinitionsProvider and implementing #registerSounds. After implementation, the provider must be added to the DataGenerator.

// On the MOD event bus
@SubscribeEvent
public void gatherData(GatherDataEvent event) {
event.getGenerator().addProvider(
// Tell generator to run only when client assets are generating
event.includeClient(),
output -> new MySoundDefinitionsProvider(output, MOD_ID, event.getExistingFileHelper())
);
}

Adding a Sound

A sound definition can be generated by specifying the sound name and definition via #add. The sound name can either be provided from a SoundEvent, ResourceLocation, or string.

caution

The sound name supplied will always assume the namespace is the mod id supplied to the constructor of the provider. There is no validation performed on the namespace of the sound name!

SoundDefinition

The SoundDefinition can be created using #definition. The definition contains the data to define a sound instance.

A definition specifies a few methods:

MethodDescription
withAdds a sound(s) which may be played when the definition is selected.
subtitleSets the translation key of the definition.
replaceWhen true, removes the sounds already defined by other sounds.json for this definition instead of appending to it.

SoundDefinition$Sound

A sound supplied to the SoundDefinition can be specified using SoundDefinitionsProvider#sound. These methods take in the reference of the sound and a SoundType if specified.

The SoundType can be one of two values:

Sound TypeDefinition
SOUNDSpecifies a reference to the sound located at assets/<namespace>/sounds/<path>.ogg.
EVENTSpecifies a reference to the name of another sound defined by the sounds.json.

Each Sound created from SoundDefinitionsProvider#sound can specify additional configurations on how to load and play the sound provided:

MethodDescription
volumeSets the volume scale of the sound, must be greater than 0.
pitchSets the pitch scale of the sound, must be greater than 0.
weightSets the likelihood of the sound getting played when the sound is selected.
streamWhen true, reads the sound from file instead of loading the sound into memory. Recommended for long sounds: background music, music discs, etc.
attenuationDistanceSets the number of blocks the sound can be heard from.
preloadWhen true, immediately loads the sound into memory as soon as the resource pack is loaded.
// In some SoundDefinitionsProvider#registerSounds
this.add(EXAMPLE_SOUND_EVENT, definition()
.subtitle("sound.examplemod.example_sound") // Set translation key
.with(
sound(new ResourceLocation(MODID, "example_sound_1")) // Set first sound
.weight(4) // Has a 4 / 5 = 80% chance of playing
.volume(0.5), // Scales all volumes called on this sound by half
sound(new ResourceLocation(MODID, "example_sound_2")) // Set second sound
.stream() // Streams the sound
)
);

this.add(EXAMPLE_SOUND_EVENT_2, definition()
.subtitle("sound.examplemod.example_sound") // Set translation key
.with(
sound(EXAMPLE_SOUND_EVENT.getLocation(), SoundType.EVENT) // Adds sounds from 'EXAMPLE_SOUND_EVENT'
.pitch(0.5) // Scales all pitches called on this sound by half
)
);