-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsampleApp.py
172 lines (152 loc) · 6.24 KB
/
sampleApp.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
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
import pydub
import random
phase = 0
root = Tk()
#root.geometry('700x500+0+0')
root.title("Sample App!")
sourceFileString = StringVar()
noiseFileString = StringVar()
outputFileString = StringVar()
#TODO: add noise factor
#TODO: add more formats that are supported:
# https://ffmpeg.org/general.html#File-Formats
acceptableFileTypes = [
('Audio Files','*.mp3;*.m4a;*.aac;*.wav;*.ogg'),
('Video Files','*.mp4;*.avi')
]
def getSourceFileString(*args):
global sourceFileString, phase
if(phase < 1):
sourceFileString.set("Empty")
return
sourceFileString.set( filedialog.askopenfilename(filetypes=acceptableFileTypes) )
def getNoiseFileString(*args):
global noiseFileString, phase
if(phase < 1):
noiseFileString.set("Empty")
return
noiseFileString.set( filedialog.askopenfilename(filetypes=acceptableFileTypes) )
def sliceAudio(*args):
global sourceFileString, noiseFileString, phase, outputFileString
if(phase < 1):
outputFileString.set("Empty!")
return
#
if(len(sourceFileString.get()) < 6):
# TODO Use dialog
print "Missing a source file!"
return
if(len(noiseFileString.get()) < 6):
# TODO Use dialog
print "Missing a noise file!"
return
print("Doing the magic!")
sourceFile = sourceFileString.get()
noiseFile = noiseFileString.get()
noiseFactor = '' #int(args['noiseFactor'])
source_audio = pydub.AudioSegment.empty()
noise_audio = pydub.AudioSegment.empty()
try:
fileFormat = sourceFile[-3:]
#print("You want to open a file of type: (%s)" % fileFormat)
source_audio = pydub.AudioSegment.from_file(sourceFile, format=fileFormat)
if(len(source_audio) < 3000):
print("error! The source file is too short! It should be at least 3 seconds long")
return
except:
print ( "error opening the source file: %s" % (sourceFile.get() ))
e = sys.exc_info()[0]
print( "\tError: %s" % e )
return
try:
fileFormat = noiseFile[-3:]
noise_audio = pydub.AudioSegment.from_file(noiseFile, format=fileFormat)
if(len(noise_audio) < 3000):
print("error! The noise file is too short! It should be at least 3 seconds long")
return
except:
print ( "error opening the noise file: %s" % (noiseFile.get() ))
e = sys.exc_info()[0]
print( "\tError: %s" % e )
return
final_audio = pydub.AudioSegment.empty()
# source_audio[2000:7000] + noise_audio[3000:5000] + source_audio[9000:14000]
#save the length of the original audio for bound checking
original_length = len(source_audio)
# leave the first 1% of the audio unedited
currentIndex = original_length/100
final_audio += source_audio[0:currentIndex]
# Define the noise factor
if(noiseFactor):
if(noiseFactor < 0):
noiseFactor = random.random()
while(noiseFactor > 1):
noiseFactor = noiseFactor / 10
else:
noiseFactor = 0.6
# A boolean flag to safeguard against playing the noise too many
# times in a row
noiseJustUsed = False
while(currentIndex < original_length):
percentage = 100*currentIndex/original_length
print ("\tProgress: %d percent\r" % percentage),
# Randomly pick how many seconds of the audio we will work on in
# this step of the loop
nextSegment = random.randint(600,2350)
audioSnippet = pydub.AudioSegment.empty()
if(random.random() < noiseFactor and noiseJustUsed == False):
# Choose a random starting point in the noise audio
startIndex = random.randint(0,len(noise_audio)-nextSegment)
# Pull out a segment of the noise file
audioSnippet = noise_audio[startIndex:startIndex+nextSegment]
noiseJustUsed = True
else:
# Pull out the next segment of the source file
audioSnippet = source_audio[currentIndex:currentIndex+nextSegment]
noiseJustUsed = False
final_audio += audioSnippet
currentIndex += nextSegment
#TODO: Add support for various export file formats
#outputFile = "./output%d.mp3" % random.randint(1,9999)
outputFile = filedialog.asksaveasfilename(filetypes=[
('Basic MP3 Audio','*.mp3')]) + ".mp3"
final_audio.export(outputFile, format="mp3")
outputFileString.set(outputFile)
def main():
global root, phase, sourceFileString, noiseFileString
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
p = ttk.Panedwindow(mainframe, orient=HORIZONTAL)
p.grid(column=1,row=1,sticky=(N,W,E,S))
# first pane, which would get widgets gridded into it:
f1 = ttk.Labelframe(p, text='Source Audio', width=400, height=300)
ttk.Label(f1, text="Press \"A\" to choose the noise file", command=getSourceFileString()).grid(sticky=W)
ttk.Label(f1, textvariable=sourceFileString).grid( sticky=(W, E))
p.add(f1)
f2 = ttk.Labelframe(p, text='Noise Audio', width=400, height=300) # second pane
ttk.Label(f2, text="Press \"K\" to choose the noise file", command=getNoiseFileString()).grid(sticky=W)
ttk.Label(f2, textvariable=noiseFileString).grid(sticky=W)
#f2.pack()
p.add(f2)
secondaryPane = ttk.Panedwindow(mainframe, orient=VERTICAL)
secondaryPane.grid(column=1,row=2,sticky=(N,W,E,S))
ttk.Label(secondaryPane, text="Press the space bar to slice the audio files", command=sliceAudio()).grid(sticky="W")
ttk.Label(secondaryPane, text="Output:").grid(sticky=W)
ttk.Label(secondaryPane, textvariable=outputFileString).grid(sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.bind('a', getSourceFileString)
root.bind('k', getNoiseFileString)
root.bind('<space>',sliceAudio)
phase += 1
# Let the window wrap all of the content we just loaded and then
# prevent it from resizing
root.resizable(FALSE,FALSE)
root.mainloop()
# this gives a main function in Python
if __name__ == "__main__":
main()