-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
123 lines (92 loc) · 3.72 KB
/
gui.py
File metadata and controls
123 lines (92 loc) · 3.72 KB
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
# ========================== Imports ==========================
import tkinter as tk
from tkinter import filedialog, ttk
import ttkbootstrap as tb
from ttkbootstrap.constants import *
import soundfile as sf
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from interpolation import filters
# ========================== Application State ==========================
audio_data = None
# ========================== Load Audio File ==========================
def load_file():
global audio_data
file_path = filedialog.askopenfilename(filetypes=[("WAV files", "*.wav")])
if file_path:
data, samplerate = sf.read(file_path)
duration = len(data) / samplerate
info = f"Sample Rate: {samplerate} Hz\nDuration: {duration:.2f} seconds"
print(info)
print("Selected Interpolation Method:", method_var.get())
print("Upsampling Factor:", L_var.get())
info_label.config(text=info)
audio_data = data
# ========================== Interpolation & Visualization ==========================
def visualize():
if audio_data is None:
return
method = method_var.get()
L = L_var.get()
x_zi = filters.insert_zeros(audio_data, L)
if method == "ZOH":
x_interp = filters.zoh(x_zi)
elif method == "Linear":
x_interp = filters.linear(x_zi, L)
elif method == "FIR":
x_interp = filters.fir(x_zi, L)
else:
return
ax.clear()
ax.plot(x_interp[:500], label=method)
ax.set_title("Interpolated Signal")
ax.legend()
canvas.draw()
# ========================== GUI Setup ==========================
root = tb.Window(themename="flatly")
root.title("Audio Interpolation GUI")
# ---------- Top: File Load ----------
top_frame = ttk.Frame(root)
top_frame.grid(row=0, column=0, padx=10, sticky="w")
load_button = ttk.Button(top_frame, text="Load WAV File", command=load_file)
load_button.grid(row=0, column=0, padx=(0, 10))
info_label = ttk.Label(top_frame, text="No file loaded.")
info_label.grid(row=0, column=1, sticky="w")
# ---------- Parameters ----------
param_frame = ttk.Frame(root)
param_frame.grid(row=1, column=0, padx=10, pady=5, sticky="w")
# Upsampling Factor
L_var = tk.IntVar(value=4)
L_label = ttk.Label(param_frame, text="Upsampling Factor (L):")
L_label.grid(row=0, column=0, sticky="w")
L_entry = ttk.Entry(param_frame, textvariable=L_var, width=5)
L_entry.grid(row=0, column=1, padx=(5, 20))
# Interpolation Method
method_var = tk.StringVar(value="ZOH")
method_label = ttk.Label(param_frame, text="Interpolation Method:")
method_label.grid(row=1, column=0, sticky="w")
methods = ["ZOH", "Linear", "FIR"]
for i, method in enumerate(methods):
rb = ttk.Radiobutton(param_frame, text=method, variable=method_var, value=method)
rb.grid(row=1, column=i + 1, padx=5)
# ---------- Action Button ----------
action_frame = ttk.Frame(root)
action_frame.grid(row=2, column=0, padx=10, pady=10, sticky="ew")
action_frame.columnconfigure(0, weight=1)
visualize_button = ttk.Button(action_frame, text="Visualize", command=visualize)
visualize_button.grid(row=0, column=0)
# ---------- Plot Frame ----------
plot_frame = ttk.Frame(root)
plot_frame.grid(row=3, column=0, pady=(0, 10), sticky="w")
figure, ax = plt.subplots(figsize=(6, 3))
canvas = FigureCanvasTkAgg(figure, master=plot_frame)
canvas.get_tk_widget().grid(row=0, column=0)
# ---------- Final Layout ----------
root.update_idletasks()
width, height = root.winfo_reqwidth(), root.winfo_reqheight()
x = (root.winfo_screenwidth() // 2) - (width // 2)
y = (root.winfo_screenheight() // 2) - (height // 2)
root.geometry(f"{width}x{height}+{x}+{y}")
root.resizable(False, False)
root.protocol("WM_DELETE_WINDOW", root.quit)
root.mainloop()