Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TamTam.py
blob: 3119f288ce52611c6e12e89c3bbbe19d4095f99e (plain)
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

import signal , time , sys , os, shutil
import pygtk
pygtk.require( '2.0' )
import gtk

import gobject
import time

import Config
from   Util.CSoundClient import new_csound_client
from   Util.Profiler import TP

from   Util.InstrumentPanel import InstrumentPanel
from   miniTamTam.miniTamTamMain import miniTamTamMain
from   Jam.JamMain import JamMain
from   Edit.MainWindow import MainWindow
from   Welcome import Welcome
from   SynthLab.SynthLabWindow import SynthLabWindow
from   Util.Trackpad import Trackpad
from   gettext import gettext as _
#from   Util.KeyboardWindow import KeyboardWindow
import commands

if __name__ != '__main__':
    try:
        from sugar.activity.activity import Activity
        from sugar.activity import activity
        FAKE_ACTIVITY = False
        if Config.DEBUG: print 'using sugar Activity'
    except ImportError:
        from FActivity import FakeActivity as Activity
        FAKE_ACTIVITY = True
        if Config.DEBUG: print 'using fake activity'
else:
        from FActivity import FakeActivity as Activity
        if Config.DEBUG: print 'using fake activity'


class TamTam(Activity):
    # TamTam is the topmost container in the TamTam application
    # At all times it has one child, which may be one of
    # - the welcome screen
    # - the mini-tamtam
    # - the synth lab
    # - edit mode

    def __init__(self, handle, mode='welcome'):
        Activity.__init__(self, handle)
        self.ensure_dirs()

        color = gtk.gdk.color_parse(Config.WS_BCK_COLOR)
        self.modify_bg(gtk.STATE_NORMAL, color)

        self.set_title('TamTam')
        self.set_resizable(False)

        self.trackpad = Trackpad( self )
        #self.keyboardWindow = KeyboardWindow(size = 8, popup = True)
        #self.keyboardWindow.color_piano()

        self.preloadTimeout = None

        self.focusInHandler = self.connect('focus_in_event',self.onFocusIn)
        self.focusOutHandler = self.connect('focus_out_event',self.onFocusOut)
        self.connect('notify::active', self.onActive)
        self.connect('destroy', self.onDestroy)
        self.connect( "key-press-event", self.onKeyPress )
        self.connect( "key-release-event", self.onKeyRelease )
        #self.connect( "key-press-event", self.keyboardWindow.handle_keypress)
        #self.connect( "key-release-event", self.keyboardWindow.handle_keyrelease)
        #self.connect( "button-press-event", self.keyboardWindow.handle_mousePress)
        #self.connect( "button-release-event", self.keyboardWindow.handle_mouseRelease)

        self.mode = None
        self.modeList = {}

        self.instrumentPanel = InstrumentPanel( force_load = False )
        self.preloadList = [ self.instrumentPanel ]

        #load the sugar toolbar
        self.toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(self.toolbox)

        self.activity_toolbar = self.toolbox.get_activity_toolbar()
        self.activity_toolbar.share.hide()
        self.activity_toolbar.keep.hide()

        self.toolbox.show()

        if self._shared_activity: # if we're joining a shared activity force mini
            self.set_mode("mini")
        else:
            self.set_mode(mode)

    def onPreloadTimeout( self ):
        if Config.DEBUG > 4: print "TamTam::onPreloadTimeout", self.preloadList

        t = time.time()
        if self.preloadList[0].load( t + 0.100 ): # finished preloading this object
            self.preloadList.pop(0)
            if not len(self.preloadList):
                if Config.DEBUG > 1: print "TamTam::finished preloading", time.time() - t
                self.preloadTimeout = False
                return False # finished preloading everything

        if Config.DEBUG > 4: print "TamTam::preload returned after", time.time() - t

        return True

    def doNothing(): #a callback function to appease SynthLab
        pass
    
    def set_mode(self, mode, arg = None):
        if Config.DEBUG: print 'DEBUG: TamTam::set_mode from', self.mode, 'to', mode

        if self.mode != None:
            self.modeList[ self.mode ].onDeactivate()
            if FAKE_ACTIVITY:
                self.remove( self.modeList[ self.mode ] )

        self.mode = None
        self.trackpad.setContext(mode)

        if mode == 'welcome':
            if not (mode in self.modeList):
                self.modeList[mode] = Welcome(self, self.set_mode)
            self.mode = mode
            if len( self.preloadList ):
                self.preloadTimeout = gobject.timeout_add( 300, self.onPreloadTimeout )
        elif self.preloadTimeout:
            gobject.source_remove( self.preloadTimeout )
            self.predrawTimeout = False

        if mode == 'jam':
            if not (mode in self.modeList):
                self.modeList[mode] = JamMain(self, self.set_mode)
            self.mode = mode

        if mode == 'mini':
            if not (mode in self.modeList):
                self.metadata['title'] = 'TamTam Mini'
                self.modeList[mode] = miniTamTamMain(self, self.set_mode)
            else:
                self.modeList[mode].regenerate()
            if self.instrumentPanel in self.preloadList:
                self.instrumentPanel.load() # finish loading
            self.modeList[mode].setInstrumentPanel( self.instrumentPanel )
            self.mode = mode

        if mode == 'edit':
            if not (mode in self.modeList):
                self.metadata['title'] = 'TamTam Edit'
                self.modeList[mode] = MainWindow(self, self.set_mode)
            if self.instrumentPanel in self.preloadList:
                self.instrumentPanel.load() # finish loading
            self.modeList[mode].setInstrumentPanel( self.instrumentPanel )
            self.mode = mode

        if mode == 'synth':
            if not (mode in self.modeList):
                self.metadata['title'] = 'TamTam SynthLab'
                self.modeList[mode] = SynthLabWindow(self, self.set_mode, None)
            self.mode = mode

        if self.mode == None:
            print 'DEBUG: TamTam::set_mode invalid mode:', mode
        else:
            try: # activity mode
                self.set_canvas( self.modeList[ self.mode ] )
            except: # fake mode
                self.add( self.modeList[ self.mode ] )
            self.modeList[ self.mode ].onActivate(arg)
            self.show()

    def onFocusIn(self, event, data=None):
        if Config.DEBUG > 3: print 'DEBUG: TamTam::onFocusOut in TamTam.py'
        csnd = new_csound_client()
        csnd.connect(True)
        if self.mode == 'synth':
            self.modeList[ self.mode ].updateSound()
            self.modeList[ self.mode ].updateTables()
        #csnd.load_instruments()

    def onFocusOut(self, event, data=None):
        if Config.DEBUG > 3: print 'DEBUG: TamTam::onFocusOut in TamTam.py'
        csnd = new_csound_client()
        csnd.connect(False)
        
    def onActive(self, widget = None, event = None):
        pass
        
    def onKeyPress(self, widget, event):
        if Config.DEBUG > 5: print 'DEBUG: TamTam::onKeyPress in TamTam.py'
        if event.state == gtk.gdk.MOD1_MASK:
            key = event.keyval
            if key == gtk.keysyms.j:
                self.set_mode("jam")
                return
            elif key == gtk.keysyms.m:
                self.set_mode('mini')
                return
            elif key == gtk.keysyms.s:
                self.set_mode('synth')
                return
            elif key == gtk.keysyms.w:
                self.set_mode('welcome')
                return
            elif key == gtk.keysyms.e:
                self.set_mode('edit')
                return
            elif key == gtk.keysyms.t:
                self.toolbox.show()
                return
            elif key == gtk.keysyms.y:
                self.toolbox.hide()
        if self.mode:
            self.modeList[ self.mode ].onKeyPress(widget, event)

    def onKeyRelease(self, widget, event):
        if Config.DEBUG > 5: print 'DEBUG: TamTam::onKeyRelease in TamTam.py'
        self.modeList[ self.mode ].onKeyRelease(widget, event)
        pass

    def onDestroy(self, arg2):
        if Config.DEBUG: print 'DEBUG: TamTam::onDestroy()'
        os.system('rm -f ' + Config.PREF_DIR + '/synthTemp*')

        for m in self.modeList:
            if self.modeList[m] != None:
                self.modeList[m].onDestroy()

        csnd = new_csound_client()
        csnd.connect(False)
        csnd.destroy()

        gtk.main_quit()

    def ensure_dir(self, dir, perms=0777, rw=os.R_OK|os.W_OK):
        if not os.path.isdir( dir ):
            try:
                os.makedirs(dir, perms)
            except OSError, e:
                print 'ERROR: failed to make dir %s: %i (%s)\n' % (dir, e.errno, e.strerror)
        if not os.access(dir, rw):
            print 'ERROR: directory %s is missing required r/w access\n' % dir

    def ensure_dirs(self):
        self.ensure_dir(Config.TUNE_DIR)
        self.ensure_dir(Config.SYNTH_DIR)
        self.ensure_dir(Config.SNDS_DIR)

        if not os.path.isdir(Config.PREF_DIR):
            os.mkdir(Config.PREF_DIR)
            os.system('chmod 0777 ' + Config.PREF_DIR + ' &')
            for snd in ['mic1','mic2','mic3','mic4','lab1','lab2','lab3','lab4', 'lab5', 'lab6']:
                shutil.copyfile(Config.SOUNDS_DIR + '/' + snd , Config.SNDS_DIR + '/' + snd)
                os.system('chmod 0777 ' + Config.SNDS_DIR + '/' + snd + ' &')

    def read_file(self,file_path):
        subactivity_name = self.metadata['tamtam_subactivity']
        if subactivity_name == 'edit' or subactivity_name == 'synth':
            self.set_mode(subactivity_name)
            self.modeList[subactivity_name].handleJournalLoad(file_path)
        elif subactivity_name == 'mini':
            self.set_mode(subactivity_name)
        else:
            return

    def write_file(self,file_path):
        if self.mode == 'edit':
            self.metadata['tamtam_subactivity'] = self.mode
            self.modeList[self.mode].handleJournalSave(file_path)
        if self.mode == 'synth':
            self.metadata['tamtam_subactivity'] = self.mode
            self.modeList[self.mode].handleJournalSave(file_path)
        if self.mode == 'mini':
            self.metadata['tamtam_subactivity'] = self.mode
            f = open(file_path,'w')
            f.close()


