-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
309 lines (244 loc) · 9.45 KB
/
functions.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import tkinter as tk
import pygame
from os import listdir
from os.path import isfile, join
import subprocess
import os
import shutil
import subprocess
import pygame
from tkinter import filedialog
from tkinter import messagebox
import time
from tkinter.ttk import *
def get_split_files() -> list:
mypath = 'split'
filenames = [f for f in listdir(mypath) if isfile(join(mypath, f))]
# alternative way
# from os import walk
# filenames = next(walk(mypath), (None, None, []))[2] # [] if no file
return filenames
def get_rubberband_files() -> list:
mypath = 'rubberband'
filenames = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return filenames
def get_output_files() -> list:
mypath = 'output'
filenames = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return filenames
def divide_chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
def clear_frame(frame3):
for widgets in frame3.winfo_children():
widgets.destroy()
pass
def generate_output_previw(frame3):
print('generate_output_previw.....')
clear_frame(frame3)
files = get_output_files()
if len(files) == 0:
show_error_dialog("Please generate files first")
else:
n = 6
files_chunks = list(divide_chunks(files, n))
grid_rows = len(files_chunks)
for row in range(grid_rows):
for col in range(len(files_chunks[row])):
frame = tk.Frame(
master=frame3,
relief=tk.RAISED,
borderwidth=1
)
frame.grid(row=row, column=col, padx=5, pady=5)
file_output = str(f"{files_chunks[row][col]}")
button = tk.Button(master=frame, text=file_output,
activebackground='magenta',
command=lambda file_output3=file_output: play_audio_output(file_output3))
button.grid(row=row, column=col, sticky="nsew")
def generate_rubberband_previw(frame3):
print('generate_rubberband_previw.....')
clear_frame(frame3)
files = get_rubberband_files()
if len(files) == 0:
show_error_dialog("Please generate files first")
else:
n = 8
files_chunks = list(divide_chunks(files, n))
grid_rows = len(files_chunks)
for row in range(grid_rows):
for col in range(len(files_chunks[row])):
frame = tk.Frame(
master=frame3,
relief=tk.RAISED,
borderwidth=1
)
frame.grid(row=row, column=col, padx=5, pady=5)
file_rubberband = str(f"{files_chunks[row][col]}")
button = tk.Button(master=frame, text=file_rubberband,
activebackground='darkorange',
command=lambda file_rubberband3=file_rubberband: play_audio_rubberband(
file_rubberband3))
button.grid(row=row, column=col, sticky="nsew")
def generate_split_previw(frame3):
# time.sleep(2.4)
print('generate_split_previw.....')
clear_frame(frame3)
files = get_split_files()
if len(files) == 0:
show_error_dialog("Please generate files first")
else:
n = 8
files_chunks = list(divide_chunks(files, n))
grid_rows = len(files_chunks)
for row in range(grid_rows):
for col in range(len(files_chunks[row])):
frame = tk.Frame(
master=frame3,
relief=tk.RAISED,
borderwidth=1
)
frame.grid(row=row, column=col, padx=5, pady=5)
# label = tk.Label(master=frame, text=f"Row {i}\nColumn {j}")
# label = tk.Label(master=frame, text=f" {files_chunks[i][j]} ")
# label.pack(padx=5, pady=5)
file_split = str(f"{files_chunks[row][col]}")
button = tk.Button(master=frame, text=file_split, activebackground='darkorange',
command=lambda file_split3=file_split: play_audio_split(file_split3))
button.grid(row=row, column=col, sticky="nsew")
#######################################
# Dialog Warning Error
#######################################
def show_info_dialog(message):
messagebox.showinfo("Dialog", message)
def show_warning_dialog(message):
messagebox.showwarning("Warning", message)
def show_error_dialog(message):
messagebox.showerror("Error", message)
def show_confirm_dialog(message):
result = messagebox.askyesno("Confirmation", message)
if result:
print("User clicked Yes")
else:
print("User clicked No")
#######################################
# top menu function file
#######################################
def file_open():
file_path = filedialog.askopenfilename(filetypes=[("MP3 Files", "*.mp3"), ("WAV Files", "*.wav")])
if file_path:
print(file_path)
save_file(file_path)
def load_file():
file_path = filedialog.askopenfilename(filetypes=(("Audio Files", ".wav .mp3"), ("All Files", "*.*")))
if file_path:
# Process the loaded file here
print(file_path)
save_file(file_path)
def save_file(file_path):
file_name, file_extension = os.path.splitext(file_path)
# save_path = filedialog.asksaveasfilename(defaultextension=".mp3")
save_path = './load/in' + file_extension
if save_path:
# Save the file to the specified path
shutil.copy(file_path, save_path)
show_info_dialog("File saved to:" + file_path)
if file_extension == ".wav":
command = f'ffmpeg -i load/in.wav -y load/in.mp3'
subprocess.check_output(command, shell=True).decode()
#######################################
# player functions
#######################################
def play_audio():
pygame.mixer.music.load("./load/in.mp3")
pygame.mixer.music.play()
def stop_audio():
pygame.mixer.music.stop()
def play_audio_splits(file):
pass
# print(file)
def play_audio_rubberband(file):
print(file)
pygame.mixer.music.load("./rubberband/" + file)
pygame.mixer.music.play()
def play_audio_output(file):
print(file)
pygame.mixer.music.load("./output/" + file)
pygame.mixer.music.play()
def play_audio_split(file):
print(file)
pygame.mixer.music.load("./split/" + file)
pygame.mixer.music.play()
"""
from pygame import mixer
mixer.init()
mixer.music.load(f"split/{file}")
print("music started playing....")
mixer.music.set_volume(0.7)
mixer.music.play()
"""
#######################################
# clean old random files
#######################################
def clean_tmp_files(output_label):
command = 'rm -f split/*'
output = subprocess.check_output(command, shell=True).decode()
output_label.config(text=output)
command = 'rm -f output/*'
output = subprocess.check_output(command, shell=True).decode()
output_label.config(text=output)
command = 'rm -f rubberband/*'
output = subprocess.check_output(command, shell=True).decode()
output_label.config(text=output)
#######################################
# bash cmd for split rubberband shuffle
#######################################
def execute_command_split(frame3):
# label = tk.Label(frame3, text="creating split files ...", bg="lightgrey", anchor="nw")
# label.pack()
# time.sleep(2.4)
# clean_tmp_files(output_label)
# subprocess.call(["bash", "-c", "ls -lha"])
# command = "ls -la"
command = 'bash zmix_split.sh -i ./load/in.mp3 -d yes -t 1 || echo "failed"'
# command = "zmix_pitch.sh -i load/in.mp3 -d yes -t 1"
output = subprocess.check_output(command, shell=True).decode()
# output_label.config(text=output)
show_info_dialog("Split files created succesfully")
def execute_command_rubberband(frame3):
#label = tk.Label(frame3, text="creating rubberband files ...", bg="lightgrey", anchor="nw")
#label.pack()
# clean_tmp_files()
command = 'bash zmix_rubberband.sh -i load/in.mp3 -d yes -t 1 || echo "failed"'
output = subprocess.check_output(command, shell=True).decode()
# output_label.config(text=output)
show_info_dialog("Rubberband files created succesfully")
# def execute_command_shuffle():
# # clean_tmp_files(output_label)
# command = 'bash zmix_shuffle.sh -i load/in.mp3 -d yes -t * || echo "failed"'
# output = subprocess.check_output(command, shell=True).decode()
# # output_label.config(text=output)
# show_info_dialog("Shuffle files created succesfully")
# def button_click():
# label.config(text="Button Clicked!")
# def load_file():
# file_path = filedialog.askopenfilename()
# if file_path:
# # Process the file here
# print("File selected:", file_path)
# def load_file():
# file_path = filedialog.askopenfilename()
# if file_path:
# # Process the loaded file here
# save_file(file_path)
#
#
# def save_file(file_path):
# save_path = filedialog.asksaveasfilename(defaultextension=".txt")
# if save_path:
# # Save the file to the specified path
# with open(save_path, "w") as output_file:
# #with open(file_path, "rb") as input_file:
# with open(file_path, encoding="utf8", errors='ignore') as input_file:
# output_file.write(input_file.read())
# print("File saved to:", save_path)