TTSModule.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 MissingTTSParameter(Exception):
  11. pass
  12. class TtsGenerateAudioFunctionNotFound(Exception):
  13. pass
  14. class FailToLoadSoundFile(Exception):
  15. pass
  16. class TTSModule(object):
  17. def __init__(self, **kwargs):
  18. """
  19. Mother class of TTS module. Handle:
  20. - Cache: call cache object to create file, delete file, check if file exist
  21. - Player: call the default player to play the generated file
  22. """
  23. # set parameter from what we receive from the settings
  24. self.cache = kwargs.get('cache', False)
  25. self.language = kwargs.get('language', None)
  26. self.voice = kwargs.get('voice', "default")
  27. # the name of the TSS is the name of the Tss module that have instantiated TTSModule
  28. self.tts_caller_name = self.__class__.__name__
  29. # 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
  30. self.words = None
  31. self.file_path = None
  32. self.base_cache_path = None
  33. # load settings
  34. self.settings = SettingLoader.get_settings()
  35. print "Class TTSModule called from module %s, cache: %s, language: %s, voice: %s" % (self.tts_caller_name,
  36. self.cache,
  37. self.language,
  38. self.voice)
  39. def play_audio(self):
  40. """
  41. Play the audio file
  42. :return:
  43. """
  44. Mplayer.play(self.file_path)
  45. def generate_and_play(self, words, generate_audio_function_from_child=None):
  46. """
  47. Generate an audio file from <words> if not already in cache and call the Player to play it
  48. :param words: Sentence text from which we want to generate an audio file
  49. :param generate_audio_function_from_child: The child function to generate a file if necessary
  50. :return:
  51. """
  52. if generate_audio_function_from_child is None:
  53. raise TtsGenerateAudioFunctionNotFound
  54. self.words = words
  55. # we can generate the file path from info we have
  56. self.file_path = self._get_path_to_store_audio()
  57. if not self.cache:
  58. # no cache, we need to generate the file
  59. generate_audio_function_from_child()
  60. else:
  61. # we check if the file already exist. If not we generate it with the TTS engine
  62. if not self.is_file_already_in_cache():
  63. generate_audio_function_from_child()
  64. # then play the generated audio file
  65. self.play_audio()
  66. # if the user don't want to keep the cache we remove the file
  67. if not self.cache:
  68. FileManager.remove_file(self.file_path)
  69. def _get_path_to_store_audio(self):
  70. """
  71. Get a sentence (a text) an return the full path of the file
  72. Path syntax:
  73. </path/in/settings>/<tts.name>/tts.parameter["language"]/tts.parameter["voice"]/<md5_of_sentence.tts
  74. E.g:
  75. /tmp/kalliope/voxygene/fr/abcd12345.tts
  76. :return: path String
  77. """
  78. md5 = self.generate_md5_from_words(self.words)+".tts"
  79. self.base_cache_path = os.path.join(self.settings.cache_path, self.tts_caller_name, self.language, self.voice)
  80. returned_path = os.path.join(self.base_cache_path, md5)
  81. logger.debug("get_path_to_store_audio return: %s" % returned_path)
  82. return returned_path
  83. @staticmethod
  84. def generate_md5_from_words(words):
  85. """
  86. Generate a md5 hash from received text
  87. :param words: Text to convert into md5 hash
  88. :return: String md5 hash from the received words
  89. """
  90. if isinstance(words, unicode):
  91. words = words.encode('utf-8')
  92. return hashlib.md5(words).hexdigest()
  93. def is_file_already_in_cache(self):
  94. """
  95. Return true if the file to generate has already been generated before
  96. :return:
  97. """
  98. # generate sub folder
  99. FileManager.create_directory(self.base_cache_path)
  100. # check if the audio file exist
  101. exist_in_cache = os.path.exists(self.file_path)
  102. if exist_in_cache:
  103. logger.debug("TTSModule, File already in cache: %s" % self.file_path)
  104. else:
  105. logger.debug("TTSModule, File not yet in cache: %s" % self.file_path)
  106. return exist_in_cache