Skip to content

Commit 189791f

Browse files
authored
update
visual update prefix add option
1 parent e5944fd commit 189791f

1 file changed

Lines changed: 151 additions & 100 deletions

File tree

ABS.py

Lines changed: 151 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,102 +5,146 @@
55
from pydub import AudioSegment, effects
66

77

8+
### color settings
89

10+
scolor="violetred3"
11+
scolor2="DeepPink4"
12+
bgcolor="snow4"
13+
windowcolor="grey18"
914

1015
#### Layout
1116

1217

13-
selector = [[sg.Text('Folder'), sg.In(size=(25,1), enable_events=True ,key='-FOLDER-'), sg.FolderBrowse()]]
18+
selector = [[
19+
sg.Text('Folder', background_color=bgcolor),
20+
sg.In(size=(25,1), enable_events=True ,key='-FOLDER-',background_color="snow1", readonly=True),
21+
sg.FolderBrowse(button_color=scolor),
22+
sg.Checkbox('Iterate down folder tree? (Recursive)', key="-REC-", enable_events=True, background_color=scolor),
23+
sg.Checkbox('Show paths?', key="-SHOWPATHS-", enable_events=True, background_color=scolor)
24+
]]
1425

15-
filelist = [sg.Listbox([], size=(128,16), key= '-LIST-', background_color="purple", text_color="white")]
26+
filelist = [sg.Listbox([], size=(0,16), key= '-LIST-', background_color=bgcolor, text_color="white", expand_x=True, sbar_background_color=scolor)]
1627

17-
loglist = [sg.Listbox([], no_scrollbar=True, size=(128,8), key= '-LOG-', background_color="dark slate blue", text_color="white")]
28+
loglist = [sg.Listbox([], no_scrollbar=True, size=(0,8), key= '-LOG-', background_color=scolor2, text_color="white", expand_x=True)]
1829
log = []
1930

20-
modulecol = [
21-
[sg.Checkbox('Trim Leading Silence', key='-TRIM-')],
22-
[sg.Checkbox('Normalize peak to 0db', key='-NORM-')],
23-
[sg.Checkbox('Convert 32Bit to 24Bit', key="-BIT-")],
24-
[sg.Checkbox('Delete empty audio files', key="-EMPTY-")],
25-
[sg.Checkbox('Include _Master / _Current files? (FL)',default=True, key="-EMPTYFL-", pad=(30,0), background_color="purple")],
26-
27-
28-
29-
30-
]
31-
32-
bottomcol = [sg.Button("Process"), sg.Checkbox('Iterate down folder tree? (Recursive)', key="-REC-", enable_events=True)]
31+
modulecol1 = sg.Column([
32+
[sg.Checkbox('Trim Leading Silence', key='-TRIM-', background_color=bgcolor)],
33+
[sg.Checkbox('Normalize peak to 0db', key='-NORM-', background_color=bgcolor)],
34+
[sg.Checkbox('Convert 32Bit to 24Bit', key="-BIT-", background_color=bgcolor)],
35+
[sg.Checkbox('Delete empty audio files', key="-EMPTY-", background_color=bgcolor)],
36+
[sg.Checkbox('Include _Master / _Current files? (FL)',default=True, key="-EMPTYFL-", pad=(30,0), background_color=bgcolor,)]
37+
],
38+
vertical_alignment="top",
39+
background_color=bgcolor,
40+
expand_y=True)
41+
42+
modulecol2 = sg.Column([
43+
[sg.Checkbox('Prefix', key="-PREFIXBOOL-", background_color=bgcolor), sg.In(size=(10,0), key="-PREFIXSTR-")]
44+
],
45+
vertical_alignment="top",
46+
background_color=bgcolor,
47+
expand_y=True)
48+
49+
modulelist = [modulecol1, modulecol2]
50+
51+
bottomcol = sg.Column([[sg.Button("Process", button_color=scolor, size=30, border_width=4)], loglist], element_justification="c", justification="c", expand_x=True, background_color=windowcolor)
3352
folderset = False
3453

