使用pyaudio录音 [英] voice recording using pyaudio

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

问题描述

我正在尝试使用 python 录制语音.我尝试使用 pyaudio 模块,它在我的电脑上保存了一个 wav 文件,但录制了一个静态声音.有什么建议吗?

i am trying to record voice using python. i tried to use the pyaudio module it saved a wav file on my computer but recorded a static voice. any suggestions?

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "voice.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()

推荐答案

首先,确保您的麦克风已实际连接、打开且未静音.

First, make sure your microphone is actually connected, on and not muted.

您在打开流时没有提供设备索引.这意味着您将获得 PyAudio 认为默认的设备.这可能不是您的麦克风.

You are not providing a device index when opening the stream. This means that you will get the device that PyAudio considers the default. Which might not be your microphone.

在交互式 Python 会话中使用 PyAudio 对象的 get_device_countget_device_info_by_index 方法.打印 get_device_info_by_index 返回的字典以确定哪个设备索引代表您的麦克风,并在打开流时提供该索引号作为 input_device_index 参数.

Use the get_device_count and get_device_info_by_index methods of the PyAudio object in an interactive Python session. Print the dictionaries that get_device_info_by_index returns to determine which device index represents your microphone, and provide that index number as the input_device_index parameter when opening the stream.

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

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