JS Random soundfiles / TTS voices & playback speed

My pleasure!

1 Like

You missed a semicolon, at the end of the rand = randomInt(audios.length) line.

The original code worked for me without semicolon.
But you can see my version where I compacted the code a little.

Semicolons are supplementary in JS. Missing semicolons doesn’t disrupt code from running (executing).

@dae I still have some loading lag on my cards on Anki mobile (not on desktop) because of this function so I was wondering, if I may ask :

What is the order of evaluation of cards’ code in Anki Mobile? Is the styling page of a card type evaluated more than once or only at startup?

I’m wondering if the issue is declaring the same function (randomInt) over and over in the front or back of my card, while i would just need to declare it once and only evaluate it in the card.
Perhaps I could use the Styling page for declaration instead? would it still be in scope for use in front or back?

Thanks !

Oh and a second question for you Damien @dae, I think for some reason this solution for random audio messes up with how Anki mobile deals with other apps’ audio layer.

Since I added it, i have audio ducking for instance on spotify or safari (listening to music) when audio is triggered, which wasn’t the case without the random function. Now I wonder if you know of a way (or even third party app) to prevent this?
I found in Anki mobile’s preferences the always duck setting (a never duck setting would be great…!) which could work but volume is bit weak. If « never ducking » is impossible, I wish I could at least select a ducking volume to fake it somehow.

Styling is applied on every card transition. It’s unlikely to be the definition of the function that is slow. I’m surprised it even works as is, as the script looks like it’s run before the tags are inserted into the document. Do you have autoplay enabled for the deck? If it’s trying to play a sound after sound has automatically started playing, maybe iOS is slowing down handling the interruption.

iOS does not provide control over the ducking level. Manually triggered audio uses a different audio category so that it plays even if the mute switch is on.

I’m not sure how the scripts ends up working either :sweat_smile:
Perhaps @ODeer would have some explanation?

To answer your question, indeed autoplay has to be disabled for the script to work (as we discussed above in this thread), here’s a screenshot of my options for this deck

Actually after fiddling with Ankimobile’s settings yesterday on iPad, I really don’t know why but I ended up fixing the loading delay of cards !
I checked on iPhone (which still had the latency), copied those settings and now it also works fine ! The ones I suspect are involved i marked in the screenshot.

I don’t know why any of those would be connected, but I’m happy it did the trick :laughing:

1 Like

Props for figuring out a solution and sharing it here.

As for this:

As I said, I don’t use AnkiMobile or TTS - I simply changed what I use for my AnkiDroid cards to fit your application. AnkiDroid has none of these settings (that I’m aware of) and I’ve never had any problems with it either.

The earlier request has been solved by altering a little to “[…document.querySelectorAll(“#sentences .soundLink”)];”
But I found that there are a lot pinyin audio files in sentences_field, too. Fortunately, the sentences audio references share the same pattern starting with [sound:p0 . Is there a workaround to confine the code only effective to audio files only matching with the pattern?
─────────earlier request ──────────
:wave: Hello! Thanks for your code! Can you tell me how to confine the script only effective to certain fields or sections?

Let’s say I have a section {{pinyin}} which is linked to a field named pinyin_field.
And another section {{sentences}} which is linked to a field named sentences_field.

I have {{pinyin}} and {{sentences}} and your script on frontside. And I want the the only one mp3 file from pinyin_field and one random mp3 file out of many mp3 files from sentences_field both be played. But sometimes the script will let {{pinyin}} be played twice since it can also be recognized as one of the random audio files. The random one I want is one from {{sentences}}.

I would really appreciate it if you can help me and tell me the exact code since I know nothing about script codes.

@bkztei @BlackBeans @julienvincenot @ODeer
Would you please take a look at my request and kindly give me a hint?

@dxxxxp How do you trigger playing two audios sequentially one by one? Can you share your code, please?

Anki plays audio files sequentially by default. If it doesn’t do that, you may want to change your settings rather than revise code. Turn silent mode on your phone off or try to set study option as below.
Study Option
Audio
Don’t play audio automatically→uncheck

That’s not relevant for me. I’m using Anki desktop.

Can you share the script you use on AnkiDroid? Thank you!

I don’t think there’s a particular version of that script for AnkiDroid — I use Anki Mobile on iOS and it’s the same as Desktop. You need to integrate the script above yourself (it’s complete) in your own card type on Anki Desktop, then sync with your device.

@julienvincenot AnkiDroid uses a different selector than both mobile and desktop, so there is a slightly different version that works for AnkiDroid.

@tangda you need to replace .soundLink with .replaybutton in the script.

To make it easier, the following should work on all clients:

<script>
/*-----------------------------------------
 * Choose random audio from card
 * by J. Hirsch | @DonCiervo
-----------------------------------------*/
{
		function randomInt(max) {
			return Math.floor(Math.random() * max);
		}		
				
		let audios = [...document.querySelectorAll(".soundLink")]
		if (!audios.length) {
			audios = [...document.querySelectorAll(".replaybutton")];
		}	

		rand = randomInt(audios.length)
		audios[rand].click();
}
</script>

 

Cheerio!

2 Likes

thank you so much! It works like a charm on both desktop and ankidroid