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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
import pygtk
pygtk.require( '2.0' )
import gtk
import os
import random #TEMP
import sets
from common.Util.CSoundClient import new_csound_client
from common.Util.ScrolledToolbar import ScrolledToolbar
import common.Config as Config
from gettext import gettext as _
from sugar.graphics.palette import Palette, WidgetInvoker
from common.Util import ControlStream
from common.Util import InstrumentDB
from Jam import Block
class Picker( ScrolledToolbar ):
def __init__( self, owner, filter = None ):
ScrolledToolbar.__init__( self )
self.owner = owner
# take drawing setup from owner
self.gc = owner.gc
self.colors = owner.colors
self.blockMask = owner.blockMask
self.filter = filter
self.desktop = owner.getDesktop()
self.pickerBox = gtk.HBox()
self.set_viewport(self.pickerBox)
self.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] )
# spacers
self.pickerBox.pack_start( gtk.Label(" "), True, True )
self.pickerBox.pack_end( gtk.Label(" "), True, True )
self.show_all()
self.scroll = {}
self.scroll[filter] = 0
self.blocks = []
def addBlock( self, data, name, block ):
# tooltip
invoker = WidgetInvoker(block)
invoker._position_hint = WidgetInvoker.AT_CURSOR
invoker.set_palette(Palette(name))
block.add_events( gtk.gdk.BUTTON_PRESS_MASK
| gtk.gdk.BUTTON_RELEASE_MASK
| gtk.gdk.ENTER_NOTIFY_MASK
| gtk.gdk.LEAVE_NOTIFY_MASK
| gtk.gdk.POINTER_MOTION_MASK
| gtk.gdk.POINTER_MOTION_HINT_MASK )
block.connect( "button-press-event", self.on_button_press )
block.connect( "button-release-event", self.on_button_release )
block.connect( "motion-notify-event", self.on_motion_notify )
block.data = data
self.blocks.append( block )
if self._testAgainstFilter( block ):
self.pickerBox.pack_start( block, False, False, 3 )
block.show_all()
return block
def getFilter( self ):
return filter
def setFilter( self, filter ):
if filter == self.filter:
return
self.scroll[self.filter] = self.get_adjustment().get_value()
self.filter = filter
for block in self.pickerBox.get_children()[1:-1]: # outside children are spacers
self.pickerBox.remove( block )
for block in self.blocks:
if self._testAgainstFilter( block ):
self.pickerBox.pack_start( block, False, False, 3 )
if self.scroll.has_key( filter ):
self.get_adjustment().set_value( self.scroll[filter] )
else:
self.get_adjustment().set_value( 0 )
self.scroll[filter] = 0
def _testAgainstFilter( self, block ):
return True
def on_button_press( self, widget, event ):
pass
def on_button_release( self, widget, event ):
self.desktop.on_button_release( widget, event )
def on_motion_notify( self, widget, event ):
self.desktop.on_motion_notify( widget, event )
class Instrument( Picker ):
def __init__( self, owner, filter = ( "All" ) ):
Picker.__init__( self, owner, filter )
self.type = Instrument
self.instrumentDB = InstrumentDB.getRef()
all = []
lab = []
mic = []
for i in self.instrumentDB.getSet( "All" ):
if i.name.startswith('lab'):
lab.append(i)
elif i.name.startswith('mic'):
mic.append(i)
elif not i.kitStage and not i.kit:
all.append(i)
all += sorted(lab, key=lambda i: i.name)
all += sorted(mic, key=lambda i: i.name)
for inst in all:
self.addBlock( inst.instrumentId )
def addBlock( self, id ):
# match data structure of Block.Instrument
data = { "name": _(self.instrumentDB.instId[id].name),
"id": id }
win = gtk.gdk.get_default_root_window()
width = Block.Instrument.WIDTH
height = Block.Instrument.HEIGHT
pixmap = gtk.gdk.Pixmap( win, width, height )
self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) )
# draw bg
self.gc.foreground = self.colors["Picker_Bg"]
pixmap.draw_rectangle( self.gc, True, 0, 0, width, height )
self.gc.set_clip_mask( self.blockMask )
# draw border
self.gc.foreground = self.colors["Border_Inactive"]
self.gc.set_clip_origin( -Block.Instrument.MASK_START, 0 )
pixmap.draw_rectangle( self.gc, True, 0, 0, width, height )
# draw block
inst = self.owner.getInstrumentImage( data["id"] )
self.gc.set_clip_origin( -Block.Instrument.MASK_START, -height )
pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height )
image = gtk.Image()
image.set_from_pixmap( pixmap, None )
block = gtk.EventBox()
block.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] )
block.add( image )
Picker.addBlock( self, data, data["name"], block )
def _testAgainstFilter( self, block ):
if "All" in self.filter:
return True
if self.instrumentDB.getInstrument( block.data["id"] ).category in self.filter:
return True
return False
def on_button_press( self, widget, event ):
walloc = widget.get_allocation()
valloc = self.get_viewport_allocation()
loc = ( valloc.x + walloc.x + event.x, -1 )
block = self.desktop.addBlock( Block.Instrument, widget.data, loc, True )
self.desktop.activateInstrument( block )
class Drum( Picker ):
def __init__( self, owner, filter = None ):
Picker.__init__( self, owner, filter )
self.type = Drum
self.instrumentDB = InstrumentDB.getRef()
for inst in self.instrumentDB.getSet( "percussions" ):
if self.instrumentDB.instNamed[inst.name].kit:
self.addBlock( inst.instrumentId )
def addBlock( self, id ):
# match data structure of Block.Drum
data = { "name": _(self.instrumentDB.instId[id].name),
"id": id }
win = gtk.gdk.get_default_root_window()
width = Block.Drum.WIDTH
height = Block.Drum.HEIGHT
pixmap = gtk.gdk.Pixmap( win, width, height )
self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) )
# draw bg
self.gc.foreground = self.colors["Picker_Bg"]
pixmap.draw_rectangle( self.gc, True, 0, 0, width, height )
self.gc.set_clip_mask( self.blockMask )
# draw border
self.gc.foreground = self.colors["Border_Inactive"]
self.gc.set_clip_origin( -Block.Drum.MASK_START, 0 )
pixmap.draw_rectangle( self.gc, True, 0, 0, width, height )
# draw block
inst = self.owner.getInstrumentImage( data["id"] )
self.gc.set_clip_origin( -Block.Drum.MASK_START, -height )
pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height )
image = gtk.Image()
image.set_from_pixmap( pixmap, None )
block = gtk.EventBox()
block.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] )
block.add( image )
Picker.addBlock( self, data, data["name"], block )
def on_button_press( self, widget, event ):
walloc = widget.get_allocation()
valloc = self.get_viewport_allocation()
loc = ( valloc.x + walloc.x + event.x, -1 )
self.desktop.addBlock( Block.Drum, widget.data, loc, True )
class Loop( Picker ):
def __init__( self, owner, filter = None ):
Picker.__init__( self, owner, filter )
self.type = Loop
self.presetLoops = self._scanDirectory( Config.FILES_DIR+"/Loops" )
def _loadFile( self, fullpath, filename ):
if filename[-4:] != ".ttl":
if Config.DEBUG >= 3: print "WARNING: incorrect extension on loop file: " + filename
return -1
try:
oldPages = sets.Set( self.owner.noteDB.getTune() )
ifile = open( fullpath, 'r' )
ttt = ControlStream.TamTamTable ( self.owner.noteDB )
ttt.parseFile( ifile )
ifile.close()
curPages = sets.Set( self.owner.noteDB.getTune() )
newPages = curPages.difference( oldPages )
if len(newPages) != 1:
print "ERROR: bad loop file, contains more than one page (or none)"
return -1
id = newPages.pop() # new pageId
self.owner.noteDB.getPage( id ).setLocal( False ) # flag as a global page
self.addBlock( id, filename[:-4] )
return id
except OSError,e:
print 'ERROR: failed to open file %s for reading\n' % ofilename
return -1
def _scanDirectory( self, path ):
dirlist = os.listdir( path )
ids = []
for fpath in dirlist:
id = self._loadFile( path+"/"+fpath, fpath )
if id != -1: ids.append(id)
return ids
def addBlock( self, id, name ):
# match data structure of Block.Loop
data = { "name": _(name),
"id": id }
self.owner.updateLoopImage( data["id"] )
loop = self.owner.getLoopImage( data["id"] )
page = self.owner.noteDB.getPage( id )
win = gtk.gdk.get_default_root_window()
width = Block.Loop.WIDTH[page.beats]
height = Block.Loop.HEIGHT
pixmap = gtk.gdk.Pixmap( win, width, height )
self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) )
# draw bg
self.gc.foreground = self.colors["Picker_Bg"]
pixmap.draw_rectangle( self.gc, True, 0, 0, width, height )
self.gc.set_clip_mask( self.blockMask )
self.gc.foreground = self.owner.colors["Border_Inactive"]
#-- draw head -----------------------------------------
# draw border
self.gc.set_clip_origin( -Block.Loop.MASK_START, 0 )
pixmap.draw_rectangle( self.gc, True, 0, 0, Block.Loop.HEAD, height )
# draw block
self.gc.set_clip_origin( -Block.Loop.MASK_START, -height )
pixmap.draw_drawable( self.gc, loop, 0, 0, 0, 0, Block.Loop.HEAD, height )
#-- draw beats ----------------------------------------
beats = page.beats - 1 # last beat is drawn with the tail
curx = Block.Loop.HEAD
while beats > 3:
# draw border
self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, 0 )
pixmap.draw_rectangle( self.gc, True, curx, 0, Block.Loop.BEAT_MUL3, height )
# draw block
self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, -height )
pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.BEAT_MUL3, height )
curx += Block.Loop.BEAT_MUL3
beats -= 3
if beats:
w = Block.Loop.BEAT*beats
# draw border
self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, 0 )
pixmap.draw_rectangle( self.gc, True, curx, 0, w, height )
# draw block
self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, -height )
pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, w, height )
curx += w
#-- draw tail -----------------------------------------
# draw border
self.gc.set_clip_origin( curx-Block.Loop.MASK_TAIL, 0 )
pixmap.draw_rectangle( self.gc, True, curx, 0, Block.Loop.TAIL, height )
# draw block
self.gc.set_clip_origin( curx-Block.Loop.MASK_TAIL, -height )
pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.TAIL, height )
image = gtk.Image()
image.set_from_pixmap( pixmap, None )
block = gtk.EventBox()
block.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] )
block.add( image )
Picker.addBlock( self, data, data["name"], block )
def on_button_press( self, widget, event ):
walloc = widget.get_allocation()
valloc = self.get_viewport_allocation()
loc = ( valloc.x + walloc.x + event.x, -1 )
data = {}
for key in widget.data.keys():
data[key] = widget.data[key]
newid = self.owner.noteDB.duplicatePages( [ data["id"] ] )[data["id"]]
self.owner.updateLoopImage( newid )
data["id"] = newid
block = self.desktop.addBlock( Block.Loop, data, loc, True )
|