Python,使用 Pyaudio 以 16000Hz 录制错误音频 [英] Python, Error audio Recording in 16000Hz using Pyaudio

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

问题描述

我使用 Python 2.7.3、Mac OS 10.8.2 和 Xcode 4.5.1

I use Python 2.7.3, Mac OS 10.8.2 and Xcode 4.5.1

我正在尝试按照 http://people.csail 中的说明使用 PyAudio 录制声音.mit.edu/hubert/pyaudio/

并使用程序

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""

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

它适用于 RATE = 44100.但我想用 RATE = 16000CHANNELS = 1

It works well with RATE = 44100. But I want to record with RATE = 16000 and CHANNELS = 1

更改值给我一个错误

我如何能够以 RATE = 16000 进行录制?

How will I be able to record with RATE = 16000?

推荐答案

我遇到了同样的问题.该问题已在 portaudio 中修复,但 brew 为我安装了旧版本.我升级了

I experienced the same problem. The issue was fixed in portaudio, but brew installed an old version for me. I upgraded with

brew install portaudio --HEAD

然后能够以 CHANNELS=1 和 RATE=16000 运行您的代码

and then was able to run your code with CHANNELS=1 and RATE=16000

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

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