Audio channels ​
Synced music from speakers instead of screens, for other resources: DJ booths, club PAs, festival stages. np_entertainment plays its decks, FX pads and the music bus of its sound system on them.
A channel uses the same server-authoritative playback as a board: a startedAt anchor, the synced clock, drift
correction, a queue of up to 25 entries, autoplay-next, YouTube titles and np_screens:playbackChanged. It is not
bound to a surface. Clients that can hear a channel play it through a hidden DUI: they are in the channel's routing
bucket, within maxDistance of an emitter and in the right interior. Only the nearest Config.AudioChannels.maxActive
channels play at once.
The volume a player hears is: channel volume (incl. ramp) × the loudest emitter's falloff × gain ×
Config.MasterVolume × the player's mute / local volume.
Sources ​
YouTube (audio from the hidden player), video files, and direct audio files (mp3 ogg oga m4a aac wav opus flac,
provider audio_file). Links must be http(s), must not have credentials or a private host, and must pass the
library's domain allow / block lists. Boards are unchanged: an .mp3 on a board still uses the webpage fallback.
Ownership ​
np_screens is an np_addon host (bridge/np_addon.lua, addon API 1). Create your channels inside
npAddon.attach('np_screens', fn, { api = 1 }) so they come back after np_screens restarts.
Every channel belongs to the resource that created it. Only that resource can drive it, and it is destroyed when that resource stops. No net event can control a channel: clients can only request a resync and report a probed track duration.
Exports ​
server
Called as exports.np_screens:<name>(...). id is your local id ('booth1') or the full id createAudioChannel
returns ('np_entertainment:booth1'). They return ok, reason? unless noted.
| Export | Notes |
|---|---|
createAudioChannel(id, opts) → ok, fullId | reason |
opts: emitters, bucket (default 0), interior, label, volume (0–100, default 100), loop, autoAdvance (queue autoplay, default true), probeDuration (default true, see Durations). Fails with exists, limit or invalid_* |
destroyAudioChannel(id) |
Silences it for everyone |
setChannelEmitters(id, emitters, opts?) |
Replaces the emitters. opts.interior / opts.label can change those too |
setChannelOptions(id, { loop?, autoAdvance?, probeDuration? }) |
|
channelLoad(id, url, opts?) |
Replaces the current track. opts: meta, position (s), autoplay (default true; false cues it paused at position), title, duration, requestedBy |
channelPlay(id) |
Resumes a paused track, or starts the queue head when idle |
channelPause(id) / channelResume(id) / channelStop(id) / channelSkip(id) |
stop clears the track and keeps the queue |
channelSeek(id, seconds) |
|
channelQueueAdd(id, url, meta?, mode?, opts?) → ok, 'playing' | 'queued' | reason, position |
mode: 'end' (default), 'next', 'now' |
channelQueueRemove(id, index) / channelQueueMove(id, from, to) / channelQueueClear(id) |
1-based |
channelSetVolume(id, volume, { rampMs?, at? }) |
Volume 0–100, optionally ramped (Ramps) |
getChannelState(id) → table | nil |
Read-only, any resource, by full id: the synced state plus channel (definition), serverNow and volumeNow |
getServerTime() → ms |
The playback clock (startedAt, ramp at) |
Emitters ​
Up to 16 per channel, for example PA left / right and delay towers:
{ coords = vec3(-1600.0, -3010.0, -79.0), refDistance = 10.0, maxDistance = 60.0, gain = 1.0 }
-- or a speaker prop / a vehicle:
{ netId = NetworkGetNetworkIdFromEntity(speaker), refDistance = 8.0, maxDistance = 40.0 }
The volume stays full inside refDistance and falls linearly to 0 at maxDistance. Emitters don't add up: the
loudest one counts. interior = <interior id> restricts the channel to players whose
GetInteriorFromEntity(PlayerPedId()) matches; interior = true uses the interior the first emitter stands in. Either
way, nothing leaks through club walls.
Ramps ​
channelSetVolume(id, v, { rampMs = 4000, at = t }) goes linearly from the volume at t to v over rampMs. t is
server time from getServerTime() (default: now) and can be up to 5 minutes ahead; a t in the past starts the ramp
now. The ramp is part of the synced state (state.ramp = { from, to, at, ms, prev? }, state.volume = target).
Every client evaluates it against the synced clock, so a crossfade lands at the same moment for everyone.
Scheduling a ramp never makes the volume jump: the previous ramp keeps running until the new one's at, and the new one
starts from wherever that leaves it. A volume change reaches clients as a small np_screens:channelVolume delta, not
the whole state.
Meta ​
meta on a load or queue entry is a flat table with up to 8 keys and about 1 KB in total. Values are strings (≤ 256
bytes), numbers or booleans; anything else is dropped. It shows up as state.trackMeta for the current track and
queue[i].trackMeta, also in np_screens:playbackChanged. Channels fire that event with
contentId = '@channel/<fullId>'.
Meta is public
Meta is sent with every sync to every client in the channel's bucket. Don't put anything private in it.
Durations ​
Autoplay-next needs the track's length. Pass duration with channelLoad / channelQueueAdd; the owner's value
always wins. Otherwise listeners' clients probe it. A probed value only fills a duration that is still missing, for the
track that is loaded, and the first accepted value is final. It must be more than the current position + 5 s and at
most 6 h. When two or more players are in range, two different players have to report values within ±2 s of each
other. Set probeDuration = false to rely on your own duration only.
Example: a two-deck crossfade ​
npAddon.attach('np_screens', function()
local pa = { { coords = vec3(-1600.0, -3010.0, -79.0), refDistance = 8.0, maxDistance = 35.0 } }
for _, deck in ipairs({ 'deckA', 'deckB' }) do
exports.np_screens:createAudioChannel(deck, { emitters = pa, interior = true, label = 'Club ' .. deck })
end
end, { api = 1 })
local function crossfade(fromDeck, toDeck, url, ms)
local s = exports.np_screens
s:channelSetVolume(toDeck, 0)
s:channelLoad(toDeck, url, { meta = { deck = toDeck } })
local at = s:getServerTime() + 500 -- same instant on every client
s:channelSetVolume(toDeck, 100, { rampMs = ms, at = at })
s:channelSetVolume(fromDeck, 0, { rampMs = ms, at = at })
end
Config ​
Config.AudioChannels in config/audio_channels.lua:
| Key | Default | |
|---|---|---|
maxPerOwner |
32 |
Channels one resource may hold at once |
maxActive |
3 |
Hidden DUIs a client runs at once; the nearest channels win |
rangeInterval |
1000 |
ms between range / interior checks (DUI create / destroy) |
volumeInterval |
250 |
ms between volume updates of an active channel |
rampInterval |
50 |
ms between volume updates while a ramp runs |
hysteresis |
5.0 |
Metres past an emitter's maxDistance before a running channel's DUI is destroyed |
duiWidth / duiHeight |
256 / 256 |
Size of the hidden DUI; YouTube's player wants at least 200 × 200 |