Skip to content

Instantly share code, notes, and snippets.

@6r1d
Last active June 20, 2021 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6r1d/d21a9348d485f4e7f733a457c8873e32 to your computer and use it in GitHub Desktop.
Save 6r1d/d21a9348d485f4e7f733a457c8873e32 to your computer and use it in GitHub Desktop.
A tiny TTS interface
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
"title": 'Read "%s"',
"contexts": ["selection"],
"id": "ReadTTS"
});
})
let readText = function(info) {
console.log({
'text': info.selectionText
})
fetch('http://localhost:4277/speak', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'text': info.selectionText
})
})
}
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == 'ReadTTS') {
readText(info)
}
})
chrome.commands.onCommand.addListener(function(command) {
if (command == 'tts-read') {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
method: "getSelection"
}, function(response) {
readText(response)
});
});
}
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.method == "getSelection") {
var selection = window.getSelection().toString()
sendResponse({
selectionText: selection
})
} else {
sendResponse({})
}
}
)
{
"name": "Something TTS",
"action": {},
"manifest_version": 3,
"version": "0.5",
"description": "Reads text",
"icons": {
"128": "icon.png"
},
"host_permissions": ["http://localhost/*"],
"permissions": [
"activeTab",
"contextMenus",
"identity",
"storage",
"tabs"
],
"optional_permissions": [
"webRequest",
"webNavigation"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],
"commands": {
"tts-read": {
"description": "Read a selected text aloud",
"suggested_key": {
"windows": "Alt+R",
"mac": "Alt+R",
"chromeos": "Alt+R",
"linux": "Alt+R"
}
}
}
}
#!/usr/bin/env python3
"""
A simple script to run RHVoice through REST using Bottle.py and speechd
Got tired to read today and wrote it to make my life easier.
You need to install RHVoice and python3-speechd in Debian / Ubuntu,
then Bottle in Python through PIP for it to work.
HINT: if you're listening to a long text and it suddenly interrupts,
go to /etc/speech-dispatcher/speechd.conf
and set Timeout as 300, you can try more and you can try less.
TODO: play with punctuation modes later.
TODO: control selected areas with keybindings. https://stackoverflow.com/questions/6190143/
Example:
c.set_punctuation(PunctuationMode.ALL)
"""
from speechd.client import PunctuationMode, CallbackType, SSIPClient, Scope, Speaker
from bottle import route, request, run
PORT = 4277
HOST = 'localhost'
# How fast the text is being read
RATE = 35
@route('/speak', method='POST')
def speak():
c = SSIPClient('rh_client')
c.set_language('en')
c.set_voice('female1')
for module in c.list_output_modules():
if (module == 'rhvoice'):
c.set_output_module(module)
c.set_rate(35)
c.speak(request.json['text'])
c.close()
return 'Done'
def main():
"""
Runs the server on a given host and port
"""
run(host=HOST, port=PORT)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment