Ver código fonte

#19 turn outputs into services

Martin 10 anos atrás
pai
commit
b0416d71d9

+ 18 - 14
voice_control/output/__init__.py

@@ -1,28 +1,32 @@
 import logging
 import audio_output
+import console
+
+SERVICES = [
+    audio_output.AudioOutput(),
+    console.Console(),
+]
 
 def success():
-    audio_output.play_clip(audio_output.Clips.SUCCESS)
-    logging.info('OK.')
+    for service in SERVICES:
+        service.success()
 
 def failure(s=None):
-    audio_output.play_clip(audio_output.Clips.FAILURE)
-    if s is None:
-        logging.error('Failed.')
-    else:
-        logging.error(s)
+    for service in SERVICES:
+        service.failure(s)
 
 def cancelled():
-    tts('cancelled')
-    logging.info('Cancelled.')
+    for service in SERVICES:
+        service.cancelled()
 
 def tts(s):
-    audio_output.tts(s)
-    logging.info(s)
+    for service in SERVICES:
+        service.tts(s)
 
 def text(s):
-    audio_output.tts(s)
-    logging.info(s)
+    for service in SERVICES:
+        service.text(s)
 
 def debug(s):
-    logging.debug(s)
+    for service in SERVICES:
+        service.debug(s)

+ 30 - 23
voice_control/output/audio_output.py

@@ -1,13 +1,13 @@
 import os
 import time
-import logging
 import hashlib
 
 import pyaudio
 from gtts import gTTS
 import pygame.mixer
 
-_SOUND_ENABLED = True
+import base
+
 _CLIP_FOLDER = '/Users/martin/workspaces/visual-code/voice_control/resources/clips'
 
 _CLIP_DATA_CACHE = {}
@@ -33,26 +33,7 @@ def _play(fname, block):
         while pygame.mixer.music.get_busy():
             time.sleep(0.1)
 
-def tts(text, lang='en', block=True):
-    if not text:
-        return
-
-    if not _SOUND_ENABLED:
-        return
-
-    tts_hash = hashlib.md5(text.lower()).hexdigest()
-    cache_fname = os.path.join(TMP_DIR, tts_hash) + '.mp3'
-    if os.path.isfile(cache_fname):
-        _play(cache_fname, block)
-        return
-
-    tts_stream = gTTS(text=text, lang=lang)
-    with open(cache_fname, 'w') as f:
-        tts_stream.write_to_fp(f)
-
-    _play(cache_fname, block)
-
-def play_clip(clip):
+def _play_clip(clip):
     """Simple callback function to play a wave file. By default it plays
     a Ding sound.
 
@@ -62,4 +43,30 @@ def play_clip(clip):
 
     fname = os.path.join(_CLIP_FOLDER, clip)
     _play(fname, True)
-    return
+    return
+
+class AudioOutput(base.Base):
+    def tts(self, text):
+        if not text:
+            return
+
+        tts_hash = hashlib.md5(text.lower()).hexdigest()
+        cache_fname = os.path.join(TMP_DIR, tts_hash) + '.mp3'
+        if os.path.isfile(cache_fname):
+            _play(cache_fname, True)
+            return
+
+        tts_stream = gTTS(text=text, lang='en')
+        with open(cache_fname, 'w') as f:
+            tts_stream.write_to_fp(f)
+
+        _play(cache_fname, True)
+    
+    def success(self):
+        _play_clip(Clips.SUCCESS)
+    
+    def failure(self, *_):
+        _play_clip(Clips.FAILURE)
+    
+    def cancelled(self):
+        self.tts('cancelled')

+ 13 - 0
voice_control/output/base.py

@@ -0,0 +1,13 @@
+class Base(object):
+    def success(self):
+        pass
+    def failure(self, s=None):
+        pass
+    def cancelled(self):
+        pass
+    def tts(self, s): 
+        pass
+    def text(self, s):
+        pass
+    def debug(self, s):
+        pass

+ 24 - 0
voice_control/output/console.py

@@ -0,0 +1,24 @@
+import logging
+import base
+
+class Console(base.Base):
+    def failure(self, s=None):
+        if s is None:
+            logging.error('Failed.')
+        else:
+            logging.error(s)
+        
+    def tts(self, s):
+        logging.info(s)
+    
+    def text(self, s):
+        logging.info(s)
+    
+    def debug(self, s):
+        logging.debug(s)
+    
+    def cancelled(self):
+        logging.info('cancelled.')
+    
+    def success(self):
+        logging.info('OK.')