if __name__ == "__main__":
    if len(sys.argv) > 1 :
        mainwin = TamTam(None, sys.argv[1])
    else:
        mainwin = TamTam(None, 'welcome')

    gtk.gdk.threads_init()
    gtk.main()

    sys.exit(0)








    def run_edit_mode():
        tamtam = MainWindow()
        mainwin = gtk.Window(gtk.WINDOW_TOPLEVEL)
        mainwin.set_title('TamTam Player')
        display = mainwin.get_display()
        screen = gtk.gdk.Display.get_default_screen(display)
        mainwin.set_geometry_hints( None, screen.get_width(), screen.get_height(), screen.get_width(), screen.get_height(), screen.get_width(), screen.get_height() )
        #mainwin.fullscreen() # don't need to specify full screen, it seem to sit properly anyway
        mainwin.set_resizable(False)
        mainwin.connect('destroy' , tamtam.destroy )
        #mainwin.connect( "configure-event", tamtam.handleConfigureEvent )
        mainwin.connect( "key-press-event", tamtam.onKeyPress )
        mainwin.connect( "key-release-event", tamtam.onKeyRelease )
        mainwin.connect( "delete_event", tamtam.delete_event )
        mainwin.add(tamtam)
        tamtam.show()
        mainwin.show()
        gtk.main()