2022-10-09 09:43:13 +02:00
|
|
|
import std/[logging, os]
|
2022-10-06 04:18:57 +02:00
|
|
|
import jacket
|
2022-10-09 19:48:55 +02:00
|
|
|
import signal
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-06 16:28:25 +02:00
|
|
|
var jclient: ClientTPtr
|
2022-10-06 04:18:57 +02:00
|
|
|
var status: cint
|
2022-10-09 19:48:55 +02:00
|
|
|
var log = newConsoleLogger(lvlDebug)
|
2022-10-09 09:43:13 +02:00
|
|
|
|
|
|
|
proc errorCb(msg: cstring) {.cdecl.} =
|
|
|
|
# Suppress verbose JACK error messages when server is not available by
|
|
|
|
# default. Pass ``lvlAll`` when creating the logger to enable them.
|
|
|
|
debug "JACK error: " & $msg
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 19:48:55 +02:00
|
|
|
proc cleanup(sig: cint) {.noconv.} =
|
2022-10-09 09:43:13 +02:00
|
|
|
debug "Cleaning up..."
|
2022-10-06 04:42:42 +02:00
|
|
|
|
2022-10-06 16:28:25 +02:00
|
|
|
if jclient != nil:
|
2022-10-09 09:43:13 +02:00
|
|
|
discard jclient.deactivate()
|
|
|
|
discard jclient.clientClose()
|
2022-10-06 16:28:25 +02:00
|
|
|
jclient = nil
|
2022-10-06 04:42:42 +02:00
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
quit QuitSuccess
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-06 05:10:49 +02:00
|
|
|
proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer) {.cdecl.} =
|
2022-10-09 09:43:13 +02:00
|
|
|
let portAPtr = jclient.portById(portA)
|
|
|
|
let portBPtr = jclient.portById(portB)
|
2022-10-06 04:18:57 +02:00
|
|
|
|
|
|
|
if portAPtr != nil:
|
2022-10-09 09:43:13 +02:00
|
|
|
echo("Port A: ", portName(portAPtr))
|
2022-10-06 04:18:57 +02:00
|
|
|
else:
|
|
|
|
echo "Port A: <unknown>"
|
|
|
|
|
|
|
|
if portAPtr != nil:
|
2022-10-09 09:43:13 +02:00
|
|
|
echo("Port B: ", portName(portBPtr))
|
2022-10-06 04:18:57 +02:00
|
|
|
else:
|
|
|
|
echo "Port B: <unknown>"
|
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
echo("Action: ", if connect > 0: "connect" else: "disconnect")
|
2022-10-06 04:18:57 +02:00
|
|
|
|
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
addHandler(log)
|
|
|
|
setErrorFunction(errorCb)
|
|
|
|
jclient = clientOpen("jacket_port_register", NoStartServer.ord, status.addr)
|
|
|
|
debug "Server status: " & $status
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-06 16:28:25 +02:00
|
|
|
if jclient == nil:
|
2022-10-09 09:43:13 +02:00
|
|
|
error getJackStatusErrorString(status)
|
2022-10-06 04:18:57 +02:00
|
|
|
quit 1
|
|
|
|
|
2022-10-09 19:48:55 +02:00
|
|
|
when defined(windows):
|
|
|
|
setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
|
|
|
|
else:
|
|
|
|
setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
|
2022-10-06 16:41:23 +02:00
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
|
|
|
|
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
if jclient.setPortConnectCallback(portConnected, nil) != 0:
|
|
|
|
error "Error: could not set port connection callback."
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
if jclient.activate() == 0:
|
|
|
|
while true:
|
|
|
|
sleep(50)
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 19:48:55 +02:00
|
|
|
cleanup()
|