-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequalizer-gui.lua
More file actions
810 lines (684 loc) · 29.2 KB
/
Copy pathequalizer-gui.lua
File metadata and controls
810 lines (684 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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
-- [[
-- 名称: mpv-equalizer-gui (全功能持久化自动保存版 - 含极简开关 + 预设下拉)
-- 特性: 1080p-4K自适应、uosc风格推子、胶囊开关、常用EQ预设下拉、配置自动保存与启动自动加载
-- ]]
local mp = require("mp")
local utils = require("mp.utils")
-- EQ 总开关状态 (默认开启)
local eq_enabled = true
-- 音频声道模式
-- auto = 自动
-- stereo = 立体声
-- left = 左声道
-- right = 右声道
local channel_mode = "auto"
local channel_mode_labels = {
auto = "Auto",
stereo = "Stereo",
left = "Left",
right = "Right"
}
-- 10 段 EQ 配置 (对应 FFmpeg equalizer 滤镜频率)
local bands = {
{ label = "31.5", freq = "31.25", val = 0 },
{ label = "63", freq = "62.5", val = 0 },
{ label = "125", freq = "125", val = 0 },
{ label = "250", freq = "250", val = 0 },
{ label = "500", freq = "500", val = 0 },
{ label = "1k", freq = "1000", val = 0 },
{ label = "2k", freq = "2000", val = 0 },
{ label = "4k", freq = "4000", val = 0 },
{ label = "8k", freq = "8000", val = 0 },
{ label = "16k", freq = "16000", val = 0 }
}
-- ============================================================
-- 常用 EQ 预设 (增益单位 dB, 顺序对应 bands 10 段)
-- 31.5 / 63 / 125 / 250 / 500 / 1k / 2k / 4k / 8k / 16k
-- ============================================================
local presets = {
{ name = "平直 Flat", vals = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ name = "流行 Pop", vals = { -1, 2, 4, 5, 3, 0, -1, -1, 1, 2 } },
{ name = "摇滚 Rock", vals = { 5, 4, 2, 0, -1, -1, 1, 3, 4, 5 } },
{ name = "爵士 Jazz", vals = { 3, 2, 1, 2, -1, -1, 0, 1, 2, 3 } },
{ name = "古典 Classical", vals = { 4, 3, 2, 1, -1, -1, 0, 2, 3, 4 } },
{ name = "舞曲 Dance", vals = { 6, 5, 3, 0, 0, -2, -3, -2, 1, 2 } },
{ name = "嘻哈 Hip-Hop", vals = { 5, 4, 2, 3, -1, -1, 1, -1, 2, 3 } },
{ name = "人声 Vocal", vals = { -3, -2, -1, 1, 4, 5, 4, 2, 0, -1 } },
{ name = "低音增强 Bass", vals = { 7, 6, 5, 3, 1, 0, 0, 0, 0, 0 } },
{ name = "高音增强 Treble", vals = { 0, 0, 0, 0, 0, 1, 3, 5, 6, 7 } },
{ name = "响度 Loudness", vals = { 6, 4, 0, -1, -2, -1, 0, 2, 5, 6 } },
{ name = "原声 Acoustic", vals = { 3, 3, 2, 1, 1, 1, 2, 3, 3, 2 } },
{ name = "金属 Metal", vals = { 5, 4, 3, 0, -2, -1, 1, 3, 5, 6 } },
{ name = "电子 Electronic", vals = { 4, 3, 2, 0, -2, 2, 1, 2, 4, 5 } },
}
-- 当前选中的预设索引 (nil = 自定义)
local current_preset = nil
-- 通过当前曲线反查匹配的预设 (允许 ±0.01 浮点误差)
local function detect_preset()
for i, p in ipairs(presets) do
local match = true
for j = 1, #bands do
if math.abs(bands[j].val - p.vals[j]) > 0.01 then
match = false
break
end
end
if match then return i end
end
return nil
end
-- 配置文件存储路径解析 (使用 mpv 标准 expand-path 接口)
local CONFIG_PATH = mp.command_native({"expand-path", "~~/script-opts/equalizer-gui.json"})
local ALT_CONFIG_PATH = mp.command_native({"expand-path", "~~/equalizer-gui.json"})
local overlay = mp.create_osd_overlay("ass-events")
local is_visible = false
-- 基准虚拟画布尺寸
local BASE_W = 1280
local PANEL_W, PANEL_H = 680, 380
-- 预设下拉列表项高度与内边距
local PRESET_ITEM_H = 22
local PRESET_PAD = 4
local ui = {
px = 0, py = 0,
dragging_band = nil,
hover_band = nil,
selected_band = 1,
preset_open = false
}
local mouse = { x = 0, y = 0, vx = 0, vy = 0, down = false }
-- 按钮组件配置 (添加 toggle 开关 + preset 下拉)
local btns = {
reset = { label = "重置 (Reset)", x = 0, y = 0, w = 110, h = 36, radius = 18 },
save = { label = "保存(Save)", x = 0, y = 0, w = 110, h = 36, radius = 18 },
close = { label = "✕", x = 0, y = 0, w = 32, h = 32, radius = 16 },
toggle = { label = "", x = 0, y = 0, w = 44, h = 22, radius = 11 },
preset = { label = "", x = 0, y = 0, w = 160, h = 36, radius = 18 }
}
-- ASS 颜色定义
local colors = {
bg = "&H221C1C&",
btn = "&H333333&",
btn_hover = "&H444444&",
btn_border = "&H666666&",
close_hover = "&H3333CC&",
track = "&H111111&",
active = "&H53C531&", -- 绿色
hover = "&H78FF55&", -- 高亮绿
text = "&HAAAAAA&",
text_hi = "&HFFFFFF&",
line = "&H444444&"
}
-- 获取当前声道模式对应的 pan 滤镜
-- 左声道:把第 1 个输入声道复制到左右输出
-- 右声道:把第 2 个输入声道复制到左右输出
local function get_channel_filter()
if channel_mode == "left" then
return "pan=stereo|c0=c0|c1=c0"
elseif channel_mode == "right" then
return "pan=stereo|c0=c1|c1=c1"
end
return nil
end
-- 应用音频 EQ + 声道模式到 mpv 引擎
local function apply_audio_eq()
-- Auto:由 mpv 自动决定;
-- Stereo / Left / Right:最终输出为立体声。
if channel_mode == "auto" then
mp.set_property("audio-channels", "auto")
else
mp.set_property("audio-channels", "stereo")
end
local channel_filter = get_channel_filter()
-- EQ 关闭时,也保留 Left / Right 声道选择。
if not eq_enabled then
if channel_filter then
mp.commandv("af", "set", "@eq_gui:lavfi=[" .. channel_filter .. "]")
else
mp.commandv("af", "remove", "@eq_gui")
end
return
end
local filters = {}
for _, b in ipairs(bands) do
table.insert(filters, string.format("equalizer=f=%s:width_type=o:w=1:g=%.1f", b.freq, b.val))
end
-- 声道滤镜放在 EQ 后面,EQ 调节时不会丢失声道模式。
if channel_filter then
table.insert(filters, channel_filter)
end
local filter_str = "@eq_gui:lavfi=[" .. table.concat(filters, ",") .. "]"
mp.commandv("af", "set", filter_str)
end
-- 设置声道模式
local function set_channel_mode(mode)
if not channel_mode_labels[mode] then
return
end
channel_mode = mode
apply_audio_eq()
mp.osd_message("Audio Channel: " .. channel_mode_labels[mode], 2)
end
-- 将增益数据与开关状态写入本地 JSON 配置文件
local function save_config()
local band_vals = {}
for i, b in ipairs(bands) do
band_vals[i] = b.val
end
local data = {
enabled = eq_enabled,
bands = band_vals
}
local json_str, _ = utils.format_json(data)
if json_str then
local file = io.open(CONFIG_PATH, "w")
-- 若 script-opts 文件夹不存在,回退写入配置根目录
if not file then
CONFIG_PATH = ALT_CONFIG_PATH
file = io.open(CONFIG_PATH, "w")
end
if file then
file:write(json_str)
file:close()
mp.osd_message("EQ has been saved locally", 2)
return true
end
end
mp.osd_message("Save failed: Unable to write to configuration file", 2)
return false
end
-- 启动/换集时读取本地 JSON 配置并自动启用
local function load_config()
local file = io.open(CONFIG_PATH, "r")
if not file then
file = io.open(ALT_CONFIG_PATH, "r")
end
if not file then return end
local content = file:read("*a")
file:close()
if not content or content == "" then return end
local data, _ = utils.parse_json(content)
if data and type(data) == "table" then
if data.bands and type(data.bands) == "table" then
for i, val in ipairs(data.bands) do
if bands[i] and type(val) == "number" then
bands[i].val = val
end
end
if type(data.enabled) == "boolean" then
eq_enabled = data.enabled
end
else
-- 兼容旧版纯数组格式
for i, val in ipairs(data) do
if bands[i] and type(val) == "number" then
bands[i].val = val
end
end
end
current_preset = detect_preset()
apply_audio_eq()
end
end
-- 实时更新鼠标坐标并做矩阵映射 (物理 ➔ 虚拟)
local function update_mouse_pos()
local m = mp.get_property_native("mouse-pos")
local osd = mp.get_property_native("osd-dimensions")
if m and osd and osd.w > 0 and osd.h > 0 then
mouse.x = m.x
mouse.y = m.y
-- 虚拟画布与实际 OSD 保持相同宽高比,因此 X/Y 使用同一个缩放比例。
local virtual_h = BASE_W * osd.h / osd.w
mouse.vx = m.x * (BASE_W / osd.w)
mouse.vy = m.y * (virtual_h / osd.h)
end
end
-- 贝塞尔绘制圆角矩形路径
local function draw_round_rect(x, y, w, h, r)
r = math.min(r, w / 2, h / 2)
local k = 0.55228 * r
return string.format(
"m %f %f l %f %f b %f %f %f %f %f %f l %f %f b %f %f %f %f %f %f l %f %f b %f %f %f %f %f %f l %f %f b %f %f %f %f %f %f ",
x + r, y,
x + w - r, y,
x + w - r + k, y, x + w, y + r - k, x + w, y + r,
x + w, y + h - r,
x + w, y + h - r + k, x + w - r + k, y + h, x + w - r, y + h,
x + r, y + h,
x + r - k, y + h, x, y + h - r + k, x, y + h - r,
x, y + r,
x, y + r - k, x + r - k, y, x + r, y
)
end
-- 贝塞尔绘制圆形
local function draw_circle(x, y, r)
local k = 0.55228 * r
return string.format(
"m %f %f b %f %f %f %f %f %f b %f %f %f %f %f %f b %f %f %f %f %f %f b %f %f %f %f %f %f ",
x, y - r,
x + k, y - r, x + r, y - k, x + r, y,
x + r, y + k, x + k, y + r, x, y + r,
x - k, y + r, x - r, y + k, x - r, y,
x - r, y - k, x - k, y - r, x, y - r
)
end
-- 计算预设下拉列表的几何尺寸 (向上弹出)
local function get_preset_list_geom()
local n = #presets
local w = btns.preset.w
local h = n * PRESET_ITEM_H + PRESET_PAD * 2
local x = btns.preset.x
local y = btns.preset.y - 6 - h
return x, y, w, h
end
-- 命中测试:返回鼠标所在的下拉列表项索引
local function hit_preset_item(vx, vy)
if not ui.preset_open then return nil end
local lx, ly, lw = get_preset_list_geom()
if vx < lx or vx > lx + lw then return nil end
for i = 1, #presets do
local iy = ly + PRESET_PAD + (i - 1) * PRESET_ITEM_H
if vy >= iy and vy <= iy + PRESET_ITEM_H then
return i
end
end
return nil
end
-- 核心渲染逻辑
local function render()
if not is_visible then return end
local osd = mp.get_property_native("osd-dimensions")
if not osd or osd.w == 0 or osd.h == 0 then return end
-- 关键修复:不要把固定的 1280x720 画布强行拉伸到任意 OSD。
-- 让虚拟画布动态匹配 OSD 的宽高比,这样所有图形会等比缩放,弹窗不会被压扁/拉长。
local virtual_h = BASE_W * osd.h / osd.w
overlay.res_x = BASE_W
overlay.res_y = math.floor(virtual_h + 0.5)
ui.px = math.floor((BASE_W - PANEL_W) / 2)
ui.py = math.floor((virtual_h - PANEL_H) / 2)
local px, py = ui.px, ui.py
-- ================= 修改开始 =================
-- 保持预设按钮左边缘与原布局一致(即 px + PANEL_W - 432),统一间距后向右顺延
local btn_gap = 16 -- 统一按钮间距,可根据需要微调
btns.preset.x = px + PANEL_W - 432
btns.reset.x = btns.preset.x + btns.preset.w + btn_gap
btns.save.x = btns.reset.x + btns.reset.w + btn_gap
btns.preset.y = py + PANEL_H - 52
btns.reset.y = py + PANEL_H - 52
btns.save.y = py + PANEL_H - 52
-- ================= 修改结束 =================
btns.close.x = px + PANEL_W - 44
btns.close.y = py + 12
btns.toggle.x = btns.close.x - 56
btns.toggle.y = py + 17
local ass = ""
-- 1. 弹窗背景底板 (14px 圆角)
local panel_path = draw_round_rect(px, py, PANEL_W, PANEL_H, 14)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\1a&H0F&\\3c&H555555&\\bord2}%s{\\p0}\n", colors.bg, panel_path)
-- 2. 标题
ass = ass .. string.format("{\\pos(%d,%d)\\an7\\fs20\\1c%s\\b1} Equalizer{\\b0}\n", px + 20, py + 18, colors.text_hi)
-- 3. 右上角“✕”关闭按钮
local close_hover = (mouse.vx >= btns.close.x and mouse.vx <= btns.close.x + btns.close.w and mouse.vy >= btns.close.y and mouse.vy <= btns.close.y + btns.close.h)
local close_path = draw_round_rect(btns.close.x, btns.close.y, btns.close.w, btns.close.h, btns.close.radius)
if close_hover then
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0}%s{\\p0}\n", colors.close_hover, close_path)
end
ass = ass .. string.format("{\\pos(%d,%d)\\an5\\fs20\\1c%s\\b1}%s{\\b0}\n",
btns.close.x + btns.close.w / 2, btns.close.y + btns.close.h / 2, close_hover and colors.text_hi or colors.text, btns.close.label)
-- 4. 极简胶囊开关 (无文字 uosc/iOS 风格)
local toggle_hover = (mouse.vx >= btns.toggle.x and mouse.vx <= btns.toggle.x + btns.toggle.w and mouse.vy >= btns.toggle.y and mouse.vy <= btns.toggle.y + btns.toggle.h)
local pill_color, knob_color
if eq_enabled then
pill_color = toggle_hover and colors.hover or colors.active
knob_color = colors.text_hi
else
pill_color = toggle_hover and "&H555555&" or "&H333333&"
knob_color = toggle_hover and "&HABABAB&" or "&H888888&"
end
local toggle_path = draw_round_rect(btns.toggle.x, btns.toggle.y, btns.toggle.w, btns.toggle.h, btns.toggle.radius)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0}%s{\\p0}\n", pill_color, toggle_path)
local knob_x = eq_enabled and (btns.toggle.x + btns.toggle.w - 11) or (btns.toggle.x + 11)
local knob_y = btns.toggle.y + btns.toggle.h / 2
local knob_path = draw_circle(knob_x, knob_y, 8)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0}%s{\\p0}\n", knob_color, knob_path)
-- 5. 刻度参考线
local line_y_15 = py + 75
local line_y_m15 = py + PANEL_H - 95
local line_y_0 = line_y_15 + (line_y_m15 - line_y_15) / 2
local line_fmt = "{\\pos(0,0)\\an7\\p1\\1c%s\\1a&H88&}m %d %d l %d %d l %d %d l %d %d{\\p0}\n"
ass = ass .. string.format(line_fmt, colors.line, px + 45, line_y_15 - 1, px + PANEL_W - 45, line_y_15 - 1, px + PANEL_W - 45, line_y_15, px + 45, line_y_15)
ass = ass .. string.format(line_fmt, colors.line, px + 45, line_y_0 - 1, px + PANEL_W - 45, line_y_0 - 1, px + PANEL_W - 45, line_y_0, px + 45, line_y_0)
ass = ass .. string.format(line_fmt, colors.line, px + 45, line_y_m15 - 1, px + PANEL_W - 45, line_y_m15 - 1, px + PANEL_W - 45, line_y_m15, px + 45, line_y_m15)
ass = ass .. string.format("{\\pos(%d,%d)\\an6\\fs12\\1c%s\\b1}+15dB{\\b0}\n", px + 40, line_y_15, colors.text_hi)
ass = ass .. string.format("{\\pos(%d,%d)\\an6\\fs12\\1c%s\\b1}-15dB{\\b0}\n", px + 40, line_y_m15, colors.text_hi)
-- 6. 10 段推子渲染 (关闭时呈现暗灰色,开启时亮起)
local gap = (PANEL_W - 90) / #bands
local start_x = px + 45 + gap / 2
for i, b in ipairs(bands) do
local sx = start_x + (i - 1) * gap
local val_clamped = math.max(-15, math.min(15, b.val))
local sy = line_y_0 - (val_clamped / 15) * ((line_y_m15 - line_y_15) / 2)
local is_active = (i == ui.dragging_band or i == ui.hover_band or i == ui.selected_band)
local act_color
if not eq_enabled then
act_color = "&H666666&"
else
act_color = is_active and colors.hover or colors.active
end
-- 轨道底色
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\1a&H00&}m %d %d l %d %d l %d %d l %d %d{\\p0}\n",
colors.track, sx - 3, line_y_15, sx + 3, line_y_15, sx + 3, line_y_m15, sx - 3, line_y_m15)
-- 动态增益填充条
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\1a&H00&}m %d %d l %d %d l %d %d l %d %d{\\p0}\n",
act_color, sx - 3, line_y_0, sx + 3, line_y_0, sx + 3, sy, sx - 3, sy)
-- 推进把手
local ring = draw_circle(sx, sy, is_active and 11 or 9)
local inner = draw_circle(sx, sy, is_active and 5 or 4)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\3c%s\\bord3\\1a&H00&}%s{\\p0}\n", colors.bg, act_color, ring)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0}%s{\\p0}\n", act_color, inner)
-- 数值与频段标签
ass = ass .. string.format("{\\pos(%d,%d)\\an2\\fs13\\1c%s\\b1}%.1f{\\b0}\n", sx, line_y_15 - 12, colors.text_hi, b.val)
ass = ass .. string.format("{\\pos(%d,%d)\\an8\\fs13\\1c%s\\b1}%s{\\b0}\n", sx, line_y_m15 + 12, is_active and colors.text_hi or colors.text, b.label)
end
-- 7. 底部胶囊按钮绘制
local reset_hover = (mouse.vx >= btns.reset.x and mouse.vx <= btns.reset.x + btns.reset.w and mouse.vy >= btns.reset.y and mouse.vy <= btns.reset.y + btns.reset.h)
local save_hover = (mouse.vx >= btns.save.x and mouse.vx <= btns.save.x + btns.save.w and mouse.vy >= btns.save.y and mouse.vy <= btns.save.y + btns.save.h)
-- 重置按钮
local reset_path = draw_round_rect(btns.reset.x, btns.reset.y, btns.reset.w, btns.reset.h, btns.reset.radius)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\3c%s\\bord1}%s{\\p0}\n", reset_hover and colors.btn_hover or colors.btn, colors.btn_border, reset_path)
ass = ass .. string.format("{\\pos(%d,%d)\\an5\\fs14\\1c%s}%s\n", btns.reset.x + btns.reset.w/2, btns.reset.y + btns.reset.h/2, colors.text_hi, btns.reset.label)
-- 保存按钮
local save_path = draw_round_rect(btns.save.x, btns.save.y, btns.save.w, btns.save.h, btns.save.radius)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\3c%s\\bord1}%s{\\p0}\n", save_hover and colors.btn_hover or colors.btn, colors.btn_border, save_path)
ass = ass .. string.format("{\\pos(%d,%d)\\an5\\fs14\\1c%s}%s\n", btns.save.x + btns.save.w/2, btns.save.y + btns.save.h/2, colors.active, btns.save.label)
-- 8. 预设下拉按钮
local preset_hover = (mouse.vx >= btns.preset.x and mouse.vx <= btns.preset.x + btns.preset.w and
mouse.vy >= btns.preset.y and mouse.vy <= btns.preset.y + btns.preset.h)
local preset_path = draw_round_rect(btns.preset.x, btns.preset.y, btns.preset.w, btns.preset.h, btns.preset.radius)
local preset_bg = (preset_hover or ui.preset_open) and colors.btn_hover or colors.btn
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\3c%s\\bord1}%s{\\p0}\n",
preset_bg, colors.btn_border, preset_path)
local preset_name = current_preset and presets[current_preset].name or "自定义 Custom"
ass = ass .. string.format("{\\pos(%d,%d)\\an5\\fs14\\1c%s}%s\n",
btns.preset.x + btns.preset.w / 2 - 8, btns.preset.y + btns.preset.h / 2, colors.text_hi, preset_name)
-- 下拉三角箭头
local aax = btns.preset.x + btns.preset.w - 18
local aay = btns.preset.y + btns.preset.h / 2 - 1
local arrow = string.format("m %f %f l %f %f l %f %f ", aax - 5, aay - 2, aax + 5, aay - 2, aax, aay + 4)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0}%s{\\p0}\n", colors.text, arrow)
-- 9. 展开的预设列表 (向上弹出,绘制在最上层)
if ui.preset_open then
local lx, ly, lw, lh = get_preset_list_geom()
local list_path = draw_round_rect(lx, ly, lw, lh, 10)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\3c%s\\bord1\\1a&H08&}%s{\\p0}\n",
colors.bg, colors.btn_border, list_path)
for i, p in ipairs(presets) do
local iy = ly + PRESET_PAD + (i - 1) * PRESET_ITEM_H
local hov = (mouse.vx >= lx and mouse.vx <= lx + lw and
mouse.vy >= iy and mouse.vy <= iy + PRESET_ITEM_H)
if hov then
local hpath = draw_round_rect(lx + 3, iy + 1, lw - 6, PRESET_ITEM_H - 2, 6)
ass = ass .. string.format("{\\pos(0,0)\\an7\\p1\\1c%s\\bord0\\1a&H50&}%s{\\p0}\n",
colors.active, hpath)
end
local col = colors.text
if current_preset == i then
col = colors.active
elseif hov then
col = colors.text_hi
end
ass = ass .. string.format("{\\pos(%d,%d)\\an4\\fs14\\1c%s}%s\n",
lx + 14, iy + PRESET_ITEM_H / 2, col, p.name)
end
end
overlay.data = ass
overlay:update()
end
-- 根据虚拟 Y 坐标计算增益值
local function calc_gain_from_vy(vy)
local line_y_15 = ui.py + 75
local line_y_m15 = ui.py + PANEL_H - 95
local line_y_0 = line_y_15 + (line_y_m15 - line_y_15) / 2
local half_h = (line_y_m15 - line_y_15) / 2
local val = (line_y_0 - vy) / half_h * 15
val = math.max(-15, math.min(15, val))
return math.floor(val * 2 + 0.5) / 2
end
-- 应用指定预设到均衡器曲线
local function apply_preset(idx)
local p = presets[idx]
if not p then return end
for i = 1, #bands do
bands[i].val = p.vals[i] or 0
end
current_preset = idx
apply_audio_eq()
render()
mp.osd_message("EQ 预设: " .. p.name, 2)
end
-- 检测鼠标悬停在哪个频段
local function check_hover_band()
local gap = (PANEL_W - 90) / #bands
local start_x = ui.px + 45 + gap / 2
local hit_top = ui.py + 45
local hit_bottom = ui.py + PANEL_H - 65
ui.hover_band = nil
for i, _ in ipairs(bands) do
local sx = start_x + (i - 1) * gap
if mouse.vx >= sx - (gap/2) and mouse.vx <= sx + (gap/2) and
mouse.vy >= hit_top and mouse.vy <= hit_bottom then
ui.hover_band = i
break
end
end
end
local toggle_ui
-- 鼠标左键按下
local function on_mouse_down()
if not is_visible then return end
update_mouse_pos()
mouse.down = true
-- 优先处理预设下拉框
local preset_btn_hit = (mouse.vx >= btns.preset.x and mouse.vx <= btns.preset.x + btns.preset.w and
mouse.vy >= btns.preset.y and mouse.vy <= btns.preset.y + btns.preset.h)
if ui.preset_open then
local idx = hit_preset_item(mouse.vx, mouse.vy)
if idx then
ui.preset_open = false
apply_preset(idx)
return
end
if not preset_btn_hit then
ui.preset_open = false
render()
end
end
if preset_btn_hit then
ui.preset_open = not ui.preset_open
render()
return
end
-- 点击右上角“✕”关闭按钮
if mouse.vx >= btns.close.x and mouse.vx <= btns.close.x + btns.close.w and
mouse.vy >= btns.close.y and mouse.vy <= btns.close.y + btns.close.h then
toggle_ui()
return
end
-- 点击胶囊开关按钮
if mouse.vx >= btns.toggle.x and mouse.vx <= btns.toggle.x + btns.toggle.w and
mouse.vy >= btns.toggle.y and mouse.vy <= btns.toggle.y + btns.toggle.h then
eq_enabled = not eq_enabled
apply_audio_eq()
render()
return
end
-- 点击重置按钮
if mouse.vx >= btns.reset.x and mouse.vx <= btns.reset.x + btns.reset.w and
mouse.vy >= btns.reset.y and mouse.vy <= btns.reset.y + btns.reset.h then
for _, b in ipairs(bands) do b.val = 0 end
current_preset = detect_preset()
apply_audio_eq()
render()
return
end
-- 点击保存按钮 (写盘并关闭)
if mouse.vx >= btns.save.x and mouse.vx <= btns.save.x + btns.save.w and
mouse.vy >= btns.save.y and mouse.vy <= btns.save.y + btns.save.h then
save_config()
toggle_ui()
return
end
-- 推子点击与拖拽锁
check_hover_band()
if ui.hover_band then
ui.dragging_band = ui.hover_band
ui.selected_band = ui.hover_band
bands[ui.dragging_band].val = calc_gain_from_vy(mouse.vy)
current_preset = detect_preset()
apply_audio_eq()
render()
end
end
-- 鼠标左键抬起
local function on_mouse_up()
mouse.down = false
ui.dragging_band = nil
render()
end
-- 鼠标右键点击 (重置单个频段)
local function on_right_click()
if not is_visible then return end
update_mouse_pos()
check_hover_band()
if ui.hover_band then
bands[ui.hover_band].val = 0
current_preset = detect_preset()
apply_audio_eq()
render()
end
end
-- 滚轮调节
local function on_wheel(delta)
if not is_visible then return end
local idx = ui.hover_band or ui.selected_band
if idx then
local new_val = math.max(-15, math.min(15, bands[idx].val + delta * 0.5))
bands[idx].val = new_val
current_preset = detect_preset()
apply_audio_eq()
render()
end
end
-- 键盘方向键调节
local function on_key_nav(dir)
if not is_visible then return end
if dir == "left" then
ui.selected_band = math.max(1, ui.selected_band - 1)
elseif dir == "right" then
ui.selected_band = math.min(#bands, ui.selected_band + 1)
elseif dir == "up" then
bands[ui.selected_band].val = math.min(15, bands[ui.selected_band].val + 0.5)
current_preset = detect_preset()
apply_audio_eq()
elseif dir == "down" then
bands[ui.selected_band].val = math.max(-15, bands[ui.selected_band].val - 0.5)
current_preset = detect_preset()
apply_audio_eq()
end
render()
end
-- 高频监听鼠标移动
mp.observe_property("mouse-pos", "native", function(name, val)
if not is_visible or not val then return end
update_mouse_pos()
if mouse.down and ui.dragging_band then
bands[ui.dragging_band].val = calc_gain_from_vy(mouse.vy)
current_preset = detect_preset()
apply_audio_eq()
else
check_hover_band()
end
render()
end)
-- 监听分辨率/窗口大小改变
mp.observe_property("osd-dimensions", "native", function()
if is_visible then render() end
end)
-- 关键事件绑定:播放器装载文件时,自动加载本地保存的配置文件
mp.register_event("file-loaded", function()
load_config()
end)
-- UI 显示/隐藏切换
toggle_ui = function()
is_visible = not is_visible
if is_visible then
update_mouse_pos()
-- 独占按键与屏蔽冲突行为
mp.add_forced_key_binding("MBTN_LEFT", "eq-click", function(t)
if t.event == "down" then on_mouse_down()
elseif t.event == "up" then on_mouse_up() end
end, {complex = true})
mp.add_forced_key_binding("MBTN_LEFT_DBL", "eq-dblclick-block", function() end)
mp.add_forced_key_binding("MBTN_RIGHT", "eq-right-click", on_right_click)
-- 滚轮与键盘绑定
mp.add_forced_key_binding("WHEEL_UP", "eq-wheel-up", function() on_wheel(1) end)
mp.add_forced_key_binding("WHEEL_DOWN", "eq-wheel-down", function() on_wheel(-1) end)
mp.add_forced_key_binding("LEFT", "eq-left", function() on_key_nav("left") end)
mp.add_forced_key_binding("RIGHT", "eq-right", function() on_key_nav("right") end)
mp.add_forced_key_binding("UP", "eq-up", function() on_key_nav("up") end)
mp.add_forced_key_binding("DOWN", "eq-down", function() on_key_nav("down") end)
mp.add_forced_key_binding("ENTER", "eq-enter", function()
save_config()
toggle_ui()
end)
mp.add_forced_key_binding("r", "eq-reset", function()
for _, b in ipairs(bands) do b.val = 0 end
current_preset = detect_preset()
apply_audio_eq()
render()
end)
render()
else
ui.dragging_band = nil
ui.preset_open = false
mouse.down = false
-- 解除按键独占
mp.remove_key_binding("eq-click")
mp.remove_key_binding("eq-dblclick-block")
mp.remove_key_binding("eq-right-click")
mp.remove_key_binding("eq-wheel-up")
mp.remove_key_binding("eq-wheel-down")
mp.remove_key_binding("eq-left")
mp.remove_key_binding("eq-right")
mp.remove_key_binding("eq-up")
mp.remove_key_binding("eq-down")
mp.remove_key_binding("eq-enter")
mp.remove_key_binding("eq-reset")
overlay:remove()
end
end
-- 打开/关闭 快捷键
mp.register_script_message("toggle-equalizer-gui", toggle_ui)
-- mp.add_key_binding("e", "toggle-equalizer-gui", toggle_ui)
-- 同时提供脚本消息,方便在 input.conf 中自定义快捷键。
mp.register_script_message("audio-channel-auto", function()
set_channel_mode("auto")
end)
mp.register_script_message("audio-channel-stereo", function()
set_channel_mode("stereo")
end)
mp.register_script_message("audio-channel-left", function()
set_channel_mode("left")
end)
mp.register_script_message("audio-channel-right", function()
set_channel_mode("right")
end)
-- ESC:优先收起下拉框,其次关闭整个面板
mp.add_key_binding("ESC", "close-equalizer-gui", function()
if not is_visible then return end
if ui.preset_open then
ui.preset_open = false
render()
return
end
toggle_ui()
end)