Browse Source

add audio player static class

nico 8 years ago
parent
commit
9c7636e2a9
7 changed files with 94 additions and 71 deletions
  1. 1 0
      Docs/dev_env_install.md
  2. 44 0
      core/AudioPlayer.py
  3. 2 0
      core/__init__.py
  4. 3 2
      neurons/Neurone.py
  5. 28 26
      test.py
  6. 11 4
      tts/pico2wave/pico2wave.py
  7. 5 39
      tts/voxygen/voxygen.py

+ 1 - 0
Docs/dev_env_install.md

@@ -30,4 +30,5 @@ Then install libs
 pip install SpeechRecognition
 pip install pyaudio
 pip install ansible
+pip install pygame
 ```

+ 44 - 0
core/AudioPlayer.py

@@ -0,0 +1,44 @@
+import logging
+import pygame
+import os
+
+
+class AudioPlayer:
+    AUDIO_FREQUENCY = 16000
+    AUDIO_SIZE = -16
+    AUDIO_CHANNEL = 1
+    AUDIO_BUFFER = 2048
+
+    @classmethod
+    def play_audio(cls, music_file, volume=0.8, keep_file=False):
+        try:
+            cls._init_player_audio(music_file, volume)
+            logging.debug("Music file %s loaded!", music_file)
+        except pygame.error:
+            cls._remove_file(music_file)
+            logging.error("File %s not found! (%s)", music_file, pygame.get_error())
+            return
+
+        cls._start_player_audio()
+        if not keep_file:
+            cls._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)
+        pygame.mixer.music.set_volume(volume)
+        pygame.mixer.music.load(music_file)
+
+    @staticmethod
+    def _start_player_audio():
+        logging.info("Starting pygame audio player")
+        pygame.mixer.music.play()
+        clock = pygame.time.Clock()
+        while pygame.mixer.music.get_busy():
+            clock.tick(10)
+        return
+
+    @staticmethod
+    def _remove_file(file_path):
+        if os.path.exists(file_path):
+            return os.remove(file_path)

+ 2 - 0
core/__init__.py

@@ -2,3 +2,5 @@ from core.ConfigurationManager.ConfigurationManager import ConfigurationManager
 from core.JarvisTrigger import JarvisTrigger
 from core.OrderAnalyser import OrderAnalyser
 from core.OrderListener import OrderListener
+from core.AudioPlayer import AudioPlayer
+

+ 3 - 2
neurons/Neurone.py

@@ -4,8 +4,9 @@ import importlib
 class Neurone:
     def __init__(self, stt=None, tts=None):
         # TODO load the stt and tts from settings
-        self.stt = "snowboy"
-        self.tts = "pico2wave"
+        self.stt = "google"
+        # self.tts = "pico2wave"
+        self.tts = "voxygen"
 
     def say(self, message):
         # here we use the tts to make jarvis talk

+ 28 - 26
test.py

@@ -2,7 +2,12 @@ from core import ConfigurationManager
 from core.NeuroneLauncher import NeuroneLauncher
 from core.OrderAnalyser import OrderAnalyser
 from core.OrderListener import OrderListener
+from neurons import Say
 from neurons.ansible_tasks.ansible_tasks import Ansible_tasks
+import logging
+
+logger = logging.getLogger()
+logger.setLevel(logging.DEBUG)
 
 
 # run command
@@ -15,30 +20,27 @@ from neurons.ansible_tasks.ansible_tasks import Ansible_tasks
 # ansible_tasks = Ansible_tasks(tasks_file)
 
 
-def test_multi_args(*args , **kwargs):
-    if kwargs is not None:
-        for key, value in kwargs.iteritems():
-            print "%s == %s" % (key, value)
-
-
-conf = ConfigurationManager(brain_file_name="test.yml")
-
-brain = conf.brainLoader.get_config()
-print brain
-
-
-for el in brain:
-    print el["neurons"]
-
-    neurons = el["neurons"]
-    for neuron in neurons:
-        NeuroneLauncher().start_neurone(neuron)
-
-
-    # test_multi_args(**el["neurons"][0]["say"])
-
-
-
-
-
+# def test_multi_args(*args , **kwargs):
+#     if kwargs is not None:
+#         for key, value in kwargs.iteritems():
+#             print "%s == %s" % (key, value)
+#
+#
+# conf = ConfigurationManager(brain_file_name="test.yml")
+#
+# brain = conf.brainLoader.get_config()
+# print brain
+#
+#
+# for el in brain:
+#     print el["neurons"]
+#
+#     neurons = el["neurons"]
+#     for neuron in neurons:
+#         NeuroneLauncher().start_neurone(neuron)
+#
+#
+#     # test_multi_args(**el["neurons"][0]["say"])
+
+Say(message="bonjour monsieur")
 

+ 11 - 4
tts/pico2wave/pico2wave.py

@@ -1,10 +1,17 @@
 import subprocess
 import os
+from core import AudioPlayer
+
+AUDIO_FREQUENCY = 16000
+AUDIO_SIZE = -16
+AUDIO_CHANNEL = 1
+AUDIO_BUFFER = 2048
 
 
 def say(words):
-    tempfile = "temp.wav"
+    temp_file = "/tmp/temp.wav"
     devnull = open("/dev/null", "w")
-    subprocess.call(["pico2wave", "-l=fr-FR", "-w", tempfile, words], stderr=devnull)
-    subprocess.call(["aplay", tempfile], stderr=devnull)
-    os.remove(tempfile)
+    subprocess.check_output(["/usr/bin/pico2wave", "-l=fr-FR", "-w=%s" % temp_file, words], stderr=devnull)
+    AudioPlayer.play_audio(temp_file)
+    devnull.close()
+

+ 5 - 39
tts/voxygen/voxygen.py

@@ -5,6 +5,9 @@ import shutil
 import pygame
 import requests
 import logging
+import sys
+
+from core import AudioPlayer
 
 VOXYGEN_LANGUAGES = dict(
     fr=dict(electra="Electra", emma="Emma", becool="Becool", agnes="Agnes", loic="Loic", fabienne="Fabienne", helene="Helene", marion="Marion", matteo="Matteo",
@@ -31,14 +34,13 @@ AUDIO_CHANNEL = 1
 AUDIO_BUFFER = 2048
 
 
-def say(words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=None):
+def say(words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=True):
     voice = get_voice(voice, language)
 
     file_path = get_file_path(words, voice, language)
 
     if get_audio(voice, words, file_path, cache):
-        play_audio(file_path)
-        remove_temp_file(file_path, cache)
+        AudioPlayer.play_audio(file_path, keep_file=cache)
 
 
 def get_file_path(words, voice, language):
@@ -87,42 +89,6 @@ def get_audio(voice, text, file_path, cache):
         return True
 
 
-def play_audio(music_file, volume=0.8):
-    try:
-        init_player_audio(music_file, volume)
-        logging.debug("Music file %s loaded!", music_file)
-    except pygame.error:
-        remove_file(music_file)
-        logging.error("File %s not found! (%s)", music_file, pygame.get_error())
-        return
-
-    start_player_audio()
-
-
-def init_player_audio(music_file, volume):
-    pygame.mixer.init(AUDIO_FREQUENCY, AUDIO_SIZE, AUDIO_CHANNEL, AUDIO_BUFFER)
-    pygame.mixer.music.set_volume(volume)
-    pygame.mixer.music.load(music_file)
-
-
-def start_player_audio():
-    pygame.mixer.music.play()
-    clock = pygame.time.Clock()
-    while pygame.mixer.music.get_busy():
-        clock.tick(10)
-    return
-
-
-def remove_temp_file(file_path, cache):
-    if not cache:
-        remove_file(file_path)
-
-
-def remove_file(file_path):
-    if os.path.exists(file_path):
-        os.remove(file_path)
-
-
 def write_in_file(file_path, content):
     with open(file_path, "wb") as file_open:
         file_open.write(content)