3554
layout = [[
3655
filelist,
37-
selector,
38-
modulecol,
39-
bottomcol,
40-
loglist,
56+
selector,
57+
[sg.HorizontalSeparator(pad=(0,10))],
58+
modulelist,
59+
[sg.HorizontalSeparator(pad=(0,10))],
60+
bottomcol
4161
]]
4262

43-
44-
sg.theme("DarkPurple1")
45-
window = sg.Window('Dions Scripts V1', layout,resizable=False, finalize=True)
63+
window = sg.Window('ABS', layout,resizable=True, finalize=True, background_color=windowcolor, size=(960,712))
4664

4765

4866
### helpers
4967

68+
def getfilenames(paths):
69+
fnames = []
70+
for path in paths:
71+
fnames.append(os.path.basename(path))
72+
return fnames
73+
74+
def updatefilelist(showpaths, paths):
75+
if showpaths:
76+
window.Element('-LIST-').update(values=paths)
77+
else:
78+
window.Element('-LIST-').update(values=getfilenames(paths))
79+
5080
def getfiles(directory, recursive):
51-
files = []
52-
for file in glob.iglob(directory + '**/**', recursive=recursive):
53-
if(file.endswith('.wav')):
54-
files.append(file)
55-
if len(files) == 0:
56-
filelog("No valid wavs found!")
57-
return files
81+
files = []
82+
for file in glob.iglob(directory + '**/**', recursive=recursive):
83+
if(file.endswith('.wav')):
84+
files.append(file)
85+
if len(files) == 0:
86+
filelog("No valid wavs found!")
87+
return files
5888

5989
def detect_leading_silence(sound, silence_threshold=-50.0, chunk_size=1):
60-
trim_ms = 0
61-
assert chunk_size > 0
62-
while sound[trim_ms:trim_ms+chunk_size].dBFS < silence_threshold and trim_ms < len(sound):
63-
trim_ms += chunk_size
90+
trim_ms = 0
91+
assert chunk_size > 0
92+
while sound[trim_ms:trim_ms+chunk_size].dBFS < silence_threshold and trim_ms < len(sound):
93+
trim_ms += chunk_size
6494

65-
return trim_ms
95+
return trim_ms
6696

6797
### Modules
6898

6999
def trimsilence(files):
70-
for file in files:
71-
sound = AudioSegment.from_file(file)
72-
start_trim = detect_leading_silence(sound)
73-
end_trim = detect_leading_silence(sound.reverse())
74-
duration = len(sound)
75-
trimmed_sound = sound[start_trim:duration - end_trim]
76-
trimmed_sound.export(file, format="wav")
77-
filelog("Trimmed " + file)
78-
100+
for file in files:
101+
fname = os.path.basename(file)
102+
sound = AudioSegment.from_file(file)
103+
start_trim = detect_leading_silence(sound)
104+
end_trim = detect_leading_silence(sound.reverse())
105+
duration = len(sound)
106+
trimmed_sound = sound[start_trim:duration - end_trim]
107+
trimmed_sound.export(file, format="wav")
108+
filelog("Trimmed " + fname)
79109

80110
def process_32to24(files):
81-
for file in files:
82-
if(file.endswith('.wav')):
83-
data, samplerate = soundfile.read(file)
84-
soundfile.write(file, data, samplerate, subtype='PCM_24')
85-
filelog("Converting " + file + " to 24 - bit")
86-
111+
for file in files:
112+
fname = os.path.basename(file)
113+
if(file.endswith('.wav')):
114+
data, samplerate = soundfile.read(file)
115+
soundfile.write(file, data, samplerate, subtype='PCM_24')
116+
filelog("Converting " + fname + " to 24 - bit")
87117

88118
def normalize(files):
89-
for file in files:
90-
sound = AudioSegment.from_file(file, "wav")
91-
normalized_sound = effects.normalize(sound, headroom=0)
92-
normalized_sound.export(file, format="wav")
93-
filelog("Normalizing " + file)
94-
95-
96-
def rmempty(files):
97-
for file in files:
98-
audio = AudioSegment.from_file(file)
99-
loudness = audio.dBFS
100-
if loudness == float('-inf') or file.endswith(("_Master.wav", "_Current.wav")) or "SC" in file:
101-
filelog("Removing " + str(file))
102-
os.remove(file)
103-
119+
for file in files:
120+
fname = os.path.basename(file)
121+
sound = AudioSegment.from_file(file, "wav")
122+
normalized_sound = effects.normalize(sound, headroom=0)
123+
normalized_sound.export(file, format="wav")
124+
filelog("Normalizing " + fname)
125+
126+
def rmempty(files, FLStudio):
127+
for file in files:
128+
fname = os.path.basename(file)
129+
audio = AudioSegment.from_file(file)
130+
loudness = audio.dBFS
131+
if FLStudio:
132+
if loudness == float('-inf') or file.endswith(("_Master.wav", "_Current.wav")) or "SC" in file:
133+
filelog("Removing " + fname)
134+
os.remove(file)
135+
else:
136+
if loudness == float('-inf'):
137+
filelog("Removing " + fname)
138+
os.remove(file)
139+
140+
def setprefix(files, prefix):
141+
for file in files:
142+
fname = os.path.basename(file)
143+
d = os.path.dirname(file)
144+
filelog("Renamed to " + prefix + fname)
145+
os.rename(file, d + "/" + prefix + fname)
146+
sleep(1)
147+
updatefilelist(values["-SHOWPATHS-"], files)
104148

105149
### GUI Logic
106150

@@ -110,39 +154,46 @@ def filelog(logmsg):
110154

111155
filelog("Ready to juice! By Dion Timmer")
112156

113-
157+
## Event Loop
158+
114159
while True:
115-
event, values = window.read()
116-
if event in (sg.WIN_CLOSED, 'Exit'):
117-
break
118-
if event == '-FOLDER-':
119-
folder = values['-FOLDER-']
120-
currentfiles = getfiles(folder, values['-REC-'])
121-
window.Element('-LIST-').update(values=currentfiles)
122-
folderset = True
123-
filelog("Folder Set")
124-
125-
if event == '-REC-':
126-
if folderset == True:
127-
currentfiles = getfiles(folder, values['-REC-'])
128-
window.Element('-LIST-').update(values=currentfiles)
129-
130-
if event == 'Process':
131-
if folderset == True:
132-
currentfiles = getfiles(folder, values['-REC-'])
133-
try:
134-
if values['-BIT-'] == True:
135-
process_32to24(currentfiles)
136-
if values['-TRIM-'] == True:
137-
trimsilence(currentfiles)
138-
if values['-NORM-'] == True:
139-
normalize(currentfiles)
140-
if values['-EMPTY-'] == True:
141-
rmempty(currentfiles)
142-
143-
144-
145-
except NameError as error:
146-
filelog(error)
147-
148-
window.close()
160+
event, values = window.read()
161+
if event in (sg.WIN_CLOSED, 'Exit'):
162+
break
163+
if event == '-FOLDER-':
164+
folder = values['-FOLDER-']
165+
currentfiles = getfiles(folder, values['-REC-'])
166+
updatefilelist(values["-SHOWPATHS-"], currentfiles)
167+
folderset = True
168+
filelog("Folder Set")
169+
170+
if event == '-REC-':
171+
if folderset == True:
172+
currentfiles = getfiles(folder, values['-REC-'])
173+
updatefilelist(values["-SHOWPATHS-"], currentfiles)
174+
175+
if event == '-SHOWPATHS-':
176+
if folderset == True:
177+
updatefilelist(values["-SHOWPATHS-"], currentfiles)
178+
179+
180+
if event == 'Process':
181+
if folderset == True:
182+
currentfiles = getfiles(folder, values['-REC-'])
183+
try:
184+
if values['-BIT-'] == True:
185+
process_32to24(currentfiles)
186+
if values['-TRIM-'] == True:
187+
trimsilence(currentfiles)
188+
if values['-NORM-'] == True:
189+
normalize(currentfiles)
190+
if values['-EMPTY-'] == True:
191+
rmempty(currentfiles, values['-EMPTYFL-'])
192+
if values['-PREFIXBOOL-'] == True:
193+
setprefix(currentfiles, values["-PREFIXSTR-"])
194+
195+
196+
except NameError as error:
197+
filelog(error)
198+
199+
window.close()

0 commit comments

Comments
 (0)