使用 Python 读取输入的声音信号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35344649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading input sound signal using Python
提问by Victor Pira
I need to get a sound signal from a Hyman-connected microphone and use the data for immediate processing in Python.
我需要从连接插孔的麦克风获取声音信号,并使用该数据在 Python 中进行即时处理。
The processing and subsequent steps are clear. I am lost only in getting the signal from the program. The number of channels is irrelevant, one is enough. I am not going to play the sound back so there should be no need for ASIO on soundcard.
处理和后续步骤清晰。我只在从程序中获取信号时迷失了方向。通道的数量无关紧要,一个就足够了。我不打算播放声音,所以声卡上应该不需要 ASIO。
My question is: how can I capture Hyman audio from Python? (It would be great if there were a package, well documented and niches examples :-).
我的问题是:如何从 Python 中捕获 Hyman 音频?(如果有一个包,有据可查和利基示例,那就太好了:-)。
采纳答案by alr
Have you tried pyaudio? To install:
你试过pyaudio吗?安装:
python -m pip install pyaudio
Recording example, from the official website:
录音示例,来自官网:
PyAudio example: Record a few seconds of audio and save it to a WAVE file.
PyAudio 示例:录制几秒钟的音频并将其保存为 WAVE 文件。
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
This example works on my laptop with Python 2.7.11 (and 3.5.1) in Windows 8.1, pyaudio 0.2.9.
这个例子在我的笔记本电脑上运行,在 Windows 8.1 中使用 Python 2.7.11(和 3.5.1),pyaudio 0.2.9。
回答by wrkyle
I would consider using pysox, the python bindings for libsox.
我会考虑使用pysox,libsox的 python 绑定。
You can get pysox packagefrom PyPI.
您可以从 PyPI获取pysox 包。
回答by gpoo
If the requirement is Hyman, then you may want to use PyHyman, which is the Python binding for Hyman.
如果要求是Hyman,那么您可能需要使用PyHyman,它是 Hyman 的 Python 绑定。
Furthermore, the source code has an example for what you want to do, that is, to capture audio. See the file capture.py
此外,源代码有一个示例说明您想要做什么,即捕获音频。查看文件capture.py
You must consider that to avoid missing a block, you must call Hyman.process
every 500 *(buffer_size/sample_rate)
milliseconds.
Hyman.process
throw exceptions when you miss audio blocks (Hyman.InputSyncError
and Hyman.OutputSyncError
).
您必须考虑到,为了避免丢失一个块,您必须Hyman.process
每500 *(buffer_size/sample_rate)
毫秒调用一次。
Hyman.process
当您错过音频块(Hyman.InputSyncError
和Hyman.OutputSyncError
)时抛出异常。
回答by Anil_M
Are you planning to get audio from microphone in pieces or streaming? Either case , sounddevicemay be employed.
您是否打算从麦克风中分段或流式获取音频?无论哪种情况, 都可以使用声音设备。
You can install the python module usingpip install sounddevice --user
您可以使用安装 python 模块pip install sounddevice --user
Please refer to official site for API details.
API详情请参考官方网站。
sounddevice will record audio from your laptop microphone (standard audio input) and play on speaker or headphones (standard audio output). You can use the sound object for further processing.
sounddevice 将从您的笔记本电脑麦克风(标准音频输入)录制音频并在扬声器或耳机上播放(标准音频输出)。您可以使用声音对象进行进一步处理。
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
fs=44100
duration = 5 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
print "Recording Audio"
sd.wait()
print "Audio recording complete , Play Audio"
sd.play(myrecording, fs)
sd.wait()
print "Play Audio Complete"
Here is the Output : Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. ================================ RESTART ===================
Recording Audio
Audio recording complete , Play Audio
Play Audio Complete
这是输出:Win32 上的 Python 2.7.9(默认,2014 年 12 月 10 日,12:24:55)[MSC v.1500 32 位(英特尔)]更多信息。================================ 重新开始 ================== ==
Recording Audio
录音完成 , Play Audio
Play Audio Complete