Преглед на файлове

Split Cache & FileManager

Unknown преди 8 години
родител
ревизия
b049dd38dc
променени са 7 файла, в които са добавени 81 реда и са изтрити 46 реда
  1. 3 0
      .gitignore
  2. 30 20
      core/AudioPlayer.py
  3. 3 20
      core/Cache.py
  4. 32 0
      core/FileManager.py
  5. 2 1
      core/__init__.py
  6. 6 1
      tts/TTS.py
  7. 5 4
      tts/voxygen/voxygen.py

+ 3 - 0
.gitignore

@@ -87,3 +87,6 @@ ENV/
 
 # Rope project settings
 .ropeproject
+
+# Idea project
+.idea/

+ 30 - 20
core/AudioPlayer.py

@@ -1,31 +1,46 @@
 import logging
 import pygame
-import os
+
+from core.FileManager import FileManager
 
 
 class AudioPlayer:
-    AUDIO_FREQUENCY = 16000
-    AUDIO_SIZE = -16
-    AUDIO_CHANNEL = 1
-    AUDIO_BUFFER = 2048
+    PLAYER_MP3 = "MP3"
+    PLAYER_WAV = "WAV"
+
+    AUDIO_MP3_FREQUENCY = 16000
+    AUDIO_MP3_SIZE = -16
+    AUDIO_MP3_CHANNEL = 1
+    AUDIO_MP3_BUFFER = 2048
+
+    def __init__(self, default_type=None, audio_frequency=AUDIO_MP3_FREQUENCY, audio_size=AUDIO_MP3_SIZE, audio_channel=AUDIO_MP3_CHANNEL, audio_buffer=AUDIO_MP3_BUFFER):
+        if default_type == self.PLAYER_MP3:
+            self.audio_frequency = self.AUDIO_MP3_FREQUENCY
+            self.audio_size = self.AUDIO_MP3_SIZE
+            self.audio_channel = self.AUDIO_MP3_CHANNEL
+            self.audio_buffer = self.AUDIO_MP3_BUFFER
+        else:
+            self.audio_frequency = audio_frequency
+            self.audio_size = audio_size
+            self.audio_channel = audio_channel
+            self.audio_buffer = audio_buffer
+        pygame.mixer.init(audio_frequency, audio_size, audio_channel, audio_buffer)
 
-    @classmethod
-    def play_audio(cls, music_file, volume=0.8, keep_file=False):
+    def play_audio(self, music_file, volume=0.8, keep_file=False):
         try:
-            cls._init_player_audio(music_file, volume)
+            self._init_player_audio(music_file, volume)
             logging.debug("Music file %s loaded!", music_file)
         except pygame.error:
-            cls._remove_file(music_file)
+            FileManager.remove_file(music_file)
             logging.error("File %s not found! (%s)", music_file, pygame.get_error())
             return
 
-        cls._start_player_audio()
+        self._start_player_audio()
         if not keep_file:
-            cls._remove_file(music_file)
+            FileManager.remove_file(music_file)
 
-    @classmethod
-    def _init_player_audio(cls, music_file, volume):
-        pygame.mixer.init(cls.AUDIO_FREQUENCY, cls.AUDIO_SIZE, cls.AUDIO_CHANNEL, cls.AUDIO_BUFFER)
+    @staticmethod
+    def _init_player_audio(music_file, volume):
         pygame.mixer.music.set_volume(volume)
         pygame.mixer.music.load(music_file)
 
@@ -35,10 +50,5 @@ class AudioPlayer:
         pygame.mixer.music.play()
         clock = pygame.time.Clock()
         while pygame.mixer.music.get_busy():
-            clock.tick(10)
+            clock.tick(20)
         return
-
-    @staticmethod
-    def _remove_file(file_path):
-        if os.path.exists(file_path):
-            return os.remove(file_path)

+ 3 - 20
core/Cache.py

@@ -1,8 +1,9 @@
-import shutil
 import hashlib
 import os
 import logging
 
+from core.FileManager import FileManager
+
 
 class Cache:
     DEFAULT_MODULE_NAME = "default"
@@ -19,24 +20,6 @@ class Cache:
         filename = voice + "." + md5 + self._cache_extension
         cache_directory = os.path.join(self._cache_path, self._module_name, language)
         file_path = os.path.join(cache_directory, filename)
