-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignments.py
322 lines (261 loc) · 12.2 KB
/
alignments.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
310
311
312
313
314
315
316
317
318
319
320
321
'''
Original author of audio sync functions: Allison Deal
Code adapted from her VideoSync project at:
https://github.com/allisonnicoledeal/VideoSync
Special thanks to Greg Kramida:
https://github.com/Algomorph
'''
from PyQt5 import QtWidgets
import scipy.io.wavfile
import numpy as np
import math, tempfile, os, pathlib, subprocess
from urllib.parse import unquote
FFT_BIN_SIZE=1024
OVERLAP=0
BOX_HEIGHT=512
BOX_WIDTH=43
SAMPLES_PER_BOX=7
SUBJECT_DURATION=120
RERFERR_DURATION=60
def extract_audio(dir,clip_mrl: str):
# print(f"Extract audio from {clip_mrl}.")
filepath = unquote(clip_mrl.split("//")[-1])
clip_name = str(os.path.getsize(filepath)) + '_' + os.path.basename(filepath)
audio_output = ''.join(clip_name.split(".")[:-1]) + "WAV.wav" # !! CHECK TO SEE IF FILE IS IN UPLOADS DIRECTORY
outfile = dir + audio_output
of = pathlib.Path(outfile)
if of.exists():
print(f"Wave file {outfile} existed, do nothing.")
else:
subprocess.run(["ffmpeg", "-y", "-i", filepath, "-vn", "-ac", "1", "-f", "wav", outfile],stdout = subprocess.DEVNULL,stderr = subprocess.DEVNULL)
return outfile
# Read file
# INPUT: Audio file
# OUTPUT: Sets sample rate of wav file, Returns data read from wav file (numpy array of integers)
def read_audio(audio_file):
rate, data = scipy.io.wavfile.read(audio_file) # Return the sample rate (in samples/sec) and data from a WAV file
# print(rate)
return data, rate
def make_horiz_bins(data, fft_bin_size, overlap, box_height):
horiz_bins = {}
# process first sample and set matrix height
sample_data = data[0:fft_bin_size] # get data for first sample
if (len(sample_data) == fft_bin_size): # if there are enough audio points left to create a full fft bin
intensities = fourier(sample_data) # intensities is list of fft results
for i in range(len(intensities)):
box_y = int(i/box_height)
if box_y in horiz_bins:
horiz_bins[box_y].append((intensities[i], 0, i)) # (intensity, x, y)
else:
horiz_bins[box_y] = [(intensities[i], 0, i)]
# process remainder of samples
x_coord_counter = 1 # starting at second sample, with x index 1
for j in range(int(fft_bin_size - overlap), len(data), int(fft_bin_size-overlap)):
sample_data = data[j:j + fft_bin_size]
if (len(sample_data) == fft_bin_size):
intensities = fourier(sample_data)
for k in range(len(intensities)):
box_y = int(k/box_height)
if box_y in horiz_bins:
horiz_bins[box_y].append((intensities[k], x_coord_counter, k)) # (intensity, x, y)
else:
horiz_bins[box_y] = [(intensities[k], x_coord_counter, k)]
x_coord_counter += 1
return horiz_bins
# Compute the one-dimensional discrete Fourier Transform
# INPUT: list with length of number of samples per second
# OUTPUT: list of real values len of num samples per second
def fourier(sample): #, overlap):
mag = []
fft_data = np.fft.fft(sample) # Returns real and complex value pairs
for i in range(int(len(fft_data)/2)):
r = fft_data[i].real**2
j = fft_data[i].imag**2
mag.append(round(math.sqrt(r+j),2))
return mag
def make_vert_bins(horiz_bins, box_width):
boxes = {}
for key in horiz_bins.keys():
for i in range(len(horiz_bins[key])):
box_x = int(horiz_bins[key][i][1] / box_width)
if (box_x,key) in boxes:
boxes[(box_x,key)].append((horiz_bins[key][i]))
else:
boxes[(box_x,key)] = [(horiz_bins[key][i])]
return boxes
def find_bin_max(boxes, maxes_per_box):
freqs_dict = {}
for key in boxes.keys():
max_intensities = [(1,2,3)]
for i in range(len(boxes[key])):
if boxes[key][i][0] > min(max_intensities)[0]:
if len(max_intensities) < maxes_per_box: # add if < number of points per box
max_intensities.append(boxes[key][i])
else: # else add new number and remove min
max_intensities.append(boxes[key][i])
max_intensities.remove(min(max_intensities))
for j in range(len(max_intensities)):
if max_intensities[j][2] in freqs_dict:
freqs_dict[max_intensities[j][2]].append(max_intensities[j][1])
else:
freqs_dict[max_intensities[j][2]] = [max_intensities[j][1]]
return freqs_dict
def find_freq_pairs(freqs_dict_orig, freqs_dict_sample):
time_pairs = []
for key in freqs_dict_sample.keys(): # iterate through freqs in sample
if key in freqs_dict_orig: # if same sample occurs in base
for i in range(len(freqs_dict_sample[key])): # determine time offset
for j in range(len(freqs_dict_orig[key])):
time_pairs.append((freqs_dict_sample[key][i], freqs_dict_orig[key][j]))
return time_pairs
def find_delay(time_pairs):
t_diffs = {}
for i in range(len(time_pairs)):
delta_t = time_pairs[i][0] - time_pairs[i][1]
if delta_t in t_diffs:
t_diffs[delta_t] += 1
else:
t_diffs[delta_t] = 1
t_diffs_sorted = sorted(t_diffs.items(), key=lambda x: x[1])
time_delay = t_diffs_sorted[-1][0]
return time_delay
class AdjustClipPosDialog(QtWidgets.QDialog):
ALIGN_FIRST = 0
ALIGN_OVERLAP = 1
ALIGN_LAST = 2
def __init__(self, clip):
super().__init__()
self.clip = clip
self.setWindowTitle(f"Adjust Time of {clip.name}")
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(QtWidgets.QLabel(f'{clip.name} current starats at {clip.durMsStr(clip.sPos)}({clip.sPos/1000})'))
self.adjTabs = QtWidgets.QTabWidget(self)
self.manualAdjPage = QtWidgets.QWidget()
manualPagelayout = QtWidgets.QVBoxLayout()
inputs = QtWidgets.QHBoxLayout()
self.direction = QtWidgets.QComboBox(self)
self.direction.addItem('Delay')
self.direction.addItem('Advance')
# seconds = floor(clip.sPos / 1000)
# minutes = floor(seconds/60)
self.mins = QtWidgets.QSpinBox(self)
# self.mins.setValue(floor(minutes))
self.mins.setMinimum(0)
self.mins.setMaximum(59)
self.secs = QtWidgets.QSpinBox(self)
self.secs.setMinimum(0)
self.secs.setMaximum(59)
# self.secs.setValue(seconds - minutes * 60)
self.ms = QtWidgets.QSpinBox(self)
self.ms.setMinimum(0)
self.ms.setMaximum(999)
# self.ms.setValue(clip.sPos % 1000)
inputs.addStretch()
inputs.addWidget(self.direction)
inputs.addWidget(self.mins)
inputs.addWidget(QtWidgets.QLabel("m:"))
inputs.addWidget(self.secs)
inputs.addWidget(QtWidgets.QLabel("s."))
inputs.addWidget(self.ms)
manualPagelayout.addLayout(inputs)
self.manualAdjPage.setLayout(manualPagelayout)
self.adjTabs.addTab(self.manualAdjPage, "Manual Offsetting")
self.autoAdjPage = QtWidgets.QWidget()
autoPagelayout = QtWidgets.QVBoxLayout()
autoPagelayout.addWidget(QtWidgets.QLabel(f'(Experimental) Auto forward align {clip.name} with:'))
self.tracksBox = QtWidgets.QComboBox()
self.parentTrack = clip.parent()
self.tracks = self.parentTrack.parent()
if len(self.tracks.tracks) > 1:
for t in self.tracks.tracks:
if t != self.parentTrack:
self.tracksBox.addItem(t.text(), t)
else:
self.tracksBox.addItem("Can't do with single track.")
self.tracksBox.setDisabled(True)
autoPagelayout.addWidget(self.tracksBox)
self.alignOption = QtWidgets.QButtonGroup(self.autoAdjPage)
alignOptionBtns = QtWidgets.QHBoxLayout()
btn = QtWidgets.QRadioButton("First Match")
btn.setChecked(True)
self.alignOption.addButton(btn, self.ALIGN_FIRST)
alignOptionBtns.addWidget(btn)
btn = QtWidgets.QRadioButton("Overlaping Clip")
self.alignOption.addButton(btn, self.ALIGN_OVERLAP)
alignOptionBtns.addWidget(btn)
btn = QtWidgets.QRadioButton("Last Match")
self.alignOption.addButton(btn, self.ALIGN_LAST)
alignOptionBtns.addWidget(btn)
autoPagelayout.addLayout(alignOptionBtns)
self.autoAdjPage.setLayout(autoPagelayout)
self.adjTabs.addTab(self.autoAdjPage, "Auto Detect (by audio)")
self.layout.addWidget(self.adjTabs)
QBtn = QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
self.buttonBox = QtWidgets.QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
def getMS(self) -> int:
if self.adjTabs.currentWidget() == self.manualAdjPage:
direction = 1
if self.direction.currentText() == 'Advance': direction = -1
offset = ((self.mins.value() * 60 + self.secs.value())*1000 + self.ms.value()) * direction
print (f'Manually adjusted {self.clip.name} by {offset} ms.')
elif self.adjTabs.currentWidget() == self.autoAdjPage:
offset = 0
refSPos = 0
milliseconds = 0
with tempfile.TemporaryDirectory() as tmpdirname:
print(f'Temporary directory{tmpdirname} created.')
# The value represents the offset between subject and reference clips
# Negative value means the subject is ahead of reference.
milliseconds = 0
# Process the subject file
print(f"Trying to allign {self.clip.name}.")
wavFileS = extract_audio(tmpdirname+'/', self.clip.mrl)
rawAudioS, rate = read_audio(wavFileS)
binsDictS = make_horiz_bins(rawAudioS[:44100*SUBJECT_DURATION], FFT_BIN_SIZE, OVERLAP, BOX_HEIGHT)
boxesS = make_vert_bins(binsDictS, BOX_WIDTH)
ftDictS = find_bin_max(boxesS, SAMPLES_PER_BOX)
# Loop through reference clips in track
for c in self.tracksBox.currentData().clips:
if c.ePos < self.clip.sPos:
print(f"{c.name} skipped, no backward matching.")
continue
if self.alignOption.checkedId() == self.ALIGN_OVERLAP and c.sPos > self.clip.ePos:
print(f"Only compairing overlaping clips, stop now.")
break
refSPos = c.sPos
wavFileR = extract_audio(tmpdirname+'/', c.mrl)
rawAudioR, rate = read_audio(wavFileR)
binsDictR = make_horiz_bins(rawAudioR[:44100*RERFERR_DURATION], FFT_BIN_SIZE, OVERLAP, BOX_HEIGHT)
boxesR = make_vert_bins(binsDictR, BOX_WIDTH)
ftDictR = find_bin_max(boxesR, SAMPLES_PER_BOX)
# Determie time delay between subject and reference wav file
pairs = find_freq_pairs(ftDictS, ftDictR)
delay = find_delay(pairs)
samples_per_sec = float(rate) / float(FFT_BIN_SIZE)
milliseconds = int(round(float(delay) / float(samples_per_sec), 4) * 1000)
print(f"Found diff {milliseconds}ms with {c.name}.")
if self.alignOption.checkedId() == self.ALIGN_FIRST:
print("Matched first clip, stop now.")
break
offset = refSPos - self.clip.sPos + milliseconds
else:
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Critical)
msg.setText("Invaild option.")
msg.setWindowTitle("Error")
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.exec_()
return
return offset
@staticmethod
def getMsShift(clip):
dialog = AdjustClipPosDialog(clip)
result = dialog.exec()
if result == QtWidgets.QDialog.Accepted:
return dialog.getMS(), result
else:
return 0, result