/
music_box.py
282 lines (193 loc) · 6.39 KB
/
music_box.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#### Description #### ----------------------------------------------------------
# Simple MP3 Music-Player
#
# Uses pygame for event handling and mp3 playing
#
# 5 physical Buttons are used for control
#
# 1) <<- folder backward = Pin 17
# 2) <-- title backward = Pin 27
# 3) |> play pause = Pin 23
# 4) --> title forward = Pin 22
# 5) ->> folder forward = Pin 24
#
#### Imports ####
from gpiozero import Button
import pygame
import time
import os
from typing import List
#### Functions #### ------------------------------------------------------------
def list_dir_recursive(path: str) -> List[str]:
"""
Goes through folder and lists all its files.
Args:
path (str): path to look for files
Returns:
List[str]: a list of file paths
"""
res = []
for currentpath, folders, files in os.walk(path):
for file in files:
res.append(os.path.join(currentpath, file))
return res
#### Classes #### --------------------------------------------------------------
class Player:
"""
Class that serves as a music player connecting button events to play actions
depending on the current state - e.g. play/pause, current track, current folder.
"""
# object states
# - current directory
dir_list = []
dir_pos = 0
# - current title
title_list = []
title_pos = 0
# - current play state
play_state = "pause"
# - optional musicpath
music_path = '.'
# Initialize
def __init__(self, music_path: str):
# store musicpath
self.music_path = music_path
# read in directories and titles in music path
self.refresh_dir_list()
self.refresh_title_list()
# initialize music player state with first track and a ready music player
pygame.mixer.init()
pygame.mixer.music.load(self.title_list[self.title_pos])
pygame.mixer.music.play()
pygame.mixer.music.pause()
self.info("-- player ready -- ")
def refresh_title_list(self):
"""
Method for re-initializing title list: re-set position + re-list titles in folder
"""
# reset title position
self.title_pos = 0
# list files
self.title_list = list_dir_recursive(self.dir_list[self.dir_pos])
# filter
self.title_list = [ fi for fi in self.title_list if fi.endswith(".mp3") ]
# sort
self.title_list.sort()
# handle empty folders
if len(self.title_list) == 0:
self.title_list = self.music_path.join("/keine-titel-gefunden.mp3")
def refresh_dir_list(self):
"""
Method for refreshing the current list of folders.
"""
pth = self.music_path
self.dir_list = [os.path.join(pth, f) for f in os.listdir(pth) if os.path.isdir(os.path.join(pth, f))]
self.dir_list.sort()
def info(self, pre: str):
"""
Method for printing info about current player state.
Args:
pre (str): A string prefix, to be printed before
"""
print(pre, self.dir_pos, self.dir_list[self.dir_pos], self.title_pos, self.title_list[self.title_pos])
def folder_forward(self):
"""
Method for acting on folder-forward button push: going to next folder in list
"""
self.refresh_dir_list()
new_pos = min(len(self.dir_list) - 1, self.dir_pos + 1)
if new_pos is not self.dir_pos:
self.dir_pos = new_pos
self.refresh_title_list()
self.play_current_title_item()
self.info(">>")
def folder_backward(self):
"""
Method for acting on folder-backward button push: going to previous folder in list
"""
self.refresh_dir_list()
new_pos = max(0, self.dir_pos - 1)
if new_pos is not self.dir_pos:
self.dir_pos = new_pos
self.refresh_title_list()
self.play_current_title_item()
self.info("<<")
def title_forward(self):
"""
Method for acting on title-forward button push: going to next title in list
"""
new_pos = min(len(self.title_list) - 1 , self.title_pos + 1)
if new_pos is not self.title_pos:
self.title_pos = new_pos
self.play_current_title_item()
self.info("|>")
def title_backward(self):
"""
Method for acting on title-backward button push: going to previous title in list
"""
new_pos = max(0, self.title_pos - 1)
if new_pos is not self.title_pos:
self.title_pos = new_pos
self.play_current_title_item()
self.info("<|")
def play_current_title_item(self):
"""
Method for playing current title.
"""
pygame.mixer.music.load(self.title_list[self.title_pos])
pygame.mixer.music.play()
pygame.mixer.music.unpause()
self.play_state = "unpause"
def play_pause(self):
"""
Method alternating between play and pause.
"""
# check if maybe player is done playing or not
if pygame.mixer.music.get_busy() == 0:
# if player is done restart current title and unpause
pygame.mixer.music.play()
pygame.mixer.music.unpause()
self.play_state = "unpause"
else:
# if player still has some title to play switch pause state
# pause --> unpause
if self.play_state == "pause":
pygame.mixer.music.unpause()
self.play_state = "unpause"
# unpause --> pause
elif self.play_state == "unpause":
pygame.mixer.music.pause()
self.play_state = "pause"
# print some feedback to console
if self.play_state == "pause":
self.info("||")
else:
self.info("~~")
#### DoingDutyToDo #### -------------------------------------------------------
# initialize player
player = Player(music_path = '/home/pi/musicbox/music')
# initialize buttons
play_pause_btn = Button(23)
title_forward_btn = Button(22)
title_backward_btn = Button(27)
folder_forward_btn = Button(24)
folder_backward_btn = Button(17)
# wire up buttons and player functions
play_pause_btn.when_pressed = player.play_pause
title_forward_btn.when_pressed = player.title_forward
title_backward_btn.when_pressed = player.title_backward
folder_forward_btn.when_pressed = player.folder_forward
folder_backward_btn.when_pressed = player.folder_backward
print("loop")
go_on = True
while go_on == True:
# if player is idle, play next track
if pygame.mixer.music.get_busy() == 0:
if (player.title_pos + 1) < len(player.title_list):
player.title_forward()
time.sleep(1)
# if busy, just wait a little bit and check again
else:
time.sleep(0.01)
# tear down and exit
pygame.quit()