comparison orpg/tools/orpg_sound.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children c54768cffbd4
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 from orpg.orpgCore import *
2 from orpg.orpg_wx import *
3
4
5 class orpgSound(wx.Panel):
6 def __init__(self, parent):
7 wx.Panel.__init__(self, parent, -1)
8 self.parent = parent
9
10 self.log = open_rpg.get_component("log")
11 self.settings = open_rpg.get_component('settings')
12
13 self.log.log("Enter orpgSound", ORPG_DEBUG)
14
15 # Create some controls
16 try:
17 self.mc = wx.media.MediaCtrl(self, wx.ID_ANY, size=(1,1))
18 self.OldPlayer = False
19 self.Bind(wx.media.EVT_MEDIA_LOADED, self.OnMediaLoaded)
20 self.log.log("wx.media Used", ORPG_DEBUG)
21 except NotImplementedError:
22 self.OldPlayer = True
23 self.log.log("Old Player Used", ORPG_DEBUG)
24 else:
25 btn2 = wx.Button(self, -1, "Play")
26 self.Bind(wx.EVT_BUTTON, self.OnPlay, btn2)
27 self.playBtn = btn2
28 self.playBtn.Disable()
29
30 btn3 = wx.Button(self, -1, "stop")
31 self.Bind(wx.EVT_BUTTON, self.OnStop, btn3)
32 self.stopBtn = btn3
33
34 self.playList = wx.Choice(self, wx.ID_ANY)
35 self.playDict = {}
36
37 self.pslider = wx.Slider(self, wx.ID_ANY, 0, 0, 100, size=wx.Size(100, -1))
38 self.Bind(wx.EVT_SLIDER, self.onSeek, self.pslider)
39
40 self.pos = wx.StaticText(self, wx.ID_ANY, "Position:")
41
42 self.vol = wx.StaticText(self, wx.ID_ANY, "Volume:")
43 self.vslider = wx.Slider(self, wx.ID_ANY, 100, 0, 100, size=wx.Size(50, -1))
44 self.Bind(wx.EVT_SLIDER, self.onVol, self.vslider)
45
46 self.log.log("Buttons Created", ORPG_DEBUG)
47
48 self.loopSound = False
49 self.seeking = False
50 self.lastlen = 0
51
52 # setup the layout
53 sizer = wx.GridBagSizer(hgap=1, vgap=1)
54 sizer.Add(self.mc, (0,20))
55 sizer.Add(self.playList, (0,0), span=(1,2))
56 sizer.Add(btn2, (0,2))
57 sizer.Add(btn3, (0,3))
58 sizer.Add(self.pos, (1,0), flag=wx.ALIGN_CENTER)
59 sizer.Add(self.pslider, (1,1), flag=wx.EXPAND)
60 sizer.Add(self.vol, (1,2), flag=wx.ALIGN_CENTER)
61 sizer.Add(self.vslider, (1,3), flag=wx.EXPAND)
62 sizer.AddGrowableCol(0)
63 sizer.AddGrowableCol(3)
64 sizer.SetEmptyCellSize((0,0))
65
66 self.SetSizer(sizer)
67 self.SetAutoLayout(True)
68
69 self.log.log("Sizer Set", ORPG_DEBUG)
70
71 self.Fit()
72
73 self.Bind(wx.EVT_CHOICE, self.PlaySelected, self.playList)
74 self.Bind(wx.EVT_CLOSE, self.OnClose)
75 self.timer = wx.Timer(self, wx.NewId())
76 self.Bind(wx.EVT_TIMER, self.OnTimer)
77 self.timer.Start(100)
78
79 self.log.log("Events Bound", ORPG_DEBUG)
80
81 self.log.log("Exit orpgSound", ORPG_DEBUG)
82
83 def play(self, sound_file, type="local", loop_sound=False):
84 self.log.log("Enter orpgSound->play(self, sound_file, type, loop_sound)", ORPG_DEBUG)
85
86 if self.OldPlayer:
87 if wx.Platform == '__WXMSW__':
88 import winsound
89 self.play_windows(sound_file.replace('&', '&'))
90 elif wx.Platform == '__WXGTK__':
91 self.play_unix(sound_file.replace('&', '&'))
92 else:
93 self.loopSound = loop_sound
94 self.LoadSound(sound_file.replace('&', '&'), type)
95
96 self.log.log("Exit orpgSound->play(self, sound_file, type, loop_sound)", ORPG_DEBUG)
97
98 def play_windows(self,sound_file):
99 self.log.log("Enter orpgSound->play_windows(self,sound_file)", ORPG_DEBUG)
100
101 winsound.PlaySound(sound_file, winsound.SND_FILENAME)
102
103 self.log.log("Exit orpgSound->play_windows(self,sound_file)", ORPG_DEBUG)
104
105 def play_unix(self, sound_file):
106 self.log.log("Enter orpgSound->play_unix(self, sound_file)", ORPG_DEBUG)
107
108 unix_player = self.settings.get_setting('UnixSoundPlayer')
109 if unix_player != '':
110 os.system(unix_player + " " + sound_file)
111
112 self.log.log("Exit orpgSound->play_unix(self, sound_file)", ORPG_DEBUG)
113
114 def onSeek(self, evt):
115 self.log.log("Enter orpgSound->onSeek(self, evt)", ORPG_DEBUG)
116
117 offset = self.pslider.GetValue()
118 self.mc.Seek(offset)
119
120 self.log.log("Exit orpgSound->onSeek(self, evt)", ORPG_DEBUG)
121
122 def PlaySelected(self, evt):
123 self.log.log("Enter orpgSound->PlaySelected(self, evt)", ORPG_DEBUG)
124
125 name = self.playList.GetStringSelection()
126 info = self.playDict[name]
127 sound_file = info[0]
128 pos = info[1]
129
130 self.mc.LoadURI(sound_file)
131
132 pane = self.parent._mgr.GetPane("Sound Control Toolbar")
133 pane.window.SetInitialSize()
134 pane.BestSize(pane.window.GetEffectiveMinSize() + (0, 1))
135 self.parent._mgr.Update()
136
137 if pos > 0:
138 self.seeking = True
139 wx.CallAfter(self.mc.Pause)
140
141 self.log.log("Exit orpgSound->PlaySelected(self, evt)", ORPG_DEBUG)
142
143 def LoadSound(self, sound_file, type="local"):
144 self.log.log("Enter orpgSound->LoadSound(self, sound_file, type)", ORPG_DEBUG)
145
146 if self.mc.GetState() == wx.media.MEDIASTATE_PLAYING:
147 pos = self.mc.Tell()
148 len = self.mc.Length()
149 self.mc.Stop()
150 if (len-pos) <= 2000:
151 pos = 0
152 pname = self.playList.GetStringSelection()
153 self.playDict[pname][1] = pos
154
155 (path, name) = os.path.split(sound_file)
156 if type != "local":
157 self.mc.LoadURI(sound_file)
158 else:
159 self.mc.Load(sound_file)
160
161 if not self.playDict.has_key(name):
162 self.playList.Append(name)
163
164 pane = self.parent._mgr.GetPane("Sound Control Toolbar")
165 pane.window.SetInitialSize()
166 pane.BestSize(pane.window.GetEffectiveMinSize() + (0, 1))
167 self.parent._mgr.Update()
168
169 self.playDict[name] = [sound_file, 0]
170 self.playList.SetStringSelection(name)
171
172 self.log.log("Exit orpgSound->LoadSound(self, sound_file, type)", ORPG_DEBUG)
173 return
174
175 def OnMediaLoaded(self, evt):
176 self.log.log("Enter orpgSound->OnMediaLoaded(self, evt)", ORPG_DEBUG)
177
178 self.mc.Play()
179 self.playBtn.Enable()
180
181 self.log.log("Exit orpgSound->OnMediaLoaded(self, evt)", ORPG_DEBUG)
182
183 def OnPlay(self, evt):
184 self.log.log("Enter orpgSound->OnPlay(self, evt)", ORPG_DEBUG)
185
186 self.mc.Play()
187
188 self.log.log("Exit orpgSound->OnPlay(self, evt)", ORPG_DEBUG)
189
190 def OnStop(self, evt):
191 self.log.log("Enter orpgSound->OnStop(self, evt)", ORPG_DEBUG)
192
193 self.loopSound = False
194 self.mc.Stop()
195
196 self.log.log("Exit orpgSound->OnStop(self, evt)", ORPG_DEBUG)
197
198 def OnClose(self, evt):
199 self.log.log("Enter orpgSound->OnClose(self, evt)", ORPG_DEBUG)
200
201 self.timer.Stop()
202 del self.timer
203
204 self.log.log("Exit orpgSound->OnClose(self, evt)", ORPG_DEBUG)
205
206 def __del__(self):
207 self.timer.Stop()
208
209 def onVol(self, evt):
210 self.log.log("Enter orpgSound->onVol(self, evt)", ORPG_DEBUG)
211
212 vol = float(self.vslider.GetValue())/100
213 self.mc.SetVolume(vol)
214
215 self.log.log("Exit orpgSound->onVol(self, evt)", ORPG_DEBUG)
216
217 def OnTimer(self, args):
218 if not self.OldPlayer:
219 if self.mc.GetState() == wx.media.MEDIASTATE_PLAYING and not self.stopBtn.IsEnabled():
220 self.stopBtn.Enable()
221 elif self.mc.GetState() != wx.media.MEDIASTATE_PLAYING and self.stopBtn.IsEnabled():
222 self.stopBtn.Disable()
223
224 if self.mc.Length() > 0 and self.lastlen != self.mc.Length():
225 self.pslider.SetRange(0, self.mc.Length())
226 self.lastlen = self.mc.Length()
227
228 if not self.seeking:
229 self.pslider.SetValue(self.mc.Tell())
230
231 if self.mc.GetState() != wx.media.MEDIASTATE_PLAYING and self.loopSound:
232 self.mc.Play()
233
234 if self.seeking:
235 name = self.playList.GetStringSelection()
236 info = self.playDict[name]
237
238 if self.mc.Tell() >= info[1]:
239 self.seeking = False
240
241 else:
242 self.pslider.SetValue(info[1])
243 wx.CallAfter(self.onSeek, None)
244
245 elif self.stopBtn.IsShown():
246 self.playBtn.Hide()
247 self.stopBtn.Hide()
248 return
249
250 class SoundFrame(wx.Frame):
251 def __init__(self, openrpg):
252 wx.Frame.__init__(self, None, title="Sound Control Toolbar", style=wx.CAPTION | wx.SYSTEM_MENU | wx.STAY_ON_TOP)
253
254 self.soundCtrl = orpgSound(self, openrpg)
255
256 sizer = wx.BoxSizer(wx.HORIZONTAL)
257 sizer.Add(self.soundCtrl, 1, wx.EXPAND)
258 self.SetSizer(sizer)
259 self.SetSizer(sizer)
260 self.SetAutoLayout(True)
261 self.Fit()
262
263
264
265 class BlankApp(wx.App):
266 def OnInit(self):
267 self.frame = SoundFrame()
268 self.frame.Show()
269 self.SetTopWindow(self.frame)
270 return True
271
272 def OnChar(self, event):
273 #Not sure how to determin if only Tab or Shift Tab was used to initiate
274 self.tabgroup.GoNext( self.frame1.FindFocus() )
275
276 if __name__ == "__main__":
277 app = BlankApp(0)
278 app.MainLoop()