Bjoern Olausson

sound_sculptures.py
Monday, 03 January 2011 04:45

sound_sculptures.py Version:1.1

For the purpose of this tool visit:

http://www.dslr-forum.de/showthread.php?t=802496

and

http://gallery.olausson.de/Sound-Sculptures

 GNU/GPL    2011-01-03   English   Linux  5.82 KB  249

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/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()
 
Last Updated ( Tuesday, 13 December 2011 15:54 )
 

Add comment


Security code
Refresh

Comments

Qt Ambassador

Qt Ambassador

www. is deprecated

Banner

Play OGG

Banner

Gixen

web2sms

Banner