所以我想在这里做一个练习,我必须使用pyaudio记录我的声音。我不知道是否需要一个麦克风或如果代码是错误的,但这种情况不断发生,当运行程序:
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(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 -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
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 -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
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 -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Start recording
Traceback (most recent call last):
File "/home/sindri-21/Desktop/Python/teste_audio.py", line 32, in <module>
obj.setchannels(CHANNELS)
AttributeError: 'Wave_write' object has no attribute 'setchannels'
Exception ignored in: <function Wave_write.__del__ at 0x7faf94040dc0>
Traceback (most recent call last):
File "/usr/lib/python3.8/wave.py", line 327, in __del__
File "/usr/lib/python3.8/wave.py", line 445, in close
File "/usr/lib/python3.8/wave.py", line 463, in _ensure_header_written
wave.Error: # channels not specified
无论如何,这是代码:
import pyaudio
import wave
FRAMES_PER_BUFFER = 32000
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=FRAMES_PER_BUFFER
)
print("Start recording")
seconds = 5
frames = []
for i in range(0, int(RATE/FRAMES_PER_BUFFER*seconds)):
data = stream.read(FRAMES_PER_BUFFER)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
obj = wave.open("output.wav", "wb")
obj.setchannels(CHANNELS)
obj.setsampwidth(p.get_sample_size(FORMAT))
obj.setframerate(RATE)
obj.writeframes(b"".join(frames))
obj.close()
我只是想知道为什么它不工作,也许是通道,也许是pyaudio(0.2.11)的版本,缺乏麦克风,等等…
为了捕获任何声音,您的计算机需要一些硬件将声波转换为数字信号,通常是麦克风。所以你需要在一台内置麦克风的电脑(大多数笔记本电脑)上工作,或者购买一个外部麦克风。没有合适的硬件,任何软件都无法捕捉声音。