changeset 241:0a2239379466

added autopause. workaround for mpd's incorrect times
author drewp@bigasterisk.com
date Mon, 13 Jun 2005 03:16:19 +0000
parents 76e326329610
children 561b4202461d
files bin/ascoltami
diffstat 1 files changed, 46 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ascoltami	Mon Jun 13 03:15:10 2005 +0000
+++ b/bin/ascoltami	Mon Jun 13 03:16:19 2005 +0000
@@ -80,9 +80,12 @@
 
         self.pollStatus()
 
+        self.autopausedthissong = False # a song only autopauses once
+        self.mpd_is_lying = False # mpd reports bad times in certain intervals
 
     def smoothCurrentTime(self):
         """like self.current_time.get, but more accurate"""
+
         if self.last_poll_time and self.state.get() == 'play':
             dt = time.time() - self.last_poll_time
         else:
@@ -102,20 +105,41 @@
 
 
         if hasattr(stat, 'time_elapsed'):
-            if stat.song == 1:
-                t = stat.time_elapsed
-            elif stat.song == 0:
-                t = stat.time_elapsed - stat.time_total
-            elif stat.song == 2:
-                t = self.total_time.get() + stat.time_elapsed
+            elapsed = stat.time_elapsed
+            songnum = stat.song
+            total = stat.time_total
+            if self.mpd_is_lying and elapsed < 3:
+                self.mpd_is_lying = False
+
+            # mpd lies about elapsed, song, and total during the last
+            # .5sec of each song. so we coast through that part
+            if elapsed > total - .75 or self.mpd_is_lying:
+                if not self.mpd_is_lying:
+                    self.mpd_is_lying = True
+                    self.true_song_total = songnum, total
+                    self.marked_time = time.time()
+                    self.marked_val = elapsed
+                elapsed = self.marked_val + (time.time() - self.marked_time)
+                songnum, total = self.true_song_total
+            
+            if songnum == 1:
+                t = elapsed
+            elif songnum == 0:
+                t = elapsed - total
+            elif songnum == 2:
+                t = self.total_time.get() + elapsed
 
             self.current_time.set(t)
+            
+            self.last_poll_time = time.time()
 
-            self.last_poll_time = time.time()
+        self.check_autopause()
 
         reactor.callLater(.05, self.pollStatus)
 
     def play(self, song_path):
+    
+        self.autopausedthissong = False
         self.mpd.clear()
         self.mpd.add(showconfig.songInMpd(self.pre_post_names[0]))
         self.mpd.add(showconfig.songInMpd(song_path))
@@ -131,12 +155,22 @@
         self.total_time.set(stat.time_total)
         self.mpd.seek(seconds=0, song=0)
 
+    def check_autopause(self):
+        pause_time = self.total_time.get() + 10
+        if (not self.autopausedthissong and
+            self.state.get() == "play" and
+            self.current_time.get() > pause_time):
+            self.autopausedthissong = True
+            self.mpd.pause()
+
     def stop(self):
         self.mpd.seek(seconds=0, song=0)
         self.mpd.stop()
         
     def seek_to(self, time):
         if time < 0:
+            # seeking to anything within my 4-sec silence ogg goes
+            # right to zero. maybe ogg seeking is too coarse?
             self.mpd.seek(seconds=time - (-4), song=0)
         elif time > self.total_time.get():
             self.mpd.seek(seconds=time - self.total_time.get(), song=2)
@@ -277,10 +311,11 @@
         tk.Frame.__init__(self,master,bg='black')
         
         self.statebuttons = {} # lowercased name : Button
-        for txt,cmd,key in (('Stop', player.stop, "<Control-s>"),
-                            ('Pause', player.play_pause_toggle, "<Control-p>"),
-                            ('Skip Intro',lambda: player.seek_to(-.5), "<Control-i>"),
-                            ):
+        for txt,cmd,key in [
+            ('Stop', player.stop, "<Control-s>"),
+            ('Pause', player.play_pause_toggle, "<Control-p>"),
+            ('Skip Intro',lambda: player.seek_to(0), "<Control-i>"),
+            ]:
             b = tk.Button(self, text=txt, command=cmd, font='arial 16 bold',
                           height=3,**appstyle)
             b.pack(side='left', fill='x', expand=1)
@@ -290,7 +325,6 @@
 
     def update_state_buttons(self,*args):
         state = player.state.get()
-        print "State", state
 
         if state in ('stop', 'pause'):
             self.statebuttons['pause']['text'] = 'Play'