aboutsummaryrefslogtreecommitdiff
path: root/helpers/music_file.py
diff options
context:
space:
mode:
Diffstat (limited to 'helpers/music_file.py')
-rw-r--r--helpers/music_file.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/helpers/music_file.py b/helpers/music_file.py
index 892519d..9acdedf 100644
--- a/helpers/music_file.py
+++ b/helpers/music_file.py
@@ -2,6 +2,7 @@ import threading
2import pydub 2import pydub
3import pygame 3import pygame
4import math 4import math
5import time
5 6
6class MusicFile: 7class MusicFile:
7 def __init__(self, filename, lock, channel_id, name = None, gain = 1): 8 def __init__(self, filename, lock, channel_id, name = None, gain = 1):
@@ -32,6 +33,15 @@ class MusicFile:
32 def is_paused(self): 33 def is_paused(self):
33 return self.flag_paused 34 return self.flag_paused
34 35
36 @property
37 def sound_position(self):
38 if self.is_playing() and not self.is_paused():
39 return min(time.time() - self.started_at, self.sound_duration)
40 elif self.is_playing() and self.is_paused():
41 return min(self.paused_at - self.started_at, self.sound_duration)
42 else:
43 return 0
44
35 def play(self, fade_in = 0, volume = 100, start_at = 0): 45 def play(self, fade_in = 0, volume = 100, start_at = 0):
36 self.channel().set_endevent() 46 self.channel().set_endevent()
37 self.channel().set_endevent(pygame.USEREVENT) 47 self.channel().set_endevent(pygame.USEREVENT)
@@ -45,14 +55,17 @@ class MusicFile:
45 else: 55 else:
46 sound = pygame.mixer.Sound(self.raw_data) 56 sound = pygame.mixer.Sound(self.raw_data)
47 57
58 self.started_at = time.time()
48 self.channel().play(sound, fade_ms = int(fade_in * 1000)) 59 self.channel().play(sound, fade_ms = int(fade_in * 1000))
49 self.flag_paused = False 60 self.flag_paused = False
50 61
51 def pause(self): 62 def pause(self):
63 self.paused_at = time.time()
52 self.channel().pause() 64 self.channel().pause()
53 self.flag_paused = True 65 self.flag_paused = True
54 66
55 def unpause(self): 67 def unpause(self):
68 self.started_at += (time.time() - self.paused_at)
56 self.channel().unpause() 69 self.channel().unpause()
57 self.flag_paused = False 70 self.flag_paused = False
58 71