Skip to content
Docs

Voice commands

import { createWakeListener, Spot } from 'alkove'
const commands = await Spot.commands(
{
commands: [
{ id: 'timer', phrases: ['mets un minuteur de {number} minutes', 'minuteur {number} minutes'] },
{ id: 'light.on', phrases: ['allume la lumière du {room}', 'allume le {room}'] },
],
lists: { room: ['salon', 'bureau', 'cuisine'] },
},
{ langs: ['fr'] },
)
const input = await createWakeListener({ words: [await Spot.compile('Camille', { lang: 'fr' })] })
input.setCommands(commands)
input.on('command', ({ id, slots, phrase }) => console.log(id, slots, phrase))

No model is trained for your commands. Lex turns each phrase into the sounds people say, and Spot’s top for that language decodes each utterance against all of them once it ends, 0.8 s after the last speech.

A command is an id and the phrases that say it. List the wordings your users will say: a phrase that is not in the list is refused, or taken for the closest command.

  • {number} stands for any number from 1 to 99, said in words; the event gives it as a number.
  • {name} stands for one of the values of lists.name; the event gives the value said.
  • A phrase may hold several slots. {number} alone stands for 99 phrases: keep slots to the commands that need them.

With several languages, give the phrases (and the lists) per language:

const commands = await Spot.commands(
{
commands: [{ id: 'timer', phrases: { fr: ['mets un minuteur de {number} minutes'], en: ['set a timer for {number} minutes'] } }],
},
{ langs: ['fr', 'en'] },
)

Spot.commands returns one plain JSON value per language. Store it and pass it back to setCommands later; compile again after an SDK update.

In French, the phrases are also read with the liaisons and the numbers’ final sounds that the next word asks for (“six heures”, “dix minutes”).

Each utterance that says a command gives a command event (CommandEvent):

Field Meaning
id the command’s id
phrase the phrase recognised, slots filled in
slots { number: 6 }, { room: 'salon' }
score how well the phrase fits the sounds, per 30 ms frame (0 is perfect)
gap how far it leads the best phrase of another command, per frame
lang the language it was decoded in
start, end the utterance, in seconds on the input’s clock

A command is given when gap is 0.25 or more; nothing is given otherwise. For a stricter app, ignore the events under a higher gap.

Load two languages or more and each utterance also gives a language event, { lang, p, start, end }, among the languages loaded. The commands of the language heard are decoded; when that language has none, the first language’s are. See languages for the event on its own and how well it tells them apart.

import alkove
models = alkove.Models.load(["spot", "lex"], langs=["fr", "en"])
commands = alkove.commands(spec, ["fr", "en"], lex=alkove.Lex(models))
listener = alkove.Listener(models, commands=commands)
listener.push(pcm)
for e in listener.events():
print(e["type"], e.get("id"), e.get("lang"))
  • Android: lex.commands(spec, langs), input.setCommands(commands), Event.Command and Event.Language; Listener(models, words, commands = ...) and listener.events().
  • iOS: lex.commands(spec, langs:) with a Value spec, input.setCommands(_:), .command(CommandEvent) and .language(LanguageEvent); Listener(models:words:commands:) and listener.events().
  • React Native: Spot.commands(spec, { langs }), input.setCommands(commands), on.command and on.language.
  • Flutter: Spot.commands(spec, langs: [...]), input.setCommands(commands), the Command and Language events.
  • Only the phrases listed are understood: it is not speech recognition. A wording outside the list is usually refused, sometimes taken for the closest command.
  • Numbers go from 1 to 99.
  • A microphone across the room gives fewer commands than one near the mouth: more utterances are refused.
  • A pause longer than 0.8 s ends the utterance: the rest of the command comes as another one.