Created
December 25, 2010 19:54
-
-
Save nmandery/755025 to your computer and use it in GitHub Desktop.
camera_audio_trigger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import alsaaudio, time, audioop | |
import sys | |
import piggyphoto | |
# init camera | |
try: | |
C = piggyphoto.camera() | |
print "Found the following camera:" | |
print C.abilities | |
except piggyphoto.libgphoto2error: | |
print "Found no camera" | |
sys.exit(1) | |
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK) | |
inp.setchannels(1) | |
inp.setrate(8000) | |
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) | |
inp.setperiodsize(160) | |
loudness_threshold = 1000 | |
# wait at least for one second before triggering the | |
#next photo | |
after_photo_timeout = 1.0 | |
t_last_photo = 0 | |
print "waiting for sounds - the camera reaction might" | |
print "be a bit slow on the first shot" | |
while True: | |
# Read data from device | |
l,data = inp.read() | |
if l: | |
# Return the maximum of the absolute value of all samples in a fragment. | |
try: | |
max_v = audioop.max(data, 2) | |
if max_v > loudness_threshold: | |
c_time = time.time() | |
if (c_time - t_last_photo) > after_photo_timeout: | |
print "\nTaking photo (sound level @ %d)" % max_v | |
C.capture_image('snap_%s.jpg' % str(c_time)) | |
t_last_photo = time.time() | |
else: | |
sys.stdout.write(".") | |
print max_v | |
except audioop.error, e: | |
if e.message != "not a whole number of frames": | |
raise e | |
time.sleep(.0001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment