How can PyAudio use my computer microphone?

To record a 2 second wav file, I used PyAudio (with Pyzo) and the following classic code to record audio and save it:

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 by saying "aaa". Everything is fine, no mistakes. And when I read the wav file, "aaa" was not heard. I rendered the file in Audacity and I could see that everything was just silence (0). So it looks like Pyzo doesn't know where my mic is because he didn't use it. What can I do? Any ideas? Or it may not have been recording all of the recorded data, but I don't know why.

I have already verified that my microphone is 16 bit and has a baud rate of 44100.

+3


source to share


1 answer


You will need to do this step by step. To make sure you are recording from the microphone, I would suggest printing out the maximum of each snippet as you read it. Then you should see in real time the 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

      



This is usually more than 10x the difference between background noise and speech, so you can easily see the size change of the numbers as you go through.

Also, list all of your microphones first to make sure you are using the correct one ( get_device_info_by_index

). For example, you can read from "line in" rather than from the microphone.

+1


source







All Articles