-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffler.py
114 lines (90 loc) · 2.78 KB
/
Shuffler.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
import pygame
import random
import os
import numpy as np
import soundfile as sf
from tkinter import Tk, filedialog
import tkinter as tk
os.environ['SDL_VIDEODRIVER'] = 'x11'
def choose_music_folder():
Tk().withdraw()
folder_path = filedialog.askdirectory()
return folder_path
def get_music_files(folder_path):
music_files = []
for file in os.listdir(folder_path):
if file.endswith(".wav"):
music_files.append(os.path.join(folder_path, file))
return music_files
def play_next_song():
global current_song_index
current_song_index = (current_song_index + 1) % len(playlist)
pygame.mixer.music.load(playlist[current_song_index])
pygame.mixer.music.play()
def shuffle_playlist():
global playlist, shuffled_playlist, current_song_index
shuffled_playlist = playlist.copy()
np.random.shuffle(shuffled_playlist)
current_song_index = 0
pygame.mixer.music.load(shuffled_playlist[current_song_index])
pygame.mixer.music.play()
def update_current_song_label():
current_song_label.config(text=f"Current Song: {os.path.basename(playlist[current_song_index])}")
def play_pause():
global paused
if paused:
pygame.mixer.music.unpause()
paused = False
play_pause_button.config(text="Pause")
else:
pygame.mixer.music.pause()
paused = True
play_pause_button.config(text="Play")
def next_song():
play_next_song()
update_current_song_label()
def shuffle_songs():
shuffle_playlist()
update_current_song_label()
def quit_music_player():
pygame.mixer.music.stop()
window.quit()
# Initialize Pygame
pygame.init()
# Choose the music folder
music_folder = choose_music_folder()
# Get the music files from the chosen folder
playlist = get_music_files(music_folder)
# Shuffle the playlist initially
current_song_index = 0
shuffle_playlist()
# Create the GUI window
window = tk.Tk()
window.title("Song Playlist")
window.geometry("400x200")
# Current Song Label
current_song_label = tk.Label(window, text="Current Song: ")
current_song_label.pack()
# Play/Pause Button
paused = False
play_pause_button = tk.Button(window, text="Pause", command=play_pause)
play_pause_button.pack()
# Next Song Button
next_song_button = tk.Button(window, text="Next Song", command=next_song)
next_song_button.pack()
# Shuffle Button
shuffle_button = tk.Button(window, text="Shuffle", command=shuffle_songs)
shuffle_button.pack()
# Quit Button
quit_button = tk.Button(window, text="Quit", command=quit_music_player)
quit_button.pack()
# Update the initial current song label
update_current_song_label()
# Initialize mixer module
pygame.mixer.init()
pygame.mixer.music.load(playlist[current_song_index])
pygame.mixer.music.play()
# Run the GUI main loop
window.mainloop()
# Quit Pygame
pygame.quit()