Breaking News

Simple script to record sound from the microphone in Python



1 - Install "pyaudio" library : pip install pyaudio
2 - Copy paste the code
3 - Done, Just Run it

Code:

import pyaudio
import wave

# IDEA Developers
# @ideadevelopers

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5  # you can change the seconds
WAVE_OUTPUT_FILENAME = "filename.wav"

audio = pyaudio.PyAudio()

# start recording
stream = audio.open(
    format=FORMAT, channels=CHANNELS,
    rate=RATE, input=True, frames_per_buffer=CHUNK)

print("recording audio...")

frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("recording finished...")

# stop recording
stream.stop_stream()
stream.close()
audio.terminate()

# saving file 
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()

No comments