Browse Source

suppress ALSA output

nico 8 years ago
parent
commit
b71540defa
2 changed files with 29 additions and 0 deletions
  1. 1 0
      Docs/dev_env_install.md
  2. 28 0
      core/OrderListener.py

+ 1 - 0
Docs/dev_env_install.md

@@ -26,6 +26,7 @@ pip install pygame
 pip install python2-pythondialog
 pip install jinja
 pip install python-crontab
+pip install cffi
 ```
 
 ### Test your env

+ 28 - 0
core/OrderListener.py

@@ -1,4 +1,8 @@
 import logging
+import os
+from cffi import FFI as _FFI
+import sys
+
 from core import ConfigurationManager
 
 logging.basicConfig()
@@ -16,6 +20,9 @@ class OrderListener:
         :param stt: Speech to text plugin name to load. If not provided,
         we will load the default one set in settings
         """
+        # this is a trick to ignore ALSA output error
+        # see http://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time
+        self._ignore_stderr()
         self.stt = stt
         self.callback = callback
         # self.settings = main_controller.conf.settingLoader.get_config()
@@ -52,4 +59,25 @@ class OrderListener:
             else:
                 klass(self.callback, parameters)
 
+    @staticmethod
+    def _ignore_stderr():
+        """Try to forward PortAudio messages from stderr to /dev/null."""
+        ffi = _FFI()
+        ffi.cdef("""
+        /* from stdio.h */
+        FILE* fopen(const char* path, const char* mode);
+        int fclose(FILE* fp);
+        FILE* stderr;  /* GNU C library */
+        FILE* __stderrp;  /* Mac OS X */
+        """)
+        stdio = ffi.dlopen(None)
+        devnull = stdio.fopen(os.devnull.encode(), b'w')
+        try:
+            stdio.stderr = devnull
+        except KeyError:
+            try:
+                stdio.__stderrp = devnull
+            except KeyError:
+                stdio.fclose(devnull)
+