-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
373 lines (315 loc) · 12.7 KB
/
Copy pathmain.py
File metadata and controls
373 lines (315 loc) · 12.7 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import pyaudio
import audioop
import math
import time
import threading
import mido
import mido.backends.rtmidi
import tkinter as tk
# Initialisation of the LTC listener
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 48000
CHUNK = 2048
SYNC_WORD = '0011111111111101'
jam = '00:00:00:00'
now_tc = '00:00:00:00'
last_cam = '-1'
jam_advice = False
jammed = False
codes = [49,50,51,52,53,54,55,56,57,48]
cams = {}
for i,j in enumerate(codes):
cams[j] = str(i+1)
def bin_to_bytes(a,size=1):
return int(a,2).to_bytes(size,byteorder='little')
def bin_to_int(a):
return sum(int(j) * 2 ** i for i, j in enumerate(a))
def decode_frame(frame):
o = {}
o['frame_units'] = bin_to_int(frame[:4])
o['user_bits_1'] = int.from_bytes(bin_to_bytes(frame[4:8]),byteorder='little')
o['frame_tens'] = bin_to_int(frame[8:10])
o['drop_frame'] = int.from_bytes(bin_to_bytes(frame[10]),byteorder='little')
o['color_frame'] = int.from_bytes(bin_to_bytes(frame[11]),byteorder='little')
o['user_bits_2'] = int.from_bytes(bin_to_bytes(frame[12:16]),byteorder='little')
o['sec_units'] = bin_to_int(frame[16:20])
o['user_bits_3'] = int.from_bytes(bin_to_bytes(frame[20:24]),byteorder='little')
o['sec_tens'] = bin_to_int(frame[24:27])
o['flag_1'] = int.from_bytes(bin_to_bytes(frame[27]),byteorder='little')
o['user_bits_4'] = int.from_bytes(bin_to_bytes(frame[28:32]),byteorder='little')
o['min_units'] = bin_to_int(frame[32:36])
o['user_bits_5'] = int.from_bytes(bin_to_bytes(frame[36:40]),byteorder='little')
o['min_tens'] = bin_to_int(frame[40:43])
o['flag_2'] = int.from_bytes(bin_to_bytes(frame[43]),byteorder='little')
o['user_bits_6'] = int.from_bytes(bin_to_bytes(frame[44:48]),byteorder='little')
o['hour_units'] = bin_to_int(frame[48:52])
o['user_bits_7'] = int.from_bytes(bin_to_bytes(frame[52:56]),byteorder='little')
o['hour_tens'] = bin_to_int(frame[56:58])
o['bgf'] = int.from_bytes(bin_to_bytes(frame[58]),byteorder='little')
o['flag_3'] = int.from_bytes(bin_to_bytes(frame[59]),byteorder='little')
o['user_bits_8'] = int.from_bytes(bin_to_bytes(frame[60:64]),byteorder='little')
o['sync_word'] = int.from_bytes(bin_to_bytes(frame[64:],2),byteorder='little')
o['formatted_tc'] = "{:02d}:{:02d}:{:02d}:{:02d}".format(
o['hour_tens']*10+o['hour_units'],
o['min_tens']*10+o['min_units'],
o['sec_tens']*10+o['sec_units'],
o['frame_tens']*10+o['frame_units'],
)
return o
def print_tc():
global jam,now_tc
freq = str_frequency_to_int(selected_frequency.get())
print(freq)
inter = 1/freq
last_jam = jam
h,m,s,f = [int(x) for x in jam.split(':')]
while enable_listening.get():
if jam == None:
break
if jam != last_jam:
h,m,s,f = [int(x) for x in jam.split(':')]
last_jam = jam
tcp = "{:02d}:{:02d}:{:02d}:{:02d}".format(h,m,s,f)
if compare_timestamps(tcp,jam) < 1.5:
send_mtc_signal(tcp)
status_color.set("green")
else:
status_color.set("orange")
status_square.configure(bg=status_color.get())
now_tc = tcp
time.sleep(inter)
f += 1
if f >= freq:
f = 0
s += 1
if s >= 60:
s = 0
m += 1
if m >= 60:
m = 0
h += 1
def decode_ltc(wave_frames):
global jam
frames, output, last, toggle, sp = [], '', None, True, 1
for i in range(0, len(wave_frames), 2):
data = wave_frames[i:i+2]
cyc = 'Neg' if audioop.minmax(data, 2)[0] < 0 else 'Pos'
if cyc != last:
if sp >= 7:
if sp > 14:
bit = '0'
elif toggle:
bit = '1'
else:
bit = ''
output += bit
toggle = not toggle if sp <= 14 else True
if len(output) >= len(SYNC_WORD) and output[-len(SYNC_WORD):] == SYNC_WORD:
if len(output) > 80:
frame_data = output[-80:]
frames.append(frame_data)
output = ''
jam = decode_frame(frame_data)['formatted_tc']
send_mtc_signal(jam)
sp = 1
else:
sp += 1
last = cyc
def loop_decode_ltc(stream,frames):
data = stream.read(CHUNK, exception_on_overflow=False)
volume_db = get_volume_db(data)
label_volume.config(text=f"Volume: {round(volume_db)} dB")
decode_ltc(data)
frames.append(data)
if enable_listening.get():
frame.after(10,lambda:loop_decode_ltc(stream,frames))
def init_ltc_listener():
micro_selectionne = selected_microphone_index.get()
p = pyaudio.PyAudio()
t = threading.Thread(target=print_tc)
t.start()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
input_device_index=micro_selectionne)
frames = []
loop_decode_ltc(stream,frames)
# Write in MIDI port the MTC values
def send_mtc_signal(timecode_str):
midi_port = selected_midi.get()
global max_frames_per_second
global autodetect_frequency
frequency = str_frequency_to_int(selected_frequency.get())
# Verify timecode format (HH:MM:SS:FF)
try:
hours, minutes, seconds, frames = map(int, timecode_str.split(':'))
except (ValueError, IndexError):
raise ValueError("Invalid timecode format. Use HH:MM:SS:FF format (e.g., '01:23:45:15').")
# Verify validity of numbers
if not 0 <= hours < 24 or not 0 <= minutes < 60 or not 0 <= seconds < 60 or not 0 <= frames < 30:
raise ValueError("Invalid timecode values. Hours should be in the range 0-23, minutes 0-59, seconds 0-59, and frames 0-29 (depending on frame rate).")
label_timecode.config(text=f"Timecode : {timecode_str}")
# Calculating complete MTC message
mtc_hours = decimal_to_hex_pair(hours)
mtc_minutes = decimal_to_hex_pair(minutes)
mtc_seconds = decimal_to_hex_pair(seconds)
mtc_frames = decimal_to_hex_pair(frames)
# Manual frequency selector
if frequency == 24:
mtc_frequency = 0
if frequency == 25:
mtc_frequency = 1
if frequency == 30:
mtc_frequency = 2
try:
with mido.open_output(midi_port) as port:
# Send MIDI messages
message = mido.Message('quarter_frame', frame_type=0, frame_value=mtc_frames[1])
port.send(message)
message = mido.Message('quarter_frame', frame_type=1, frame_value=mtc_frames[0])
port.send(message)
message = mido.Message('quarter_frame', frame_type=2, frame_value=mtc_seconds[1])
port.send(message)
message = mido.Message('quarter_frame', frame_type=3, frame_value=mtc_seconds[0])
port.send(message)
message = mido.Message('quarter_frame', frame_type=4, frame_value=mtc_minutes[1])
port.send(message)
message = mido.Message('quarter_frame', frame_type=5, frame_value=mtc_minutes[0])
port.send(message)
message = mido.Message('quarter_frame', frame_type=6, frame_value=mtc_hours[1])
port.send(message)
message = mido.Message('quarter_frame', frame_type=7, frame_value=mtc_frequency)
port.send(message)
except (IOError, ValueError) as e:
print(f"Error: {e}")
def decimal_to_hex_pair(decimal_value):
binary_value = bin(decimal_value)[2:].zfill(8)
first_4_bits = binary_value[:4]
decimal_value_1 = int(first_4_bits, 2)
last_4_bits = binary_value[4:]
decimal_value_2 = int(last_4_bits, 2)
return [decimal_value_1, decimal_value_2]
def time_to_seconds(time):
hh, mm, ss, ff = map(int, time.split(':'))
total_seconds = hh * 3600 + mm * 60 + ss + ff / 30
return total_seconds
def compare_timestamps(timestamp1, timestamp2):
return time_to_seconds(timestamp1) - time_to_seconds(timestamp2)
# Get available microphones list
def get_default_input_device_name():
p = pyaudio.PyAudio()
default_index = p.get_default_input_device_info()['index']
default_name = p.get_device_info_by_index(default_index)['name']
p.terminate()
return default_name
def get_available_microphones():
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
num_devices = info.get('deviceCount')
microphones = []
for i in range(num_devices):
device_info = p.get_device_info_by_index(i)
if device_info.get('maxInputChannels') > 0:
microphones.append(device_info['name'])
p.terminate()
default_name = get_default_input_device_name()
if default_name in microphones:
microphones.remove(default_name)
microphones.insert(0, default_name)
return microphones
# Get available output MIDI ports
def get_available_midis():
ports = []
for port in mido.get_output_names():
ports.append(port)
return ports
# Convert string from frequency selector to extract only the integer value
def str_frequency_to_int(str):
if str == "24 Hz":
return 24
elif str == "25 Hz":
return 25
elif str == "30 Hz":
return 30
else:
return 0
def get_volume_db(data: bytes, sample_width: int = 2) -> float:
try:
rms = audioop.rms(data, sample_width)
if rms > 0:
return 20 * math.log10(rms)
else:
return float('-inf')
except Exception as e:
print(f"Erreur lors du calcul du volume : {e}")
return float('-inf')
# Toggle LTC Listener from button
def toggle_read_ltc():
enable_listening.set(True) if enable_listening.get() == False else enable_listening.set(False)
status_color.set("Orange") if enable_listening.get() == True else status_color.set("Red")
status_square.configure(bg=status_color.get())
if enable_listening.get():
toggle_button.configure(text="Disable listener")
label_microphone.configure(state="disabled")
label_frequency.configure(state="disabled")
label_midi.configure(state="disabled")
else:
toggle_button.configure(text="Enable listener")
label_microphone.configure(state="normal")
label_frequency.configure(state="normal")
label_midi.configure(state="normal")
init_ltc_listener()
# Defines values from lists
microphones_options = get_available_microphones()
frequencies_options = ["24 Hz", "25 Hz", "30 Hz"]
midis_options = get_available_midis()
# Create main frame
frame = tk.Tk()
frame.title("SMPTE LTC to MTC 1.1.0")
frame.geometry("300x450")
frame.resizable(width=False, height=False)
# Define variables from tk
selected_microphone = tk.StringVar(value=microphones_options[0])
selected_frequency = tk.StringVar(value=frequencies_options[0])
selected_midi = tk.StringVar(value=midis_options[0])
selected_microphone_index = tk.IntVar(value=0)
enable_listening = tk.BooleanVar(value=False)
status_color = tk.StringVar(value="Red")
# Configure grid to center elements
for i in range(11):
frame.grid_rowconfigure(i, weight=1)
frame.grid_columnconfigure(i, weight=1)
# Draw status square
status_square = tk.Canvas(frame, width=50, height=50, bg="red")
status_square.grid(row=0, column=4, pady=10, sticky="n")
# Draw microphone selector
label_microphone = tk.Label(frame, text="Select microphone", font=("Helvetica", 10, "bold"))
label_microphone.grid(row=1, column=4, pady=5, sticky="n")
label_microphone = tk.OptionMenu(frame, selected_microphone, *microphones_options)
label_microphone.grid(row=2, column=4, pady=5, sticky="n")
# Draw frequency selector
label_frequency = tk.Label(frame, text="Select frequency", font=("Helvetica", 10, "bold"))
label_frequency.grid(row=3, column=4, pady=5, sticky="n")
label_frequency = tk.OptionMenu(frame, selected_frequency, *frequencies_options)
label_frequency.grid(row=4, column=4, pady=5, sticky="n")
# Draw MIDI output selector
label_midi = tk.Label(frame, text="Select MIDI output", font=("Helvetica", 10, "bold"))
label_midi.grid(row=6, column=4, pady=5, sticky="n")
label_midi = tk.OptionMenu(frame, selected_midi, *midis_options)
label_midi.grid(row=7, column=4, pady=5, sticky="n")
# Draw toggle button
toggle_button = tk.Button(frame, text="Enable listener", command=toggle_read_ltc)
toggle_button.grid(row=8, column=4, pady=10, sticky="n")
# Draw timecode
label_timecode = tk.Label(frame, text="Timecode", font=("Helvetica", 10, "bold"))
label_timecode.grid(row=9, column=4, pady=10, sticky="n")
# Draw volume
label_volume = tk.Label(frame, text="Volume", font=("Helvetica", 10, "bold"))
label_volume.grid(row=11, column=4, pady=10, sticky="n")
# Autostart
#toggle_read_ltc()
# Starting main loop
frame.mainloop()