Skip to content

Instantly share code, notes, and snippets.

@edo9k
Last active May 30, 2019 21:08
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 edo9k/92dcb66d29624539dec33535206eaa12 to your computer and use it in GitHub Desktop.
Save edo9k/92dcb66d29624539dec33535206eaa12 to your computer and use it in GitHub Desktop.
Gets text from clipboard and reads in the detected language (using the espeak tts system).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# speak.py
#
# Copyleft 2016 Eduardo França <@edo9k>
#
# to install
# $ sudo apt install python-pip
# $ pip install future langdetect pyperclip sh
#
from __future__ import unicode_literals
from __future__ import print_function
from sh import espeak
from langdetect import detect
import pyperclip
def do_the_synth(text_line, language, speed):
espeak('-v', language, '-s', speed, text_line)
def main(args):
if len(args) > 1:
if args[1] == '--speed':
speed = int(args[2])
else:
speed = 360 # reading speed
text = pyperclip.paste()
language = detect(unicode(text)) # detect text language
text = text.split('\n')
text = list(filter(None, text)) # remove empty lines
for index, line in enumerate(text):
print("{}\n[{}/{}]".format(line, (index + 1), len(text)))
do_the_synth(line, language, speed)
return 0
if __name__ == '__main__':
import sys
try:
sys.exit(main(sys.argv))
except KeyboardInterrupt:
print("\n")
except Exception:
traceback.print_exc(file=sys.stdout)
@edo9k
Copy link
Author

edo9k commented May 30, 2019

Stuff to add:

  • save to audio file option (either mp3 or ogg)
  • add other tts backends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment