Python 中的录音 [英] Audio Recording in Python

查看:46
本文介绍了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?

推荐答案

这个脚本记录到 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天全站免登陆