-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyOrchestra0.5.py
More file actions
755 lines (613 loc) · 29.2 KB
/
Copy pathmyOrchestra0.5.py
File metadata and controls
755 lines (613 loc) · 29.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import cv2
import numpy as np
import time
from tkinter import Tk, filedialog
from collections import deque
import fluidsynth
import threading
from threading import Lock
import mediapipe as mp
import math
from queue import Queue
midi_queue = Queue()
import pretty_midi
current_beat = 0 # 当前播放的拍子序号
beat_lock = threading.Lock() # 节拍计数器锁
global_time_signature = (4, 4)
tuning_active = False
global_active_notes = {} # 格式: {(channel, pitch): {"end_sec": float, "velocity": int}}
global_notes_lock = threading.Lock()
# 初始化 MediaPipe Hands
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5)
rhythm_hand_label = None # 节奏手的左右信息("Left" 或 "Right")
control_hand_label = None # 变化手的左右信息("Left" 或 "Right")
last_distance_to_torso = None # 记录变化手上一帧与躯干的距离
beat_lock = Lock()
playback_thread = None
stop_playback = False
pose = mp.solutions.pose.Pose(static_image_mode=False, min_detection_confidence=0.4, min_tracking_confidence=0.4)
last_volume_update_time = None # 上一次音量调整的时间戳
velocity = 64 # 初始音量(0-127)
prev_palm_position = None # 用于记录上一帧手掌位置
volume = 150 # 初始音量
fluid_lock = Lock()
STOP_THRESHOLD = 20
STOP_DURATION = 0.02
NOTE_INTERVAL = 0.4
velocity = 64
bpm = 120
prev_position = None
prev_time = None
last_stop_time = None
current_beat = 0
root = Tk()
root.withdraw()
current_playback_position = 0.0 # 当前播放位置(秒)
playback_events = [] # 预处理的全局播放事件列表
fs = None # FluidSynth instance
def select_midi_and_soundfont_files():
"""Select MIDI and SoundFont files using a GUI dialog."""
global soundfont_path
# Select SoundFont file
print("请选择 SoundFont 文件。")
soundfont_path = filedialog.askopenfilename(
title="选择 SoundFont 文件",
filetypes=[("SoundFont 文件", "*.sf2"), ("所有文件", "*.*")]
)
if not soundfont_path:
print("未选择 SoundFont 文件,程序退出。")
cleanup_fluidsynth()
exit()
return midi_file_path
def process_frame_with_hand_detection(frame, hand_hist, prev_position, stop_detected, current_beat, beats_notes, total_beats, last_stop_time):
global hands, mp_drawing, mp_hands, pose, bpm, motion_amplitude, last_pause_info, playback_thread, stop_playback
global rhythm_hand_label, control_hand_label, velocity, last_volume_update_time, global_active_notes, global_notes_lock, tuning_active
# 初始化控制信号变量
play_beat_command = False # 节拍播放触发标志
current_bpm = bpm # 当前计算的 BPM
new_last_stop_time = last_stop_time # 用于存储新的挥手时间
speed_threshold = 120
distance_threshold = 80
MIN_INTERVAL = 0.4 # 最小挥手间隔(秒)
# 静态变量:记录上一次挥手时间和上一次velocity值
if not hasattr(process_frame_with_hand_detection, "last_swing_time"):
process_frame_with_hand_detection.last_swing_time = None
if not hasattr(process_frame_with_hand_detection, "last_velocity"):
process_frame_with_hand_detection.last_velocity = velocity
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 使用线程池并行处理姿态与手部检测
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
future_pose = executor.submit(pose.process, rgb_frame)
future_hands = executor.submit(hands.process, rgb_frame)
pose_result = future_pose.result()
hand_result = future_hands.result()
torso_center = None
if pose_result.pose_landmarks:
left_shoulder = pose_result.pose_landmarks.landmark[mp.solutions.pose.PoseLandmark.LEFT_SHOULDER]
right_shoulder = pose_result.pose_landmarks.landmark[mp.solutions.pose.PoseLandmark.RIGHT_SHOULDER]
torso_center = (
int((left_shoulder.x + right_shoulder.x) / 2 * frame.shape[1]),
int((left_shoulder.y + right_shoulder.y) / 2 * frame.shape[0])
)
if hand_result.multi_hand_landmarks and hand_result.multi_handedness:
hand_landmarks_list = hand_result.multi_hand_landmarks
handedness_list = hand_result.multi_handedness
# 在 tuning 阶段绘制手部骨骼,并区分节奏手和变化手的颜色
if tuning_active:
for idx, hand_landmarks in enumerate(hand_landmarks_list):
handedness = handedness_list[idx]
label = handedness.classification[0].label
# 如果对应标签已设置,则使用指定颜色,否则按默认绘制
if rhythm_hand_label is not None and label == rhythm_hand_label:
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
connection_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2)
)
elif control_hand_label is not None and label == control_hand_label:
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2),
connection_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2)
)
else:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 若节奏手或变化手标签未设置,则按照原逻辑赋值
if rhythm_hand_label is None or control_hand_label is None:
for idx, handedness in enumerate(handedness_list):
label = handedness.classification[0].label
if rhythm_hand_label is None:
rhythm_hand_label = label
elif control_hand_label is None and label != rhythm_hand_label:
control_hand_label = label
# 获取节奏手和变化手实例
rhythm_hand = None
control_hand = None
for idx, handedness in enumerate(handedness_list):
label = handedness.classification[0].label
if label == rhythm_hand_label:
rhythm_hand = hand_landmarks_list[idx]
elif label == control_hand_label:
control_hand = hand_landmarks_list[idx]
# 节奏手逻辑
if rhythm_hand:
wrist = rhythm_hand.landmark[mp_hands.HandLandmark.WRIST]
wrist_pos = (int(wrist.x * frame.shape[1]), int(wrist.y * frame.shape[0]))
# 挥手触发检测
if prev_position is not None:
dx = wrist_pos[0] - prev_position[0]
dy = wrist_pos[1] - prev_position[1]
distance = (dx**2 + dy**2)**0.5
if distance > distance_threshold and (distance > 0):
with global_notes_lock:
active_non_cross = any(
not note_info.get("cross_beat", False)
for note_info in global_active_notes.values()
)
if active_non_cross:
play_beat_command = False
else:
if process_frame_with_hand_detection.last_swing_time is not None:
interval = time.time() - process_frame_with_hand_detection.last_swing_time
if interval >= MIN_INTERVAL:
current_bpm = max(60, min(200, 60 / interval))
print(f"更新动态 BPM: {current_bpm:.2f}")
play_beat_command = True
new_last_stop_time = time.time()
else:
new_last_stop_time = time.time()
process_frame_with_hand_detection.last_swing_time = time.time()
prev_position = wrist_pos
#cv2.circle(frame, wrist_pos, 8, (0, 255, 0), -1)
if control_hand:
current_time = time.time()
control_wrist = control_hand.landmark[mp_hands.HandLandmark.WRIST]
control_wrist_pos = (int(control_wrist.x * frame.shape[1]), int(control_wrist.y * frame.shape[0]))
original_velocity = velocity # 记录修改前的velocity值
if torso_center:
distance_to_torso = ((control_wrist_pos[0] - torso_center[0])**2 +
(control_wrist_pos[1] - torso_center[1])**2)**0.5
if 'tuning_baseline_distance' in globals() and tuning_baseline_distance is not None:
up_threshold = tuning_baseline_distance + 80
down_threshold = tuning_baseline_distance - 80
if distance_to_torso > up_threshold:
extra = distance_to_torso - up_threshold
increments = int(extra // 30)
velocity = min(127, velocity + increments * 8)
elif distance_to_torso < down_threshold:
extra = down_threshold - distance_to_torso
decrements = int(extra // 30)
velocity = max(10, velocity - decrements * 8)
else:
dt = current_time - last_volume_update_time if last_volume_update_time else 0.2
diff = 64 - velocity
change = 15 * dt
if abs(diff) < change:
velocity = 64
else:
if diff > 0:
velocity += change
else:
velocity -= change
# 仅在velocity变化时输出
if process_frame_with_hand_detection.last_velocity != velocity:
print(int(velocity))
process_frame_with_hand_detection.last_velocity = velocity
with fluid_lock:
for channel in range(16):
fs.cc(channel, 7, int(velocity))
last_volume_update_time = current_time
#cv2.circle(frame, control_wrist_pos, 8, (0, 0, 255), -1)
return (
prev_position,
stop_detected,
current_beat,
new_last_stop_time if 'new_last_stop_time' in locals() else last_stop_time,
play_beat_command,
current_bpm
)
#未调用功能,可以自我发挥
def calculate_angle(vec1, vec2):
dot_product = vec1[0] * vec2[0] + vec1[1] * vec2[1]
magnitude1 = (vec1[0]**2 + vec1[1]**2)**0.5
magnitude2 = (vec2[0]**2 + vec2[1]**2)**0.5
if magnitude1 == 0 or magnitude2 == 0:
return 0 # 防止除以零
cos_theta = dot_product / (magnitude1 * magnitude2)
cos_theta = max(-1, min(1, cos_theta))
return math.degrees(math.acos(cos_theta))
def start_fluidsynth():
global fs, soundfont_path, sfid
if fs is not None:
return
try:
fs = fluidsynth.Synth()
fs.start(driver="coreaudio")
# 加载 SoundFont
sfid = fs.sfload(soundfont_path)
# 初始化所有通道
with fluid_lock:
for channel in range(16):
fs.program_select(channel, sfid, 0, 0)
print(f"FluidSynth 初始化完成 | 复音数: 256")
except Exception as e:
print(f"初始化 FluidSynth 时出错: {e}")
cleanup_fluidsynth()
exit()
def cleanup_fluidsynth():
global fs, playback_thread
if playback_thread and playback_thread.is_alive():
print("等待播放线程结束...")
stop_playback = True
playback_thread.join()
if fs is not None:
print("Cleaning up FluidSynth...")
fs.delete()
fs = None
def calculate_note_durations(bpm, time_signature=global_time_signature):
# 取拍号分母
numerator, denominator = time_signature
beat_duration = (60 / bpm) * (4 / denominator)
return {
"16th": beat_duration / 4,
"8th": beat_duration / 2,
"4th": beat_duration,
}
midi_file_path = filedialog.askopenfilename(title="选择 MIDI 文件", filetypes=[("MIDI 文件", "*.mid"), ("所有文件", "*.*")])
if not midi_file_path:
print("未选择 MIDI 文件,程序退出。")
cleanup_fluidsynth()
exit()
def preprocess_midi(midi_file_path):
global sfid, global_time_signature
try:
# 使用 pretty_midi 解析 MIDI 文件
midi_data = pretty_midi.PrettyMIDI(midi_file_path)
bpm = midi_data.estimate_tempo() # 获取估计的 BPM
ticks_per_beat = midi_data.resolution # 获取每拍的 ticks 数
# 提取拍号信息(取第一个拍号事件,如果存在)
if midi_data.time_signature_changes:
ts = midi_data.time_signature_changes[0]
global_time_signature = (ts.numerator, ts.denominator)
print(f"检测到拍号: {ts.numerator}/{ts.denominator}")
else:
print("未检测到拍号信息,默认 4/4")
all_voices_notes = []
# 遍历每个乐器(声部)
for instrument in midi_data.instruments:
if instrument.is_drum:
continue # 跳过鼓轨道
channel = instrument.program % 16
target_program = instrument.program % 128
with fluid_lock:
try:
fs.program_select(channel, sfid, 0, target_program)
print(f"通道{channel} 设置成功: Bank 0, Program {target_program}")
except fluidsynth.FluidError:
try:
fs.program_select(channel, sfid, 128, target_program)
print(f"通道{channel} 使用Bank 128, Program {target_program}")
except fluidsynth.FluidError:
fs.program_select(channel, sfid, 0, 0)
print(f"通道{channel} 音色不可用,已回退到钢琴")
voice_notes = {
"name": instrument.name if instrument.name else "Unnamed",
"program": channel,
"notes": [],
"original_bpm": bpm,
"next_note_index": 0 # 新增字段,用于跟踪本声部下一次要播放的音符
}
# 收集原始音符数据(以秒为单位)
for note in instrument.notes:
voice_notes["notes"].append({
"pitch": note.pitch,
"start_sec": note.start,
"end_sec": note.end,
"velocity": note.velocity,
"duration_sec": note.end - note.start
})
# 对音符列表按起始时间排序,确保后续按顺序播放
voice_notes["notes"].sort(key=lambda n: n["start_sec"])
all_voices_notes.append(voice_notes)
return all_voices_notes, ticks_per_beat, bpm
except Exception as e:
print(f"解析 MIDI 文件时出错: {e}")
cleanup_fluidsynth()
exit()
# Select and load MIDI and SoundFont files
midi_file_path = select_midi_and_soundfont_files()
start_fluidsynth()
beats_notes, ticks_per_beat, bpm = preprocess_midi(midi_file_path)
note_durations = calculate_note_durations(bpm)
total_beats = len(beats_notes)
last_pause_info = {"bpm": None, "tap_times": []}
def midi_event_processor():
global fs, stop_playback, fluid_lock, global_active_notes, global_notes_lock, midi_queue, current_beat
while True:
event = midi_queue.get() # 阻塞等待事件
with fluid_lock:
try:
if event["type"] == "note_on":
fs.noteon(event["channel"], event["pitch"], event["velocity"])
with global_notes_lock:
global_active_notes[(event["channel"], event["pitch"])] = {
"cross_beat": event.get("cross_beat", False),
"beat": event.get("beat")
}
elif event["type"] == "note_off":
fs.noteoff(event["channel"], event["pitch"])
with global_notes_lock:
key = (event["channel"], event["pitch"])
if key in global_active_notes:
del global_active_notes[key]
except fluidsynth.FluidError as e:
print(f"FluidSynth Error in midi_event_processor: {e}")
midi_queue.task_done()
def panic_non_cross_notes():
global fs, global_active_notes, global_notes_lock, fluid_lock
with fluid_lock:
with global_notes_lock:
keys_to_remove = []
for key, note_info in global_active_notes.items():
if not note_info.get("cross_beat", False):
channel, pitch = key
try:
fs.noteoff(channel, pitch)
except fluidsynth.FluidError as e:
print(f"Error turning off note {key}: {e}")
keys_to_remove.append(key)
for key in keys_to_remove:
del global_active_notes[key]
import threading
def play_midi_beat_persistent(all_voices_notes, play_beat_command, current_bpm, volume, frame):
global fs, playback_thread, stop_playback, current_beat, interrupt_flag, fluid_lock
global global_active_notes, global_notes_lock, midi_queue, beat_lock, global_time_signature
global global_playback_start_time # 用于记录当前播放段起始时间
if not play_beat_command or fs is None:
return
# 根据当前 BPM 和拍号计算当前拍时长
_, denominator = global_time_signature
beat_duration = (60.0 / current_bpm) * (4 / denominator)
# 如果已有播放线程正在运行,判断当前进度
if playback_thread and playback_thread.is_alive():
current_progress = time.perf_counter() - global_playback_start_time
if current_progress < beat_duration * 0.6:
return
else:
interrupt_flag = True
panic_non_cross_notes()
time.sleep(0.02)
with beat_lock:
current_beat += 1
# 重置中断标志,便于新拍的事件调度
interrupt_flag = False
start_time = (current_beat - 1) * beat_duration
end_time = current_beat * beat_duration
def play_notes():
global stop_playback, interrupt_flag, midi_queue, current_beat, global_playback_start_time
stop_playback = False
local_playback_start = time.perf_counter()
# 记录本次播放段起始时间,用于新一拍的中断判断
global_playback_start_time = local_playback_start
events = []
try:
# 遍历所有声部,收集当前拍内的音符事件
for voice in all_voices_notes:
program = voice["program"]
notes = voice["notes"]
idx = voice["next_note_index"]
# 处理从 idx 开始的音符(音符列表已排序)
while idx < len(notes):
note = notes[idx]
note_start = note["start_sec"]
note_end = note["end_sec"]
if note_end <= note_start:
idx += 1
continue
# 跳过当前拍之前的音符
if note_start < start_time:
idx += 1
continue
# 仅处理落在当前拍内的音符
if start_time <= note_start < end_time:
if note_end > end_time:
# 跨拍音符:按当前 BPM 重新计算时值
new_duration = note["duration_sec"] * (current_bpm / 60)
note_off_time = note_start + new_duration
cross_flag = True
else:
note_off_time = note_end
cross_flag = False
events.append({
"type": "note_on",
"time": note_start,
"pitch": note["pitch"],
"velocity": int(note["velocity"] * (volume / 127.0)),
"channel": program,
"cross_beat": cross_flag,
"beat": current_beat
})
events.append({
"type": "note_off",
"time": note_off_time,
"pitch": note["pitch"],
"channel": program,
"cross_beat": cross_flag,
"beat": current_beat
})
idx += 1
else:
# 后续音符不在当前拍内
break
# 更新该声部的 next_note_index
voice["next_note_index"] = idx
# 按时间顺序排序所有事件
events.sort(key=lambda x: x["time"])
# 逐个调度事件
for event in events:
event_offset = event["time"] - start_time
while (time.perf_counter() - local_playback_start) < event_offset:
# 如果中断标志被置且当前事件为 note_on 且非跨拍,则跳出等待
if (stop_playback or interrupt_flag) and event["type"] == "note_on" and not event.get("cross_beat", False):
break
time.sleep(0.05)
# 若中断后遇到非跨拍的 note_on 事件,则跳过该事件
if (stop_playback or interrupt_flag) and event["type"] == "note_on" and not event.get("cross_beat", False):
continue
midi_queue.put(event)
finally:
pass
playback_thread = threading.Thread(target=play_notes, daemon=True)
playback_thread.start()
def trigger_tuning():
global fs, fluid_lock, tuning_active
tuning_active = True # 开始 tuning
try:
with fluid_lock:
for channel in range(16):
fs.noteon(channel, 69, 127) # 播放A4音
time.sleep(2)
with fluid_lock:
for channel in range(16):
fs.noteoff(channel, 69) # 停止 A4 音
print("Tuning completed: A4 played for 1 second.")
finally:
tuning_active = False # 结束 tuning
def check_and_trigger_tuning(frame):
global tuning_triggered, rhythm_hand_label, control_hand_label, hands, mp_hands, pose, tuning_baseline_distance
if tuning_triggered:
return
# 将图像转为 RGB 格式
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 处理姿态,计算躯干中点(左右肩膀的中点)
pose_result = pose.process(rgb_frame)
torso_center = None
if pose_result.pose_landmarks:
left_shoulder = pose_result.pose_landmarks.landmark[mp.solutions.pose.PoseLandmark.LEFT_SHOULDER]
right_shoulder = pose_result.pose_landmarks.landmark[mp.solutions.pose.PoseLandmark.RIGHT_SHOULDER]
torso_center = (
int((left_shoulder.x + right_shoulder.x) / 2 * frame.shape[1]),
int((left_shoulder.y + right_shoulder.y) / 2 * frame.shape[0])
)
else:
return # 无法检测躯干时不触发
# 检测手部
hand_result = hands.process(rgb_frame)
if hand_result.multi_hand_landmarks and hand_result.multi_handedness:
hand_landmarks_list = hand_result.multi_hand_landmarks
handedness_list = hand_result.multi_handedness
if rhythm_hand_label is None or control_hand_label is None:
for idx, handedness in enumerate(handedness_list):
label = handedness.classification[0].label
if rhythm_hand_label is None:
rhythm_hand_label = label
elif control_hand_label is None and label != rhythm_hand_label:
control_hand_label = label
rhythm_hand = None
control_hand = None
for idx, handedness in enumerate(handedness_list):
label = handedness.classification[0].label
if label == rhythm_hand_label:
rhythm_hand = hand_landmarks_list[idx]
elif label == control_hand_label:
if handedness.classification[0].score >= 0.6:
control_hand = hand_landmarks_list[idx]
if rhythm_hand and control_hand:
# 获取两只手腕的图像坐标
rhythm_wrist = rhythm_hand.landmark[mp_hands.HandLandmark.WRIST]
control_wrist = control_hand.landmark[mp_hands.HandLandmark.WRIST]
rhythm_wrist_pos = (int(rhythm_wrist.x * frame.shape[1]), int(rhythm_wrist.y * frame.shape[0]))
control_wrist_pos = (int(control_wrist.x * frame.shape[1]), int(control_wrist.y * frame.shape[0]))
# 检查两只手腕是否都在躯干中点以上(y 坐标小于 torso_center[1])
# 且节奏手更高(节奏手的 y 坐标小于变化手的 y 坐标)
if (rhythm_wrist_pos[1] < torso_center[1] and
control_wrist_pos[1] < torso_center[1] and
rhythm_wrist_pos[1] < control_wrist_pos[1]):
tuning_triggered = True
baseline_distance = ((control_wrist_pos[0] - torso_center[0])**2 +
(control_wrist_pos[1] - torso_center[1])**2)**0.5
if not globals().get("tuning_baseline_distance", None):
tuning_baseline_distance = baseline_distance
print(f"Tuning Baseline Distance: {tuning_baseline_distance:.2f}")
threading.Thread(target=trigger_tuning, daemon=True).start()
def main():
global tuning_triggered
tuning_triggered = False
# 初始化摄像头
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("无法打开摄像头!")
cleanup_fluidsynth()
exit()
print("按 'q' 键退出程序。")
beats_notes, ticks_per_beat, bpm = preprocess_midi(midi_file_path)
total_beats = len(beats_notes) if beats_notes else 0
# 初始化BPM(从MIDI文件获取)
try:
midi_data = pretty_midi.PrettyMIDI(midi_file_path)
tempo_changes = midi_data.get_tempo_changes()
if tempo_changes[1].size > 0:
bpm = tempo_changes[1][0]
print(f"原曲 BPM 初始化为:{bpm:.2f}")
else:
print("未检测到原曲 BPM,程序退出。")
cleanup_fluidsynth()
exit()
except Exception as e:
print(f"BPM获取错误: {e}")
cleanup_fluidsynth()
exit()
note_durations = calculate_note_durations(bpm)
# 初始化手势相关变量
stop_detected = False
prev_position = None
current_beat = 0
last_stop_time = None
# 启动 MIDI 事件处理线程
midi_thread = threading.Thread(target=midi_event_processor, daemon=True)
midi_thread.start()
# 新增画面稳定相关变量
frame_counter = 0
skip_interval = 3 # 处理2帧跳1帧
last_processed_frame = None # 保存最近处理的帧
display_frame = None # 实际显示帧
try:
while True:
# 读取原始帧
ret, raw_frame = cap.read()
if not ret:
print("无法读取摄像头帧,退出程序。")
break
# 基础帧处理(始终执行)
frame = cv2.flip(raw_frame, 1) # 镜像翻转
frame = cv2.resize(frame, (int(frame.shape[1]*0.9), int(frame.shape[0]*0.9)))
# 调音检测(始终执行)
check_and_trigger_tuning(frame)
# 跳帧逻辑
frame_counter += 1
process_frame = (frame_counter % skip_interval) != 0
if process_frame:
# 深度处理当前帧(影响所有CV元素)
processed_frame = frame.copy()
prev_position, stop_detected, current_beat, last_stop_time, play_beat_command, current_bpm = process_frame_with_hand_detection(
processed_frame, None, prev_position, stop_detected, current_beat, beats_notes, total_beats, last_stop_time
)
play_midi_beat_persistent(beats_notes, play_beat_command, current_bpm, volume, processed_frame)
last_processed_frame = processed_frame # 保存处理结果
display_frame = processed_frame
# 始终显示画面(保持流畅)
cv2.imshow("Hand Gesture MIDI Control", display_frame)
# 退出检测
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
print(f"运行时错误: {e}")
finally:
cap.release()
cv2.destroyAllWindows()
cleanup_fluidsynth()
if __name__ == '__main__':
main()