How to remove PyAudio errors?
Asked Answered
S

0

1

I am trying out a solution to an accepted question that demonstrates how to create a beep with PyAudio. Strangely I see quite a lot of errors. Unfortunately I've never sent any audio data programatically to a sound card so I've no idea about how to get rid of the errors. The Internet was not very helpful, so could you provide me with some advice? Thank you!

Here's a short program that produces the beep as provided by an answer here on StackOverflow:

#!/usr/bin/python3

import math

from pyaudio import PyAudio # sudo apt-get install python{,3}-pyaudio

def sine_tone(frequency:float, duration:float, volume:int = 1, sampleRate:int = 22050):
    n_samples = int(sampleRate * duration)
    restframes = n_samples % sampleRate

    p = PyAudio()
    stream = p.open(format=p.get_format_from_width(1), # 8bit
                    channels=1, # mono
                    rate=sampleRate,
                    output=True)
    s = lambda t: volume * math.sin(2 * math.pi * frequency * t / sampleRate)
    samples = (int(s(t) * 0x7f + 0x80) for t in range(n_samples))
    if duration < 1:
        stream.write(bytes(bytearray(samples)))
    else:
        for buf in zip(*[samples]*sampleRate): # write several samples at a time
            stream.write(bytes(bytearray(buf)))

    # fill remainder of frameset with silence
    stream.write(b'\x80' * restframes)

    stream.stop_stream()
    stream.close()
    p.terminate()
#

sine_tone(500, 1)

This will result in the following output lines that are all sent to STDERR:

ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
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

Why are msgs sent to STDERR, what do they mean and how can I get rid of them (as sound seems to be played as expected)?

Shaftesbury answered 3/5, 2018 at 19:18 Comment(6)
Pass volume=.1 or less (otherwise it is too loud). coding: utf-8 is unnecessary on Python 3¶ I get similar output on my Ubuntu machine. It's been a while since I've the sound broken so I have no idea whether this output represents actual errors or it [or some parts] may be ignored safely. See PyAudio working, but spits out error messages each timeMaxa
Changing the volume settings makes no difference at all. (BTW: With .1 it is just 10% of maximum volume - much too quiet.)Shaftesbury
It seems that it can be ignored safely. But that is not the point: I've a console appplication for tests purposes that produces about 100 beeps. I get 100 times these error lines. So for every reasonable output the program should produce I get dozens of useless error messages. This shouldn't be the case.Shaftesbury
You should have a look at the link to a duplicate question (with some answers) that @Maxa has given. Here's another similar question with some answers: How can the terminal output of executables run by Python functions be silenced in a general way?.Fianna
related: Raspberry ALSA sound output / input slaveSwift
Oh, thanks. I was not able to find some information like that using classic internet search engines. Thank you, I'll have a look at it!Shaftesbury

© 2022 - 2024 — McMap. All rights reserved.