Music, Sound and Voice
Monogatari plays three kinds of audio, each from its own folder and with its own volume slider in the settings screen:
| Kind | Folder | For |
|---|---|---|
| Music | assets/music |
Background music, usually looping. |
| Sound | assets/sounds |
Sound effects: a door, footsteps, a bell. |
| Voice | assets/voices |
A recorded voice for a line of dialog. |
This page shows how to add each one and control it from your script.
Audio in a game opened from a file
Served, Monogatari reads each audio file and plays it through the browser’s Web Audio feature, which is what lets a track take audio effects. Opened from its file, Chrome and Firefox don’t let the game read the file that way, so Monogatari plays it through a plain audio player instead: music, sounds and voices play, loop, fade, pause and stop as they do served, but the effects don’t apply.
So you can write and hear your game’s audio opened from a file. Check the effects in the game served: from your game’s web host, or from an editor that previews the folder through a web address. What works opened from a file lists the ways to do that, and everything else that differs.
Add music
Put the music files in assets/music:
assets/
music/
theme.mp3
library.mp3Declare them in js/script.js, in the monogatari.assets('music', { … }) call the starter game already has. Each entry pairs a name, which your script uses, with the file name:
monogatari.assets('music', {
theme: 'theme.mp3',
library: 'library.mp3',
});The browser decodes the files itself, so use a format every browser plays: MP3 is the safe choice.
Play music
'Start': [
'show scene afternoon with fadeIn',
'play music theme with loop',
'The drawing class had ended an hour ago.',
'end',
],The music starts with the scene and plays under the lines that follow, across labels and scene changes, until something stops it. Without loop, it plays once and stops.
TIP
A misspelled name, or a file that isn't in the folder, doesn't stop the game: the story goes on in silence. While you develop, an error dialog says The music “afternon” could not be loaded. and shows the file it looked for. This goes for sounds and voices too: when some audio doesn’t play, check its name in the statement, in monogatari.assets() and on the file.
The words after with change how it plays, in any order:
| Word | What it does |
|---|---|
loop |
Starts again from the beginning when it ends. |
fade 3 |
Fades in over 3 seconds. |
volume 50 |
Plays at 50% of the player’s music volume. |
'play music library with loop fade 2 volume 60',Several tracks can play at once: play music rain with loop while theme plays adds rain on top. A track can also take audio effects, like a filter or an echo: Play Music lists every word.
Stop or pause music
'stop music theme with fade 2', // fades theme out over 2 seconds
'stop music', // stops every track at once
'pause music', // pauses every track
'play music', // resumes what was pausedA stop with fade holds the game until the fade ends: the line after 'stop music theme with fade 2' shows 2 seconds later.
Stop Music and Pause have the details.
Play a sound effect
Sounds work like music, from assets/sounds and monogatari.assets('sounds', { … }):
monogatari.assets('sounds', {
bell: 'bell.mp3',
door: 'door-creak.mp3',
});'play sound bell',
'The bell rang, and the plaza filled with voices.',
'play sound door',
'Someone opened the door behind me.',A sound without loop plays once. It doesn’t hold the game: the next line shows while it plays. Sounds take the same words as music (loop, fade, volume), and stop sound stops them. Play Sound has the details.
Voice a line
Voices come from assets/voices and monogatari.assets('voices', { … }). Play the voice right before the line it belongs to:
monogatari.assets('voices', {
'yui-001': 'yui-001.mp3',
'yui-002': 'yui-002.mp3',
});'play voice yui-001',
'y Hi, Mio! Did you finish your drawing of the palace?',
'play voice yui-002',
'y:worried I didn\'t either.',- The voice stops when the player moves on, so a player who reads faster than the recording doesn’t hear two lines at once.
- Auto-play waits for the voice to finish before it goes on to the next line.
Play Voice has the details.
Music for the main menu
To play music on the main menu, name the track in the MainScreenMusic setting in js/options.js. It must be one of your declared music assets, and it loops:
monogatari.settings({
'MainScreenMusic': 'theme',
});The starter game’s js/options.js already has a 'MainScreenMusic': '' line: put the name between its quotes. The music stops when the game starts. It plays through a plain audio player, served or opened from a file, so it takes no effects.
Browsers don’t let a page play sound before the player has clicked or tapped it. When the browser holds the main menu’s music back, Monogatari shows a Click here to allow audio playback. notice, and the music starts when the player clicks it. The music of the story doesn’t have this problem: it starts after the player has clicked Start.
What the player controls
The settings screen has a volume slider for music, sound and voice (and one for video). Every track plays at the player’s volume for its kind; volume 50 in a statement is a share of that.
Going back and loading
The game keeps track of what is playing:
- Going back (rollback) past a
playstatement stops what it started, going back past astopstatement plays that audio again, and going back past apauseor a resume undoes only that. - Loading a save starts the music and sounds that were playing when the game was saved.
You don’t need any code for this.
Related
- Play Music, Play Sound, Play Voice, Stop Music, Stop Sound, Stop Voice and Pause: the statements, in full.
- Player Preferences: the starting volumes.
- Open Your Game: the ways to serve your game.