-
Notifications
You must be signed in to change notification settings - Fork 3
/
jackmix.lua
92 lines (86 loc) · 2.64 KB
/
jackmix.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
--{{{ environment
local osc = require("osc.client")
local os = os
local io = io
local setmetatable = setmetatable
local math = math
module("jackmix")
--}}}
--{{{ local functions
local function gain2vol(gain, min_gain)
return 100 + (gain / -min_gain) * 100
end
local function vol2gain(vol, min_gain)
return ((vol / 100) * -min_gain) + min_gain
end
--}}}
--{{{ constructor
function new(settings)
local mixer = setmetatable({}, { __index = JM })
mixer.default_channel = settings.default_channel or 1
mixer.min_gain = settings.min_gain or -60
mixer.max_gain = settings.max_gain or 0
mixer.step = settings.step or 5
mixer.state_file = settings.state_file or "/tmp/oscmix"
mixer.osc = osc.new{ host = settings.host, port = settings.port }
mixer.state = mixer:volume()
return mixer
end
--}}}
--{{{ methods
JM = {}
--{{{ methods / get_gain
function JM:get_gain(ch)
local channel = ch or self.default_channel
local gain = self.osc:send{ '#bundle', os.time(), {'/mixer/channel/get_gain', 'i', channel} }
if gain then return gain[4] end
end
--}}}
--{{{ methods / set_gain
function JM:set_gain(gain, ch)
local channel = ch or self.default_channel
local gain = math.max(self.min_gain, math.min(self.max_gain, gain))
gain = self.osc:send{ '#bundle', os.time(), {'/mixer/channel/set_gain', 'i', channel, 'f', gain} }
if gain then gain = gain[4] else return end
self.state = gain2vol(gain, self.min_gain)
return gain
end
--}}}
--{{{ methods / volume
function JM:volume(vol, ch)
local channel = ch or self.default_channel
local gain
if vol then
gain = self:set_gain( vol2gain(vol, self.min_gain), channel )
else
gain = self:get_gain( channel )
end
if gain then
self.state = gain2vol(gain, self.min_gain)
return self.state
end
end
function JM:volume_up() return self:volume(self:volume() + self.step) end
function JM:volume_down() return self:volume(self:volume() - self.step) end
--}}}
--{{{ methods / mute
function JM:mute(ch)
local channel = ch or self.default_channel
local gain = self:get_gain(channel)
if gain == self.min_gain then
local f = io.open(self.state_file)
if not f then return end
local gain = f:read()
self:set_gain(gain, channel)
self.state = gain2vol(gain, self.min_gain)
else
local f = io.open(self.state_file, "w+")
if not f then return end
f:write(gain)
self:set_gain(self.min_gain, channel)
self.state = "m"
end
end
--}}}
--}}}
-- vim: foldmethod=marker:filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80