-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheq_vis.pd_lua
More file actions
537 lines (472 loc) · 19 KB
/
eq_vis.pd_lua
File metadata and controls
537 lines (472 loc) · 19 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
local eq = pd.Class:new():register("eq_vis")
local FS = 44100
local MAX_BANDS = 8
local BYPASS_COEFFS = {0, 0, 1, 0, 0}
-- 类型定义,对应 Type Index 1-7
local TYPES = {"peak", "bandpass", "notch", "lowpass", "highpass", "lowshelf", "highshelf"}
local FIXED_GAIN_TYPES = {
["notch"] = true, ["lowpass"] = true, ["highpass"] = true, ["bandpass"] = true
}
-- 8 种固定颜色,索引对应 Band ID
local PALETTE = {
{40, 140, 230},
{200, 58, 59},
{49, 177, 94},
{230, 140, 40},
{220, 220, 40},
{160, 60, 230},
{200, 200, 200},
{60, 230, 200}
}
local function log10(x) return math.log(x) / math.log(10) end
-- 辅助:获取类型名称对应的索引数字
local function get_type_index(t_str)
for i, v in ipairs(TYPES) do if v == t_str then return i end end
return 1 -- default peak
end
-- 辅助:获取索引数字对应的类型名称
local function get_type_name(t_idx)
return TYPES[math.floor(t_idx)] or "peak"
end
function eq:initialize(sel, atoms)
-- Inlet 1: 全状态加载 / Inlet 2: 单个Band控制
self.inlets = 2
-- Outlet 1: DSP系数 / Outlet 2: 单个Band状态 / Outlet 3: 全状态保存列表
self.outlets = 3
self.width, self.height = 650, 320
self:set_size(self.width, self.height)
self.f_min = 20.0
self.f_max = FS/2
self.db_range = 18
self.bands = {}
-- PD Patch加载逻辑
if atoms and #atoms > 0 then
self:in_1_list(atoms)
else
-- 默认初始状态
self:add_band(100, 0, 0.71, "lowshelf")
self:add_band(1000, 0, 0.71, "peak")
self:add_band(5000, 0, 0.71, "highshelf")
end
self.selected_id = nil -- 记录 ID
self.shift_pressed = false
self.ctrl_pressed = false
self.alt_pressed = false
self.drag_accum_step = 0
self.last_mouse_y = 0
self.dirty = true
self.tick_time = 16.67
self.click_count = 0
self.dbl_click_limit = 250
self.click_clock = pd.Clock:new():register(self, "reset_click")
self.render_clock = pd.Clock:new():register(self, "tick")
self.is_dragging = false -- 初始化拖动状态标志
return true
end
function eq:postinitialize()
self.render_clock:delay(self.tick_time)
self:output_all_coeffs()
self:output_state_list()
end
function eq:finalize()
self.render_clock:destruct()
self.click_clock:destruct()
end
-- ID 管理
-- 获取 1-8 中最小的可用 ID
function eq:get_next_available_id()
for id = 1, MAX_BANDS do
local taken = false
for _, b in ipairs(self.bands) do
if b.id == id then taken = true; break end
end
if not taken then return id end
end
return nil -- 满了
end
-- 根据 ID 获取 Band 对象
function eq:get_band_by_id(id)
for _, b in ipairs(self.bands) do
if b.id == id then return b end
end
return nil
end
-- --- DSP: RBJ Biquad ---
function eq:calc_rbj_coeffs(f0, db_gain, Q, type)
if f0 < 1 then f0 = 1 end
if f0 > FS/2 - 1 then f0 = FS/2 - 50 end
if Q < 0.01 then Q = 0.01 end
local A = 10^(db_gain / 40)
local w0 = 2 * math.pi * f0 / FS
local cs, sn = math.cos(w0), math.sin(w0)
local alpha = sn / (2 * Q)
local b0, b1, b2, a0, a1, a2 = 0,0,0,0,0,0
if type == "peak" then b0,b1,b2,a0,a1,a2 = 1+alpha*A, -2*cs, 1-alpha*A, 1+alpha/A, -2*cs, 1-alpha/A
elseif type == "lowpass" then b0,b1,b2,a0,a1,a2 = (1-cs)/2, 1-cs, (1-cs)/2, 1+alpha, -2*cs, 1-alpha
elseif type == "highpass" then b0,b1,b2,a0,a1,a2 = (1+cs)/2, -(1+cs), (1+cs)/2, 1+alpha, -2*cs, 1-alpha
elseif type == "bandpass" then b0,b1,b2,a0,a1,a2 = alpha, 0, -alpha, 1+alpha, -2*cs, 1-alpha
elseif type == "notch" then b0,b1,b2,a0,a1,a2 = 1, -2*cs, 1, 1+alpha, -2*cs, 1-alpha
elseif type == "lowshelf" then
local sqA = 2*math.sqrt(A)*alpha
b0,b1,b2 = A*((A+1)-(A-1)*cs+sqA), 2*A*((A-1)-(A+1)*cs), A*((A+1)-(A-1)*cs-sqA)
a0,a1,a2 = (A+1)+(A-1)*cs+sqA, -2*((A-1)+(A+1)*cs), (A+1)+(A-1)*cs-sqA
elseif type == "highshelf" then
local sqA = 2*math.sqrt(A)*alpha
b0,b1,b2 = A*((A+1)+(A-1)*cs+sqA), -2*A*((A-1)+(A+1)*cs), A*((A+1)+(A-1)*cs-sqA)
a0,a1,a2 = (A+1)-(A-1)*cs+sqA, 2*((A-1)-(A+1)*cs), (A+1)-(A-1)*cs-sqA
else b0,b1,b2,a0,a1,a2=1,0,0,1,0,0 end
local norm = 1 / a0
return { -a1*norm, -a2*norm, b0*norm, b1*norm, b2*norm }
end
function eq:get_band_coeffs_list(b)
local list = {}
if (b.type == "lowpass" or b.type == "highpass") and b.order > 2 then
local N = b.order; local pairs = N / 2
local q_scale = b.q / 0.7071
for k = 1, pairs do
local theta = (2 * k - 1) * math.pi / (2 * N)
local final_Q = (1 / (2 * math.sin(theta))) * q_scale
local coeffs = self:calc_rbj_coeffs(b.f, b.g, final_Q, b.type)
for _, v in ipairs(coeffs) do table.insert(list, v) end
end
else
local coeffs = self:calc_rbj_coeffs(b.f, b.g, b.q, b.type)
for _, v in ipairs(coeffs) do table.insert(list, v) end
end
return list
end
function eq:get_response_db(f, flat_coeffs_list)
local total_db = 0
local w, c, s = 2 * math.pi * f / FS, math.cos(2 * math.pi * f / FS), math.sin(2 * math.pi * f / FS)
local c2, s2 = math.cos(4 * math.pi * f / FS), math.sin(4 * math.pi * f / FS)
for i = 1, #flat_coeffs_list, 5 do
local fb1, fb2, ff1, ff2, ff3 = flat_coeffs_list[i], flat_coeffs_list[i+1], flat_coeffs_list[i+2], flat_coeffs_list[i+3], flat_coeffs_list[i+4]
if fb1 then
local nr, ni = ff1 + ff2*c + ff3*c2, -ff2*s - ff3*s2
local dr, di = 1 - fb1*c - fb2*c2, fb1*s + fb2*s2
total_db = total_db + 10 * log10(math.max(1e-20, (nr^2 + ni^2) / (dr^2 + di^2)))
end
end
return total_db
end
-- --- Outlet 输出逻辑 ---
-- Outlet 1: 所有系数
function eq:output_all_coeffs()
-- 排序 bands 保证 DSP 处理顺序一致
table.sort(self.bands, function(a,b) return a.id < b.id end)
local master_list = {}
for i, b in ipairs(self.bands) do
local band_coeffs = self:get_band_coeffs_list(b)
for _, val in ipairs(band_coeffs) do table.insert(master_list, val) end
end
if #master_list == 0 then master_list = BYPASS_COEFFS end
self:outlet(1, "list", master_list)
end
-- Outlet 2: 单个 Band 状态 {ID, F, Q, G, Type_Idx}
function eq:output_single_band_status(b)
if not b then return end
local t_idx = get_type_index(b.type)
local drag_flag = self.is_dragging and 1 or 0
-- 在末尾增加 b.order
self:outlet(2, "list", {b.id, b.f, b.q, b.g, t_idx, drag_flag, b.order})
end
-- Outlet 3: 保存用全列表 (F, G, Q, Type_Idx...)
function eq:output_state_list()
local state = {}
table.sort(self.bands, function(a,b) return a.id < b.id end)
for _, b in ipairs(self.bands) do
table.insert(state, b.f)
table.insert(state, b.g)
table.insert(state, b.q)
table.insert(state, get_type_index(b.type))
table.insert(state, b.order)
end
self:outlet(3, "list", state)
end
-- --- 数据操作 ---
function eq:add_band(f, g, q, type_val, order_val)
local new_id = self:get_next_available_id()
if not new_id then return end -- 满了
local t_str = "peak"
if type(type_val) == "number" then t_str = get_type_name(type_val)
elseif type(type_val) == "string" then t_str = type_val end
local new_band = {
id = new_id,
f = f, g = g, q = q,
type = t_str,
-- 如果传入了 order 则使用,否则默认为 2
order = order_val or 2
}
table.insert(self.bands, new_band)
return new_band
end
-- Inlet 1: 全状态加载 (list: f1 g1 q1 t1 f2...)
function eq:in_1_list(l)
if not l or #l < 4 then return end
self.bands = {}
self.selected_id = nil
-- 简单判断新旧协议
local step = 5
if (#l % 5 ~= 0) and (#l % 4 == 0) then
step = 4
end
for i = 1, #l, step do
local f = l[i]
local g = l[i+1]
local q = l[i+2]
local t = l[i+3]
local ord = 2 -- 默认值
-- 只有新格式才读取第 5 位
if step == 5 then
ord = l[i+4]
end
self:add_band(f, g, q, t, ord)
end
self.dirty = true
end
-- Inlet 2: 单 Band 控制 (list: {ID, F, Q, G, Type_Idx})
function eq:in_2_list(l)
if not l or #l < 5 then return end
local target_id = math.floor(l[1])
local b = self:get_band_by_id(target_id)
if b then
b.f = math.max(self.f_min, math.min(self.f_max, l[2]))
b.q = math.max(0.01, l[3])
b.g = math.max(-self.db_range, math.min(self.db_range, l[4]))
b.type = get_type_name(l[5])
-- 也许有bug
if l[6] then
-- 限制 order 为 2, 4, 6, 8...
local raw_order = math.floor(l[6])
if raw_order < 2 then raw_order = 2 end
if raw_order % 2 ~= 0 then raw_order = raw_order + 1 end
b.order = raw_order
end
if self.selected_id == b.id then
self:output_single_band_status(b)
end
self.dirty = true
end
end
-- 键盘辅助键 (Inlet 2 接收 symbol/float)
function eq:in_2_symbol(s)
local update = false
if s == "Alt_L" or s == "Alt_R" then if not self.alt_pressed then self.alt_pressed = true; update = true end
elseif s == "Control_L" or s == "Control_R" then if not self.ctrl_pressed then self.ctrl_pressed = true; update = true end
elseif s == "Shift_L" or s == "Shift_R" then if not self.shift_pressed then self.shift_pressed = true; update = true end
elseif s == "0" then self.alt_pressed = false; self.ctrl_pressed = false; self.shift_pressed = false; update = true end
if update then self.dirty = true end
end
function eq:in_2_float(f) if f == 0 then self:in_2_symbol("0") end end
-- PD 保存接口
function eq:save()
local args = {}
table.sort(self.bands, function(a,b) return a.id < b.id end)
for _, b in ipairs(self.bands) do
table.insert(args, b.f)
table.insert(args, b.g)
table.insert(args, b.q)
table.insert(args, get_type_index(b.type))
table.insert(args, b.order)
end
return args
end
-- --- 鼠标交互 ---
function eq:handle_dbl_click(x, y)
-- 1. 尝试删除
for i, b in ipairs(self.bands) do
local px, py = self:f_to_x(b.f), self:g_to_y(b.g)
if (x-px)^2 + (y-py)^2 < 225 then
table.remove(self.bands, i)
self.selected_id = nil
self.dirty = true
return
end
end
-- 2. 尝试添加 (如果未满)
local new_f = math.max(self.f_min, math.min(self.f_max, self:x_to_f(x)))
local new_g = math.max(-self.db_range, math.min(self.db_range, self:y_to_g(y)))
local new_type = "peak"
if new_f < 100 then new_type = "highpass"; new_g = 0
elseif new_f > 10000 then new_type = "lowpass"; new_g = 0 end
local b = self:add_band(new_f, new_g, 0.71, new_type)
if b then
self.selected_id = b.id
self:output_single_band_status(b)
self.dirty = true
end
end
function eq:change_type_step(band, step)
local current_idx = get_type_index(band.type)
local new_idx = current_idx + step
if new_idx > #TYPES then new_idx = 1 end
if new_idx < 1 then new_idx = #TYPES end
band.type = TYPES[new_idx]
if FIXED_GAIN_TYPES[band.type] then band.g = 0 end
band.q = 0.71
if band.type == "lowpass" or band.type == "highpass" then band.order = 2 end
end
function eq:mouse_down(x, y)
self.click_count = self.click_count + 1
self.drag_accum_step = 0
if self.click_count == 1 then
self.click_clock:delay(self.dbl_click_limit)
-- 选中检测
for _, b in ipairs(self.bands) do
local px, py = self:f_to_x(b.f), self:g_to_y(b.g)
if (x-px)^2 + (y-py)^2 < 225 then
self.selected_id = b.id
self.last_mouse_y = y
self.is_dragging = true -- 标记开始拖动
self:output_single_band_status(b) -- 输出带 flag=1 的状态
self.dirty = true
return
end
end
self.selected_id = nil; self.dirty = true
elseif self.click_count == 2 then
self.click_clock:unset(); self.click_count = 0; self:handle_dbl_click(x, y)
end
end
function eq:mouse_drag(x, y)
if not self.selected_id then return end
local b = self:get_band_by_id(self.selected_id)
if not b then return end
local dy = self.last_mouse_y - y
self.last_mouse_y = y
if self.ctrl_pressed then
self.drag_accum_step = self.drag_accum_step + dy
if math.abs(self.drag_accum_step) > 30 then
self:change_type_step(b, self.drag_accum_step > 0 and 1 or -1)
self.drag_accum_step = 0; self.dirty = true
self:output_single_band_status(b)
end
return
end
if (b.type == "lowpass" or b.type == "highpass") then
if self.shift_pressed then
self.drag_accum_step = self.drag_accum_step + dy
if math.abs(self.drag_accum_step) > 40 then
b.order = math.max(2, math.min(8, b.order + (self.drag_accum_step > 0 and 1 or -1) * 2))
self.drag_accum_step = 0; self.dirty = true
end
else
b.f = math.max(self.f_min, math.min(self.f_max, self:x_to_f(x)))
b.q = math.max(0.1, math.min(15.0, b.q + (dy * 0.005 * b.q)))
self.dirty = true
end
else
if self.shift_pressed then
b.q = math.max(0.1, math.min(15.0, b.q + (dy * 0.05 * b.q)))
self.dirty = true
else
b.f = math.max(self.f_min, math.min(self.f_max, self:x_to_f(x)))
if not FIXED_GAIN_TYPES[b.type] then
b.g = math.max(-self.db_range, math.min(self.db_range, self:y_to_g(y)))
end
self.dirty = true
end
end
if self.dirty then self:output_single_band_status(b) end
end
function eq:mouse_up()
if self.is_dragging then
self.is_dragging = false -- 标记结束拖动
-- 拖动结束时,必须再发送一次状态,让外部接收到 0
if self.selected_id then
local b = self:get_band_by_id(self.selected_id)
if b then
self:output_single_band_status(b)
end
end
end
end
function eq:reset_click() self.click_count = 0 end
function eq:tick()
if self.dirty then
self:repaint()
self:output_all_coeffs()
self:output_state_list()
self.dirty = false
end
self.render_clock:delay(self.tick_time)
end
-- 坐标转换
function eq:x_to_f(x) return self.f_min * ((self.f_max/self.f_min)^(x/self.width)) end
function eq:f_to_x(f) return self.width * math.log(f/self.f_min) / math.log(self.f_max/self.f_min) end
function eq:g_to_y(g) return self.height/2 - (g/self.db_range)*(self.height/2 - 20) end
function eq:y_to_g(y) return (self.height/2 - y) / (self.height/2 - 20) * self.db_range end
function eq:paint(g)
g:set_color(0, 0, 0, 0); g:fill_all()
-- 1. Grid
for p = 0, 4 do
local multiplier = 10^p
for i = 1, 9 do
local f = i * multiplier
if f >= 10 and f <= self.f_max then
local x = self:f_to_x(f)
if i == 1 then
g:set_color(50, 50, 50, 60); g:fill_rect(x, 0, 1.5, self.height)
g:set_color(60, 60, 60, 255); g:draw_text((f>=1000 and (f/1000).."k" or f), x+2, self.height-12, 40, 10)
else
g:set_color(150, 150, 150, 20); g:fill_rect(x, 0, 1, self.height)
end
end
end
end
for _, db in ipairs({-18, -12, -6, 0, 6, 12, 18}) do
local y = self:g_to_y(db)
g:set_color(100, 100, 100, (db==0 and 80 or 30)); g:fill_rect(0, y, self.width, 1)
g:draw_text(db.."dB", 2, y-10, 30, 10)
end
-- 2. Curve
local bands_coeffs = {}
for i, b in ipairs(self.bands) do bands_coeffs[i] = self:get_band_coeffs_list(b) end
g:set_color(50, 50, 50)
local path = nil
for x = 0, self.width, 2 do
local f = self:x_to_f(x)
local total_db = 0
for i = 1, #self.bands do total_db = total_db + self:get_response_db(f, bands_coeffs[i]) end
if total_db > 35 then total_db = 35 elseif total_db < -35 then total_db = -35 end
local y = self:g_to_y(total_db)
if not path then path = Path(x, y) else path:line_to(x, y) end
end
if path then g:stroke_path(path, 2) end
-- 3. Handles
for i, b in ipairs(self.bands) do
local x, y = self:f_to_x(b.f), self:g_to_y(b.g)
-- 保护 ID,防止为 nil 时崩溃,默认为 1
local safe_id = b.id or 1
local col = PALETTE[safe_id] or {200,200,200}
g:set_color(col[1], col[2], col[3], 220); g:fill_ellipse(x-8, y-8, 16, 16)
g:set_color(0,0,0,200)
g:draw_text(tostring(safe_id), x-3, y-4, 20, 10)
-- 必须确保 selected_id 存在,且 ID 匹配
if self.selected_id and b.id and self.selected_id == b.id then
g:set_color(0,0,0,150); g:stroke_ellipse(x-12, y-12, 24, 24, 2)
-- Info Box
local lines = {}
local t_str = string.upper(b.type)
if self.ctrl_pressed then t_str = "> "..t_str.." <" end
-- 这里使用 tostring 进一步保护
table.insert(lines, "#"..tostring(b.id).." "..t_str)
table.insert(lines, string.format("%.0fHz", b.f))
if b.type == "lowpass" or b.type == "highpass" then
local s_prefix = self.shift_pressed and "> " or ""
local s_suffix = self.shift_pressed and " <" or ""
table.insert(lines, s_prefix..(b.order*6).."dB/oct"..s_suffix)
else
local q_prefix = self.shift_pressed and "> " or ""
local q_suffix = self.shift_pressed and " <" or ""
table.insert(lines, q_prefix.."Q: "..string.format("%.2f", b.q)..q_suffix)
end
local box_w, box_h = 120, #lines * 14 + 10
local box_x, box_y = x - box_w/2, y - 25 - box_h
g:set_color(245, 245, 245, 100); g:fill_rounded_rect(box_x, box_y, box_w, box_h,5)
g:set_color(150, 150, 150, 20); g:stroke_rounded_rect(box_x, box_y, box_w, box_h, 5,2)
g:set_color(30, 30, 30, 255)
for li, line in ipairs(lines) do g:draw_text(line, box_x + 6, box_y + 4 + (li-1)*14, 200, 12) end
end
end
end