# JS Random soundfiles / TTS voices & playback speed

**URL:** <https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387>\
**Category:** Help\
**Created:** [July 11, 2022, 6:24pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387 "2022-07-11T18:24:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 11, 2022, 6:24pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/1 "2022-07-11T18:24:39Z")

</div>

Hi

I have two questions related to playing soundfiles and TTS (in Ankimobile, so no- add-on)

I have a ton of pre-generated sounds via Chinese Support Redux, I’m satisfied with the sound for many of them (especially I can adjust things with specific sound files when a character has multiple pronounciations)

BUT

1. I’d like to be able to accelerate those pre-recorded soundfiles a little.

2. Also I’d like to be able to get random voices to not get too used to one version of playback.  
Today I tried different voices of TTS, results are unequal with different words but I think that would be really good if every time a card comes I hear a different version.

(Also, obviously, TTS already allow to change speed to some extent so that’s good)

Obviously when I got all these fields together in my card they all play one after the other which is not great…  
{{Audio}}  
{{tts zh\_CN voices=Apple\_Yu-shu:Hanzi}}  
{{tts zh\_CN voices=Apple\_Ting-Ting:Hanzi}}  
{{tts zh\_CN voices=Apple\_Li-mu:Hanzi}}

Any suggestions to do pick up randomly among them which one is played?

Thanks in advance for your help!

Julien

---

<div class="post-metadata">

**Author:** ![ODeer](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/odeer/32/10217_2.png) [@ODeer](https://forums.ankiweb.net/u/ODeer)\
**Post date:** [July 11, 2022, 9:53pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/2 "2022-07-11T21:53:24Z")

</div>

EDIT: I just noticed I completely ignored your first request. I vaguely remember Damien replying to a post somewhere ([found it](https://anki.tenderapp.com/discussions/ankimobile/11050-to-increase-audio-speed)) that that function isn’t supported. It’s possible though that was just a reply to a non-technical user, and could be done using JS. I’d be curious to see if someone can figure it out.

* * *

**But regarding the second point:**

Hi there,

this script works for me, with one caveat:

> **Neither do I use TTS, nor AnkiMobile, so you'll have to see if it works for your use case.**
>
> From experience, I think it’s only AnkiDroid that uses a different selector for audio (.replaybutton). And as long as TTS generates the same clickable object as normal audio (which I assume it does), this should be a simple solution for your problem.

```auto
<script>
/*-----------------------------------------
 * Choose random audio from card
 * by J. Hirsch | @DonCiervo
-----------------------------------------*/
{
		function randomInt(max) {
			return Math.floor(Math.random() * max);
		}		

		const audios = [...document.querySelectorAll(".soundLink")];
		rand = randomInt(audios.length)
		audios[rand].click();
}
</script>

```

Please let me know either way, I’m curious.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 12, 2022, 12:00am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/3 "2022-07-12T00:00:39Z")

</div>

Hi @ODeer  
Thank you so much, I does work in AnkiMobile !  
Ok the querySelector detects every player button on the page and clicks one, makes sense !  
In case the function is on the front card (ex: front listen → answer vocab) the sound is repeated on the answer side (as part of {{FrontSide}}) , but that’s not too much of an issue I guess.

Yes indeed I saw a few other posts mentioning this question of speed, but they were all quite old posts so I was hoping there would be some news.  
Hi Damien by any chance do you know if there’s any news on that front? @dae

Best,

Julien

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [July 12, 2022, 2:37am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/4 "2022-07-12T02:37:13Z")

</div>

AnkiMobile doesn’t provide a way to adjust the speed of audio files I’m afraid.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 12, 2022, 2:57am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/5 "2022-07-12T02:57:31Z")

</div>

Thanks Damien for your quick reply.  
I see, so it really does make sense to randomize different voices with different speeds then, to diversify testing a bit.

Now while I have you @dae, if you look at @ODeer’s code :  
I installed it in my decks and while it works seemlessly in Anki desktop, on Anki Mobile for some reason now there’s often a slight delay when open a page containing that random choice of audio to click. Do you see any reason why?  
(If you need I can see you a copy of my deck by email)

Best,

Julien

---

<div class="post-metadata">

**Author:** ![ODeer](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/odeer/32/10217_2.png) [@ODeer](https://forums.ankiweb.net/u/ODeer)\
**Post date:** [July 12, 2022, 8:51am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/6 "2022-07-12T08:51:18Z")

</div>

> [@julienvincenot](#):
>
> the sound is repeated on the answer side (as part of {{FrontSide}})

Unless there is something specific you need {{FrontSide}} for, you can just copy the HTML from the front to the back of the template (minus the JavaScript ofc) and it won’t repeat when flipping the card.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 12, 2022, 12:49pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/7 "2022-07-12T12:49:25Z")

</div>

Oh sure, I tried that actually, but in that case without the JS the audio will all play in turn.

And if I remove the audio… well the audio buttons disappear, so it’s impossible de replay if needed ☹

That’s why I’d rather have repetition on card flip. It’s actually not that bad, and in my case only happens for card 2 with pinyin-audio → character-french. It’s not that bad because the sound may reinforce memorization when discovering the character.

---

<div class="post-metadata">

**Author:** ![ODeer](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/odeer/32/10217_2.png) [@ODeer](https://forums.ankiweb.net/u/ODeer)\
**Post date:** [July 12, 2022, 1:03pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/8 "2022-07-12T13:03:27Z")

</div>

> [@julienvincenot](#):
>
> And if I remove the audio… well the audio buttons disappear, so it’s impossible de replay if needed

I think you’re making this more complicated than it has to be.

In your deck options, you can find Audio settings. Make sure these are toggled (well, really, the first one is important, but doesn’t matter in your case).

 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/2/22f6ce189adced65b6ecb9d3a0dc2dcec83b0cd1.png)

This means you can include Audio fields wherever - and only add the JavaScript whenever you want a random audio to be played.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 12, 2022, 2:27pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/9 "2022-07-12T14:27:06Z")

</div>

> [@ODeer](#):
>
> This means you can include Audio fields wherever - and only add the JavaScript whenever you want a random audio to be played.

Oh indeed I completely missed that second option yesterday when I needed to stop autoplay.  
Now it works if instead of {{FrontSide}} I just copy the content of the front minus the function, like you suggested.  
Thank you so much !

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 16, 2022, 5:32pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/10 "2022-07-16T17:32:45Z")

</div>

Hi Damien @dae

Sorry to bother you again but I think you missed my second question (related to Anki Mobile) hidden inside the rest of the discussion with ODeer.

I installed his function in my decks to randomize between different voices, and while it works seemlessly in Anki desktop, on Anki Mobile for some reason now there’s almost often a slight delay when open a card containing that random choice of audio to click.

Do you see any reason why?  
I tested this on both iPad and iPhone, and result is the same. It seems the function takes a long time to evaluate, or maybe it’s the time to instantiate all the sound objects before clicking one of them randomly?  
(If you need I can send you a copy of my deck by email, I just forgot how to create a new support ticket for Anki Mobile…)

Many thanks in advance !  
Best,

Julien

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [July 17, 2022, 2:06am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/11 "2022-07-17T02:06:46Z")

</div>

Please bear in mind that I can’t provide support for JavaScript: [Styling & HTML - Anki Manual](https://docs.ankiweb.net/templates/styling.html#javascript)

I’m not sure exactly what the problem is, but perhaps you could work around it by deferring the script or performing the click after a delay.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 17, 2022, 4:39am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/12 "2022-07-17T04:39:47Z")

</div>

Thanks for the tip Damien @dae ! Yes I understand perfectly you can’t handle how everyone use Javascript, especially noobs like me 😅

Your answer was a great clue I think :  
I put that line inside a 10 milliseconds delay, and it seems it did the trick and the app is much more reactive on iPad. A few cases still slow down, I need to figure out why. I’ll keep experimenting and hopefully I can find the right delayvalue.

```auto
setTimeout(function() {
  audios[randomInt(audios.length)].click();
}, 10);

```

Thanks again !

---

<div class="post-metadata">

**Author:** ![bkztei](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bkztei](https://forums.ankiweb.net/u/bkztei)\
**Post date:** [July 17, 2022, 11:35am UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/13 "2022-07-17T11:35:01Z")

</div>

Sorry to bother you. I can’t make your code run on the Anki desktop app.  
Am I doing something wrong?

```auto
{{Front}}

<br>

{{audio1}}
{{audio2}}
{{audio3}}

<script>

{
		function randomInt(max) {
			return Math.floor(Math.random() * max);
		}		

		const audios = [...document.querySelectorAll(".soundLink")];
		rand = randomInt(audios.length);
		audios[rand].click();
}

</script>

```

Both tumblers to disable autoplaying audios are enabled. Adding a `setTimeout` function to add a delay like [julienvincenot](https://forums.ankiweb.net/u/julienvincenot) did doesn’t help. Can it be run on desktop Anki at all?

---

<div class="post-metadata">

**Author:** ![ODeer](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/odeer/32/10217_2.png) [@ODeer](https://forums.ankiweb.net/u/ODeer)\
**Post date:** [July 17, 2022, 12:43pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/14 "2022-07-17T12:43:35Z")

</div>

> [@bkztei](#):
>
> I can’t make your code run on the Anki desktop app.

Does this mean it works on AnkiMobile for you or are you trying to run it on desktop only?

> [@bkztei](#):
>
> Am I doing something wrong?

Not as far as I can tell. It works on desktop for me.

What version of Anki are you running? And does the replay button appear on your cards in the first place?

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 17, 2022, 4:13pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/15 "2022-07-17T16:13:29Z")

</div>

Here’s my code if you want to compare.  
Like @ODeer it works on Anki Desktop (and Mobile for me).  
I put the function before but I think it works even if I put after the sounds.

```auto
<script>
{
		function randomInt(max) {
			return Math.floor(Math.random() * max);
		}	
		const audios = [...document.querySelectorAll(".soundLink")];
setTimeout(function() {
  audios[randomInt(audios.length)].click();
}, 10);
		
}
</script>

<div style='font-family: Hoefler Text; font-size: 70px;'>{{Pinyin}}</div>

  {{Audio}}
  {{tts zh_CN voices=Apple_Ting-Ting:Hanzi}}
  {{tts zh_CN voices=Apple_Li-mu:Hanzi}}
  {{tts zh_CN voices=Apple_Yu-shu:Hanzi}}
  {{tts zh_TW voices=Apple_Mei-Jia:Hanzi}}

```

I also attach a screenshot of what it’s supposed to look like on the front of a “card 2” on my setup (pinyin + audio)

 ![Capture d’écran 2022-07-17 à 6.12.04 PM](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/6/60afb7866b439f7e390172d1d6d348ae2bbd9219.png)

---

<div class="post-metadata">

**Author:** ![bkztei](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bkztei](https://forums.ankiweb.net/u/bkztei)\
**Post date:** [July 18, 2022, 4:04pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/16 "2022-07-18T16:04:15Z")

</div>

I am only using the desktop version. I don’t have a mobile one. The version of my Anki app is 2.1.54 (b6a7760c)⁩ for macOS.

> And does the replay button appear on your cards in the first place?

No. I don’t have this one. What is the “replay button”? Do you mean some media play button – ▶ ?

---

<div class="post-metadata">

**Author:** ![bkztei](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bkztei](https://forums.ankiweb.net/u/bkztei)\
**Post date:** [July 18, 2022, 4:07pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/17 "2022-07-18T16:07:30Z")

</div>

Thanks for the screenshots and code. But I still can’t make the code work for me. Here are mine:

 ![Screen Shot 2022-07-18 at 18.52.02](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/d/d0093d5e01c334def1f7ec5d708a36e8e23a4f58.jpeg)  
 ![Screen Shot 2022-07-18 at 18.52.23](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/8/8c4d9d811e4fec58ef13e17e03cbf2e0bab8ea20.jpeg)  
 ![Screen Shot 2022-07-18 at 18.53.06](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/4/46ce65c5bfb593f06cc0d5fd28a55e44f61a1649.jpeg)

How did you make the circled play buttons ▶ appear on your cards? I don’t have these ones.

---

<div class="post-metadata">

**Author:** ![julienvincenot](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/julienvincenot/32/4090_2.png) [@julienvincenot](https://forums.ankiweb.net/u/julienvincenot)\
**Post date:** [July 18, 2022, 4:28pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/18 "2022-07-18T16:28:05Z")

</div>

> [@bkztei](#):
>
> How did you make the circled play buttons ▶ appear on your cards? I don’t have these ones.

Yeah that’s what I was going to ask you : how come you don’t have any of those buttons? 😅

Did you ever get some sound running in Anki cards? Any sound object (fields represented by {{ … }} with Audio in it) should show one such icon. So in my case 5 sounds → 5 icons.

What happens if you remove the `<script> ... </script>` part?

Also, I wonder, did you try renaming your soundfiles? Mp3 format is fine, but I’ve had some cases where too long phrases in audio filenames could not be read by Anki. Maybe make them shorter and more descriptive…?

---

<div class="post-metadata">

**Author:** ![ODeer](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/odeer/32/10217_2.png) [@ODeer](https://forums.ankiweb.net/u/ODeer)\
**Post date:** [July 18, 2022, 5:18pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/19 "2022-07-18T17:18:27Z")

</div>

> [@bkztei](#):
>
> How did you make the circled play buttons ▶ appear on your cards? I don’t have these ones.

If you go to `Tools >> Preferences` what do you see? There is an option to show play buttons on cards, which should be toggled by default.

 ![grafik](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/d/dfa8d800757033a2203e20f5e81ddaa56d058f9e.png)

&nbsp;

Try this before you fiddle around with the file names!

---

<div class="post-metadata">

**Author:** ![bkztei](https://avatars.discourse-cdn.com/v4/letter/b/3be4f8/32.png) [@bkztei](https://forums.ankiweb.net/u/bkztei)\
**Post date:** [July 18, 2022, 5:32pm UTC](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387/20 "2022-07-18T17:32:19Z")

</div>

Thank you so much! You’ve made my day. Everything works great now.

[Next page](https://forums.ankiweb.net/t/js-random-soundfiles-tts-voices-playback-speed/21387.md?page=2)
