Addon Help Needed (or Interesting in Taking Over an Addon?)

@mmdj2 or @gullempalausalva
I have just had a chance to test the addon. Thanks so much!

I uploaded it here (https://ankiweb.net/shared/info/1649407720) so that you can see it. However, I don’t plan on keeping it posted, as I agree some type of config file would need to exist to make it more user friendly, and that is beyond my ability. ANH25’s solution might work for that, but I haven’t been able to test it yet.

Anyhow, is there any way for multiple add-on sounds to play consecutively. For example, if a card has both a “marked” and a “leech” tag, could it play two different sounds back to back? In the current config (as best as I can test it), it seems that it will only play a single sound, which ever tag happens to come first in the “if” order.

1 Like

My solution should handle playing multiple custom sounds consecutively without problems.

Try something like:

{
  "marked": "sound1.ogg",
  "leech": "sound2.ogg"
}

with a note tagged with both marked and leech.

Here is what i have asked 10 days ago:

Please take a look at it; I will be happy to provide more details.
Thanks

@abdo: I hate to ask such a dumb question that so clearly shows my lack of coding skills, but I assume that the text you wrote should go in a config file (based on your earlier comments). I’ve never created a config file before. I assume it isn’t so simple as pasting the text that you wrote into a .py file and call in “config.py”? Is there any place you can direct me that I can read about how to set up the config file? Thanks!

1 Like

It’s not a dumb question :slight_smile:

The text should go in a file called config.json.

Put the python code (the first one in this reply) in a file called __init__.py.

Put these two files in some folder, say soundOnTag, and copy the folder to the add-ons folder.

Feel free to ask if there is something unclear.

2 Likes

@abdo: Thanks again! Super helpful. I’d like to say this is my last question, but that will only be if everything works. :slight_smile:

Anyhow, do the audio files just go in my regular media folder? I assume I should append them with an underscore, so they don’t get discovered as unused media during a Check Media, correct?

Yes, you should put them in the media folder and prepend them with an underscore (e.g. _filename).

@abdo: It looks like it is working. I have posted it here (https://ankiweb.net/shared/info/400375023). I am going to keep testing it tomorrow, but have to get to bed now. Thanks for all the help. After more testing, if all looks good, you are welcome to take it and post it as one of your add-ons.

this won’t let any other add-on use _showQuestion function tho.

Monkey patching is useful in the testing stage, and while waiting for new hooks to be integrated into Anki. But please don’t rely on it long term, as monkey patching is very fragile, and will tend to break as Anki is updated in the future.

https://addon-docs.ankiweb.net/#/monkey-patching

(i don’t really have that much of coding experience and have started coding just to add some functionalities/change some parts that i needed in anki. when i started, i used this method, while it was easier to work with and i could modify them better, it would break other add-ons frequently)

3 Likes

Maybe someone could submit a new hook, something like reviewer_will_play_question_tags:

Hook(name="reviewer_will_play_question_tags", args=["tags: List[anki.sound.AVTag]"])

And we can add the add-on sounds to the list?

I don’t know much about writing hooks (I’m a beginner myself). But looking at gui_hooks.py and genhooks_gui.py, it does not seem to be hard. So maybe I’ll have a try at it :slight_smile:

2 Likes

@abdo: Thanks for giving this a try. Please keep us updated.

@mmdj2: You mentioned you don’t have much coding experience as did @abdo, but you are a million times further along than am, and I’m deeply grateful that you both are helping.

@mmdj2 I assume that the solution you posted doesn’t use monkey patching, correct? Also, with the code you posted, is there a way to get get multiple add-on sounds to play back-to-back (when a card has more than one tag that triggers a sound)?

hmm, it can play multiple audio files when i use Reviewer.nextCard = wrap(Reviewer.nextCard, play_sound, 'after') but not when i use Reviewer._showQuestion= wrap(Reviewer._showQuestion, play_sound, 'after')
i honestly don’t understand why :frowning: i’ll try to find a way to play multiple audios with _showQuestion too. don’t know if i can tho. meanwhile you can use the code that @abdo posted if it works for you.

1 Like

@mmdj2 @abdo @guillempalausalva Thanks for the help with this. I have sounds functioning during reviews again. :partying_face:

I do think it could be a helpful add-on for the community, and would like to eventually share it (or have one of you share it), but I’m going to pull both of them down, at least, temporarily.

@abdo’s approach can play multiple sounds consecutively, but uses monkey patching. But ANH25 is looking into submitting a new hook.

@mmdj2’s approach doesn’t use monkey patching, but isn’t playing consecutive sounds for some unknown reason. mmdj is trying to find a way around this.

For now, I have something that works :tada: , and if one of these other developments happen, we can post a full version of it then.

Thanks again.

:clap:

1 Like

I’ve submitted a pull request to add two new hooks:

The add-on would look something like this with these hooks:

from aqt import gui_hooks
from anki.sound import SoundOrVideoTag
from aqt import mw


def get_sounds(config, card):
    sounds = []
    tags = [tag.lower() for tag in card.note().tags]
    for tag in config.keys():
        if tag in tags:
            sounds.append(SoundOrVideoTag(filename=config[tag]))
    return sounds

def myhook(card, sounds):    
    my_sounds = get_sounds(mw.addonManager.getConfig(__name__), card)
    for s in my_sounds:
        sounds.insert(0, s)

gui_hooks.reviewer_will_play_question_sounds.append(myhook)

2 Likes

@abdo: Are you still active on Anki? You helped me earlier with an addon. It worked up until 2.1.44, but now needs a bit of help to work with 2.1.45+. Seems like there are some easy porting directions to get addons to work with 2.1.45+, but nevertheless it is over my head.

Any chance you might have a moment free to help?

Here is the addon: Sound On Tag (Beta) - AnkiWeb

@abdo Lol. I just edited this post with this comment because I saw you had “devotee” status on here. Way to go! Stupid question for me to ask if you were still active. :man_facepalming:

Did that pull request ever get accepted?

Yes, it got accepted.

I’ll test the code with the new hook that I posted earlier on Anki 2.1.45+ and return to you.

The new code works fine on 2.1.46
Just replace the old code with it and it should work.

1 Like

@abdo Thanks for your help with this. I have edited this post, because I seem to have it working now. Thanks again so much for your help.

Because you have done so much work on this addon, I’m glad to pass along ownership of the addon to you if you prefer. But either way, I’m super grateful for the help.

1 Like