Skip to content

Instantly share code, notes, and snippets.

@mabdrabo
Created January 28, 2014 23:05
Show Gist options
  • Save mabdrabo/8678538 to your computer and use it in GitHub Desktop.
Save mabdrabo/8678538 to your computer and use it in GitHub Desktop.
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()
@PiepsC
Copy link

PiepsC commented Mar 8, 2019

Excellent, thanks

@ecambronero
Copy link

My code executes however the file is empty when I try to open it after the program runs

@progware
Copy link

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

I am getting the same error. Did you solve it??

Hi, just type:
jack_control start

@progware
Copy link

progware commented Mar 15, 2019

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?

same here, did you find the fix ?

I had same issue on RPi3b+ and fixed it with picking up input device:

stream = self.audio.open(format=FORMAT, input_device_index = 2,
                                 channels=CHANNELS,
                                 rate=RATE,
                                 input=True,
                                 frames_per_buffer=CHUNK)

@kanik9
Copy link

kanik9 commented Jun 25, 2019

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

in this code this part can not execute
file is created but output is not come

Output is:
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(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
recording...
finished recording

@ajfarkas
Copy link

ajfarkas commented Aug 9, 2019

My code executes however the file is empty when I try to open it after the program runs

I had this same problem on Mac. Some new privacy settings block application access to the microphone by default. I had to specifically enable access to Terminal, where I was running this script.

https://support.apple.com/en-us/HT209175

@davekimble2
Copy link

You will notice that the open() on line 14 does not have a device index parameter, so how does it know what device to use? In my case the answer is via PulseAudio settings, and probably those with Jack likewise. My microphones are combined with a webcam or a headset. ALSA sees them as sub-devices. PyAudio doesn't recognise sub-devices and always uses sub-device #0, which doesn't read from the microphone, it reads from the webcam and gets nothing.

@salilsaxena
Copy link

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
I don't know if this has been answered or not but anyways:
Yes, LatinAmericanProgramer you cant certainly plot it, sample code is shown below:
But before running it make sure to install librosa,numpy and matplotlib.pyplot.
import librosa as lb
import matplotlib.pyplot as plt
import numpy as np
a = r"C:\USers\Salil Saxena\Desktop\file.wav"
audio,freq = lb.load(a)
time = np.arange(0,len(audio))/freq
plt.plot(time,audio)
plt.savefig("file_name.png") //this is used to save the current plot at os.getcwd()
plt.show()

You can also save the plot by just running this simple line:

@jdelange
Copy link

jdelange commented Feb 1, 2020

Works on Win 10, Visual Studio 2019, Anaconda, Python 3.7.x

Needed to make following changes:

print("recording...")
print("finished recording")

Using CHANNELS = 2

Gave error in:
Line 16 frames_per_buffer=CHUNK

Error:

Exception Unhandled [Errno -9998] Invalid number of channels

Changed to CHANNELS = 1

Then it works.

@Raman99444
Copy link

I have received the "Segmentation fault (core dumped)" error while running the program. How I can solve this problem.
image

Thanks in advance.

@soni30
Copy link

soni30 commented Mar 18, 2020

this code records both system audio and microphone audio...what if I only want to record the system audio/output.

@Shinoy-12
Copy link

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

I'm facing the same problem can you please tell me where the file will be saved?

@muthiyanbhushan
Copy link

Hi,

I am having a following error when I run the file with Python3.

Traceback (most recent call last):
File "sound_recorder.py", line 16, in
frames_per_buffer=CHUNK)
File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line 441, in init
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels

Any suggestions?
Thank you.

@YveOms
Copy link

YveOms commented Apr 16, 2020

Hi,

I am having a following error when I run the file with Python3.

Traceback (most recent call last):
File "sound_recorder.py", line 16, in
frames_per_buffer=CHUNK)
File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line 441, in init
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels

Any suggestions?
Thank you.

send the entire code in order to help you

@jdelange
Copy link

jdelange commented Apr 16, 2020 via email

@creboy
Copy link

creboy commented Apr 22, 2020

