Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect and record a sound with python

I'm using this program to record a sound in python:

Detect & Record Audio in Python

I want to change the program to start recording when sound is detected by the sound card input. Probably should compare the input sound level in chunk, but how do this?

like image 945
Jean-Pierre Avatar asked Apr 19 '10 15:04

Jean-Pierre


People also ask

How do I record audio in Python?

Recording AudioThe python-sounddevice and pyaudio libraries provide ways to record audio with Python. python-sounddevice records to NumPy arrays and pyaudio records to bytes objects. Both of these can be stored as WAV files using the scipy and wave libraries, respectively.

Can you get Python to play a sound?

You can play sound files with the pydub module. It's available in the pypi repository (install with pip). This module can use PyAudio and ffmpeg underneath.

Is there a sound module in Python?

The simpleaudio module is a package of Python 3 that can play audio sounds. This module is mainly designed to play wav files and NumPy arrays. You will need to install the package before using this module. This sound package directly depends on another package called libasound2-dev.


2 Answers

You could try something like this:

based on this question/answer

# this is the threshold that determines whether or not sound is detected
THRESHOLD = 0

#open your audio stream    

# wait until the sound data breaks some level threshold
while True:
    data = stream.read(chunk)
    # check level against threshold, you'll have to write getLevel()
    if getLevel(data) > THRESHOLD:
        break

# record for however long you want
# close the stream

You'll probably want to play with your chunk size and threshold values until you get the desired behavior.

Edit:

You can use the built-in audioop package to find the root-mean-square (rms) of a sample, which is generally how you would get the level.

import audioop
import pyaudio

chunk = 1024

p = pyaudio.PyAudio()

stream = p.open(format=pyaudio.paInt16,
                channels=1,
                rate=44100,
                input=True,
                frames_per_buffer=chunk)

data = stream.read(chunk)

rms = audioop.rms(data, 2)  #width=2 for format=paInt16
like image 94
tgray Avatar answered Sep 22 '22 02:09

tgray


Detecting when there isn't silence is usually done by using the root mean square(RMS) of some chunk of the sound and comparing it with some threshold value that you set (the value will depend on how sensitive your mic is and other things so you'll have to adjust it). Also, depending on how quickly you want the mic to detect sound to be recorded, you might want to lower the chunk size, or compute the RMS for overlapping chunks of data.

like image 26
Justin Peel Avatar answered Sep 20 '22 02:09

Justin Peel