1"""
2 File:
3 JetPreview.py
4
5 Contents and purpose:
6 Plays the preview of a segment or event via the dialog box
7
8 Copyright (c) 2008 Android Open Source Project
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21"""
22
23from __future__ import with_statement
24
25import wx
26import threading
27
28from JetDefs import *
29from JetCtrls import *
30from JetFile import *
31from JetUtils import *
32from eas import *
33from JetStatusEvent import *
34
35class PreviewPlayer(wx.Frame):
36    """ Segment player """
37    def __init__ (self, play_button, segment):
38        self.segment = segment
39        self.play_button = play_button
40        self.mute_button = None
41        self.trigger_button = None
42        self.playerLock = threading.RLock()
43        self.SetKeepPlayingFlag(False)
44        self.graph = None
45
46    def SetGraphCtrl(self, graph, parentWin):
47        """ Sets the graph control for the player """
48        self.graph = graph
49        self.parentWin = parentWin
50
51    def SetGraphCallbackFct(self, ClickCallbackFct):
52        """ Sets the callback function for the graph control to update """
53        self.ClickCallbackFct = ClickCallbackFct
54
55    def GraphTriggerClip(self, sClipName, iEventId):
56        """ Triggers a clip by clicking on it """
57        with self.playerLock:
58            try:
59                self.jet.TriggerClip(iEventId)
60                return True
61            except:
62                return False
63
64    def SetMuteFlag(self, trackNum, mute):
65        """ Sets a mute flag """
66        sync = JetDefs.DEFAULT_MUTE_SYNC
67        with self.playerLock:
68            try:
69                self.jet.SetMuteFlag(trackNum, mute, sync)
70                logging.info("SetMuteFlag() Track:%d Mute:%d Sync:%d" % (trackNum, mute, sync))
71                return True
72            except:
73                return False
74
75    def TriggerClip(self, eventID):
76        """ Triggers a clip via function """
77        with self.playerLock:
78            try:
79                self.jet.TriggerClip(eventID)
80                logging.info("TriggerClip() eventID: %d" % eventID)
81                return True
82            except:
83                return False
84
85    def MuteTrackViaButton(self, button, trackNum):
86        """ Mutes a track via a button """
87        with self.playerLock:
88            self.mute_button = button
89            if button.GetLabel() == JetDefs.BUT_MUTE:
90                if self.SetMuteFlag(trackNum, True):
91                    button.SetLabel(JetDefs.BUT_UNMUTE)
92            else:
93                if self.SetMuteFlag(trackNum, False):
94                    button.SetLabel(JetDefs.BUT_MUTE)
95
96    def Start(self):
97        """ Starts the playback.  Called as a thread from dialog boxes """
98        self.paused = False
99
100        wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_PLAY, None))
101
102        # create a temporary config file, and jet output file
103        FileKillClean(JetDefs.TEMP_JET_CONFIG_FILE)
104
105        self.jet_file = JetFile(JetDefs.TEMP_JET_CONFIG_FILE, "")
106
107        self.jet_file.AddSegment(self.segment.segname,
108                                 self.segment.filename,
109                                 self.segment.start,
110                                 self.segment.end,
111                                 self.segment.length,
112                                 SegmentOutputFile(self.segment.segname, JetDefs.TEMP_JET_CONFIG_FILE),
113                                 self.segment.quantize,
114                                 self.segment.jetevents,
115                                 self.segment.dlsfile,
116                                 None,
117                                 self.segment.transpose,
118                                 self.segment.repeat,
119                                 self.segment.mute_flags)
120        userID = 0
121        dls_num = -1
122        seg_num = 0
123
124        if len(self.segment.dlsfile) > 0:
125            self.jet_file.libraries.append(self.segment.dlsfile)
126            dls_num = 0
127
128        self.jet_file.SaveJetConfig(JetDefs.TEMP_JET_CONFIG_FILE)
129        self.jet_file.WriteJetFileFromConfig(JetDefs.TEMP_JET_CONFIG_FILE)
130
131        if not ValidateConfig(self.jet_file):
132            return
133
134        self.queueSegs = []
135        self.queueSegs.append(QueueSeg(self.segment.segname, userID, seg_num, dls_num, self.segment.repeat, self.segment.transpose, self.segment.mute_flags))
136
137        self.jet = JET()
138        self.jet.eas.StartWave()
139        self.jet.OpenFile(self.jet_file.config.filename)
140
141        # queue first segment and start playback
142        index = 0
143        Queue(self.jet, self.queueSegs[index])
144
145        index += 1
146        self.jet.Play()
147
148        self.SetKeepPlayingFlag(True)
149        while self.GetKeepPlayingFlag():
150            self.jet.Render()
151            status = self.jet.Status()
152
153            # if no more segments - we're done
154            if status.numQueuedSegments == 0:
155                break
156
157            self.jet.GetAppEvent()
158
159            # if less than 2 segs queued - queue another one
160            if (index < len(self.queueSegs)) and (status.numQueuedSegments < 2):
161                Queue(self.jet, self.queueSegs[index])
162                index += 1
163
164            wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_UPD_LOCATION, status.location))
165
166        SafeJetShutdown(self.playerLock, self.jet)
167
168        FileKillClean(SegmentOutputFile(self.segment.segname, JetDefs.TEMP_JET_CONFIG_FILE))
169        FileKillClean(JetDefs.TEMP_JET_CONFIG_FILE)
170        FileKillClean(self.jet_file.config.filename)
171
172        self.SetKeepPlayingFlag(False)
173
174        wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_DONE, None))
175
176        wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_UPD_LOCATION, 0))
177
178    def SetKeepPlayingFlag(self, val):
179        """ Sets the flag to tell us wheter to keep playing """
180        with self.playerLock:
181            self.keepPlaying = val
182
183    def GetKeepPlayingFlag(self):
184        """ Gets the keep playing flag """
185        with self.playerLock:
186            return self.keepPlaying
187
188    def Pause(self):
189        """ Pauses playback """
190        if self.jet is None:
191            return
192        if not self.paused:
193            self.jet.Pause()
194            self.paused = True
195            wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_PAUSE, None))
196        else:
197            self.jet.Play()
198            self.paused = False
199            wx.PostEvent(self.parentWin, JetStatusEvent(JetDefs.PST_RESUME, None))
200
201