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
|
|
|
|
2023-05-22 05:13:31 +02:00
|
|
|
var jclient: ClientP
|
2022-10-06 04:18:57 +02:00
|
|
|
var status: cint
|
2022-10-10 20:35:00 +02:00
|
|
|
var exitSignalled: bool = false
|
2022-10-09 21:59:40 +02:00
|
|
|
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
|
2022-10-09 09:43:13 +02:00
|
|
|
|
2022-10-10 20:35:00 +02:00
|
|
|
proc cleanup(sig: cint = 0) =
|
2022-10-09 09:43:13 +02:00
|
|
|
debug "Cleaning up..."
|
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-10 20:35:00 +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
|
|
|
|
|
|
|
|
proc signalCb(sig: cint) {.noconv.} =
|
|
|
|
info "Received signal: " & $sig
|
|
|
|
exitSignalled = true
|
|
|
|
|
|
|
|
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
|
|
|
|
info "JACK server has shut down."
|
|
|
|
exitSignalled = true
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2023-05-22 05:13:31 +02:00
|
|
|
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)
|
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)
|
2022-10-10 20:35:00 +02:00
|
|
|
jclient = clientOpen("jacket_port_connect_cb", NoStartServer.ord, status.addr)
|
|
|
|
debug "JACK 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-10 20:35:00 +02:00
|
|
|
quit QuitFailure
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 19:48:55 +02:00
|
|
|
when defined(windows):
|
2022-10-10 20:35:00 +02:00
|
|
|
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM)
|
2022-10-09 19:48:55 +02:00
|
|
|
else:
|
2022-10-10 20:35:00 +02:00
|
|
|
setSignalProc(signalCb, 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:
|
2022-10-10 20:35:00 +02:00
|
|
|
error "Error: could not set JACK port connection callback."
|
|
|
|
|
|
|
|
jclient.onShutdown(shutdownCb, nil)
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-09 09:43:13 +02:00
|
|
|
if jclient.activate() == 0:
|
2022-10-10 20:35:00 +02:00
|
|
|
while not exitSignalled:
|
2022-10-09 09:43:13 +02:00
|
|
|
sleep(50)
|
2022-10-06 04:18:57 +02:00
|
|
|
|
2022-10-10 20:35:00 +02:00
|
|
|
cleanup()
|