Attachment 'metronome1.py'
Download 1 '''metronome1
2 Now try to get a flexible timer by constantly checking the number of beats per
3 minute after every beat.
4 '''
5 import wx
6
7 mspm = 60*1000 # milliseconds per minute
8
9 class MFrame(wx.Frame):
10 def __init__(self,):
11 wx.Frame.__init__(self,None)
12 #define the components
13 self.go = wx.Button(self,wx.ID_ANY,"Start")
14 self.stop = wx.Button(self,wx.ID_ANY,"Stop")
15 self.bpm = wx.SpinCtrl(self,wx.ID_ANY,value="120",min=1,max=240)
16 self.dingcheck = wx.CheckBox(self,wx.ID_ANY)
17 self.downbeat = wx.SpinCtrl(self,wx.ID_ANY,value="4",min = 2, max=12)
18
19 self.dingcheck.SetValue(False)
20 self.downbeat.Enable(False)
21
22 #do the layout
23 sizer = wx.BoxSizer(wx.VERTICAL)
24 sizer.Add(wx.StaticText(self,wx.ID_ANY,"Set the beats per minute"))
25 sizer.Add(self.bpm)
26 sizer.Add(self.go)
27 sizer.Add(self.stop)
28 s2 = wx.BoxSizer(wx.HORIZONTAL)
29 s2.Add(self.dingcheck,0,wx.ALIGN_CENTRE_VERTICAL)
30 s2.Add(wx.StaticText(self,wx.ID_ANY,"Beats per measure"),0,wx.ALIGN_CENTRE_VERTICAL)
31 s2.Add(self.downbeat)
32 sizer.Add(s2)
33 self.SetSizerAndFit(sizer)
34 self.Layout()
35
36 #Bind events
37 self.Bind(wx.EVT_BUTTON,self.Start,self.go)
38 self.Bind(wx.EVT_BUTTON,self.Stop,self.stop)
39 self.Bind(wx.EVT_TIMER,self.Tick)
40 self.Bind(wx.EVT_CHECKBOX,self.SetDownBeat,self.dingcheck)
41
42 def SetDownBeat(self,evt):
43 if self.dingcheck.IsChecked():
44 self.downbeat.Enable(True)
45 else:
46 self.downbeat.Enable(False)
47
48 def Start(self,evt):
49 self.timer=wx.Timer(self)
50 self.count = 0
51 self.timer.Start(mspm/float(self.bpm.GetValue()))
52 self.go.Enable(False)
53 self.stop.Enable(True)
54
55 def Stop(self,evt):
56 self.timer.Stop()
57 del self.timer
58 self.go.Enable(True)
59 self.stop.Enable(False)
60
61 def Tick(self,evt):
62 if bool(self.count):
63 sound = wx.Sound('tick2.wav')
64 else:
65 if self.dingcheck.IsChecked():
66 sound = wx.Sound('ding.wav')
67 else:
68 sound = wx.Sound('tick2.wav')
69 sound.Play(wx.SOUND_ASYNC)
70 self.count = (self.count + 1) % int(self.downbeat.GetValue())
71 wx.YieldIfNeeded()
72
73 class TestApp(wx.App):
74 def OnInit(self):
75 mainframe = MFrame()
76 self.SetTopWindow(mainframe)
77 mainframe.Show()
78 return 1
79
80
81 app = TestApp(0)
82 app.MainLoop()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.