简体   繁体   English

用Python录音

[英]Audio Recording in Python

I want to record short audio clips from a USB microphone in Python. 我想用Python录制USB麦克风的短音频片段。 I have tried pyaudio, which seemed to fail communicating with ALSA, and alsaaudio, the code example of which produces an unreadable files. 我已经尝试了pyaudio,它似乎无法与ALSA通信,而alsaaudio,其代码示例产生了一个不可读的文件。

So my question: What is the easiest way to record clips from a USB mic in Python? 所以我的问题是:在Python中用USB麦克风录制剪辑的最简单方法是什么?

This script records to test.wav while printing the current amplitute: 此脚本在打印当前放大器时记录到test.wav:

import alsaaudio, wave, numpy

inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
inp.setchannels(1)
inp.setrate(44100)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(1024)

w = wave.open('test.wav', 'w')
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(44100)

while True:
    l, data = inp.read()
    a = numpy.fromstring(data, dtype='int16')
    print numpy.abs(a).mean()
    w.writeframes(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM