Detect and record a sound with python
Posted
by Jean-Pierre
on Stack Overflow
See other posts from Stack Overflow
or by Jean-Pierre
Published on 2010-04-19T15:00:10Z
Indexed on
2010/04/19
15:03 UTC
Read the original article
Hit count: 496
I'm using this program to record a sound in python: import pyaudio import wave import sys
chunk = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, frames_per_buffer = chunk)
print "* recording" all = [] for i in range(0, RATE / chunk * RECORD_SECONDS): data = stream.read(chunk) all.append(data) print "* done recording"
stream.close() p.terminate()
write data to WAVE file
data = ''.join(all) wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(data) wf.close() 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?
© Stack Overflow or respective owner