Dynamic Native TTS


I’m trying to do something relatively simple: adding a (native) tts component to my card.

const toTts = someDynamicValue;
const language = isFromWordQuestion ? 'ja_JP' : 'en_US';
// where it goes wrong
const ttsStr =  `[anki:tts lang=${language}]${toTts}[/anki:tts]`;

The issue is that anki immediately evaluates the string litteral, resulting in the following error:


By using other concatenation methods I did manage to get a string with an anki:tts tag that did contain all my dynamic values, but it would never get evaluated.


Unfortunately, the documentation did not help, and any google search for anki + tts leads to addon results. As for LLMs, they’re hallucinating hard.

Also, before anyone asks: it’s totally impossible for me to handle this statically. The front is an app I embeded within anki, and the data comes dynamically from an API I also coded (including the data used by the TTS). The point of my deck is to have to manually enter as little information as possible and create my cards quickly on the fly.

Any help would be appreciated. Thanks in advance.

OS: Win11
Version ⁨24.04.1 (ccd9ca1a)⁩
Python 3.9.18 Qt 6.6.2 PyQt 6.6.1


I came up with the following solution:

  function triggerTTS(text, lang, rate) {
    if (window.speechSynthesis) {
      const voices = window.speechSynthesis.getVoices();
      const utterThis = new SpeechSynthesisUtterance();

      utterThis.text = text;
      utterThis.lang = lang;
      utterThis.rate = rate;

      const voice = voices.find(({ lang: vLang }) => vLang === lang);

      if (!voice) {
        console.warn(`No TTS voice for ${lang}`);

      utterThis.voice = voice;

    } else {
      console.warn(`speechSynthesis is unavailable`)

From what I could remember speechSynthesis was not available within anki card templates, but it would seem that it was either fixed, or that it was a false memory - although I think I did make a post on this very forum about it-.

Regardless it’s nothing but a workaround that may not work in other people’s environment, and while I understand that the need for dynamic tts may be marginal, it still would be nice to be able to use it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.