Changeset - 0a2239379466
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 years ago 2005-06-13 03:16:19
drewp@bigasterisk.com
added autopause. workaround for mpd's incorrect times
1 file changed with 46 insertions and 12 deletions:
0 comments (0 inline, 0 general)
bin/ascoltami
Show inline comments
 
@@ -80,9 +80,12 @@ class Player:
 

	
 
        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 @@ class Player:
 

	
 

	
 
        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 @@ class Player:
 
        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 @@ class ControlButtons(tk.Frame):
 
        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 @@ class ControlButtons(tk.Frame):
 

	
 
    def update_state_buttons(self,*args):
 
        state = player.state.get()
 
        print "State", state
 

	
 
        if state in ('stop', 'pause'):
 
            self.statebuttons['pause']['text'] = 'Play'
0 comments (0 inline, 0 general)