refactor: tweak jacket_sine example and add comments

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2022-10-10 19:45:13 +02:00
parent 5edb9724a0
commit 25bf6b31cf
1 changed files with 17 additions and 12 deletions

View File

@ -3,7 +3,7 @@ import signal
import jacket import jacket
var jclient: ClientTPtr var jclient: ClientTPtr
var out1: PortTPtr var outPort: PortTPtr
var status: cint var status: cint
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug) var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
@ -56,11 +56,11 @@ proc cleanup(sig: cint = 0) {.noconv.} =
quit QuitSuccess quit QuitSuccess
proc shutdownCb(arg: pointer = nil) {.cdecl.} = proc shutdownCb(arg: pointer = nil) {.cdecl.} =
debug "Server has shut down" debug "JACK server has shut down"
cleanup() cleanup()
proc process(nFrames: NFramesT, arg: pointer): cint {.cdecl.} = proc processCb(nFrames: NFramesT, arg: pointer): cint {.cdecl.} =
var outbuf = cast[JackBufferPtr](portGetBuffer(out1, nFrames)) var outbuf = cast[JackBufferPtr](portGetBuffer(outPort, nFrames))
let osc = cast[SineOscPtr](arg) let osc = cast[SineOscPtr](arg)
for i in 0 ..< nFrames: for i in 0 ..< nFrames:
@ -70,34 +70,39 @@ proc process(nFrames: NFramesT, arg: pointer): cint {.cdecl.} =
addHandler(log) addHandler(log)
# create JACK client # Create JACK client
setErrorFunction(errorCb) setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr) jclient = clientOpen("jacket_sine", NoStartServer.ord or UseExactName.ord, status.addr)
debug "Server status: " & $status debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
error getJackStatusErrorString(status) error getJackStatusErrorString(status)
quit 1 quit 1
# Create sine oscillator
let sampleRate =(float) jclient.getSampleRate() let sampleRate =(float) jclient.getSampleRate()
debug "JACK sample rate: " & $sampleRate
var osc = initSineOsc(sampleRate, sineFreq) var osc = initSineOsc(sampleRate, sineFreq)
# Set up signal handlers to clean up on exit
when defined(windows): when defined(windows):
setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
else: else:
setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
if jclient.setProcessCallback(process, osc.addr) != 0: # Register JACK callbacks
error "Could not set process callback function." if jclient.setProcessCallback(processCb, osc.addr) != 0:
error "Could not set JACK process callback function."
cleanup() cleanup()
jclient.onShutdown(shutdownCb, nil) 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: if jclient.activate() == 0:
# keep running until the Ctrl+C # ... and keep running until a signal is received
while true: while true:
sleep(50) sleep(50)