简体   繁体   English

PyAudio如何使用我的电脑麦克风?

[英]How can PyAudio use my computer's microphone?

In order to record a 2 second wav file I used PyAudio (with Pyzo) and the following classical code to record a sound and save it : 为了录制2秒的wav文件,我使用了PyAudio(带有Pyzo)和以下经典代码来录制声音并保存它:

import pyaudio
import wave


chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "my_path//a_test_2.wav"

p = pyaudio.PyAudio()

# Création et initialisation de l'objet stream...
s = p.open(format = FORMAT, 
       channels = CHANNELS,
       rate = RATE,
       input = True, 
       frames_per_buffer = chunk)

print("---recording---")

d = []

print((RATE / chunk) * RECORD_SECONDS)

for i in range(0, (RATE // chunk * RECORD_SECONDS)): 

    data = s.read(chunk)
    d.append(data)
    #s.write(data, chunk)

print("---done recording---")

s.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(d))
wf.close()

Then I used it, saying "aaa". 然后我用它,说“aaa”。 Everything's fine, no error. 一切都很好,没有错误。 And when I read the wav file, no "aaa" could be heard. 当我阅读wav文件时,没有听到“aaa”。 I visualized the file in Audacity and I could see everything was just silence (0). 我在Audacity中可视化文件,我可以看到一切都只是沉默(0)。 So it seems Pyzo doesn't know where my microphone is, because it didn't use it. 所以似乎Pyzo不知道我的麦克风在哪里,因为它没有使用它。 What can I do ? 我能做什么 ? Any idea ? 任何想法 ? Or maybe it didn't write all the data recorded, but I don't know why. 或者它可能没有写出所有记录的数据,但我不知道为什么。

I have already checked that my microphone is 16 bits and has a 44100 rate. 我已经检查过我的麦克风是16位并且有44100的速率。

You'll need to do get this working step-by-step. 你需要一步一步地完成这项工作。 To make sure that you're recording from the mic, I would suggest printing out the max of each chunk as you read it. 为了确保您从麦克风录音,我建议您在阅读时打印出每个块的最大值。 Then you should see, in real time, a difference between background noise and your speech. 那么你应该实时看到背景噪音和你的演讲之间的差异。 For example: 例如:

import audioop

# all the setup stuff, then in your main data loop:

for i in range(0, (RATE // chunk * RECORD_SECONDS)): 
    data = s.read(chunk)
    mx = audioop.max(data, 2)
    print mx

Usually this difference between background noise and speech is more than 10x, so you can easily see the size change in the numbers as they fly by. 通常背景噪音和语音之间的差异超过10倍,因此您可以轻松查看飞行时数字的大小变化。

Also, at the start, list all of your microphones to make sure that you're using the right one ( get_device_info_by_index ). 此外,在开始时,列出所有麦克风以确保您使用正确的麦克风( get_device_info_by_index )。 For example, you could be reading from the "line in" rather than the microphone. 例如,您可能正在阅读“线路输入”而不是麦克风。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM