windows – Recording noises at night


You could use Python with PyAudio by adapting the accepted answer to this SO Question.

  • Python & PyAudio are both free, open source & cross platform so the following should work on most platforms.
  • I would suggest modifying the example code to start recording to a file named for the date and time when the sound level exceeds some limit and stop a second or longer after the last time that the sound level was exceeded.

Something like, (note that this is not production quality code):

import pyaudio
import wave
import audioop
import datetime

now = datetime.datetime.now()  # Get the time now
ENDTIME = now + datetime.timedelta(hours=8) # Record for 8 hours
OVERRUN = datetime.timedelta(seconds=5)  # How long to continue after last loud
THRESHOLD = 123  # Use some number determined experimentally

# The following are the pyAudio parameters from the original example
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

recording = False
outfile = None
last_over = datetime.datetime(0)
started_recording_at = None

while now < ENDTIME:  # Run till endtime
    data = stream.read(CHUNK)     # get the latest sound chunk
    now = datetime.datetime.now()  # Get the current time
    rms = audioop.rms(data, 2)    # here's where you calculate the volume

    if not recording and rms > THRESHOLD:  # Loud so start recording
        fname = now.strftime('%Y-%m-%d-%H-%M-%S.wav')  # Create the filename
        outfile = open(fname, 'wb')  # Open for binary write
        print("RMS = {}: Started recording to {}".format(rms, fname))
        recording = True
        started_recording_at = now

    if recording:  # We are recording
        outfile.write(data)  # Save the sound chunk
        if rms > THRESHOLD:  # Still loud?
            last_over = now  # Save time
        if now - last_over > OVERRUN:  # Quiet for long enough
            recording = False  # Stop recording
            outfile.close()   # Close the file
            print("Recording Length {}".format(now-started_recording_at))
# Tidy Up!
stream.stop_stream()
stream.close()
p.terminate()

If you are unfamiliar with python placing the code above in a text file called nightwatch.py then running with:

python nightwatch

Should do more or less what you asked for.



Source link

Related Posts

About The Author

Add Comment