Play Music

Play music media

Description

'play music <music_id> [with [properties]]'

The play music action let's you, as it name says, play some background music for your game. You can play as many songs as you want simultaneously.

To stop the music, check out the Stop Music documentation.

Action ID: Music

Reversible: Yes

Requires User Interaction: No

Parameters

Name

Type

Description

music_id

string

The name of the music you want to play. These assets must be declared beforehand.

properties

string

Optional. A list of comma separated properties with their respective value.

Properties

The following is a comprehensive list of the properties available for you to modify certain behaviors of the play music action.

Property Name

Type

Description

fade

string

The fade property let's you add a fade in effect to the music, it accepts a time in seconds, representing how much time you want it to take until the music reaches it's maximum volume.

volume

number

The volume property let's you define how high the music will be played.

loop

none

Make the music loop. This property does not require any value.

Assets Declarations

To play a song, you must first add the file to your assets/music/ directory and then declare it. To do so, Monogatari has an has a function that will let you declare all kinds of assets for your game.

Supported Formats

Each browser has it's own format compatibility. MP3 however is the format supported by every browser.

If you wish to use other formats, you can check a compatibility table to discover what browsers will be able to play it.

Examples

Play Music

The following will play the song, and once the song ends, it will simply stop.

Loop Music

The following will play the song, and once the song ends, it will start over on an infinite loop until it is stopped using the Stop Music Action.

Fade In effect

The following will play the song, and will use a fade in effect.

Custom Volume

The following will set the volume of this song to 73%.

Please note however, that the user's preferences regarding volumes are always respected, which means that this percentage is taken from the current player preferences, meaning that if the player has set the volume to 50%, the actual volume value for the song will be the result of:

500.73=36.550 * 0.73 = 36.5%

All Together

Of course, you can combine all of this properties, and remember the order doesn't really matter, you can write the properties on the order that feels more natural to you.

Audio Effects

You can apply multiple effects to your audio by specifying them in the properties.

Effect Syntax

Effects are specified by their name followed by their parameters:

Available Effects

Filter

Applies a Biquad filter (lowpass, highpass, etc.)

Parameters:

  • type - Filter type: lowpass, highpass, bandpass, notch, allpass, peaking, lowshelf, highshelf (default: lowpass)

  • frequency - Cutoff frequency in Hz (default: 800)

  • Q - Quality factor (default: 1)

  • gain - Filter gain in dB (default: 0)

Example:

Delay

A simple delay effect with feedback

Parameters:

  • time - Delay time in seconds (default: 0.4)

  • feedback - Feedback amount 0-1 (default: 0.5)

  • mix - Wet/dry mix 0-1 (default: 0.5)

Example:

Compressor

Dynamic range compression

Parameters:

  • threshold - Compression threshold in dB (default: -24)

  • knee - Knee width in dB (default: 30)

  • ratio - Compression ratio (default: 12)

  • attack - Attack time in seconds (default: 0.003)

  • release - Release time in seconds (default: 0.25)

Example:

Tremolo

Modulates the amplitude of the signal

Parameters:

  • frequency - Modulation frequency in Hz (default: 5)

  • depth - Modulation depth 0-1 (default: 0.8)

Example:

Distortion

Applies wave-shaping distortion

Parameters:

  • amount - Distortion amount (default: 50)

  • oversample - Oversampling: 2x, 4x, or none (default: 4x)

Example:

Convolution Reverb

Convolution reverb with a generated impulse response

Parameters:

  • seconds - Reverb duration in seconds (default: 2)

  • decay - Decay rate (default: 2)

  • reverse - Reverse the impulse response (default: false)

Example:

Bitcrusher

Reduces bit depth and sample rate of the signal

Parameters:

  • bits - Bit depth 1-16 (default: 4)

  • frequency - Sample rate reduction 0-1 (default: 0.1)

Example:

AutoWah

An envelope-following filter (auto-wah)

Parameters:

  • baseFrequency - Base frequency in Hz (default: 100)

  • octaves - Frequency range in octaves (default: 6)

  • sensitivity - Envelope sensitivity 0-1 (default: 0.5)

  • Q - Filter Q factor (default: 10)

Example:

Panner

Positions the sound in 3D space

Parameters:

  • x - X position (default: 0)

  • y - Y position (default: 0)

  • z - Z position (default: 0)

Example:

Phaser

A sweeping phase-shifting effect

Parameters:

  • frequency - LFO frequency in Hz (default: 0.5)

  • depth - Modulation depth in Hz (default: 1000)

  • feedback - Feedback amount 0-1 (default: 0.5)

  • stages - Number of filter stages (default: 4)

Example:

Chorus

Creates a thicker sound by modulating a delayed signal

Parameters:

  • frequency - LFO frequency in Hz (default: 1.5)

  • delay - Delay time in seconds (default: 0.025)

  • depth - Modulation depth in seconds (default: 0.002)

  • mix - Wet/dry mix 0-1 (default: 0.5)

Example:

Wah

A sweeping filter effect, like a guitar wah-wah pedal

Parameters:

  • baseFrequency - Base frequency in Hz (default: 350)

  • Q - Filter Q factor (default: 15)

  • depth - Frequency sweep range in Hz (default: 1500)

  • frequency - LFO frequency in Hz (default: 2)

Example:

Ring Modulation

Ring modulation for creating metallic, bell-like sounds

Parameters:

  • frequency - Modulator frequency in Hz (default: 30)

  • mix - Wet/dry mix 0-1 (default: 0.5)

Example:

Saturator

Soft clipping for warmth and harmonics

Parameters:

  • drive - Drive amount (default: 5)

Example:

Limiter

A hard compressor to prevent signal peaks from exceeding a threshold

Parameters:

  • threshold - Limiting threshold in dB (default: -1.0)

  • release - Release time in seconds (default: 0.05)

Example:

Fade In/Out Effects

Built-in fade effects for smooth transitions

Parameters:

  • duration - Fade duration in seconds (default: 1.0)

Example:

Multiple Effects

You can combine multiple effects on a single music track:

Effect Compatibility

Some effects require AudioWorklet support, which may not be available in all browsers:

  • Bitcrusher and AutoWah require AudioWorklet support

  • If AudioWorklet is not available, these effects will fall back to a simple gain node (no effect)

  • Other effects use standard Web Audio API nodes and work in all modern browsers

AudioPlayer Properties

The AudioPlayer instance provides several useful properties and methods:

Properties:

  • isPlaying - Whether the audio is currently playing

  • isPaused - Whether the audio is paused

  • paused - Alias for isPaused

  • ended - Whether the audio has ended

  • hasEnded - Alias for ended

  • duration - Total duration of the audio in seconds

  • currentTime - Current playback position in seconds

  • volume - Current volume (0-1)

  • loop - Whether the audio loops

Methods:

  • play() - Start playback

  • pause() - Pause playback

  • stop() - Stop playback and reset to beginning

  • fadeIn(duration) - Fade in over specified duration

  • fadeOut(duration) - Fade out over specified duration

Example: Complex Audio Setup

This example creates a layered audio experience with:

  • A main theme with low-pass filtering, delay, and looping

  • An ambient track with high-pass filtering and reverb

Last updated

Was this helpful?