|
|
@@ -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')
|