Skip to content

Tutorials · Sound and Music · Chapter 5

Music

35 minutes Uses: AudioSource, Area, play_music, stop_music, in_group

By the end

  • The title’s music, set on its row in the Levels panel, streamed
  • The crypt’s own track: an Audio Source on the crypt’s root playing crypt-calm.wav on the Music bus, which takes the title’s music’s place as the crypt opens
  • The music box’s tune, a placed sound on Sfx heard only inside its alcove, louder as he nears it
  • alcove.rgs: stop_music() as he comes into the alcove and play_music("calm") as he leaves, the calm a trigger on the alcove’s own Audio Source
  • Who plays each music, and why the box is not music
On this page

Three pieces of music

The title gets StarNinjas’ Abandoned Castle Music, a loop of 31.26 seconds. The crypt gets its own music, crypt-calm.wav: a music box over a soft pad, eight bars in 24 seconds, made to loop. And the music box plays its tune, music-box.wav: the same melody alone, 24 seconds too. The two made ones came with the starter.

  1. Download Abandoned Castle Music from its page below: the download is the one file. Copy it into your project’s audio folder, and press Refresh in the Assets panel.
  2. Click it in audio, set its Stream to Always in Details, and press Save.
  3. Leave crypt-calm.wav and music-box.wav on Auto.
The Assets panel with Details open on abandoned_castle_music_-_starninjas_6.ogg: the path bar reads Assets / audio, the folder open with the file first in its list, over bell.wav, caught.wav, the creaks and the starter’s other sounds; Audio, hand-edited, Play, Stop and 31.26 s, a waveform of evenly spaced pulses, and Stream with Always lit
The title’s music: 31.26 s, Stream on Always.

All three are over three seconds, so Auto streams them anyway, the title’s music included. Always on the title’s music says what is meant: a long track that streams whatever it is trimmed to. Auto would hold it whole the day it were cut under three seconds; Always streams even a clip of a second and a half.

The title’s music

A level can name a music that plays as its scene opens. The title is a level, the first in the Levels panel’s list.

  1. Click the Levels tab, in the dock under the Viewport, and click the Title row.
  2. Drag abandoned_castle_music_-_starninjas_6.ogg from Assets onto the row’s Music button. The button reads its name; the ▶ beside it plays it.
  3. Leave The Crypt row’s Music empty.
The Levels panel: Levels, 2 in order, a New level box and Add level; row 1, Title, scenes/title.ron, startup, selected, with Up, Down, Open, Play, Scene and Remove, and a button reading Music: abandoned_castle_music_-_starninjas_6.ogg with a play button beside it; row 2, The Crypt, scenes/crypt.ron; and Then open: (none)
The title’s Music.
A level’s Music, in the Levels panel
A sound that plays as the level’s scene opens, in the editor’s Play and in an exported game, as the game’s music: on the Music bus, from nowhere, looping. There is one music at a time: the level’s replaces what was playing, and a level that names the music already playing leaves it playing. Click the button to clear it.

The crypt’s own track

The crypt has a music of its own, and it belongs to the crypt’s scene: a looping Autoplay on the Music bus is the room’s own track. It is the game’s music from the moment the room opens, and it takes the title’s music’s place.

  1. Select Crypt, the root of the crypt’s scene. Press Add component and choose AudioSourceComponent.
  2. Set Clip to crypt-calm.wav, Bus to Music and Volume to 0.7. Tick Looping and Autoplay. In Spatial, untick Positional.
  3. Press Ctrl+S.
The Inspector’s column, widened and narrowed to audio, on Crypt: AudioSourceComponent with Clip crypt-calm.wav, Bus Music, Volume 0.700, Pitch 1.00, Looping and Autoplay ticked, Spatial with Positional unticked, Max Distance 512.00 dimmed and Pan 0.000, Max Voices 0 and Triggers with no items
Crypt’s own track: crypt-calm.wav on Music.

Press Run game, which starts at the title, and listen, then press Play on the title.

