4

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 :

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". Everything's fine, no error. And when I read the wav file, no "aaa" could be heard. I visualized the file in Audacity and I could see everything was just silence (0). So it seems Pyzo doesn't know where my microphone is, because it didn't use it. 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.

1
  • check this command on Command Terminal to make sure that MIC is working or not arecord -f cd -D plughw:1,0 -d 20 test.wav Commented Jan 28, 2015 at 16:01

1 Answer 1

1

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.

Also, at the start, list all of your microphones to make sure that you're using the right one (get_device_info_by_index). For example, you could be reading from the "line in" rather than the microphone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.