Skip to main content

Change audio levels through the API

This extends the commentary mix with independent program and commentary volume controls. Use the shared setup and the complete download below. Connect the SRT receiver before sending commands.

Each input first becomes packed 48 kHz stereo PCM (pcm_s16le, format = "s16"). The volume stage needs a compatible PCM sample format. Keep these preparation stages when adapting the example to AAC inputs so the volume output can feed the next processing stage.

Each volume node lives in its own compositor (program-gain and commentary-gain). Their outputs feed a third compositor, mix, which runs amix. Nodes read local compositor inputs; feeding a node ID directly into another node in the same compositor is unsupported.

Mute and restore commentary​

After all tasks have started, mute commentary while the program continues:

bash
curl --fail-with-body -X POST \
http://127.0.0.1:4040/api/compositors/commentary-gain/level/command \
-H 'Content-Type: application/json' -d '{"volume":0.0}'

Restore its configured gain:

bash
curl --fail-with-body -X POST \
http://127.0.0.1:4040/api/compositors/commentary-gain/level/command \
-H 'Content-Type: application/json' -d '{"volume":0.3}'

For program gain use /api/compositors/program-gain/level/command with, for example, {"volume":0.5}. Values are linear multipliers: 0 mutes and 1 is unity. These are immediate changes, not an automatic fade or ducking controller. The mixer weights remain [1.0, 1.0] because the upstream nodes apply the gains.

For a simple balance change you do not need the separate gain stages: the commentary mix can take new weights on its amix node directly. A weights command sets every input at once, so use the gain stages when operators or systems adjust each input on its own.

A successful HTTP response acknowledges dispatch. Confirm the 660 Hz tone disappears and returns while the 440 Hz tone and red picture continue. Check logs if the command is accepted but inaudible. Filter commands are transient: restart or graph recreation restores the configured levels.

Complete configuration​

Download live-audio-levels.toml.

toml
# Vindral Bridge Packager example
# Mix program audio and commentary with levels you change through the API, sent over SRT.

version = "3"

[api]
bind = "0.0.0.0:4040"

[[sources]]
id = "program"
source_type = "file"
realtime_pacing = true
protocol_options = { url = "/media/camera-a.mp4" }

[[sources]]
id = "commentary"
source_type = "file"
realtime_pacing = true
protocol_options = { url = "/media/commentary.m4a" }

[[transcoders]]
id = "program-pcm"
source_ids = ["program"]
device = "cpu"
[[transcoders.tracks]]
id = "audio"
type = "audio"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[transcoders.tracks.renditions]]
id = "pcm"
codec = "pcm_s16le"
format = "s16"
sample_rate = 48_000
channels = 2

[[compositors]]
id = "program-gain"
[[compositors.audio.sources]]
id = "input"
source_id = "program-pcm"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[compositors.audio.nodes]]
id = "level"
type = "volume"
source_ids = ["input"]
volume = 0.7

[[transcoders]]
id = "commentary-pcm"
source_ids = ["commentary"]
device = "cpu"
[[transcoders.tracks]]
id = "audio"
type = "audio"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[transcoders.tracks.renditions]]
id = "pcm"
codec = "pcm_s16le"
format = "s16"
sample_rate = 48_000
channels = 2

[[compositors]]
id = "commentary-gain"
[[compositors.audio.sources]]
id = "input"
source_id = "commentary-pcm"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[compositors.audio.nodes]]
id = "level"
type = "volume"
source_ids = ["input"]
volume = 0.3

[[compositors]]
id = "mix"
[[compositors.audio.sources]]
id = "program-audio"
source_id = "program-gain"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[compositors.audio.sources]]
id = "commentary-audio"
source_id = "commentary-gain"
track_selector = { type = "track-kind", kind = "audio", is_required = true }
[[compositors.audio.nodes]]
id = "sum"
type = "amix"
source_ids = ["program-audio", "commentary-audio"]
weights = [1.0, 1.0]
normalize = false

[[compositors.passthrough.sources]]
id = "program-video"
source_id = "program"
track_selector = { type = "track-kind", kind = "video", is_required = true }

[[transcoders]]
id = "output"
source_ids = ["mix"]
device = "cpu"

[[transcoders.tracks]]
id = "video"
type = "video"
passthrough = true
track_selector = { type = "track-kind", kind = "video", is_required = true }

[[transcoders.tracks]]
id = "audio"
type = "audio"
track_selector = { type = "track-kind", kind = "audio", is_required = true }

[[transcoders.tracks.renditions]]
id = "audio-output"
codec = "aac"
sample_rate = 48_000
channels = 2
bit_rate = 128_000

[[sinks]]
id = "srt"
sink_type = "srt"
source_ids = ["output"]
wait_for_all_sources = true
protocol_options = { url = "srt://0.0.0.0:10000?mode=listener&latency=120000" }