XChat plugin for synthesizing speech of chats
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123 lines
5.2 KiB

13 years ago
#!/usr/bin/env python2
13 years ago
"""
Copyright (C) 2011
13 years ago
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
try:
import simplejson as json
except ImportError as exception:
print exception
import json
13 years ago
import xchat
import argparse
import os
13 years ago
from espeak import espeak
options_path = os.path.expanduser("~/.xchat2/options.json")
13 years ago
__module_name__ = "IRC Speak"
13 years ago
__module_version__ = "1.1"
13 years ago
__module_description__ = "Allows you to make certain users' text run through a voice synthesizer"
editor = argparse.ArgumentParser(description="""
Specify a user. If that user already exists their options will be updated. If they do not exist they will be added.
If you do not specify arg(s) then the default values will be used for each of them.
""")
editor.add_argument("--name", nargs=1, type=str, required=True, help="e.g. /ircspeak set -volume=<int> -pitch=<int> --name=foo")
editor.add_argument("-volume", nargs=1, type=int, default=[70], help="default=70")
editor.add_argument("-pitch", nargs=1, type=int, default=[50], help="default=50")
editor.add_argument("-rate", nargs=1, type=int, default=[175], help="default=175")
editor.add_argument("-vrange", nargs=1, type=int, default=[50], help="default=50")
editor.add_argument("-language", nargs=1, type=str, help="type \"/ircspeak langlist\" to get a list of languages", default=["english"])
options = json.load(open(options_path, "rw"))
13 years ago
aliases = {"rate" : 1,
"volume" : 2,
"pitch" : 3,
"vrange" : 4}
13 years ago
13 years ago
def edit_users(**new_options):
13 years ago
"""Edits the options dictionary to add/update a user's settings"""
nick = new_options['name'][0]
13 years ago
if not nick in options:
options[nick] = {"args": {}}
13 years ago
options[nick]["args"] = {key : value[0] for key,value in new_options.iteritems() if (key and (key!="name") and (key!="language"))}
# don't want to use name or language, because language needs to get set separately
# and name is only used to create the main dictionary for their options
if new_options["language"]:
options[nick]["language"] = new_options["language"][0]
13 years ago
return options[nick]
13 years ago
def irc_speak(word, word_eol, users):
13 years ago
"""Checks to see if a user is set to be voiced, and if so synthesizes their text"""
13 years ago
if word[0] in options: # check to see if the user is in the options dictionary (word[0] == their nick)
[espeak.set_parameter(aliases[arg], options[word[0]]["args"][arg]) for arg in options[word[0]]["args"]]
# options[word[0]]["args"][arg] is the same as options[nickname]["args"][arg] (where arg is some one of the names in aliases
# which corresponds to some integer value in options[nick][args])
espeak.set_voice(name=options[word[0]]["language"])
espeak.synth(word[1])
13 years ago
xchat.emit_print("Channel", word[0], word[1])
return xchat.EAT_NONE
return xchat.EAT_NONE # return nothing because they weren't in the options dictionary
13 years ago
def set_user(arguments):
13 years ago
"""Parses arguments and calls the edit_users function"""
try:
arguments = editor.parse_args(arguments)
return edit_users(**vars(arguments))
# argparse raises a SystemExit error if you use --help or -h, or have the wrong arguments
except SystemExit as exception:
return "Exited with status %s" % (exception)
def save():
13 years ago
"""Saves a user in the json file"""
with open(options_path, mode='w') as jsonfile:
json.dump(options, jsonfile)
return "Saved!"
def deluser(user):
13 years ago
"""Deletes a user from the global dictionary"""
13 years ago
if user[0] in options:
del options[user[0]]
return "Deleted!"
return "No such user"
13 years ago
def commands(word, word_eol, users):
13 years ago
"""Function for doing different commands with XChat"""
13 years ago
commands = {"set" : set_user,
"rm" : deluser,
13 years ago
"langlist" : (lambda x: ", ".join([item["name"] for item in espeak.list_voices()])),
"save" : (lambda x: save()),
"list" : (lambda x: "\n".join(["%s : %s" % (key, value["args"]) for key, value in options.iteritems()]))}
13 years ago
arguments = word[1:]
try:
13 years ago
print commands[arguments[0]](arguments[1:])
13 years ago
except KeyError:
return xchat.EAT_XCHAT
13 years ago
13 years ago
xchat.hook_command("ircspeak", commands, help="/ircspeak set --help")
13 years ago
xchat.hook_print("Channel Message", irc_speak)