-        self.create_directory(cache_directory)
+        FileManager.create_directory(cache_directory)
         logging.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
         return file_path
-
-    @staticmethod
-    def create_directory(cache_path):
-        if not os.path.exists(cache_path):
-            os.makedirs(cache_path)
-
-    def write_in_file(self, file_path, content):
-        with open(file_path, "wb") as file_open:
-            file_open.write(content)
-            file_open.close()
-        return not self.file_is_empty(file_path)
-
-    def wipe_cache(self):
-        shutil.rmtree(self._cache_path)
-
-    @staticmethod
-    def file_is_empty(file_path):
-        return os.path.getsize(file_path) == 0

+ 32 - 0
core/FileManager.py

@@ -0,0 +1,32 @@
+import os
+import shutil
+
+
+class FileManager:
+    def __init__(self):
+        pass
+
+    @staticmethod
+    def create_directory(cache_path):
+        if not os.path.exists(cache_path):
+            os.makedirs(cache_path)
+
+    @staticmethod
+    def write_in_file(file_path, content):
+        with open(file_path, "wb") as file_open:
+            file_open.write(content)
+            file_open.close()
+        return not FileManager.file_is_empty(file_path)
+
+    @staticmethod
+    def wipe_cache(cache_path):
+        shutil.rmtree(cache_path)
+
+    @staticmethod
+    def file_is_empty(file_path):
+        return os.path.getsize(file_path) == 0
+
+    @staticmethod
+    def remove_file(file_path):
+        if os.path.exists(file_path):
+            return os.remove(file_path)

+ 2 - 1
core/__init__.py

@@ -4,4 +4,5 @@ from core.OrderAnalyser import OrderAnalyser
 from core.OrderListener import OrderListener
 from core.AudioPlayer import AudioPlayer
 from core.ShellGui import ShellGui
-from core.Cache import Cache
+from core.Cache import Cache
+from core.FileManager import FileManager

+ 6 - 1
tts/TTS.py

@@ -1,6 +1,11 @@
+from core import AudioPlayer
 from core import Cache
 
 
 class TTS:
-    def __init__(self):
+    def __init__(self, audio_player_type=None):
         self.cache = Cache(module_name=self.__class__.__name__)
+        self.audio_player = AudioPlayer(audio_player_type)
+
+    def play_audio(self, music_file, volume=0.8, keep_file=False):
+        self.audio_player.play_audio(music_file, volume, keep_file)

+ 5 - 4
tts/voxygen/voxygen.py

@@ -5,6 +5,7 @@ import logging
 import sys
 
 from core import AudioPlayer
+from core import FileManager
 from tts import TTS
 
 
@@ -27,7 +28,7 @@ class Voxygen(TTS):
     VOXYGEN_TIMEOUT_SEC = 30
 
     def __init__(self):
-        TTS.__init__(self)
+        TTS.__init__(self, AudioPlayer.PLAYER_MP3)
 
     def say(self, words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=True):
         voice = self.get_voice(voice, language)
@@ -35,7 +36,7 @@ class Voxygen(TTS):
         file_path = self.cache.get_audio_file_cache_path(words, voice, language)
 
         if self.get_audio(voice, words, file_path, cache):
-            AudioPlayer.play_audio(file_path, keep_file=cache)
+            self.play_audio(file_path, keep_file=cache)
 
     def get_voice(self, voice, language):
         if language in self.VOXYGEN_LANGUAGES and voice in self.VOXYGEN_LANGUAGES[language]:
@@ -45,7 +46,7 @@ class Voxygen(TTS):
         return self.VOXYGEN_VOICE_DEFAULT
 
     def get_audio(self, voice, text, file_path, cache):
-        if not cache or not os.path.exists(file_path) or self.cache.file_is_empty(file_path):
+        if not cache or not os.path.exists(file_path) or FileManager.file_is_empty(file_path):
             payload = {
                 "method": "redirect",
                 "text": text.encode('utf8'),
@@ -59,7 +60,7 @@ class Voxygen(TTS):
 
             try:
                 if r.status_code == requests.codes.ok and content_type == self.VOXYGEN_CONTENT_TYPE:
-                    return self.cache.write_in_file(file_path, r.content)
+                    return FileManager.write_in_file(file_path, r.content)
                 else:
                     return False
             except IOError as e: