用Python录音 [英] Audio Recording in Python

查看:122
本文介绍了用Python录音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Python录制来自USB麦克风的简短音频片段.我尝试了pyaudio,该音频似乎无法与ALSA通信,而alsaaudio的代码示例生成了无法读取的文件.

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.

所以我的问题是:用Python从USB麦克风录制片段的最简单方法是什么?

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

推荐答案

此脚本在打印当前amplitute时记录到test.wav:

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)

这篇关于用Python录音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