Create a gist now

Instantly share code, notes, and snippets.

Embed
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.

Show comment
Hide comment
@LatinAmericanProgramer

LatinAmericanProgramer 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

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.

Show comment
Hide comment
@rachit1994

rachit1994 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)

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.

Show comment
Hide comment
@ashishmd

ashishmd Mar 8, 2016

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

I am new to sound processing.

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.

Show comment
Hide comment
@nmnhut2010

nmnhut2010 Apr 15, 2016

Hi ashishmd,

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

BRs

Hi ashishmd,

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

BRs

@MihailRis

This comment has been minimized.

Show comment
Hide comment
@MihailRis

MihailRis Jul 8, 2016

Great!!!

Great!!!

@Akshay0724

This comment has been minimized.

Show comment
Hide comment
@Akshay0724

Akshay0724 Nov 18, 2016

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

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

@rdhara

This comment has been minimized.

Show comment
Hide comment
@rdhara

rdhara 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

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.

Show comment
Hide comment
@xe1gyq

xe1gyq 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

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.

Show comment
Hide comment
@maelfosso

maelfosso 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 ?

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.

Show comment
Hide comment
@HarrisMx

HarrisMx 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

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.

Show comment
Hide comment
@danidz

danidz 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

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.

Show comment
Hide comment
@Dinesh2903

Dinesh2903 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

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.

Show comment
Hide comment
@marijnruyts

marijnruyts Nov 29, 2017

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

NameError: name 'join' is not defined

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

NameError: name 'join' is not defined

@nl22

This comment has been minimized.

Show comment
Hide comment
@nl22

nl22 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.

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.

Show comment
Hide comment
@riadibadulla

riadibadulla Dec 28, 2017

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

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