Traceback (most recent call last):
File "a.py", line 16, in
frames_per_buffer=CHUNK)
File "/usr/lib/python2.7/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/pyaudio.py", line 441, in init
self._stream = pa.open(**arguments)
IOError: [Errno -9996] Invalid input device (no default output devicce)

archlinux on termux,python2.7. struggle by this for a long time. how to solve it ?

@kongkip
Copy link

kongkip commented May 19, 2020

How do you read this file after writing?

@el07694
Copy link

el07694 commented Jun 2, 2020

ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(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 -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
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 -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
python3.6: src/hostapi/alsa/pa_linux_alsa.c:3636: PaAlsaStreamComponent_BeginPolling: Assertion `ret == self->nfds' failed.
Aborted (core dumped)


I may not have the easy_install. Is that the problem?

Thanks in advance,
Chris Pappas

@shilan
Copy link

shilan commented Jun 10, 2020

Hi, it just worked like a charm. :) I am new to sound features. If I want to record a wave file as described below:

Each utterance is stored as a one-second (or less)
WAVE format file, with the sample data encoded as
linear 16-bit single-channel PCM values, at a 16 KHz
rate.

Should I change the Constants like:

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024
RECORD_SECONDS = 1
WAVE_OUTPUT_FILENAME = "file.wav"

how should I reflect the 16-bit?

@TheEccentricDuck
Copy link

Thank yo for yo code, ma man (or woman).

@adfadasdf-cymklajkjsk
Copy link

Sorry but the code won't work

@SpirosArk
Copy link

Sorry but the code won't work

What's the error?

@TheEccentricDuck
Copy link

Sorry but the code won't work

I think I know the problem, you may have a python3 interpreter while this code is written in python2. You need to delete the spaces after any print and turn it into print(...)

@bg172
Copy link

bg172 commented May 29, 2021

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?

same here, did you find the fix ?

I had same issue on RPi3b+ and fixed it with picking up input device:

stream = self.audio.open(format=FORMAT, input_device_index = 2,
                                 channels=CHANNELS,
                                 rate=RATE,
                                 input=True,    # Save the recorded data in a .wav format
                             frames_per_buffer=CHUNK)

I achieved a much better stability by setting CHUNK to an integer fraction of RATE, e.g. CHUNK = int(RATE/10), otherwise still had same exception mostly already in 10s to 80s after starting the loop with stream.read(chunk). And anyway for such data sources it is a good approach to use "try" and be ready for exceptions and know how much gets lost:)

@sinanaybar
Copy link

Hi,thanks good

@deluxerootit
Copy link

How can I add timestamp on the recorded audio?

I mean like putting current time on each frame in opencv:
cv2.putText(frame, str(datetime.now()), (20, 40), font, 2, (255, 255, 255), 2, cv2.LINE_AA)

@el07694
Copy link

el07694 commented Jun 24, 2022

You can use pydub module for this i think.
For example

from pydub import AudioSegment
...
...
...
    data = stream.read(CHUNK)
    slice = AudioSegment(data, sample_width=???, frame_rate=???, channels=???)
    slice_len = len(slice) #slice_len is the duration of data frame in milliseconds

@sujantkumarkv
Copy link

hey all, i know this is too old a gist, but i came here from nvidia forum. Any help is appreciated.

This script is assuming i physically have the jetson right and using its microphone? because i'm connected to it via ssh on my macbook & physically very far. So, i guess I somehow need to send the audio packets/bytes to it?

and does pyaudio help with that? anyone has clues how to proceed? thanks :)

cc @mabdrabo @el07694 @deluxerootit @sinanaybar @bg172 @TheEccentricDuck @shilan @kongkip @YveOms @creboy @jdelange @muthiyanbhushan @Raman99444 @soni30 @Shinoy-12 @salilsaxena @davekimble2 @ajfarkas @kanik9 @progware @ecambronero @ClaudiaMarchPiris @PiepsC @Skykiller664 @pomarec @s1st3r @morenol @sharpriy @riadibadulla @marijnruyts @Dinesh2903 @danidz @HarrisMx @maelfosso @xe1gyq @rdhara @MihailRis @Akshay0724 @nmnhut2010 @ashishmd @rachit1994 @kevinmgamboa

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