Built-in TTS html problems

Hello,
I currently have a separate deck for different languages they are all the same except the text to speech language is different in every notetype. I use the internal anki text to speech that looks like this: {{tts ru-RU voices=Apple_Milena_(Enhanced):Front}} It works perfectly. I wanted to merge all of those notetypes into one. So to make it dynamic and depending on the subdeck the card is in to give me different tts with the variable {{Subdeck}}. So basically a different tts should be triggered for each language.

I tried a lot but nothing seems to work. I read most forums and even html articles/guides even though im not a programmer. i even tried to hide the tts that should not be activated with „display:none;“ but nothing seems to work. Anki seems to ignore the html formatting completely and it always triggeres the audio of all tts thats included on the frontside without checking any formatting.

I am mainly using anki on my iphone. Unfortunately it misses some features so i have to switch to laptop for certain stuff.

Also because of this i dont want to use any addons but instead the integrated tts which works perfectly so far.

Does anyone know of a way to change the tts thats being triggered depending on the subdeck? Is it even possible? I would be incredibly greatful for any help. If this works i have a single ultimate notetype for everything.

Things i tried (Im not a programmer):

{{#Subdecks}}
{{#is test1}}
{{tts en_US:Front}}
{{else}}
{{#is test2}}
{{tts es_ES:Front}}
{{/is}}
{{/is}}
{{/Subdecks}}

(With this i get error messages its possible my syntax is wrong)

(Then i tried a script but anki always triggers both tts)

Looking forward for your reply :blush:

1 Like

It looks like you’re trying to use Conditional Replacement – but it’s not a flexible if-then-pattern-matching syntax. The only thing you can use with {{# or {{^ is the name of a field. It is only looking for “is there data in that field?” And the only answer are “yes” and “no.”

If you want to see a practical use of it, you can also take a look at the templates for the “Basic (optional reversed card)” notetype.

That’s not necessary though, right? Your multiple notetypes are working just fine?

I admit, there are derfinite advantages to being able to make an improvement in just one place, rather than on multiple notetypes. But I don’t think there’s a simple way to do this. And if you implement a complicated way that someone else suggests, you’re not going to be able to maintain it yourself. It seems like you’re just injecting instability into your collection unnecessarily.

If you’re dead-set on creating the one-notetype-to-rule-them-all, why not implement a simple solution, using bedrock Anki functionality? To do anything in this direction, you’ll need to have something that tells the tool/script/code which language to apply to this card, right?

Following the model of basic-optional-reversed – you could add a field to indicate a Note belongs to a language. One field, where you name the language? Nope. A field for each language you are studying – isRussian, isSpanish, isTurkish, isZulu, etc… Put something (anything) in the appropriate field for each note (see how fast sticky fields becomes your new best friend!). Then you can make your ultimate audio card template like this:

{{#isRussian}}
    {{tts ru-RU voices=Apple_Milena_(Enhanced):Front}}
{{/isRussian}}

{{#isSpanish}}
    {{tts es-ES:Front}}
{{/isSpanish}}

{{#isTurkish}}
    {{tts tr-TR:Front}}
{{/isTurkish}}

{{#isZulu}}
    {{tts zu-ZA:Front}}
{{/isZulu}}

– as many languages as you want. Readable, easily maintainable, and all contained on one template.

3 Likes

Hello Danika. Thanks a lot for taking your time and for your very in depth answer!

I read this Blog where the Author explains how to add Contextual images based on tags which led me to the idea to add Contextual TTS based on the Subdeck the card is in. Do you think there is a way to do it using a similar method like in the blog above?

[quote=“Danika_Dakika, post:2, topic:42303”]
A field for each language you are studying – isRussian, isSpanish, isTurkish,
[/quote] I got almost 100.000 language sentence cards so the option to add a field for each language (for example: isTurkish) is not really an option.

So its also not possible to make a single field and based of the flag inserted (:north_korea::micronesia::oman::lithuania::macedonia:) give me different tts?

[quote=“Danika_Dakika, post:2, topic:42303”]
I admit, there are derfinite advantages to being able to make an improvement in just one place, rather than on multiple notetypes.
[/quote] Yes exactly. Also i would never need to create nor edit my notetypes ever again and could use it for everything.

[quote=“Danika_Dakika, post:2, topic:42303”]
But I don’t think there’s a simple way to do this. And if you implement a complicated way that someone else suggests, you’re not going to be able to maintain it yourself.
[/quote] Ok sad i really thought that there was an easy solution i just didnt find yet. I checked whole anki manual, reddit and forum for someone that tried to do something similar but didnt find anything.

Thanks again. I really appreciate it. You seem like a true expert. :slightly_smiling_face:

Btw i also tried using a script to select the right tts dynamically but appearantly thats not supported by anki.

:person_shrugging:t4:
Have you tried it? Sync and/or backup your collection first. There’s zero risk to trying something.

Don’t get me wrong – it might be possible to do what you’re asking – but not with my javascript knowledge, or with yours. Putting something on your cards that you don’t understand, you can’t modify, and you can’t fix doesn’t seem prudent. I’ve seen you post this in multiple places, and the internet-people aren’t rushing in with an easy solution. That suggests to me it’s non-obvious, at least.

Not with Anki field syntax or Conditional Replacement.

You flatter me. But let’s roll that back to savvy-novice-who-knows-her-limits. :sweat_smile:

1 Like

Ok have a nice day :slight_smile:

1 Like

Hi i found the solution/problem. (For anyone reading this post in the future who wants to know)

I found a way to make dynamic conditional anki cards using javascript, the code below works perfectly it will display a different Field either {{Front}} or {{Back}} depending on what Subdeck the card is in. This works for Tags, Notetype etc as well! Here it is:

<script>
var x = "{{Subdeck}}";

if (x === "Deck1") {
    document.getElementById("front-content").innerHTML = "{{Front}}";
} else {
    document.getElementById("front-content").innerHTML = "{{Back}}";
}
</script>

Unfortunately here is the problem. If you switch the Fields with the actual TTS Tags like this:

<script>
var x = "{{Subdeck}}";

if (x === "Deck1") {
    document.getElementById("front-content").innerHTML = "{{tts es-ES voices=Apple_Marisol_(Enhanced):Front}}";
} else {
    document.getElementById("front-content").innerHTML = "
{{tts en-GB voices=Apple_Serena_(Premium):Front}}";
}
</script>

Anki will still trigger both tts tags! The tag is even triggered when putting the tag into html comments like this (which should make the content invisible and not be rendered within anki, works with all other content within anki)

<!-- {{tts en-GB voices=Apple_Serena_(Premium):Front}}"}} -->

That means that Anki appearantly completely ignores all styling of TTS tags and just checks if the tags exists and triggers them before executing the actual template code, which is really sad because that means it is impossible to reach my goal of making TTS dynamic within the anki templates. :frowning:

I hope there will be a feature to modify this to ones liking one day. Should i make a separate post into the „Suggestions“ Category? Or will this be sufficient?

1 Like

Somehow my script code is not visible that i included. How can make it visible it?

Use pre-formatted text. This is the </> option at the top of the input box.

Alternatively, you can do it manually like this:

```
type or paste code here
```

1 Like

Ty found it!

The ``` needs to be on a line by itself, with nothing else on the same line.

If you just have one line (or a few words) where you want the pre-formatted text, you can use a single backquote instead of three:

`some text` gives this: some text

1 Like

Perfect :grin:

1 Like

No need to make another post.

2 Likes

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