开发者

Audio Recording in Python

开发者 https://www.devze.com 2023-03-23 13:06 出处:网络
I want to record short audio clips from a USB microphone in Python. I have开发者_如何学运维 tried pyaudio, which seemed to fail communicating with ALSA, and alsaaudio, the code example of which produc

I want to record short audio clips from a USB microphone in Python. I have开发者_如何学运维 tried pyaudio, which seemed to fail communicating with ALSA, and alsaaudio, the code example of which produces an unreadable files.

So my question: What is the easiest way to record clips from a USB mic in Python?


This script records to test.wav while printing the current amplitute:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号