-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioplayer.py
116 lines (85 loc) · 2.69 KB
/
audioplayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import configparser
import logging
import pygame
# TODO Convert to a Class???
config = configparser.ConfigParser()
config.read('./config/config.ini')
AUDIO_PATH = config['AUDIO']['FILE_PATH']
LOOPS = int(config['AUDIO']['LOOPS'])
# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def init():
pygame.mixer.pre_init()
pygame.mixer.init()
# BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
# pygame.mixer.init(buffer=BUFFER)
pygame.mixer.music.set_volume(0.7)
def start():
logging.info(f"Loading audio file {AUDIO_PATH}...")
pygame.mixer.music.load(AUDIO_PATH)
def play(busy=False):
logging.info("Playing...")
pygame.mixer.music.play(loops=LOOPS)
if busy:
while pygame.mixer.music.get_busy() is True:
continue
def stop():
logging.info("Stopping...")
pygame.mixer.music.stop()
def pause():
res = f"Pausing at {_get_current_time_position()}..."
logging.debug(res)
pygame.mixer.music.pause()
return res
def unpause():
res = f"Resuming at {_get_current_time_position()}..."
logging.debug(res)
pygame.mixer.music.unpause()
return res
def quit_player():
logging.info("Good bye!")
pygame.quit()
# TODO implement get status function
def get_status() -> str:
# TODO check if it is playing
# if is_playing:
# TODO get current position
pos = pygame.mixer.music.get_pos()
time = _convert_millis_to_human_time(pos)
# TODO get volume
# TODO get song name
res = time # FIXME
return res
def volume_up():
v = pygame.mixer.music.get_volume()
v = 1 if v > 0.9 else v+0.1
pygame.mixer.music.set_volume(v)
def volume_down():
v = pygame.mixer.music.get_volume()
v = 0 if v < 0.1 else v-0.1
pygame.mixer.music.set_volume(v)
def _get_current_time_position() -> str:
pos = pygame.mixer.music.get_pos()
time = _convert_millis_to_human_time(pos)
return time
def _convert_millis_to_human_time(ms: int) -> str:
""" Convert milliseconds to the human readable format: hh:mm:ss. """
seconds = (ms / 1000) % 60
minutes = (ms / (1000 * 60)) % 60
hours = (ms / (1000 * 60 * 60)) % 24
# return int(hours), int(minutes), int(seconds)
time = "{0:02d}:{1:02d}:{2:02d}".format(int(hours), int(minutes), int(seconds))
return time
# clock = pygame.time.Clock()
# while pygame.mixer.music.get_busy():
# clock.tick(1000)
if __name__ == '__main__':
logging.info("Starting AudioPlayer...")
# init()
# start()
# play()
# while pygame.mixer.music.get_busy() is True:
# continue
# quit()