TTSModule.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # coding: utf8
  2. import hashlib
  3. import logging
  4. import os
  5. from core.FileManager import FileManager
  6. from core.ConfigurationManager import SettingLoader
  7. from core.Players import Mplayer
  8. logging.basicConfig()
  9. logger = logging.getLogger("kalliope")
  10. class TTSModule(object):
  11. def __init__(self, **kwargs):
  12. """
  13. Mother class of TTS module. Handle:
  14. - Cache: call cache object to create file, delete file, check if file exist
  15. - Player: call the default player to play the generated file
  16. """
  17. # set parameter from what we receive from the settings
  18. self.cache = kwargs.get('cache', False)
  19. self.language = kwargs.get('language', None)
  20. self.voice = kwargs.get('voice', "default")
  21. # the name of the TSS is the name of the Tss module that have instantiated TTSModule
  22. self.tts_caller_name = self.__class__.__name__
  23. # we don't know yet the words that will be converted to an audio and so we don't have the audio path yet too
  24. self.words = None
  25. self.file_path = None
  26. self.base_cache_path = None
  27. # load settings
  28. self.settings = SettingLoader.get_settings()
  29. print "Class TTSModule called from module %s, cache: %s, language: %s, voice: %s" % (self.tts_caller_name,
  30. self.cache,
  31. self.language,
  32. self.voice)
  33. def set_words(self, words):
  34. """
  35. Give the sentence to pronounce to the TTS module so it can generate the file path
  36. :param words: string that contain words to give the the TTS engine
  37. :return:
  38. """
  39. self.words = words
  40. # we can generate the file path from info we have
  41. self.file_path = self._get_path_to_store_audio()
  42. def play_audio(self):
  43. """
  44. Play the audio file
  45. :return:
  46. """
  47. Mplayer.play(self.file_path)
  48. def _get_path_to_store_audio(self):
  49. """
  50. Get a sentence (a text) an return the full path of the file
  51. Path syntax:
  52. </path/in/settings>/<tts.name>/tts.parameter["language"]/tts.parameter["voice"]/<md5_of_sentence.tts
  53. E.g:
  54. /tmp/kalliope/voxygene/fr/abcd12345.tts
  55. :return: path String
  56. """
  57. md5 = self.generate_md5_from_words(self.words)+".tts"
  58. self.base_cache_path = os.path.join(self.settings.cache_path, self.tts_caller_name, self.language, self.voice)
  59. returned_path = os.path.join(self.base_cache_path, md5)
  60. logger.debug("get_path_to_store_audio return: %s" % returned_path)
  61. return returned_path
  62. @staticmethod
  63. def generate_md5_from_words(words):
  64. if isinstance(words, unicode):
  65. words = words.encode('utf-8')
  66. return hashlib.md5(words).hexdigest()
  67. def is_file_already_in_cache(self):
  68. """
  69. Return true if the file to generate has already been generated before
  70. :return:
  71. """
  72. # generate sub folder
  73. FileManager.create_directory(self.base_cache_path)
  74. # check if the audio file exist
  75. exist_in_cache = os.path.exists(self.file_path)
  76. if exist_in_cache:
  77. logger.debug("TTSModule, File already in cache: %s" % self.file_path)
  78. else:
  79. logger.debug("TTSModule, File not yet in cache: %s" % self.file_path)
  80. return exist_in_cache