-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundboard_app.py
120 lines (103 loc) · 4.82 KB
/
soundboard_app.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
import os
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog
import tkinter.colorchooser as colorchooser
from audio_manager import AudioManager
from sound_button import SoundButton
from keyboard_shortcut_handler import KeyboardShortcutHandler
# Generate a random hex colour on startup
import random
random_number = random.randint(0,16777215)
hex_number = str(hex(random_number))
hex_number ='#'+ hex_number[2:]
# Debug Log it
print(" ")
print("LOG: Randomly generated hex colour: ", hex_number)
class SoundboardApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Soundboard")
self.audio_manager = AudioManager()
self.bar_color = hex_number # Apply the previously generated hex colour
self.create_menu()
self.buttons_frame = tk.Frame(self)
self.buttons_frame.pack(pady=10)
self.create_buttons()
# Create an instance of KeyboardShortcutHandler
self.shortcut_handler = KeyboardShortcutHandler(self)
def create_menu(self):
menu_bar = tk.Menu(self)
self.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Load File", command=self.load_file)
file_menu.add_command(label="Load Folder", command=self.load_folder)
settings_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="Settings", menu=settings_menu)
settings_menu.add_command(label="Change Bar Colour", command=self.pick_color)
settings_menu.add_command(label="Assign Shortcuts", command=self.assign_shortcuts)
help_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=self.show_about)
self.color_bar_frame = tk.Frame(self, height=5, bg=self.bar_color)
self.color_bar_frame.pack(side=tk.TOP, fill=tk.X)
def pick_color(self):
new_color = colorchooser.askcolor(title="Choose Bar Colour")[1]
if new_color:
self.bar_color = new_color
self.color_bar_frame.config(bg=new_color)
def create_buttons(self):
self.buttons = []
for i in range(8):
row = i // 4
col = i % 4
button = SoundButton(self.buttons_frame, self.audio_manager, text="Empty")
button.grid(row=row, column=col, padx=10, pady=10)
self.buttons.append(button)
def load_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3 *.wav")])
if file_path:
try:
self.audio_manager.load_audio(file_path)
audio_name = os.path.basename(file_path)
for button in self.buttons:
if button.audio_path is None:
button.set_audio(file_path, audio_name)
break
except Exception as e:
messagebox.showerror("Error", str(e))
def load_folder(self):
folder_path = filedialog.askdirectory()
if folder_path:
try:
folder_name = os.path.basename(folder_path)
self.title(f"Soundboard - {folder_name}")
audio_files = [f for f in os.listdir(folder_path) if f.endswith(('.mp3', '.wav'))]
for i, audio_file in enumerate(audio_files):
file_path = os.path.join(folder_path, audio_file)
self.audio_manager.load_audio(file_path)
audio_name = os.path.basename(file_path)
if i < len(self.buttons):
self.buttons[i].set_audio(file_path, audio_name)
else:
button = SoundButton(self.buttons_frame, self.audio_manager, text=audio_name)
button.set_audio(file_path, audio_name)
button.grid(row=(i // 4), column=(i % 4), padx=10, pady=10)
self.buttons.append(button)
except Exception as e:
messagebox.showerror("Error", str(e))
def assign_shortcuts(self):
for button in self.buttons:
if button.audio_path:
shortcut_key = simpledialog.askstring(
"Assign Shortcut",
f"Enter a shortcut key for '{button['text']}':",
parent=self
)
if shortcut_key:
self.shortcut_handler.register_shortcut(button, shortcut_key)
def show_about(self):
messagebox.showinfo(title="Soundboard", message="Version 1.22.10\nhttps://github.com/ViciousSquid/Soundboard")
if __name__ == "__main__":
app = SoundboardApp()
app.mainloop()