Voice commands ​
F1 25 has voice commands of its own: the player holds or presses the Talk binding, the voice panel on the MFD lists what can be said, and the engineer answers. Behind it sits the Microsoft Speech Platform. The voice library lets one addon take the place of that recogniser. The game keeps everything else: its binding, its panel, its rule of when to listen, its commands and the engineer's answers. The recogniser addon hears when the game opens and closes its listening window and hands back the text it heard. The core matches the text against the game's commands and runs the game's own command, so the panel shows it as recognised and the engineer answers as usual. Any other text goes to the addons as voice.command, and an addon that acts on it has the panel show it as recognised. Addons can add commands of their own: the panel lists them after the game's, and the recogniser can use them to bias what it hears. An addon can also run one of the game's commands with no speech at all, as if the player had said it.
#include <swofty/mixin.hpp>
#include <swofty/systems/voice.hpp>
namespace vc = swofty::voice;
void open_mic(bool on); // your capture: start on 1, stop and transcribe on 0, then call vc::heard(text)
SWOFTY_ON_LOAD
{
vc::Result r = vc::recognise([](void *, int on) { open_mic(on != 0); });
if (r != vc::Result::ok)
swofty::log("[my-recogniser] not the recogniser: %s", vc::name(r));
}
SWOFTY_ADDON()#include <swofty/events.hpp>
#include <swofty/mixin.hpp>
#include <swofty/systems/voice.hpp>
namespace vc = swofty::voice;
namespace ev = swofty::events;
uint32_t space_id;
SWOFTY_ON_LOAD
{
if (!vc::live())
return;
space_id = vc::add("is there space to pit").id;
ev::on<vc::events::Command>().replace([](vc::events::Command &c, const ev::Context &) {
if (c.id != space_id)
return false;
swofty::log("[pit-questions] asked for space (%.2f)", c.confidence);
return true;
});
}
SWOFTY_ADDON()[voice] voice commands armed: an addon can take the place of the game's speech recogniser
[voice] author.my-recogniser is the recogniser
[voice] author.my-recogniser recognises speech in place of the game's recogniser
[voice] the game gave the core's recogniser 5 keyword list(s)
[voice] active list Race: 33 command(s)
[voice] author.pit-questions added the command "is there space to pit" (80000001)
[voice] voice panel: 6 game row(s), 1 addon row(s), 1 page(s)
[voice] heard "Box this lap": the game's command "box this lap"
[voice] heard "is there space to pit": handled by an addon
[voice] author.strategy runs the game's command "box this lap" (Box)- Header:
swofty/systems/voice.hpp, namespaceswofty::voice; host test fakeswofty/fake_voice.hpp - Core table: the
SwoftyCoreVoiceexport,SwoftyVoiceinswofty_api.h, table version 1 (SWOFTY_VOICE_VERSION);SWOFTY_API_VERSIONstays 9 - Events:
voice.listen(notify) andvoice.command(cancel), through the hooks table (API 9) - Feature:
events(stable) - Thread:
recognise,heard,commands,add,stateandrunfrom any thread;listenon the game's thread; the events on the flow thread - Changes in the game: with a recogniser registered, the core wraps
Voice_Create_Recogniser_Thunkand hands the game its own recogniser in place of the Speech Platform one, and wrapsVoice_Build_CommandListto add the addons' commands to the voice panel. Without one the game keeps its own
Is it there ​
bool vc::available(); // this core exports SwoftyCoreVoice
bool vc::live(); // the core fires voice.listen and voice.command
vc::State vc::state();| State | Means |
|---|---|
off | no voice module on this core, the events feature is off, SWOFTY_VOICE=0, or the build is not one the module was checked on |
game | the game keeps its own recogniser: no addon registered before the game set up speech recognition |
waiting | a recogniser is registered and the game has not set up speech recognition yet |
idle | the addon is the game's recogniser and the game is not listening |
listening | the game's listening window is open |
A core before this module has no SwoftyCoreVoice export: every call answers Result::off and nothing fires.
Being the recogniser ​
namespace swofty::voice {
Result recognise(SwoftyVoiceListenFn listen, void *user = nullptr); // void listen(void *user, int on)
Result withdraw();
Result heard(std::string_view text, float confidence = 1.0f);
std::vector<std::string> commands();
}Call recognise from SWOFTY_ON_LOAD. The game creates its recogniser once, when it sets up speech recognition at boot, so a recogniser registered after that is refused with late and the game keeps its own until it restarts. One addon is the recogniser; another gets taken.
The game calls listen(user, 1) when it opens its listening window and listen(user, 0) when it closes it, on its own thread, so return at once. The game decides when: the Talk binding opens the voice panel and the window, and the window closes on the game's timer, when the panel closes, when a command is recognised, and while the game plays a line of speech of its own (the window opens again after it). Capture the player's voice while the window is open (keep a short ring of audio from before it opens if your capture takes time to start), then call heard with the text, from any thread. Call heard("") when nothing was understood. The core reads one text per listening window: a second heard before the core takes the first replaces it.
commands() lists the game's commands of the moment (the active list: the race, qualifying or practice commands, as the game enables them) and then every addon command, one phrase each. A recogniser can bias its decoding toward them or snap a near miss to the nearest one; the core matches text by its words only (case, punctuation and runs of spaces do not count).
| Result | Means |
|---|---|
ok | done |
off | no voice module, the events feature is off, SWOFTY_VOICE=0 |
no_addon | the calling DLL is in no addon's manifest |
taken | another addon is the recogniser |
late | the game has set up speech recognition with its own recogniser already |
not_recogniser | heard from an addon that is not the recogniser |
bad_text | text that is not valid UTF-8, holds a control character, or is over its byte cap |
full | add only: the addons hold COMMANDS (64) commands |
too_small | the C table's commands only: the buffer is short; len is the size it needs, less the terminator |
What the core does with the text ​
- Text that matches a command of the game's active list runs the game's own command: the core reports it to the game as the game's recogniser would, the panel shows it, and the engineer answers. The game ignores a command while its own speech plays, as it does with its own recogniser.
- Other text fires
voice.command. When areplacehandler returns true, the panel shows it as recognised and the window closes. - When no handler takes it, or the text is empty, the game hears it as not recognised: the engineer asks to say it again.
Adding commands ​
struct Added { Result result; uint32_t id; bool ok() const; };
Added vc::add(std::string_view phrase); // 1 to 63 bytes of UTF-8 with at least one letter or digitadd returns an id with bit 31 set (ID_BIT). The same phrase from the same addon gets the same id. voice.command carries the id and the adding addon when its text matches the phrase, and id 0 for free text. An addon command never takes the place of a game command: text that matches both runs the game's.
The voice panel shows the phrase as given, after the game's commands, from the next time the game builds the panel (when its active list changes). The panel shows ten rows a page: the game's come first, then every addon command in the order added, which fill the game's last page and go on to new pages. loader.log counts the rows and pages. When the core hands a handled command to the game, its row is selected and the panel turns to its page, as with the game's own. The rows show only while an addon is the recogniser: with the game's own recogniser nothing would hear them.
Running a game command ​
Result vc::run(std::string_view command); // ok, not_active, bad_text or offrun hands the game one of its own commands as if the player had said it, with no speech and no listening window. command is the keyword's name as the game's speech_recognition.xml spells it, the same in every language ("Box", "Engineer Quiet", "Positive Strategy Change"), or its phrase in the speech language ("box this lap"), matched as heard text is. The core finds it in the game's active list and reports it to the game as its recogniser would, so the game takes it on its next speech update exactly as a spoken command: the panel shows it, the game acts, and the engineer answers.
ok: the active list holds the command and the game takes it. Whether the game then acts is its own rule, as for a spoken command: it ignores a command while its own speech plays, and the engineer's answer says what it did.not_active: the game has no speech recognition (no recogniser at all, orSpeechRecognition.enabledoff), or its active list has no such enabled command right now. Each session type has its own list, and while the engineer asks a question (the strategy change) the list is that question's answers.- It works whether an addon or the game's own recogniser recognises speech, and any addon may call it.
if (vc::run("Box") != vc::Result::ok)
swofty::log("[strategy] the game has no box command now");The Race list has Box and no command that cancels a stop.
Testing ​
swofty/fake_voice.hpp installs a fake core for host tests: a hooks table that runs your voice.listen and voice.command handlers and a voice table that keeps what your addon registers, adds, hears and runs.
#include <swofty/fake_voice.hpp>
swofty::fake::Voice fake;
fake.install();
fake.game_commands({{"Box", "box this lap"}, {"Engineer Quiet", "engineer quiet"}});
my_addon_on_load();
fake.listen(true); // your listen(user, 1), then voice.listen
bool handled = fake.command("is there space to pit"); // voice.command with your command's id
swofty::voice::run("Box"); // fake.ran() == {"Box"}The events ​
namespace swofty::voice::events {
struct Listen { // notify, the flow thread
static constexpr const char *point = "voice.listen";
uint32_t size, payload_version;
uint8_t on; // 1: the game opened its listening window, 0: closed it
uint8_t reserved[3];
};
struct Command { // cancel, the flow thread; replace returns true when it acted on the text
static constexpr const char *point = "voice.command";
uint32_t size, payload_version;
uint32_t id; // the addon command it matches, 0 for free text
float confidence; // as the recogniser gave it, 0 to 1
char addon[64]; // the addon whose command it matches, "" for free text
char text[256]; // the text heard, UTF-8
};
}
std::string_view vc::text(const events::Command &c);
std::string_view vc::addon(const events::Command &c);voice.listen follows the game's window one frame late; the recogniser has its listen callback for the moment itself.
The game's side ​
- The game's Talk binding is the only binding: there is none of the library's own. It is in the game's controls settings.
- The game creates a recogniser only when its
SpeechRecognition.enabledis on. - The voice panel lists the game's commands of the moment, then the addon commands, ten rows a page. A question the engineer asks (the strategy change) has its own rows and no addon rows.
- How the game reaches its recogniser, its keyword lists and its listening rule are in the reverse-engineering notes,
re/db/subsystems/voice.md.