#!/usr/bin/env python # -*- coding: utf-8 -*- # #----------------------------------------------------------------------- #sound_sculptures.py v1.1, Copyright Bjoern Olausson #----------------------------------------------------------------------- #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation; either version 3 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #To view the license visit #http://www.gnu.org/licenses/gpl.html #or write to #Free Software Foundation, Inc., 51 Franklin St, Fifth Floor #Boston, MA 02110-1301 USA #----------------------------------------------------------------------- #----------------------------------------------------------------------- # #For the purpose of this script visit: #http://www.dslr-forum.de/showthread.php?t=802496 import Tkinter import tkSnack import time import sys import subprocess import os import shutil from multiprocessing import Process from optparse import OptionParser, OptionGroup opa = OptionParser() opa.add_option("-f", "--freq", action="store", dest="freq", metavar="FLOAT", default="220", help="Frequenzy in Hz") opa.add_option("-s", "--silent", action="store_true", dest="freq_silent", default=False, help="No Sound. Use this first if camera is in standby.") opa.add_option("-t", "--freq_duration", action="store", dest="freq_duration", metavar="INT", default="4000", help="Time to play the frequenzy in some arbitrary units. \ 18600 equals ~ 1 Second") opa.add_option("-u", "--freq_delay", action="store", dest="freq_delay", metavar="FLOAT", default="1.25", help="Delay before playing the frequenzy. The lower this value the \ earlier the picture will be taken. This value heavily depends on \ gphoto2 initialization time and the camera response") opa.add_option("-p", "--pulse", action="store", dest="pulse", metavar="INT", default="1", help="Pulse the frequenzy N times") opa.add_option("-d", "--pulse_delay", action="store", dest="pulse_delay", metavar="FLOAT", default="0.1", help="Pulse delay") opa.add_option("-c", "--capture", action="store_true", dest="capture", default=False, help="Capture Image") opa.add_option("-l", "--capture_delay", action="store", dest="capture_delay", metavar="FLOAT", default="0", help="Delay before taking the image") opa.add_option("-r", "--repeat", action="store", dest="repeat", metavar="INT", default="1", help="Repeat the process N times") opa.add_option("-y", "--repeat_delay", action="store", dest="repeat_delay", metavar="FLOAT", default="1", help="Repeat the process N times") (options, args) = opa.parse_args() freq = float(options.freq) freq_silent = options.freq_silent freq_duration = int(options.freq_duration) freq_delay = float(options.freq_delay) pulse = int(options.pulse) pulse_delay = float(options.pulse_delay) capture = options.capture capture_delay = float(options.capture_delay) repeat = int(options.repeat) repeat_delay = float(options.repeat_delay) storage = "sound_sculptures" root = Tkinter.Tk() tkSnack.initializeSnack(root) tkSnack.audio.play_gain(100) snd = tkSnack.Sound() try: os.mkdir(storage) except Exception, e: pass def PlayFreq(freq, freq_duration, freq_delay, pulse, pulse_delay): print "\tInitializing Sound" print "\tDelaying sound for %s" % (freq_delay) time.sleep(freq_delay) filt = tkSnack.Filter('generator', freq, 30000, 0.0, 'sine', freq_duration) snd.stop() for i in range(pulse): sound_start = time.time() if not freq_silent: snd.play(filter=filt, blocking=1) sound_end = time.time() - sound_start print "\tSound time: %fs" % (sound_end) snd.stop() else: print "No sound played!" time.sleep(pulse_delay) def CaptureImg(capture, capture_delay): print "Initializing Camera" print "Capture Delay: %s" % (capture_delay) time.sleep(capture_delay) capture_start = time.time() if not capture: print "No picture taken. Use -c to take a picture" time.sleep(2) else: try: cap = subprocess.Popen(["gphoto2", "--quiet", "--capture-image-and-download"], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout, stderr = cap.communicate() ct = time.time() try: shutil.move("capt0000.jpg", storage) os.rename(storage + "/capt0000.jpg", storage + "/capt%s.jpg" % (ct)) except Exception, e: pass try: shutil.move("capt0000.cr2", storage) os.rename(storage + "/capt0000.cr2", storage + "/capt%s.cr2" % (ct)) except Exception, e: pass except Exception, e: print "Failed to capture image:", e print stdout print stderr capture_end = time.time() - capture_start print "Capture time: %fs\n" % (capture_end) #for i in range(0,3): #count = 3 - i #sys.stdout.write(str(count)+" ") #sys.stdout.flush() #time.sleep(1) #print "GO!\n" for i in range(1, repeat + 1): print "Try %s of %s" % (i, repeat) pc = Process(target=CaptureImg, args=(capture, capture_delay,)) pf = Process(target=PlayFreq, args=(freq, freq_duration, freq_delay, pulse, pulse_delay )) pc.start() pf.start() pc.join() pf.join() time.sleep(repeat_delay) print "" root.withdraw()