The game opens on its title; four seconds in, Play is pressed and the crypt opens. Listen for the title’s music, then, the moment the crypt opens, the crypt’s calm in its place: the title’s music stops, and one music plays.
What it sounds like

The title’s music, the Abandoned Castle loop; it stops, and the crypt’s own music, a gentle music-box melody over a soft pad, plays in its place, with a fire crackling quietly under it.

The editor two seconds into Play on the crypt: the Sound tab’s Music and Master meters moving; under Music, Clip crypt-calm.wav, From Autoplay on Crypt, heard at -3.1 dB; Voices (6): gate gate-grind.wav silent, music crypt-calm.wav Music -3.1 dB, music_box music-box.wav Sfx silent, torch_left -9.1 dB, torch_right -31.2 dB, well silent; the Inspector shows the music box’s Audio Source
In the Sound tab, the music: crypt-calm.wav, from Autoplay on Crypt, heard at -3.1 dB, its 0.7.

The Sound tab’s Music band says what the music is and who asked for it: here, Autoplay on Crypt, the room’s own track. On the title it would say the level’s Music. Its voice is listed as music, on Music: the game’s music, no node’s.

The music box

At the far end of the crypt, in the alcove up through the gate, a music box plays a tune. The knight finds it by ear. It is a thing in the room, not the game’s music: a placed sound on Sfx, heard only near it.

  1. Select music_box, in alcove. Press Add component and choose AudioSourceComponent.
  2. Set Clip to music-box.wav and Volume to 0.3. Leave Bus on Sfx and Positional ticked. Tick Looping and Autoplay.
  3. Set Max Distance to 128, Rolloff to Linear and Min Distance to 24.
  4. Press Ctrl+S.
The whole crypt at 85.5% with music_box selected in the alcove at the top right: a small pink ring round the box on the alcove’s north wall and a larger pink ring centred on it covering the alcove and a little of the rock round it, short of the flooded passage to its west and of the gate room below; the Inspector shows Clip music-box.wav, Bus Sfx, Volume 0.30, Looping and Autoplay ticked, Positional ticked, Max Distance 128.00, Blend 1.00, Rolloff Linear, Min Distance 24.00, Pan 0.00
The box’s rings: Max Distance 128, the alcove and the rock round it, short of the passage and the gate room.
The Problems tab with one warning: music_box, This looping Autoplay on the Music bus is the room’s music track, which is never positional, so Positional does nothing — untick Positional
The Problems tab with the box put on Music: a second music, the room’s, which is never placed.

The alcove

The calm and the box’s tune are the same melody, and both play from the moment the crypt opens. In the alcove the calm would cover the box, so the alcove stops the music as the knight comes in, and plays it again as he leaves. It needs an Area to notice him, and the music to play again: a trigger on its own Audio Source.

  1. Select alcove. Press Add component and choose AreaComponent. In its Shape, set Size to 160, 144: the alcove’s floor.
  2. Press Add component and choose AudioSourceComponent. Leave Clip empty. Set Bus to Music and Volume to 0.7, the root’s, so the calm comes back as loud as it went. In Spatial, untick Positional.
  3. Add a trigger calm, its Clip crypt-calm.wav.
  4. In Assets, click Assets in the path bar, so the script is written at the project’s top, then click Create, choose New script, type alcove and press Enter. In the Code tab, replace everything in alcove.rgs with the script below, and press Ctrl+S.
  5. Select alcove, press Add component and choose Alcove. Press Ctrl+S.
