From 25bf6b31cfca624907704704fbb282cae7c709e7 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Mon, 10 Oct 2022 19:45:13 +0200 Subject: [PATCH] refactor: tweak jacket_sine example and add comments Signed-off-by: Christopher Arndt --- ...cket_simple_client.nim => jacket_sine.nim} | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) rename examples/{jacket_simple_client.nim => jacket_sine.nim} (72%) diff --git a/examples/jacket_simple_client.nim b/examples/jacket_sine.nim similarity index 72% rename from examples/jacket_simple_client.nim rename to examples/jacket_sine.nim index d078e7c..acbd2a6 100644 --- a/examples/jacket_simple_client.nim +++ b/examples/jacket_sine.nim @@ -3,7 +3,7 @@ import signal import jacket var jclient: ClientTPtr -var out1: PortTPtr +var outPort: PortTPtr var status: cint var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug) @@ -56,11 +56,11 @@ proc cleanup(sig: cint = 0) {.noconv.} = quit QuitSuccess proc shutdownCb(arg: pointer = nil) {.cdecl.} = - debug "Server has shut down" + debug "JACK server has shut down" cleanup() -proc process(nFrames: NFramesT, arg: pointer): cint {.cdecl.} = - var outbuf = cast[JackBufferPtr](portGetBuffer(out1, nFrames)) +proc processCb(nFrames: NFramesT, arg: pointer): cint {.cdecl.} = + var outbuf = cast[JackBufferPtr](portGetBuffer(outPort, nFrames)) let osc = cast[SineOscPtr](arg) for i in 0 ..< nFrames: @@ -70,34 +70,39 @@ proc process(nFrames: NFramesT, arg: pointer): cint {.cdecl.} = addHandler(log) -# create JACK client +# Create JACK client setErrorFunction(errorCb) -jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr) -debug "Server status: " & $status +jclient = clientOpen("jacket_sine", NoStartServer.ord or UseExactName.ord, status.addr) +debug "JACK server status: " & $status if jclient == nil: error getJackStatusErrorString(status) quit 1 +# Create sine oscillator let sampleRate =(float) jclient.getSampleRate() +debug "JACK sample rate: " & $sampleRate var osc = initSineOsc(sampleRate, sineFreq) - +# Set up signal handlers to clean up on exit when defined(windows): setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM) else: setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) -if jclient.setProcessCallback(process, osc.addr) != 0: - error "Could not set process callback function." +# Register JACK callbacks +if jclient.setProcessCallback(processCb, osc.addr) != 0: + error "Could not set JACK process callback function." cleanup() jclient.onShutdown(shutdownCb, nil) -out1 = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) +# Create output port +outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) +# Activate JACK client ... if jclient.activate() == 0: - # keep running until the Ctrl+C + # ... and keep running until a signal is received while true: sleep(50)