From e68223f1e6e625e221631907fb2056f4f75782fb Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Sun, 9 Oct 2022 09:43:13 +0200 Subject: [PATCH] refactor: more examples streamlining --- examples/test_jacket.nim | 38 +++++++++++------------ examples/test_port_connect_cb.nim | 50 +++++++++++++++++-------------- examples/test_port_register.nim | 28 ++++++++++------- 3 files changed, 64 insertions(+), 52 deletions(-) diff --git a/examples/test_jacket.nim b/examples/test_jacket.nim index 26e2ef2..64871a7 100644 --- a/examples/test_jacket.nim +++ b/examples/test_jacket.nim @@ -1,30 +1,30 @@ +import std/logging import jacket var jclient: ClientTPtr -var time: TimeT var status: cint +var log = newConsoleLogger(lvlInfo) -jclient = clientOpen("test_jacket", NullOption.ord, addr status) +proc errorCb(msg: cstring) {.cdecl.} = + # Suppress verbose JACK error messges when server is not available by + # default. Pass ``lvlAll`` when creating the logger to enable them. + debug "JACK error: " & $msg -echo "Server status: " & $status +addHandler(log) +setErrorFunction(errorCb) +jclient = clientOpen("test_jacket", NullOption.ord, status.addr) +debug "Server status: " & $status if jclient == nil: - echo getJackStatusErrorString(status) + error getJackStatusErrorString(status) quit 1 -time = getTime() -let ver = getVersionString() -echo "JACK version: " & $ver -let rate = getSampleRate(jclient) -echo "Sample rate: " & $rate -let bufsize = getBufferSize(jclient) -echo "Buffer size: " & $bufsize -let load = cpuLoad(jclient) -echo "DSP load: " & $load & "%" -echo "Server time: " & $time -let name = getClientName(jclient) -echo "Client name: " & $name -let rt = if isRealtime(jclient) > 0: "yes" else: "no" -echo "RT enabled: " & rt +echo("JACK version: ", getVersionString()) +echo("Sample rate: ", jclient.getSampleRate) +echo("Buffer size: ", jclient.getBufferSize) +echo("DSP load: ", jclient.cpuLoad, "%") +echo("Server time: ", getTime()) +echo("Client name: ", jclient.getClientName) +echo("RT enabled: ", if jclient.isRealtime > 0: "yes" else: "no") -discard clientClose(jclient) +discard jclient.clientClose diff --git a/examples/test_port_connect_cb.nim b/examples/test_port_connect_cb.nim index 9fa0700..7935a69 100644 --- a/examples/test_port_connect_cb.nim +++ b/examples/test_port_connect_cb.nim @@ -1,55 +1,61 @@ -import std/os +import std/[logging, os] import jacket var jclient: ClientTPtr var status: cint +var log = newConsoleLogger(lvlInfo) + +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 cleanup() {.noconv.} = - echo "Cleaning up..." + debug "Cleaning up..." if jclient != nil: - discard deactivate(jclient) - discard clientClose(jclient) + discard jclient.deactivate() + discard jclient.clientClose() jclient = nil - quit 0 + quit QuitSuccess proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer) {.cdecl.} = - let portAPtr = portById(jclient, portA) - let portBPtr = portById(jclient, portB) + let portAPtr = jclient.portById(portA) + let portBPtr = jclient.portById(portB) if portAPtr != nil: - echo "Port A: " & $portName(portAPtr) + echo("Port A: ", portName(portAPtr)) else: echo "Port A: " if portAPtr != nil: - echo "Port B: " & $portName(portBPtr) + echo("Port B: ", portName(portBPtr)) else: echo "Port B: " - echo "Action: " & (if connect > 0: "connect" else: "disconnect") + echo("Action: ", if connect > 0: "connect" else: "disconnect") -jclient = clientOpen("jacket_port_register", NoStartServer.ord, addr status) - -echo "Server status: " & $status +addHandler(log) +setErrorFunction(errorCb) +jclient = clientOpen("jacket_port_register", NoStartServer.ord, status.addr) +debug "Server status: " & $status if jclient == nil: - echo getJackStatusErrorString(status) + error getJackStatusErrorString(status) quit 1 setControlCHook(cleanup) -discard portRegister(jclient, "in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) -discard portRegister(jclient, "out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) +discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) +discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) -if setPortConnectCallback(jclient, portConnected, nil) != 0: - echo "Error: could not set port connection callback." +if jclient.setPortConnectCallback(portConnected, nil) != 0: + error "Error: could not set port connection callback." -discard activate(jclient) - -while true: - sleep(50) +if jclient.activate() == 0: + while true: + sleep(50) cleanup() diff --git a/examples/test_port_register.nim b/examples/test_port_register.nim index 97a3db4..57c5e82 100644 --- a/examples/test_port_register.nim +++ b/examples/test_port_register.nim @@ -1,31 +1,37 @@ -import std/os +import std/[logging, os] import jacket var jclient: ClientTPtr var status: cint +var log = newConsoleLogger(lvlInfo) + +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 cleanup() {.noconv.} = - echo "Cleaning up..." + debug "Cleaning up..." if jclient != nil: - discard clientClose(jclient) + discard jclient.clientClose jclient = nil - quit 0 + quit QuitSuccess - -jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, addr status) - -echo "Server status: " & $status +addHandler(log) +setErrorFunction(errorCb) +jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr) +debug "Server status: " & $status if jclient == nil: - echo getJackStatusErrorString(status) + error getJackStatusErrorString(status) quit 1 setControlCHook(cleanup) -discard portRegister(jclient, "in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) -discard portRegister(jclient, "out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) +discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) +discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) while true: sleep(50)