Repository navigation
Unity Adaptive Music
One song, and one number from your game that decides how much of the band plays it. This page shows the idea, the ten lines of code that drive it, and how the showcase uses it over a whole level.

The demo song. Each row is an instrument with its LAYER mark; each column is a value of music.intensity. At 0
only the kick is let through, at 0.5 six instruments, at 1 all nine. Which of them you hear in a bar also depends on
the pattern: not every pattern has notes for every instrument. The other seven sounds of the kit have no notes in
this song.
Every sound of the song has a LAYER mark. A sound plays only while
intensityis at or above its mark.
| Word | What it is |
|---|---|
| LAYER mark | a number for each of the song's 16 sounds, 0 to 1. It is set on the device and stored in the song. 0 = the sound always plays. Read it with SfxSong.Layer(sound)
|
intensity |
a field of SfxMusicPlayer, 0 to 1. Your game sets it. It is not in the song |
Two details that save a surprise:
- A mark only lets a sound through. The sound still needs notes in the pattern that plays. In the demo song the choir is only in pattern E, bells are in C, D and E, brass is in B, C, D and F.
- Intensity decides which notes start. It is compared when a step begins, so a held note that already sounds is not cut when the intensity falls under its mark.
The demo song's marks, as the picture shows them:
intensity at or above |
joins |
|---|---|
| 0 | kick |
| 0.16 | hi-hat |
| 0.28 | FM bass |
| 0.39 | snare |
| 0.47 | strings, choir |
| 0.57 | FM keys |
| 0.71 | brass |
| 0.79 | bells |
flowchart LR
V["a game value 0..1<br/>(speed, danger, combo)"] --> F["floor<br/>the least the band plays"]
F --> S["smoothing<br/>fast up, slow down"]
S --> I["music.intensity"]
I --> L{"at or above<br/>a sound's LAYER mark?"}
L -->|yes| P["its notes start"]
L -->|no| Q["its notes are skipped"]
The player does not smooth intensity. If you write a jumpy value into it, instruments flicker in and out. Smooth it
yourself: up quickly, so the band arrives with the action, and down slowly, so it lingers.
using CatSplat.SfxForge;
using UnityEngine;
public class DangerMusic : MonoBehaviour
{
public SfxMusicPlayer music; // one that has a song, for example DemoSong.asset
[Range(0f, 1f)] public float danger; // set it from your game: 0 calm … 1 the enemy is on you
public float floor = 0.15f; // the least the band ever plays
public float riseSeconds = 0.6f, fallSeconds = 2.5f;
float intensity;
void Update()
{
float target = Mathf.Max(floor, danger);
float seconds = target > intensity ? riseSeconds : fallSeconds;
intensity = Mathf.MoveTowards(intensity, target, Time.deltaTime / seconds);
music.intensity = intensity;
}
}Try it:
- Get the demo song playing: the five steps of Unity Music First Song.
- Save the script as
DangerMusic.csand add it to any GameObject. - Drag the object with the SFX Music Player onto the script's Music field.
- Press Play. You hear the kick alone:
dangeris 0, and the floor of 0.15 is under every other mark. - Drag Danger up in the Inspector. Hi-hat, bass, snare and the rest join within 0.6 seconds. Drag it down: they leave over 2.5 seconds.
Then let the game write danger. Any value from 0 to 1 will do; this one is the distance to an enemy:
// in: DangerMusic dangerMusic, Transform enemy, Transform player
float distance = Vector3.Distance(enemy.position, player.position);
dangerMusic.danger = Mathf.InverseLerp(20f, 3f, distance); // 0 at 20 m or more, 1 at 3 m or lessriseSeconds and fallSeconds are the time to cross the whole range from 0 to 1. Pick the floor from the table
above: 0.15 is under the hi-hat's mark, so at rest this script leaves the kick alone; 0.42 would keep kick, hi-hat,
FM bass and snare.

