2021-01-10 11:09:45 +01:00
|
|
|
import math
|
|
|
|
|
|
|
|
from find_loudness import process_find_loudness
|
|
|
|
|
|
|
|
graph_density = 8000
|
2021-01-11 16:46:53 +01:00
|
|
|
threshold_at_point = 1
|
2021-01-10 11:09:45 +01:00
|
|
|
inertia_s = 0.3
|
|
|
|
inertia_samples = inertia_s * graph_density
|
|
|
|
|
|
|
|
|
2021-01-18 12:14:23 +01:00
|
|
|
def set_up_threshold(new_value: int):
|
|
|
|
"""Setup threshold value"""
|
|
|
|
global threshold_at_point
|
|
|
|
if new_value > 0 and isinstance(new_value, int):
|
|
|
|
threshold_at_point = int(new_value)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-01-10 11:09:45 +01:00
|
|
|
def s(n: int):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Frame conversions"""
|
2021-01-10 11:09:45 +01:00
|
|
|
global graph_density
|
|
|
|
return n / graph_density
|
|
|
|
|
|
|
|
|
|
|
|
def seconds(units: int):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Return seconds."""
|
2021-01-10 11:09:45 +01:00
|
|
|
return math.floor(s(units) % 60)
|
|
|
|
|
|
|
|
|
|
|
|
def minutes(units: int):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Return minutes."""
|
2021-01-10 11:09:45 +01:00
|
|
|
return math.floor(s(units) / 60)
|
|
|
|
|
|
|
|
|
|
|
|
def hours(units: int):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Return hours."""
|
2021-01-10 11:09:45 +01:00
|
|
|
return math.floor(s(units) / 60 / 60)
|
|
|
|
|
|
|
|
|
|
|
|
def formatTime(units: int):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Format time HH:MM:SS."""
|
2021-01-10 11:09:45 +01:00
|
|
|
return f"{hours(units)}:{minutes(units)}:{seconds(units)}"
|
|
|
|
|
|
|
|
|
|
|
|
def new_mode(m, s):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Set new node"""
|
2021-01-10 11:09:45 +01:00
|
|
|
data = m
|
|
|
|
data[s['label']] = s['loud']
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def mode_to_string(mode):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Convert mode type to string"""
|
2021-01-10 11:09:45 +01:00
|
|
|
if mode['left'] and mode['right']:
|
|
|
|
return 'both'
|
|
|
|
elif mode['left']:
|
|
|
|
return 'left'
|
|
|
|
elif mode['right']:
|
|
|
|
return 'right'
|
|
|
|
else:
|
|
|
|
return "none"
|
|
|
|
|
|
|
|
|
|
|
|
def run(tmp_dir):
|
2021-01-18 12:14:23 +01:00
|
|
|
"""Main method to run generating demuxer.txt"""
|
2021-01-10 11:09:45 +01:00
|
|
|
global inertia_samples
|
|
|
|
out_demuxer = 'demuxer.txt'
|
|
|
|
|
|
|
|
with open(out_demuxer, 'w') as demuxer:
|
|
|
|
# Execute process_find_loudness for left and right side
|
|
|
|
left_loudness = process_find_loudness(
|
|
|
|
tmp_dir + "/audio/leftraw",
|
|
|
|
threshold_at_point=threshold_at_point,
|
|
|
|
inertia_samples=inertia_samples,
|
|
|
|
label="left")
|
|
|
|
|
|
|
|
right_loudness = process_find_loudness(
|
|
|
|
tmp_dir + "/audio/rightraw",
|
|
|
|
threshold_at_point=threshold_at_point,
|
|
|
|
inertia_samples=inertia_samples,
|
|
|
|
label="right")
|
|
|
|
|
|
|
|
merged = [*left_loudness, *right_loudness]
|
|
|
|
sorted_list = sorted(merged, key=lambda x: x['position_start'])
|
|
|
|
|
|
|
|
demuxer.write(F"file {tmp_dir}/pics/none.png\n")
|
|
|
|
|
|
|
|
last_point = 0
|
|
|
|
mode = {'left': False, 'right': False}
|
|
|
|
last_file = ''
|
|
|
|
total = 0
|
|
|
|
|
|
|
|
for i in range(2, len(sorted_list)):
|
|
|
|
point = sorted_list[i]
|
|
|
|
mode = new_mode(m=mode, s=point)
|
|
|
|
|
|
|
|
file = F"{tmp_dir}/pics/{mode_to_string(mode)}.png"
|
|
|
|
duration = (point['position_start'] - last_point) / graph_density
|
|
|
|
demuxer.write(F"duration {duration}\n")
|
|
|
|
demuxer.write(F"file {file}\n")
|
|
|
|
last_point = point['position_start']
|
|
|
|
last_file = file
|
|
|
|
total += duration * graph_density
|
|
|
|
|
|
|
|
demuxer.write(F"duration {sorted_list[len(sorted_list) - 1]['duration'] / graph_density}\n")
|
|
|
|
demuxer.write(F"file {last_file}\n")
|
|
|
|
print(F"{total} {formatTime(total)}\n")
|