-
Notifications
You must be signed in to change notification settings - Fork 16
/
Rainy.py
274 lines (242 loc) · 10.1 KB
/
Rainy.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
import dearpygui.dearpygui as dpg
import ntpath
import json
from mutagen.mp3 import MP3
from tkinter import Tk,filedialog
import threading
import pygame
import time
import random
import os
import atexit
dpg.create_context()
dpg.create_viewport(title="Rainy Music",large_icon="icon.ico",small_icon="icon.ico")
pygame.mixer.init()
global state
state=None
global no
no = 0
_DEFAULT_MUSIC_VOLUME = 0.5
pygame.mixer.music.set_volume(0.5)
def update_volume(sender, app_data):
pygame.mixer.music.set_volume(app_data / 100.0)
def load_database():
songs = json.load(open("data/songs.json", "r+"))["songs"]
for filename in songs:
dpg.add_button(label=f"{ntpath.basename(filename)}", callback=play, width=-1,
height=25, user_data=filename.replace("\\", "/"), parent="list")
dpg.add_spacer(height=2, parent="list")
def update_database(filename: str):
data = json.load(open("data/songs.json", "r+"))
if filename not in data["songs"]:
data["songs"] += [filename]
json.dump(data, open("data/songs.json", "r+"), indent=4)
def update_slider():
global state
while pygame.mixer.music.get_busy() or state != 'paused':
dpg.configure_item(item="pos",default_value=pygame.mixer.music.get_pos()/1000)
time.sleep(0.7)
state = None
dpg.configure_item("cstate",default_value=f"State: None")
dpg.configure_item("csong",default_value="Now Playing : ")
dpg.configure_item("play",label="Play")
dpg.configure_item(item="pos",max_value=100)
dpg.configure_item(item="pos",default_value=0)
def play(sender, app_data, user_data):
global state,no
if user_data:
no = user_data
pygame.mixer.music.load(user_data)
audio = MP3(user_data)
dpg.configure_item(item="pos",max_value=audio.info.length)
pygame.mixer.music.play()
thread=threading.Thread(target=update_slider,daemon=False).start()
if pygame.mixer.music.get_busy():
dpg.configure_item("play",label="Pause")
state="playing"
dpg.configure_item("cstate",default_value=f"State: Playing")
dpg.configure_item("csong",default_value=f"Now Playing : {ntpath.basename(user_data)}")
def play_pause():
global state,no
if state=="playing":
state="paused"
pygame.mixer.music.pause()
dpg.configure_item("play",label="Play")
dpg.configure_item("cstate",default_value=f"State: Paused")
elif state=="paused":
state="playing"
pygame.mixer.music.unpause()
dpg.configure_item("play",label="Pause")
dpg.configure_item("cstate",default_value=f"State: Playing")
else:
song = json.load(open("data/songs.json", "r"))["songs"]
if song:
song=random.choice(song)
no = song
pygame.mixer.music.load(song)
pygame.mixer.music.play()
thread=threading.Thread(target=update_slider,daemon=False).start()
dpg.configure_item("play",label="Pause")
if pygame.mixer.music.get_busy():
audio = MP3(song)
dpg.configure_item(item="pos",max_value=audio.info.length)
state="playing"
dpg.configure_item("csong",default_value=f"Now Playing : {ntpath.basename(song)}")
dpg.configure_item("cstate",default_value=f"State: Playing")
def pre():
global state,no
songs = json.load(open('data/songs.json','r'))["songs"]
try:
n = songs.index(no)
if n == 0:
n = len(songs)
play(sender=any,app_data=any,user_data=songs[n-1])
except:
pass
def next():
global state,no
try:
songs = json.load(open('data/songs.json','r'))["songs"]
n = songs.index(no)
if n == len(songs)-1:
n = -1
play(sender=any,app_data=any,user_data=songs[n+1])
except:
pass
def stop():
global state
pygame.mixer.music.stop()
state=None
def add_files():
data=json.load(open("data/songs.json","r"))
root=Tk()
root.withdraw()
filename=filedialog.askopenfilename(filetypes=[("Music Files", ("*.mp3","*.wav","*.ogg"))])
root.quit()
if filename.endswith(".mp3" or ".wav" or ".ogg"):
if filename not in data["songs"]:
update_database(filename)
dpg.add_button(label=f"{ntpath.basename(filename)}",callback=play,width=-1,height=25,user_data=filename.replace("\\","/"),parent="list")
dpg.add_spacer(height=2,parent="list")
def add_folder():
data=json.load(open("data/songs.json","r"))
root=Tk()
root.withdraw()
folder=filedialog.askdirectory()
root.quit()
for filename in os.listdir(folder):
if filename.endswith(".mp3" or ".wav" or ".ogg"):
if filename not in data["songs"]:
update_database(os.path.join(folder,filename).replace("\\","/"))
dpg.add_button(label=f"{ntpath.basename(filename)}",callback=play,width=-1,height=25,user_data=os.path.join(folder,filename).replace("\\","/"),parent="list")
dpg.add_spacer(height=2,parent="list")
def search(sender, app_data, user_data):
songs = json.load(open("data/songs.json", "r"))["songs"]
dpg.delete_item("list", children_only=True)
for index, song in enumerate(songs):
if app_data in song.lower():
dpg.add_button(label=f"{ntpath.basename(song)}", callback=play,width=-1, height=25, user_data=song, parent="list")
dpg.add_spacer(height=2,parent="list")
def removeall():
songs = json.load(open("data/songs.json", "r"))
songs["songs"].clear()
json.dump(songs,open("data/songs.json", "w"),indent=4)
dpg.delete_item("list", children_only=True)
load_database()
with dpg.theme(tag="base"):
with dpg.theme_component():
dpg.add_theme_color(dpg.mvThemeCol_Button, (130, 142, 250))
dpg.add_theme_color(dpg.mvThemeCol_ButtonActive, (137, 142, 255, 95))
dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (137, 142, 255))
dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_ChildRounding, 4)
dpg.add_theme_style(dpg.mvStyleVar_FramePadding, 4, 4)
dpg.add_theme_style(dpg.mvStyleVar_WindowRounding, 4, 4)
dpg.add_theme_style(dpg.mvStyleVar_WindowTitleAlign, 0.50, 0.50)
dpg.add_theme_style(dpg.mvStyleVar_WindowBorderSize,0)
dpg.add_theme_style(dpg.mvStyleVar_WindowPadding,10,14)
dpg.add_theme_color(dpg.mvThemeCol_ChildBg, (25, 25, 25))
dpg.add_theme_color(dpg.mvThemeCol_Border, (0,0,0,0))
dpg.add_theme_color(dpg.mvThemeCol_ScrollbarBg, (0,0,0,0))
dpg.add_theme_color(dpg.mvThemeCol_TitleBgActive, (130, 142, 250))
dpg.add_theme_color(dpg.mvThemeCol_CheckMark, (221, 166, 185))
dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (172, 174, 197))
with dpg.theme(tag="slider_thin"):
with dpg.theme_component():
dpg.add_theme_color(dpg.mvThemeCol_FrameBgActive, (130, 142, 250,99))
dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (130, 142, 250,99))
dpg.add_theme_color(dpg.mvThemeCol_SliderGrabActive, (255, 255, 255))
dpg.add_theme_color(dpg.mvThemeCol_SliderGrab, (255, 255, 255))
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (130, 142, 250,99))
dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_GrabMinSize, 30)
with dpg.theme(tag="slider"):
with dpg.theme_component():
dpg.add_theme_color(dpg.mvThemeCol_FrameBgActive, (130, 142, 250,99))
dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (130, 142, 250,99))
dpg.add_theme_color(dpg.mvThemeCol_SliderGrabActive, (255, 255, 255))
dpg.add_theme_color(dpg.mvThemeCol_SliderGrab, (255, 255, 255))
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (130, 142, 250,99))
dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_GrabMinSize, 30)
with dpg.theme(tag="songs"):
with dpg.theme_component():
dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 2)
dpg.add_theme_color(dpg.mvThemeCol_Button, (89, 89, 144,40))
dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (0,0,0,0))
with dpg.font_registry():
monobold = dpg.add_font("fonts/MonoLisa-Bold.ttf", 12)
head = dpg.add_font("fonts/MonoLisa-Bold.ttf", 15)
with dpg.window(tag="main",label="window title"):
with dpg.child_window(autosize_x=True,height=45,no_scrollbar=True):
dpg.add_text(f"Now Playing : ",tag="csong")
dpg.add_spacer(height=2)
with dpg.group(horizontal=True):
with dpg.child_window(width=200,tag="sidebar"):
dpg.add_text("Rainy Musicart",color=(137, 142, 255))
dpg.add_text("Build by NotCookey")
dpg.add_spacer(height=2)
dpg.add_button(label="Support",width=-1,height=23,callback=lambda:webbrowser.open(url="https://github.com/NotCookey/Rainy"))
dpg.add_spacer(height=5)
dpg.add_separator()
dpg.add_spacer(height=5)
dpg.add_button(label="Add File",width=-1,height=28,callback=add_files)
dpg.add_button(label="Add Folder",width=-1,height=28,callback=add_folder)
dpg.add_button(label="Remove All Songs",width=-1,height=28,callback=removeall)
dpg.add_spacer(height=5)
dpg.add_separator()
dpg.add_spacer(height=5)
dpg.add_text(f"State: {state}",tag="cstate")
dpg.add_spacer(height=5)
dpg.add_separator()
with dpg.child_window(autosize_x=True,border=False):
with dpg.child_window(autosize_x=True,height=80,no_scrollbar=True):
with dpg.group(horizontal=True):
with dpg.group(horizontal=True):
dpg.add_button(label="Play",width=65,height=30,tag="play",callback=play_pause)
dpg.add_button(label="Pre",width=65,height=30,show=True,tag="pre",callback=pre)
dpg.add_button(label="Next",tag="next",show=True,callback=next,width=65,height=30)
dpg.add_button(label="Stop",callback=stop,width=65,height=30)
dpg.add_slider_float(tag="volume", width=120,height=15,pos=(10,59),format="%.0f%.0%",default_value=_DEFAULT_MUSIC_VOLUME * 100,callback=update_volume)
dpg.add_slider_float(tag="pos",width=-1,pos=(140,59),format="")
with dpg.child_window(autosize_x=True,delay_search=True):
with dpg.group(horizontal=True,tag="query"):
dpg.add_input_text(hint="Search for a song",width=-1,callback=search)
dpg.add_spacer(height=5)
with dpg.child_window(autosize_x=True,delay_search=True,tag="list"):
load_database()
dpg.bind_item_theme("volume","slider_thin")
dpg.bind_item_theme("pos","slider")
dpg.bind_item_theme("list","songs")
dpg.bind_theme("base")
dpg.bind_font(monobold)
def safe_exit():
pygame.mixer.music.stop()
pygame.quit()
atexit.register(safe_exit)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("main",True)
dpg.maximize_viewport()
dpg.start_dearpygui()
dpg.destroy_context()