Radio lines ​
F1 25 only has radio for the player's own car. The radio library lets an addon put a line on the radio for any driver, such as a crashed driver's "I'm OK" or a rival's engineer calling a stop, and lets a receiver such as a broadcast overlay show it. The poster gives the speaker, the text and, if it wants sound, a clip from its own folder. The core queues the lines one at a time, plays the clip, and fires three events a receiver follows: the line posted, started and ended. The voice line events stay the game's own lines.
#include <swofty/events.hpp>
#include <swofty/mixin.hpp>
#include <swofty/systems/race_control.hpp>
#include <swofty/systems/radio.hpp>
namespace rc = swofty::race_control;
namespace rd = swofty::radio;
namespace ev = swofty::events;
SWOFTY_ON_LOAD
{
if (!rd::available()) {
swofty::log("[incident-radio] this core has no radio module");
return;
}
ev::on<rc::events::CarStatus>().after([](const rc::events::CarStatus &e, const ev::Context &) {
if (e.status != rc::Status::retired)
return;
rd::Message m;
m.text = "Engine's gone, I'm stopping the car.";
m.vehicle = uint8_t(e.car);
m.role = rd::Role::driver;
m.category = rd::Category::incident;
m.audio = "radio/engine_gone.wav";
rd::Posted p = rd::post(m);
if (!p.ok())
swofty::log("[incident-radio] vehicle %u: %s", e.car, rd::name(p.result));
});
}
SWOFTY_ADDON()#include <swofty/events.hpp>
#include <swofty/mixin.hpp>
#include <swofty/systems/radio.hpp>
namespace rd = swofty::radio;
namespace ev = swofty::events;
SWOFTY_ON_LOAD
{
if (!rd::live()) {
swofty::log("[radio-log] this core does not fire radio line events");
return;
}
ev::on<rd::events::Started>().after([](const rd::events::Started &l, const ev::Context &) {
auto car = rd::vehicle(l);
swofty::log("[radio-log] %s of vehicle %d (%s): \"%.*s\", %u ms", rd::name(l.role), car ? int(*car) : -1,
rd::name(l.category), int(rd::text(l).size()), rd::text(l).data(), l.duration_ms);
});
ev::on<rd::events::Ended>().after([](const rd::events::Ended &l, const ev::Context &) {
swofty::log("[radio-log] line %08x %s after %u ms", l.id, rd::name(l.reason), l.played_ms);
});
}
SWOFTY_ADDON()[radio] first line posted by author.incident-radio
[radio] line 80000001 posted by author.incident-radio: driver of vehicle 7, incident, 2400 ms, clip yes, "Engine's gone, I'm stopping the car."
[radio] line 80000001 started (clip playing)
[radio-log] driver of vehicle 7 (incident): "Engine's gone, I'm stopping the car.", 2400 ms
[radio] line 80000001 ended after 2412 ms: finished
[radio-log] line 80000001 finished after 2412 ms- Header:
swofty/systems/radio.hpp, namespaceswofty::radio; host test fakeswofty/fake_radio.hpp - Core table: the
SwoftyCoreRadioexport,SwoftyRadioinswofty_api.h, table version 1 (SWOFTY_RADIO_VERSION);SWOFTY_API_VERSIONstays 9 - Events:
radio.line,radio.line.startandradio.line.end, notify, through the hooks table (API 9) - Feature:
events(stable) - Thread:
post,cancelandpendingfrom any thread; the three events on the flow thread - Changes nothing in the game: it hooks no game function; it plays your clip beside the game's audio
Is it there ​
bool rd::available(); // this core exports SwoftyCoreRadio: a poster checks it
bool rd::live(); // the core fires the three events: a receiver checks it
uint32_t rd::version(); // the core's radio table version, 0 without the module
bool rd::clips_supported(); // version 2: Message::clips and Message::gain
bool rd::transcripts_supported(); // version 2: Message::transcriptA core before this module has no SwoftyCoreRadio export and refuses the three event names, so an addon built against the new headers loads on it: available() and live() are false, post answers Result::off and nothing fires. A receiver subscribes only when live() is true. live() is false too with SWOFTY_RADIO=0 in the game's environment, which turns the module off.
Posting a line ​
namespace swofty::radio {
enum class Role : uint8_t { engineer, driver };
enum class Category : uint8_t { other, strategy, incident };
struct Message {
std::string text; // UTF-8, 1 to 240 bytes, no control characters
uint8_t vehicle = NO_CAR; // the speaker's vehicle id, 0 to 21
std::string driver; // or a driver reference ("game/hamilton"), at most 63 bytes
Role role = Role::engineer; // whose voice: the driver's or his race engineer's
Category category = Category::other;
uint32_t duration_ms = 0; // 0: the clip's length, else from the text
std::string audio; // a .wav of your own folder, "" for no sound
std::vector<std::string> clips; // version 2: up to 24 .wavs played back to back as the line's clip
float gain = 1.0f; // version 2: 0 to 4 times the clip's level; 0 plays it as recorded
bool transcript = false; // version 2: what a driver said, shown off the channel, no audio
};
struct Posted { Result result; uint32_t id; bool ok() const; };
Posted post(const Message &m); // queues the line; never plays from this call
Result cancel(uint32_t id); // a queued line never starts, the playing one stops
uint32_t pending(); // this addon's lines queued or playing
}post returns the line's id, which the three events carry as id. A radio id has bit 31 set (ID_BIT), so it never equals the id of a game voice line. Name the speaker with vehicle, driver or both; one is required. A receiver takes vehicle when it is set and otherwise looks the driver reference up in the live state, so a line for "game/hamilton" finds his car when he is in the session.
The result ​
| Result | Means |
|---|---|
ok | queued, and id is set |
off | no radio module on this core, the events feature is off, or SWOFTY_RADIO=0; a transcript on a version 1 core |
no_addon | the calling DLL is in no addon's manifest, so it has no folder and no name for the log |
bad_text | the text is empty, not valid UTF-8, or holds a control character (a newline or a tab too) |
too_long | the text is over 240 bytes; it is refused, never cut |
bad_audio | the path is not a relative . inside your folder, or the file is missing, or it is not PCM; for clips also more than 24, audio set beside them, a clip that is not 16-bit PCM, or clips of different rates or channel counts; a transcript with audio or clips; a gain other than 1 on an audio that is not 16-bit PCM |
bad_speaker | no vehicle and no driver, a vehicle past 21, an unknown role or category, or an invalid or 64 byte driver reference |
rate_limited | the addon made 8 calls in the last 10 s |
full | 16 lines wait and none is of a lower category than this one, or for a transcript, 16 transcripts are showing |
unknown | cancel only: no such line of yours, because it ended, or it is another addon's |
Strings are validated as UTF-8 (overlong forms, surrogates and values past U+10FFFF are invalid) and are copied. A refusal queues nothing, fires nothing and is written once to swofty/loader.log:
[radio] post refused for author.incident-radio: the text is over the byte cap
[radio] post refused for author.incident-radio: the clip is not a PCM .wav inside the addon's folder (radio/gone.wav is not in swofty/addons/author.incident-radio/)Limits ​
- Text: at most
TEXT_BYTES(240) bytes, 1 minimum. - Rate: at most
SWOFTY_RADIO_RATE_LINES(8) calls topostper addon in anySWOFTY_RADIO_RATE_WINDOW_MS(10 s). Every call counts, refused ones too, so a loop that posts a bad line is cut off as well. - Queue:
SWOFTY_RADIO_QUEUE_LINES(16) lines wait for the channel. A line posted to a full queue takes the place of the lowest, newest waiting line when its category is higher, and is refused withfullotherwise. - Waiting: a line that has waited
SWOFTY_RADIO_WAIT_MS(30 s) without starting ends asexpired. - Length: a line plays for
duration_mswhen you give one, clamped to 800 ms to 30 s, else for its clip, else for1200 ms + 60 ms per characterof its text up to 15 s. A clip longer than the line is stopped when the line ends.
The channel ​
There is one channel, so lines play one at a time, whoever posted them. The rule:
- A line waits until nothing plays and the gap of 0.4 s after the last line, an addon's or the game's, is over.
- The next line is the waiting one of the highest category:
incident, thenstrategy, thenother; among equals the one posted first. - A playing line is never cut for another addon's line, whatever its category.
canceland the line's own length end it. - The game's own radio wins. While the player's engineer or driver reply is on the air (the voice line events, category 2 from
engineerordrivers), no addon line starts, and an addon line that is playing when the game's line starts ends aspreemptedand is not played again. The core cannot hold the game's own lines back, so one the game posts during an addon line starts on top of it for the moment before the core stops the clip, at the next frame. On a build without the speech events the core never sees the game's radio, and only addon lines use the channel. - A session change ends every line as
lost.
Lines of the same moment keep their order: three lines posted in one frame announce in the order posted, then the best starts.
A transcript never takes the channel: rules 1 to 4 do not apply to it.
The events ​
namespace swofty::radio {
enum class End : uint8_t { finished, cancelled, preempted, expired, lost, dropped };
namespace events {
struct Line { // notify, the flow thread
static constexpr const char *point = "radio.line";
static constexpr uint32_t version = 1;
uint32_t size, payload_version;
uint32_t id; // the id post returned, bit 31 set
uint32_t duration_ms; // the length the core planned, in all three events
uint32_t played_ms; // ms since radio.line.start, 0 before it
uint8_t vehicle; // the speaker's vehicle id, NO_CAR when the poster named the driver only
Role role;
Category category;
uint8_t has_audio; // 1 when the line has a clip, or clips
End reason; // radio.line.end only
uint8_t flags; // FLAG_TRANSCRIPT (1) for a transcript, 0 from a version 1 core
uint8_t reserved[2];
char addon[64]; // the posting addon's id
char driver[64]; // the driver reference, "" when none
char text[256]; // the text, UTF-8
}; // 412 bytes
struct Started : Line { static constexpr const char *point = "radio.line.start"; };
struct Ended : Line { static constexpr const char *point = "radio.line.end"; };
}
std::string_view text(const events::Line &l);
std::string_view driver(const events::Line &l);
std::string_view addon(const events::Line &l);
std::optional<uint32_t> vehicle(const events::Line &l); // vehicle, else the driver reference's car from the live state
bool incident(const events::Line &l);
bool transcript(const events::Line &l); // flags has FLAG_TRANSCRIPT
const char *name(End e); // "finished", "cancelled", "preempted", "expired", "lost", "dropped"
}Subscribe with swofty::events::on<E>().after(...) from SWOFTY_ON_LOAD, as for every notify event (see Events).
radio.linefires on the first flow frame after the core takes the line, once per line, even when it never plays. The line is queued; it has not started.radio.line.startfires when the line begins: the clip starts, or at once for a line with no clip. A line that is cancelled, preempted before it began, dropped, expired or lost while waiting has none.radio.line.endfires once per line.reasonisfinishedwhen its time is up,cancelledaftercancel,preemptedwhen the game's own radio cut it,expiredafter 30 s of waiting,droppedwhen a higher line took its queue place, andlostwhen the session changed.played_msis how long it was on the air, 0 for a line that never started.
The order for one line is always radio.line, then radio.line.start when it starts, then radio.line.end. A receiver matches the three by id. Channel lines come one at a time: a start is never fired while another channel line is open. A transcript can be open beside one, so a receiver that shows a single line keeps transcripts in a place of their own, or shows them over the channel line and goes back to it when the transcript ends.
Audio ​
audio is a path to a PCM .wav inside your own folder, swofty/addons/<your id>/, relative, with no .., drive or leading slash, at most 120 bytes. The core checks at post that the file exists and reads its length from the RIFF header, so a bad clip fails the call (bad_audio) rather than playing nothing later. The clip starts with radio.line.start and is stopped when the line ends for any reason but finished.
No std or core call played an addon's own audio before this one (the game's sound is changed with engine sounds and the speech events only observe). The core plays a clip with winmm's PlaySound, which mixes beside the game's own audio. It supports PCM .wav only and one clip at a time, which the channel already guarantees; convert an .ogg or an .mp3 to PCM .wav first (mono 22 or 44 kHz keeps a clip small).
Through the game's engineer ​
On a core with speech control, a line with role engineer and audio or clips is not played beside the game: the core hands it to the game's speech system as one of the engineer's own lines and gives the audio engine the clip in place of his recording. It gets everything his lines get, in his order: the radio click when the channel opens, his 0.7 s delay, the radio effect, the speech volume slider, the ducking of the engine, music and commentary, the game's queue, and the game's subtitle with the line's text. Mutes on the engineer (see Voice lines) do not hold it back.
- Render engineer clips clean, without a radio filter of your own, at his level (about -14.5 LUFS): the game adds its radio stage. Use
gainto bring a quieter clip there. radio.line.startfires when the line is handed over andradio.line.endwhen the game's line ends (finished). The core says one line through the game at a time, and asks again while the game will not play it yet (a line of higher priority is on); a line it has not played within 20 seconds endsdropped.cancelends the linecancelled; before it plays the game never says it, once it plays the game says it to the end.- Other roles, and a core or game build that cannot hand a line over, play through
PlaySoundas below. The log says which ([speech] radio through the game's engineer: ...).
Subtitles ​
Every radio line shows its text in the game's own subtitle box, under the player's subtitle setting (Settings, Accessibility, Subtitles), like the game's own lines. A line through the game's engineer gets the game's subtitle. The core shows any other line there itself, a driver's clips, a text-only line or a transcript, for as long as the line lasts and hides it when the line ends. With subtitles off the game draws none of them. An overlay that shows radio lines itself needs nothing from this.
Clips and gain ​
On a version 2 core (clips_supported()), clips builds a line out of parts: up to SWOFTY_RADIO_CLIPS (24) relative paths of 16-bit PCM .wav files in your folder, all of one rate and channel count. At post the core reads them, joins them end to end with a crossfade of SWOFTY_RADIO_CROSSFADE_MS (6 ms, or a third of a shorter clip) between two, multiplies the samples by gain, and plays the result from memory as the line's one clip: the gain applies before the line enters the radio. audio stays empty. The line's length is the joined length, and the 30 s cap cuts a longer mix.
rd::Message m;
m.text = "Car ahead, Leclerc, 1.4.";
m.vehicle = player;
m.clips = {"voice/car_ahead.wav", "voice/pause.c.wav", "voice/d.leclerc.m.wav", "voice/pause.c.wav", "voice/g.1.4.e.wav"};
m.gain = 0.8f;
rd::post(m);gain works on audio too, which must then be 16-bit PCM; it is ignored for a text-only line. It is capped at SWOFTY_RADIO_GAIN_MAX (4), and 0, below 0 or NaN plays the clips as recorded, so a zeroed C struct sounds as it should. To mute your lines, post them without audio. A mix that would go over full scale is not clipped: a look-ahead limiter (1.5 ms ahead, 60 ms release) holds its peaks at SWOFTY_RADIO_LIMIT_MILLIDB (-0.1 dBFS). A mix that stays under full scale is not touched. Use gain to match a quiet clip to the game's engineer (his lines sit near -14.5 LUFS with peaks at 0 dBFS); a clip at -21.8 LUFS takes about 2.3.
The reads and the mix happen on the thread that calls post, so post a clip line from a worker thread or a flow event rather than the render thread. Every clip is read whole, at most 16 MB each.
A version 1 core does not read clips or gain: it shows the text with no sound. Check clips_supported() and fall back to one pre-mixed audio when you need sound there.
Transcripts ​
A transcript is what a driver said: the player's push-to-talk words, for example. Set transcript (on a version 2 core, transcripts_supported()), give no audio and no clips, and post it as any line:
rd::Message said;
said.text = "Box this lap?";
said.vehicle = player;
said.role = rd::Role::driver;
said.transcript = true;
rd::post(said);It gets radio.line and radio.line.start on the next flow frame and radio.line.end after its duration (duration_ms, or from the text). It never takes the radio channel: it does not wait for the playing line or the 0.4 s gap, holds back no line that waits, and the game's own radio does not preempt it. So the engineer's reply, posted right after, starts as soon as the channel is free. A transcript ends early only by cancel (cancelled) or a session change (lost). At most 16 show at once (full). It counts against the rate limit like any post, and pending() counts it while it shows.
The events carry FLAG_TRANSCRIPT in flags (rd::transcript(l)), so subtitles and overlays can show it as the driver's words. On a version 1 core post answers off for a transcript and posts nothing, because that core would queue it as a normal line on the channel.
Test it on the host ​
swofty/fake_radio.hpp installs a fake core whose hooks table runs your handlers and whose radio table records what you post.
#include <swofty/fake_radio.hpp>
#include <cassert>
int main()
{
swofty::fake::Radio fake;
fake.install();
std::string shown;
swofty::events::on<swofty::radio::events::Started>().after([&](const swofty::radio::events::Started &l, const swofty::events::Context &) {
shown = std::string(swofty::radio::text(l));
});
uint32_t id = fake.line(3, swofty::radio::Role::driver, "Contact, front wing!", swofty::radio::Category::incident);
fake.start(id); // radio.line.start
assert(shown == "Contact, front wing!");
fake.end(id); // radio.line.end, reason finished
swofty::radio::Message m; // the poster side
m.text = "Box, box.";
m.vehicle = 1;
assert(swofty::radio::post(m).ok() && fake.posted().size() == 1);
}line(vehicle, role, text, category = other, duration_ms = 3000, driver = "", audio = false, transcript = false)firesradio.lineand returns the id;start(id)andend(id, reason = finished, played_ms = duration)fire the other two.posted()andcancelled()list what the addon posted and cancelled, withclips,gainandtranscript;set_result(r)makespostrefuse withr.set_version(1)plays a version 1 core:clips_supported()andtranscripts_supported()are false, a transcript is not posted, and clips and gain are not read.set_live(false)makeslive()false;set_table_present(false)removes the table, as on an older core.