rgs alcove.rgs
/// The alcove at the end of the crypt, where the music box plays. As the
/// knight comes in, the crypt's music stops, and the box is heard alone. As he
/// leaves, the crypt's music comes back, from its first bar.
component Alcove : Area {
    fn on_enter(other: NodeId) {
        if in_group(other, "knight") {
            stop_music();
        }
    }

    fn on_exit(other: NodeId) {
        if in_group(other, "knight") {
            // calm is the trigger on the alcove's own Audio Source: the
            // crypt's track, played as the music, which loops.
            play_music("calm");
        }
    }
}
The Inspector’s column, widened and narrowed to audio, on alcove: AudioSourceComponent with Clip (none), Bus Music, Volume 0.700, Pitch 1.00, Looping and Autoplay unticked, Spatial with Positional unticked, Max Distance 512.00 dimmed and Pan 0.000, Max Voices 0; Triggers with one row, 1 calm, open: Name calm, Clip crypt-calm.wav, Clips with no items, Mode Shuffle, Pitch and Volume each Min 1.00 and Max 1.00
The alcove’s Audio Source: no sound of its own, on Music, and calm.
The alcove’s Area Shape Size
The box that notices him, in world units, round the node’s middle: 160 by 144, the alcove’s floor, wall to wall, so on_enter runs as he comes in through the gate’s doorway and on_exit as he goes back out.
stop_music
Stops the game’s music, whoever asked for it: here the crypt’s own track. Sounds in the room, on Sfx, play on.
play_music
Starts a music by name. A name that is a trigger on the script’s own node’s Audio Source plays that row’s take as the game’s music: on the Music bus whatever the row says, from nowhere, looping whatever Looping says, and in place of whatever music was playing. Asking for the music already playing leaves it playing. Chapter 10 gives the crypt a music asset, whose tracks it names the same way.

Found by ear

  1. Press F5 and click the Sound tab.
  2. Go to the gate room, pull the lever, and walk up through the gate into the alcove, to the box. Then walk back out.
  3. Press F5 again to stop.
By the lever: E, the gate grinds open, and he walks up into the alcove to the box, stands, and walks back out. Listen for the calm playing all the way up; the moment he is in the alcove it stops, and the box’s tune is heard alone, louder as he comes up to it; walked back out, the calm comes back, and the box is gone.
What it sounds like

A gentle music-box melody over a soft pad, a metal clank, a rising grind and a thud, steps; the music cuts off and a quieter music-box tune alone grows closer; steps away, and the tune stops as the melody over the pad starts again.

The editor two seconds into Play with the knight a cell inside the alcove: the Viewport shows the alcove, the box on its north wall and the pink rings round it, the knight below; the Sound tab’s Music meter silent and its Music band reading No music; Voices (5): gate silent, music_box music-box.wav Sfx heard at -23.2 dB, the torches and the well silent
A cell inside the alcove: no music, and the box heard at -23.2 dB.
The game with the knight in the alcove, against its west wall between two gravestones, the music box glowing on the floor to his right, near the north wall under two red banners; the flooded passage’s water beyond the rock to the west
The alcove and its box: inside it the crypt’s music is stopped and the tune is heard alone.

The box played all the while, from the moment the crypt opened: from the gate room it reached him at nothing. Walk across in front of it and it moves from one ear to the other: a sound in the room, heard from where it stands. Leaving starts the calm again; by then he is out of the box’s earshot, so the two are never heard together.

The music

NodeComponentFieldValue
audio/abandoned_castle_music_-_starninjas_6.oggits DetailsStreamAlways
Title, in Levelsthe levelMusicabandoned_castle_music_-_starninjas_6.ogg
The Crypt, in Levelsthe levelMusicnone
CryptAudio SourceClip, Busaudio/crypt-calm.wav, Music
Volume0.7
Looping, Autoplayticked, ticked
Positionalunticked
music_boxAudio SourceClip, Busaudio/music-box.wav, Sfx
Volume0.3
Looping, Autoplay, Positionalticked, ticked, ticked
Max Distance, Rolloff, Min Distance128, Linear, 24
alcoveAreaShape Size160, 144
Audio SourceClip, Busnone, Music
Volume, Positional0.7, unticked
trigger calmClip crypt-calm.wav
Alcovealcove.rgs

Part 1 ends here: a crypt full of sounds placed, moving and played by scripts, with its music. Part 2 mixes them.

Check it works