jacket/examples/jacket_port_connect_cb.nim

73 lines
2.0 KiB
Nim
Raw Normal View History

2022-10-09 09:43:13 +02:00
import std/[logging, os]
import jacket
import signal
var jclient: ClientP
var status: cint
var exitSignalled: bool = false
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
2022-10-09 09:43:13 +02:00
proc cleanup(sig: cint = 0) =
2022-10-09 09:43:13 +02:00
debug "Cleaning up..."
if jclient != nil:
2022-10-09 09:43:13 +02:00
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
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
proc signalCb(sig: cint) {.noconv.} =
debug "Received signal: " & $sig
exitSignalled = true
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
warn "JACK server has shut down."
exitSignalled = true
proc portConnected(portA: PortId; portB: PortId; connect: cint; arg: pointer) {.cdecl.} =
2022-10-09 09:43:13 +02:00
let portAPtr = jclient.portById(portA)
let portBPtr = jclient.portById(portB)
if portAPtr != nil:
2022-10-09 09:43:13 +02:00
echo("Port A: ", portName(portAPtr))
else:
echo "Port A: <unknown>"
if portAPtr != nil:
2022-10-09 09:43:13 +02:00
echo("Port B: ", portName(portBPtr))
else:
echo "Port B: <unknown>"
2022-10-09 09:43:13 +02:00
echo("Action: ", if connect > 0: "connect" else: "disconnect")
2022-10-09 09:43:13 +02:00
addHandler(log)
setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_connect_cb", NoStartServer, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
2022-10-09 09:43:13 +02:00
error getJackStatusErrorString(status)
quit QuitFailure
when defined(windows):
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM)
else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput, 0)
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
if jclient.setPortConnectCallback(portConnected) != 0:
error "Error: could not set JACK port connection callback."
jclient.onShutdown(shutdownCb)
2022-10-09 09:43:13 +02:00
if jclient.activate() == 0:
while not exitSignalled:
2022-10-09 09:43:13 +02:00
sleep(50)
cleanup()