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)?
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 time – Maxa