Two recordings from the showcase. Above: the intensity (orange) follows the player's speed (white). Standing still it rests on the section's floor, 0.42; speed lifts it towards 1, up in 0.6 s and down in 2.5 s for the whole range. The names on the right show which sound joins at which height. Below: a hit sets the intensity to 0 at once, under the floor, so only the kick is left; the music's volume dips from 0.36 to 0.18 in 50 ms. Both come back in 2 s.
To drop the band, write 0 into your smoothed value and let the smoothing bring it back. The showcase's
MusicDirector does this, and also holds the target down for as long as the volume is dipped:
// a fragment of MusicDirector.cs: intensity is its smoothed value, duckTarget starts the volume's dip
// in: float duckTarget, float intensity
void OnHurt()
{
duckTarget = 1f; // the volume dips (see Unity Music Mix And Notes)
intensity = 0f; // only the kick is left; Update brings it back
}In the DangerMusic script above, the same is one line in a method of your own: intensity = 0f;.
The showcase is one level of eight sections. Each section puts a floor under the intensity: what plays while the player stands still. Speed fills the rest of the way to 1.
stateDiagram-v2
direction LR
[*] --> Title
Title --> Running: GameStarted
Running --> Hit: Hurt
Hit --> Running: back over 2 s
Running --> Lifted: CheckpointReached
Lifted --> Running: after one bar
Running --> Fanfare: GoalReached
Fanfare --> Running: GameStarted
| State | What the music does |
|---|---|
| Title | intensity held at 0.42: kick, hi-hat, FM bass, snare |
| Running | intensity towards Mathf.Lerp(floor, 1f, player.Speed01): the section's floor standing still, 1 at top speed |
| Hit | intensity 0 at once, volume halved; both back over 2 s |
| Lifted | the target is at least 0.8 for one bar, which is above every mark of the song; three bell notes answer |
| Fanfare | the song is stopped; brass, bells, timpani and strings are played by hand |

Above: the level to scale, with each section's floor and the pattern it holds, if any. Below: one recorded run. The dashed line is the floor, the orange line the intensity, the white line the player's speed.
| Section | Floor | Standing still you hear |
|---|---|---|
| Soft Start, The Resonator | 0.42 | kick, hi-hat, FM bass, snare |
| Rolling Sines | 0.45 | the same |
| Full Cycle, Woofer Steps, Static Field | 0.50 | and strings, choir |
| Split Signal | 0.55 | the same |
| Overdrive | 0.60 | and FM keys |
A floor is one line: Mathf.Lerp(floor, 1f, speed01) instead of Mathf.Max(floor, danger). The showcase also holds
two patterns of the song in two places; that is the other half of adaptive music, on
Unity Cues And Patterns.
SfxMusicPlayer.ApplyTelemetry(SfxTelemetry.Intensity, value01) sets the intensity the way the device does when it
receives the same value over the link. See Unity Telemetry And Encoders.
The marks are set on the device (Device Layers And Intensity). In Unity you can move a sound's LAYER in the song's Inspector, and drag an INTENSITY strip there to see which rows go dim: Unity Song Inspector.
What of this was run, and what nobody has listened to: What Is Tested.
- Unity Cues And Patterns: change the pattern on the bar line.
- Unity Music Mix And Notes: duck the volume, play a stinger.
- Unity Showcase Walkthrough: the level these pictures were recorded in.
- Unity Recipes: more scripts to paste.
- The Unity manual: Intensity and layers
- Recipe 6: Music that follows danger
- The showcase's MusicDirector
- The device's manual, section 11: Layers
-
SfxMusicPlayer.cs ·
MusicDirector.cs ·
core/sfx_song.h (
sfx_player_rendercompares the intensity withlevel) - The first picture's script: um_figures.py
SFX FORGE, firmware 1.7.2 · Home · Glossary · What Is Tested · the pages are kept in the repository's wiki/ folder: Editing This Wiki
Start
Device
- Quick Start
- Controls And Pages
- Make A Sound
- The Five Engines
- Make A Beat
- Recording
- Finer Grids
- Time Signatures
- Perform
- Song And Arrangement
- Layers And Intensity
- Automation
- Mixer And Effects
- The Stage
- Saving And Reset
- Cheat Sheet
Unity: first steps
- Install
- Quick Start
- Concepts
- Choosing A Route
- One Shots
- Live Voices
- Held Sounds
- Variations
- Presets And Patches
Unity: music
Unity: link and tools
Unity: in practice
- Recipes
- Recipes: Music
- Recipes: Link
- Showcase Walkthrough
- Copy The Showcase Pattern
- Troubleshooting
- Testing Your Audio
Unity: reference and extending
Internals
- Architecture Overview
- Repository Map
- Sound Engine
- Patch Format
- Song Format
- Link Protocol
- Firmware Internals
- Add A Page Or Key
- Add An Engine
- Add A Setting
- Building From Source
- Running The Tests
- Release Checklist
About this wiki