2

I have this program that listens to the microphone and tries to recognize what was said:

#!/usr/bin/env python3

import speech_recognition

recognizer = speech_recognition.Recognizer()

class Recognition:

    def __init__(self):
        self.recognizer = speech_recognition.Recognizer()

    def listen(self):
        with speech_recognition.Microphone() as source:
            self.recognizer.adjust_for_ambient_noise(source)
            self.audio = self.recognizer.listen(source)

    def recognize_sphinx(self):
        decoder = self.recognizer.recognize_sphinx(self.audio, show_all=True)
        for best, i in zip(decoder.nbest(), range(10)):
            return best.hypstr


r = Recognition()
r.listen()
print(r.recognize_sphinx())

Although the code works, it does display the following warnings along with the output:

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
potato

The problem is I want a script to execute the code above and read its output (potato), but the returned result is mixed with all those errors. I tried the solution I saw here, but it only suppress the alsa warnings. Probably messing with alsa/pulseaudio configs will solve this problem, but I don't want to do that on every machine that runs the code.

I also tried redirecting stdin/stderr to null, but didn't suppress anything.

I have a workaround in mind that is, when the script runs this program, to get only the last line, but I want that only as a last resource.

Community
  • 1
  • 1
Jesse
  • 1,449
  • 2
  • 15
  • 20
  • 1
    Just to point you out, that this problem is due to `alsa.conf` file having crappy settings such as `pcm.rear cards.pcm.rear`. Removing those from `/usr/share/alsa/alsa.conf` should solve the problem. – jojeck Jun 27 '16 at 15:50

1 Answers1

1

The warning/status messages go to the standard error output (stderr), while the output of your print() call goes to the standard output (stdout). It should be trivial to separate the two.

If you want to actually suppress the warning/status messages, you can have a look at my answer to the SO-question you were mentioning, or the more verbose answer I've linked to from there.

Community
  • 1
  • 1
Matthias
  • 4,524
  • 2
  • 31
  • 50
  • I'm aware stderr is not mixed with stdout, but when I ran the code externally, the errors came along with the desired output, as I stated in my answer. So yes, unless there's another solution, I need to suppress the warnings to have a code without workarounds. I'll try the module you mentioned as soon as I can. Thanks! – Jesse Jun 13 '16 at 17:37
  • How did you run your script and how did you try to grab `stdout`? – Matthias Jun 14 '16 at 06:17
  • I run it in emacs with the command `shell-command-to-string`. Now that you mentioned, maybe that's the problem. Anyway, I was able to solve this and get a better behavior using emacsclient. Thanks for the help. Cheers! – Jesse Jun 14 '16 at 12:34
  • It would be nice if pyaudio used logging for messages. – user1650613 Oct 06 '20 at 17:57