Create a gist now

Instantly share code, notes, and snippets.

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
RATE = 44100
CHUNK = 1024
audio = pyaudio.PyAudio()
# start Recording
stream =, channels=CHANNELS,
rate=RATE, input=True,
print "recording..."
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data =
print "finished recording"
# stop Recording
waveFile =, 'wb')

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

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

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

ashishmd commented Mar 8, 2016

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

I am new to sound processing.

Hi ashishmd,

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



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

rdhara commented Nov 28, 2016

Hi Askshy,
I was able to solve a similar issue by uninstalling pyaudio and following the instructions here:

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
--2017-02-05 19:04:41--
Connecting to||: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:~# tar xvf pa_stable_v190600_20161030.tgz
root@edison:~# cd portaudio/
root@edison:~/portaudio# ls
CMakeLists.txt bindings doc pablio
Doxyfile build examples
Doxyfile.developer fixdir.bat qa
LICENSE.txt cmake_support fixfile.bat src config.guess include test
README.configure.txt config.sub index.html testcvs
README.txt configure install-sh
aclocal.m4 depcomp missing
root@edison:~/portaudio# ./configure
root@edison:~/portaudio# make
root@edison:~/portaudio# make install
root@edison:~# pip install pyaudio

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 ?

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 commented Oct 17, 2017

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

Hai,Got this error

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

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

NameError: name 'join' is not defined

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.

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

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