voice recording using pyaudio
Asked Answered
P

4

6

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()
Pointdevice answered 20/11, 2016 at 12:30 Comment(0)
B
8

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

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.

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.

Backhander answered 20/11, 2016 at 13:59 Comment(4)
and how can i know what is the index of my microphone?Pointdevice
get_device_info_by_index returns me a huge dictPointdevice
Is there a way to detect if 'data' in this loop is a silence or a sound?Twoedged
@ChhandoseeBhattacharya Yes. Look at the volume or the spectrum.Backhander
P
8

below code will list indexes of the available recording devices,then user can give a specific index as the input,code will start recording via given recording device index.

import pyaudio
import wave
import math

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 512
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "recordedFile.wav"
device_index = 2
audio = pyaudio.PyAudio()

print("----------------------record device list---------------------")
info = audio.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print("Input Device id ", i, " - ", audio.get_device_info_by_host_api_device_index(0, i).get('name'))

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

index = int(input())
print("recording via index "+str(index))

stream = audio.open(format=FORMAT, channels=CHANNELS,
                rate=RATE, input=True,input_device_index = index,
                frames_per_buffer=CHUNK)
print ("recording started")
Recordframes = []
 
for i in range(0, math.ceil(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    Recordframes.append(data)
print ("recording stopped")
 
stream.stop_stream()
stream.close()
audio.terminate()
 
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(Recordframes))
waveFile.close()
Pianola answered 27/7, 2018 at 9:6 Comment(1)
wav file is saved. However, there is no sound. How I can get sound from saved wav file?Westerman
A
0

Your code works in my environment: Win7 and Python3.4 - I get my voice recorded using my Laptops's microphone. Maybe your microphone's recording level is set too low. Or is it muted or disabled?

Aronoff answered 20/11, 2016 at 13:34 Comment(0)
D
0

Make sure your Microphone is connected to the computer. It can be identified using below code.

import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for microphone(device_index{0})".format(index, name))
Donoho answered 22/8, 2017 at 8:19 Comment(1)
This requires the SpeechRecognition third-party package from PyPI, which you can install by running: pip install SpeechRecognitionParulis

© 2022 - 2024 — McMap. All rights reserved.