-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample02.lua
124 lines (103 loc) · 4.09 KB
/
example02.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
----------------------------------------------------------------------------------------------------
--[[
This example lets the user select a MIDI OUT and a MIDI IN port which are connected and
observed afterwards. The example reconnects if the connection is disconnected or
if one of the ports disappears and reappears again.
The example uses [lua-nocurses](https://github.com/osch/lua-nocurses) for user
interaction and [lua-mtmsg](https://github.com/osch/lua-mtmsg) as receiver for
JACK status events, see also [Status Messages](https://github.com/osch/lua-ljack/blob/master/doc/README.md#status-messages).
--]]
----------------------------------------------------------------------------------------------------
local nocurses = require("nocurses") -- https://github.com/osch/lua-nocurses
local mtmsg = require("mtmsg") -- https://github.com/osch/lua-mtmsg
local ljack = require("ljack")
----------------------------------------------------------------------------------------------------
local jackInfo = mtmsg.newbuffer() -- jack status callback messages
jackInfo:notifier(nocurses) -- notify nocurses in case of new messages
local client = ljack.client_open("example02.lua", jackInfo)
----------------------------------------------------------------------------------------------------
local function printbold(...)
nocurses.setfontbold(true)
print(...)
nocurses.resetcolors()
end
local function printred(...)
nocurses.setfontbold(true)
nocurses.setfontcolor("RED")
print(...)
nocurses.resetcolors()
end
----------------------------------------------------------------------------------------------------
local function choosePort(type, direction)
local list = client:get_ports(".*", type, direction)
printbold(string.format("%s %s Ports:", type, direction))
for i, p in ipairs(list) do
print(i, p)
end
if #list == 0 then
print("No ports found")
os.exit()
end
while true do
io.write(string.format("Choose %s Port (1-%d): ", direction, #list))
local inp = io.read()
if inp == "q" then os.exit() end
local p = list[tonumber(inp)]
if p then
print()
return p
end
end
end
----------------------------------------------------------------------------------------------------
local p1 = choosePort("MIDI", "OUT")
local p2 = choosePort("MIDI", "IN")
----------------------------------------------------------------------------------------------------
if p1 and p2 then
client:activate()
print(string.format("Connecting %q\n"..
" with %q.", p1, p2))
local isConnected = client:connect(p1, p2)
if not isConnected then
printred("Connection failed")
end
local function handleJackInfo(event, ...)
if event == "PortConnect" then
local id1, id2, connected = ...
if not connected
and client:port_by_id(id1):name() == p1
and client:port_by_id(id2):name() == p2
then
isConnected = client:connect(p1, p2)
if isConnected then
print("Reconnected ports.")
else
printred("Connection lost.")
end
end
elseif event == "GraphOrder" and not isConnected then
isConnected = client:connect(p1, p2)
if isConnected then
print("Reconnected ports.")
end
elseif event == "Shutdown" then
printbold("Jack server shutdown.")
os.exit()
end
return event
end
printbold("Observing connection... (Press <Q> to Quit)")
while true do
local c = nocurses.getch() -- returns for new messages in jackInfo
repeat
local hadEvent = handleJackInfo(jackInfo:nextmsg(0)) -- non-blocking read
until not hadEvent
if c then
c = string.char(c)
if c == "Q" or c == "q" then
printbold("Quit.")
break
end
end
end
end