/
pystol.py
executable file
·174 lines (121 loc) · 5.03 KB
/
pystol.py
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
#!/usr/bin/env python
import sys
import math
import impulse
from soundtolight import SoundToLight
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
import gobject
peak_heights = [ 0 for i in range( 256 ) ]
peak_acceleration = [ 0.0 for i in range( 256 ) ]
bar_color = ( 0.0, 0.6, 1.0, 0.8 )
peak_color = ( 1.0, 0.0, 0.0, 0.8 )
n_cols = 10
col_width = 16
col_spacing = 1
n_rows = 50
row_height = 5
row_spacing = 1
class PystolGTK:
"""This is an Hello World GTK application"""
def __init__(self):
#Set the Glade file
self.builder = gtk.Builder()
self.gladefile = "./glade/pystol.glade"
self.builder.add_from_file(self.gladefile)
#builder.connect_signals(anobject)
#builder.get_object(name)
self.window = self.builder.get_object("Pystol")
self.builder.connect_signals(self)
self.spectrumArea = self.builder.get_object("analyzer")
gtk.DrawingArea.__init__(self.spectrumArea)
self.spectrumArea.connect("expose-event", self.expose)
self.sound2light = SoundToLight(universe=1, fade=True, fadeStep=5, scale=True)
self.voicecontrol = None
self.fadecontrol = None
self.timer = gobject.timeout_add( 33, self.update )
def tab_switch_callback(self, notebook, page, page_num):
if page_num == 2:
if not self.voicecontrol:
from voice import VoiceControl
self.voicecontrol = VoiceControl(self.builder.get_object("voice_control"))
def soundcontrol_toggle_callback(self, button):
self.sound2light.active = button.get_active()
def fade_toggle_callback(self, button):
self.sound2light.manager.fade = button.get_active()
def scale_toggle_callback(self, button):
self.sound2light.manager.scale = button.get_active()
def universe_changed_callback(self, spinbutton):
self.sound2light.universe = spinbutton.get_value_as_int()
def fadestep_changed_callback(self, fader):
self.sound2light.manager.fadeStep = int(fader.get_value())
def update (self):
self.audio_sample_array = impulse.getSnapshot(True)
self.redraw_canvas()
if self.sound2light.active:
self.sound2light.update(self.audio_sample_array)
return True # keep running this event
def redraw_canvas (self):
if self.window.window:
alloc = self.spectrumArea.get_allocation()
#rect = gdk.Rectangle(alloc.x, alloc.y, alloc.width, alloc.height)
#self.window.invalidate_rect(rect, True)
#self.spectrumArea.queue_draw_area(alloc.x, alloc.y, alloc.width, alloc.height)
self.spectrumArea.queue_draw_area(0, alloc.y, alloc.width, alloc.height)
self.window.window.process_updates(True)
def expose(self, widget, event):
self.context = self.spectrumArea.window.cairo_create()
# set a clip region for the expose event
self.context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
#print "Expose:", event.area.x, event.area.y, event.area.width, event.area.height
self.context.clip()
self.draw(self.context)
return False
def draw(self, cr):
rect = self.spectrumArea.get_allocation()
#print "Print:", rect.x, rect.y, rect.width, rect.height
freq = len( self.audio_sample_array ) / n_cols
col_width = rect.width/(n_cols +2 )
row_height = rect.height / (n_rows)
#print col_width
for i in range( 0, len( self.audio_sample_array ), freq ):
col = i / freq
rows = int( self.audio_sample_array[ i ] * n_rows )
cr.set_source_rgba( bar_color[ 0 ], bar_color[ 1 ], bar_color[ 2 ], bar_color[ 3 ] )
if rows > peak_heights[ i ]:
peak_heights[ i ] = rows
peak_acceleration[ i ] = 0.0
else:
peak_acceleration[ i ] += .1
peak_heights[ i ] -= peak_acceleration[ i ]
if peak_heights[ i ] < 0:
peak_heights[ i ] = 0
for row in range( 0, rows ):
cr.rectangle(
col * ( col_width + col_spacing ),
rect.height - row * ( row_height + row_spacing ),
col_width, -row_height
)
cr.fill( )
cr.set_source_rgba( peak_color[ 0 ], peak_color[ 1 ], peak_color[ 2 ], peak_color[ 3 ] )
cr.rectangle(
col * ( col_width + col_spacing ),
rect.height - peak_heights[ i ] * ( row_height + row_spacing ),
col_width, -row_height
)
cr.fill( )
cr.fill( )
cr.stroke( )
if __name__ == "__main__":
pstl = PystolGTK()
pstl.window.show()
gtk.main()