Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple script to record sound from the microphone, dependencies: easy_install pyaudio
import pyaudio
import wave
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.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 "finished recording"
# stop Recording
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(frames))
waveFile.close()
@LatinAmericanProgramer

This comment has been minimized.

LatinAmericanProgramer commented Dec 21, 2015

Good Day Mabdrabo, thank you for share your code. I want to ask you if you know how can I plot & hear what you have recorded with your code?

Thanks again for sharing

@rachit1994

This comment has been minimized.

rachit1994 commented Jan 29, 2016

hi i am facing following problem on running your code please help:

data = stream.read(CHUNK)
File "D:\Python27\lib\site-packages\pyaudio.py", line 608, in read
return pa.read_stream(self ._stream, num_frames, exception_on_overflow)
TypeError: function takes exactly 2 arguments (3 given)

@ashishmd

This comment has been minimized.

ashishmd commented Mar 8, 2016

Can you please let me know what the CHUNK variable is.

I am new to sound processing.

@nmnhut2010

This comment has been minimized.

nmnhut2010 commented Apr 15, 2016

Hi ashishmd,

CHUNK is how many samples in a frame that stream will read.

BRs

@MihailRis

This comment has been minimized.

MihailRis commented Jul 8, 2016

Great!!!

@Akshay0724

This comment has been minimized.

Akshay0724 commented Nov 18, 2016

Hey I am getting error running this code....
[Errno -9996] Invalid input device (no default output device)

@rdhara

This comment has been minimized.

rdhara commented Nov 28, 2016

Hi Askshy,
I was able to solve a similar issue by uninstalling pyaudio and following the instructions here:
http://stackoverflow.com/questions/33513522/when-installing-pyaudio-pip-cannot-find-portaudio-h-in-usr-local-include

@xe1gyq

This comment has been minimized.

xe1gyq commented Feb 8, 2017

Hi @rachit1994, everyone,

"TypeError: function takes exactly 2 arguments (3 given)" was due to a mismatch between my portaudio and pyaudio versions.

Executive summary

  1. Uninstall pyaudio
  2. Install latest version of portaudio
  3. Install again pyaudio

For the avid reader

root@edison:~# pip uninstall pyaudio

Latest from its webpage

root@edison:~# wget http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz
--2017-02-05 19:04:41-- http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz
Resolving www.portaudio.com... 107.22.210.58
Connecting to www.portaudio.com|107.22.210.58|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1450572 (1.4M) [application/x-gzip]
Saving to: 'pa_stable_v190600_20161030.tgz'

100%[======================================>] 1,450,572 701KB/s in 2.0s

2017-02-05 19:04:43 (701 KB/s) - 'pa_stable_v190600_20161030.tgz' saved [1450572/1450572]

root@edison:~#
root@edison:~# tar xvf pa_stable_v190600_20161030.tgz
root@edison:~# cd portaudio/
root@edison:~/portaudio# ls
CMakeLists.txt bindings doc pablio
Doxyfile build examples portaudio-2.0.pc.in
Doxyfile.developer clear_gitrevision.sh fixdir.bat qa
LICENSE.txt cmake_support fixfile.bat src
Makefile.in config.guess include test
README.configure.txt config.sub index.html testcvs
README.txt configure install-sh update_gitrevision.sh
SConstruct configure.in ltmain.sh
aclocal.m4 depcomp missing
root@edison:~/portaudio#
root@edison:~/portaudio# ./configure
root@edison:~/portaudio# make
root@edison:~/portaudio# make install
root@edison:~# pip install pyaudio
@maelfosso

This comment has been minimized.

maelfosso commented Feb 21, 2017

Hi,
How could I use this code to send sound to the server as It is recorded that is in real time instead of save it before send the whole file ?

@HarrisMx

This comment has been minimized.

HarrisMx commented Oct 17, 2017

Hi, thanks for the code, I cant get the output file on the same folder where the script is, can you please tell me how do i get it

@danidz

This comment has been minimized.

danidz commented Oct 17, 2017

Hi,thanks for code.insterd of saving for five seconds how to code to print the frequency continuously like q array.like what the guitar tuners do

@Dinesh2903

This comment has been minimized.

Dinesh2903 commented Nov 26, 2017

Hai,Got this error

File "build/bdist.macosx-10.6-intel/egg/pyaudio.py", line 750, in open
File "build/bdist.macosx-10.6-intel/egg/pyaudio.py", line 441, in init
IOError: [Errno -9999] Unanticipated host error

@marijnruyts

This comment has been minimized.

marijnruyts commented Nov 29, 2017

Whenever i try to run the code, i get the following error:

NameError: name 'join' is not defined

@nl22

This comment has been minimized.

nl22 commented Dec 22, 2017

Is there a way choose which device your listening to? My preference is a sound processing board in my usb port, not the computer's mic.

@riadibadulla

This comment has been minimized.

riadibadulla commented Dec 28, 2017

It says AttributeError: module 'wave' has no attribute 'open'. How can I fix it, please?

@sharpriy

This comment has been minimized.

sharpriy commented Jun 26, 2018

Hi After running this script, i get following error- (Running it on Platform -Ubuntu 16.0)

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
recording...
finished recording

@morenol

This comment has been minimized.

morenol commented Jul 1, 2018

Hi, I got this error when I tried to record audio

Traceback (most recent call last):
  File "listen.py", line 23, in <module>
    data = stream.read(CHUNKSIZE)
  File "/.../python/venv/lib/python3.5/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
OSError: [Errno -9981] Input overflowed

Why it is happening?
How can I fix that?

@s1st3r

This comment has been minimized.

s1st3r commented Nov 13, 2018

Worked well on python 2.7, very simple code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment