|
8 | 8 | import gc |
9 | 9 | import json |
10 | 10 | import time |
11 | | - |
| 11 | +from threading import RLock |
12 | 12 | import sys |
13 | 13 |
|
14 | 14 | ''' |
@@ -257,42 +257,49 @@ def show2(fs,s1,s2,title=None): |
257 | 257 | show1(fs,s2,'red') |
258 | 258 | plt.show() |
259 | 259 |
|
| 260 | +lock_fallback = RLock() |
260 | 261 | def second_correlation(in1,in2): |
261 | | - begin = time.time() |
262 | | - fs,s1,s2 = read_normalized(in1,in2) |
263 | | - ls1,ls2,padsize,xmax,ca = corrabs(s1,s2) |
264 | | - ls1 = None |
265 | | - ls2 = None |
266 | | - ca = None |
267 | | - s1 = None |
268 | | - s2 = None |
269 | | - # if show: show1(fs,ca,title='Correlation',v=xmax/fs) Change if we want reports |
270 | | - #sync_text = """ |
271 | | - #============================================================================== |
272 | | - #%s needs 'ffmpeg -ss %s' cut to get in sync |
273 | | - #============================================================================== |
274 | | - #""" |
275 | | - if xmax > padsize // 2: |
276 | | - # if show: show2(fs,s1,s2[padsize-xmax:],title='1st=blue;2nd=red=cut(%s;%s)'%(in1,in2)) |
277 | | - file,offset = in2,(padsize-xmax)/fs |
278 | | - else: |
279 | | - # if show: show2(fs,s1[xmax:],s2,title='1st=blue=cut;2nd=red (%s;%s)'%(in1,in2)) |
280 | | - file,offset = in1,xmax/fs |
281 | | - #print(sync_text%(file,offset)) |
282 | | - padsize = None |
283 | | - xmax = None |
284 | | - fs = None |
285 | | - gc.collect() |
286 | | - |
287 | | - sys.stderr.write(f"\t\tSecond correlation in old function took {time.time()-begin:.2f} seconds\n\t\tand we obtain: {file} in offset {offset}\n") |
288 | 262 | try: |
289 | 263 | begin = time.time() |
290 | | - stdout, stderror, exitCode = tools.launch_cmdExt_with_timeout_reload([tools.software["audio_sync"],in1,in2],5,120) |
| 264 | + stdout, stderror, exitCode = tools.launch_cmdExt_with_timeout_reload([tools.software["audio_sync"],in1,in2],5,300) |
291 | 265 | data = json.loads(stdout.decode("utf-8").strip()) |
292 | | - sys.stderr.write(f"\t\tSecond correlation in new function took {time.time()-begin:.2f} seconds\n\t\tand we obtain: {data}\n") |
| 266 | + file = data['file'] |
| 267 | + offset = data['offset_seconds'] |
| 268 | + if tools.dev: |
| 269 | + sys.stderr.write(f"\t\tSecond correlation in new function took {time.time()-begin:.2f} seconds\n\t\tand we obtain: {data}\n") |
293 | 270 | except Exception as e: |
294 | 271 | # If audio_sync is not installed, we return the file and offset |
295 | 272 | sys.stderr.write(f"\t\taudio_sync not working: {e}\n") |
| 273 | + |
| 274 | + with lock_fallback: |
| 275 | + begin = time.time() |
| 276 | + fs,s1,s2 = read_normalized(in1,in2) |
| 277 | + ls1,ls2,padsize,xmax,ca = corrabs(s1,s2) |
| 278 | + ls1 = None |
| 279 | + ls2 = None |
| 280 | + ca = None |
| 281 | + s1 = None |
| 282 | + s2 = None |
| 283 | + # if show: show1(fs,ca,title='Correlation',v=xmax/fs) Change if we want reports |
| 284 | + #sync_text = """ |
| 285 | + #============================================================================== |
| 286 | + #%s needs 'ffmpeg -ss %s' cut to get in sync |
| 287 | + #============================================================================== |
| 288 | + #""" |
| 289 | + if xmax > padsize // 2: |
| 290 | + # if show: show2(fs,s1,s2[padsize-xmax:],title='1st=blue;2nd=red=cut(%s;%s)'%(in1,in2)) |
| 291 | + file,offset = in2,(padsize-xmax)/fs |
| 292 | + else: |
| 293 | + # if show: show2(fs,s1[xmax:],s2,title='1st=blue=cut;2nd=red (%s;%s)'%(in1,in2)) |
| 294 | + file,offset = in1,xmax/fs |
| 295 | + #print(sync_text%(file,offset)) |
| 296 | + padsize = None |
| 297 | + xmax = None |
| 298 | + fs = None |
| 299 | + gc.collect() |
| 300 | + |
| 301 | + if tools.dev: |
| 302 | + sys.stderr.write(f"\t\tSecond correlation in old function took {time.time()-begin:.2f} seconds\n\t\tand we obtain: {file} in offset {offset}\n") |
296 | 303 |
|
297 | 304 | return file,offset |
298 | 305 |
|
|
0 commit comments