-
Notifications
You must be signed in to change notification settings - Fork 20
/
audio-balance.lua
177 lines (149 loc) · 5.24 KB
/
audio-balance.lua
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
--[[
hacky port of mpv balance property to lavfi pan
unlike mpv it should also change the volume to
the surround and back lateral channels
might be broken with "6.1" channel layout, since it can use
either back or surround laterals
input.conf default bindings:
) script-binding balance-to-left
= script-binding balance-to-right
? script-binding reset-balance
<key> script-message <script-name> <value between -1.0 and 1.0>
# 'balance-to-left' is equivalent to "script-message <script-name> -0.1"
# 'balance-to-right' is equivalent to "script-message <script-name> 0.1"
--]]
options = require 'mp.options'
local opts = {
forcelayout = "",
-- if empty, will use the same layout as the original audio
left = 0.5,
right = 0.5
}
options.read_options(opts)
local left = opts.left
local right = opts.right
local function add_left_channel(left_ch_name, right_ch_name)
return string.format("%s=%.1f*%s+%.1f*%s",
left_ch_name,
math.max(0,math.min(1,left*2)), left_ch_name,
math.max(0,math.min(1,(right-0.5)*2)), right_ch_name)
end
local function add_right_channel(left_ch_name, right_ch_name)
return string.format("%s=%.1f*%s+%.1f*%s",
right_ch_name,
math.max(0,math.min(1,(left-0.5)*2)), left_ch_name,
math.max(0,math.min(1,right*2)), right_ch_name)
end
local function update_filter()
local graph = {}
local channels =
opts.forcelayout ~= "" and opts.forcelayout or
mp.get_property('audio-params/hr-channels', 'stereo')
if channels == "mono" then
return
end
graph[1] = add_left_channel('FL', 'FR')
graph[2] = add_right_channel('FL', 'FR')
if channels == "3.0" or
channels == "3.0(back)" or
channels == "3.1" or
channels == "5.0" or
channels == "5.0(side)" or
channels == "4.1" or
channels == "5.1" or
channels == "5.1(side)" or
channels == "6.0" or
channels == "6.0(front)" or
channels == "hexagonal" or
channels == "6.1" or
channels == "7.0" or
channels == "7.0(front)" or
channels == "7.1" or
channels == "7.1(wide)" or
channels == "7.1(side-side)" or
channels == "octagonal" then
graph[#graph+1] = 'FC=FC'
end
if channels == "2.1" or
channels == "3.1" or
channels == "4.1" or
channels == "5.1" or
channels == "5.1(side)" or
channels == "6.1" or
channels == "6.1(front)" or
channels == "7.1" or
channels == "7.1(wide)" or
channels == "7.1(side-side)" then
graph[#graph+1] = 'LFE=LFE'
end
if channels == "3.0(back)" or
channels == "4.0" or
channels == "4.1" or
channels == "6.0" or
channels == "hexagonal" or
channels == "6.1" or
channels == "6.1(back)" or
channels == "octagonal" then
graph[#graph+1] = 'BC=BC'
end
if channels == "quad" or
channels == "5.0" or
channels == "5.1" or
channels == "hexagonal" or
channels == "6.1(back)" or
channels == "7.0" or
channels == "7.1" or
channels == "7.1(wide)" or
channels == "octagonal" then
graph[#graph+1] = add_left_channel('BL', 'BR')
graph[#graph+1] = add_right_channel('BL', 'BR')
end
if channels == "quad(side)" or
channels == "5.0(side)" or
channels == "5.1(side)" or
channels == "hexagonal" or
channels == "6.0" or
channels == "6.0(front)" or
channels == "6.1" or
channels == "6.1(front)" or
channels == "7.0" or
channels == "7.0(front)" or
channels == "7.1" or
channels == "7.1(wide-side)" or
channels == "octagonal" then
graph[#graph+1] = add_left_channel('SL', 'SR')
graph[#graph+1] = add_right_channel('SL', 'SR')
end
if channels == "6.0(front)" or
channels == "6.1(front)" or
channels == "7.0(front)" or
channels == "7.1(wide)" or
channels == "7.1(wide-side)" then
graph[#graph+1] = add_left_channel('FLC', 'FRC')
graph[#graph+1] = add_right_channel('FLC', 'FRC')
end
mp.command(string.format('no-osd af add @balance:lavfi=[pan=%s|%s]',
channels, table.concat(graph, "|")))
mp.commandv('show-text',
string.format('Audio Balance (pan): Left: %.0f%% Right: %.0f%%',
left*100, right*100))
end
local function change_balance(val)
val = tonumber(val)
if not val or (val > 1 or val < -1) then
mp.msg.warn("Parameter should be a number between -1.0 and 1.0 (was "..val..")")
return
end
left = math.max(0,math.min(1,left + val * -1))
right = math.max(0,math.min(1,right + val))
update_filter()
end
mp.register_script_message(mp.get_script_name(), change_balance)
-- shift+9 and shift+0 in Portuguese layout
mp.add_key_binding(")", 'balance-to-left', function() change_balance(-0.05); end, { repeatable = true })
mp.add_key_binding("=", 'balance-to-right', function() change_balance(0.05); end, { repeatable = true })
mp.add_key_binding("?", 'balance-reset', function()
mp.command('no-osd af del @balance')
left = 0.5
right = 0.5
end)