Compare commits

..

No commits in common. "1127b1ec097ad0bf113334de7761938173dceb68" and "5edb9724a07e0912508f33cb4e8a724bf1bec4c2" have entirely different histories.

5 changed files with 58 additions and 107 deletions

View File

@ -3,38 +3,12 @@
A [Nim] wrapper for the [JACK] [C API] A [Nim] wrapper for the [JACK] [C API]
## Project status
This software is in *alpha status* and has no official release yet.
The basic JACK APIs (client lifecycle, ports, callbacks) have been wrapped and
are functional (see [examples]), but MIDI, transport and meta-data APIs still
need wrapping. Also, symbol names may still be changed and things moved around
before the first public release.
Also, I plan to add a higher-level abstraction on top of the direct mapping
from Nim procs and types to C functions and types, probably in the form of
a JACk client object, which takes care of a JACK client instance, registering
ports and setting up all the callbacks necessary for a well-behaved JACK
application.
# Installation
* Clone this repository.
* Change into the `jacket` directory.
* Run [`nimble install`] (or `nimble develop`).
* Run the examples with `nim compile --run examples/<example>.nim`.
## License ## License
This software is released under the *MIT License*. See the [LICENSE](./LICENSE) This software is released under the *MIT License*. See the [LICENSE](./LICENSE)
file for more information. file for more information.
[`nimble install`]: https://github.com/nim-lang/nimble#nimble-usage
[C API]: https://jackaudio.org/api/ [C API]: https://jackaudio.org/api/
[examples]: ./examples
[JACK]: https://jackaudio.org/ [JACK]: https://jackaudio.org/
[Nim]: https://nim-lang.org/ [Nim]: https://nim-lang.org/

View File

@ -3,9 +3,8 @@ import signal
import jacket import jacket
var jclient: ClientTPtr var jclient: ClientTPtr
var outPort: PortTPtr var out1: PortTPtr
var status: cint var status: cint
var exitSignalled: bool = false
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug) var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
type type
@ -41,28 +40,27 @@ proc tick(osc: SineOscPtr): float =
if osc.phase >= tableSize: if osc.phase >= tableSize:
osc.phase -= tableSize osc.phase -= tableSize
proc cleanup() =
debug "Cleaning up..."
if jclient != nil:
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
proc errorCb(msg: cstring) {.cdecl.} = proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by # Suppress verbose JACK error messages when server is not available by
# default. Pass ``lvlAll`` when creating the logger to enable them. # default. Pass ``lvlAll`` when creating the logger to enable them.
debug "JACK error: " & $msg debug "JACK error: " & $msg
proc signalCb(sig: cint) {.noconv.} = proc cleanup(sig: cint = 0) {.noconv.} =
info "Received signal: " & $sig debug "Cleaning up..."
exitSignalled = true
if jclient != nil:
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
quit QuitSuccess
proc shutdownCb(arg: pointer = nil) {.cdecl.} = proc shutdownCb(arg: pointer = nil) {.cdecl.} =
info "JACK server has shut down." debug "Server has shut down"
exitSignalled = true cleanup()
proc processCb(nFrames: NFramesT, arg: pointer): cint {.cdecl.} = proc process(nFrames: NFramesT, arg: pointer): cint {.cdecl.} =
var outbuf = cast[JackBufferPtr](portGetBuffer(outPort, nFrames)) var outbuf = cast[JackBufferPtr](portGetBuffer(out1, nFrames))
let osc = cast[SineOscPtr](arg) let osc = cast[SineOscPtr](arg)
for i in 0 ..< nFrames: for i in 0 ..< nFrames:
@ -72,40 +70,35 @@ proc processCb(nFrames: NFramesT, arg: pointer): cint {.cdecl.} =
addHandler(log) addHandler(log)
# Create JACK client # create JACK client
setErrorFunction(errorCb) setErrorFunction(errorCb)
jclient = clientOpen("jacket_sine", NoStartServer.ord or UseExactName.ord, status.addr) jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr)
debug "JACK server status: " & $status debug "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):
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM)
else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
# Register JACK callbacks when defined(windows):
if jclient.setProcessCallback(processCb, osc.addr) != 0: setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
error "Could not set JACK process callback function." else:
setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
if jclient.setProcessCallback(process, osc.addr) != 0:
error "Could not set process callback function."
cleanup() cleanup()
jclient.onShutdown(shutdownCb, nil) jclient.onShutdown(shutdownCb, nil)
# Create output port out1 = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
# Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:
# ... and keep running until a signal is received # keep running until the Ctrl+C
while not exitSignalled: while true:
sleep(50) sleep(50)
cleanup() cleanup() # normally not reached

View File

@ -12,8 +12,8 @@ proc errorCb(msg: cstring) {.cdecl.} =
addHandler(log) addHandler(log)
setErrorFunction(errorCb) setErrorFunction(errorCb)
jclient = clientOpen("jacket_info", NullOption.ord, status.addr) jclient = clientOpen("test_jacket", NullOption.ord, status.addr)
debug "JACK server status: " & $status debug "Server status: " & $status
if jclient == nil: if jclient == nil:
error getJackStatusErrorString(status) error getJackStatusErrorString(status)

View File

@ -4,28 +4,22 @@ import signal
var jclient: ClientTPtr var jclient: ClientTPtr
var status: cint var status: cint
var exitSignalled: bool = false
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug) var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
proc cleanup(sig: cint = 0) =
debug "Cleaning up..."
if jclient != nil:
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
proc errorCb(msg: cstring) {.cdecl.} = proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by # Suppress verbose JACK error messages when server is not available by
# default. Pass ``lvlAll`` when creating the logger to enable them. # default. Pass ``lvlAll`` when creating the logger to enable them.
debug "JACK error: " & $msg debug "JACK error: " & $msg
proc signalCb(sig: cint) {.noconv.} = proc cleanup(sig: cint = 0) {.noconv.} =
info "Received signal: " & $sig debug "Cleaning up..."
exitSignalled = true
proc shutdownCb(arg: pointer = nil) {.cdecl.} = if jclient != nil:
info "JACK server has shut down." discard jclient.deactivate()
exitSignalled = true discard jclient.clientClose()
jclient = nil
quit QuitSuccess
proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer) {.cdecl.} = proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer) {.cdecl.} =
let portAPtr = jclient.portById(portA) let portAPtr = jclient.portById(portA)
@ -46,28 +40,26 @@ proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer)
addHandler(log) addHandler(log)
setErrorFunction(errorCb) setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_connect_cb", NoStartServer.ord, status.addr) jclient = clientOpen("jacket_port_register", NoStartServer.ord, status.addr)
debug "JACK server status: " & $status debug "Server status: " & $status
if jclient == nil: if jclient == nil:
error getJackStatusErrorString(status) error getJackStatusErrorString(status)
quit QuitFailure quit 1
when defined(windows): when defined(windows):
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
else: else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.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) discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
if jclient.setPortConnectCallback(portConnected, nil) != 0: if jclient.setPortConnectCallback(portConnected, nil) != 0:
error "Error: could not set JACK port connection callback." error "Error: could not set port connection callback."
jclient.onShutdown(shutdownCb, nil)
if jclient.activate() == 0: if jclient.activate() == 0:
while not exitSignalled: while true:
sleep(50) sleep(50)
cleanup() cleanup() # normally not reached

View File

@ -4,48 +4,40 @@ import jacket
var jclient: ClientTPtr var jclient: ClientTPtr
var status: cint var status: cint
var exitSignalled: bool = false
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug) var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
proc cleanup(sig: cint = 0) =
debug "Cleaning up..."
if jclient != nil:
discard jclient.clientClose()
jclient = nil
proc errorCb(msg: cstring) {.cdecl.} = proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by # Suppress verbose JACK error messages when server is not available by
# default. Pass ``lvlAll`` when creating the logger to enable them. # default. Pass ``lvlAll`` when creating the logger to enable them.
debug "JACK error: " & $msg debug "JACK error: " & $msg
proc signalCb(sig: cint) {.noconv.} = proc cleanup(sig: cint = 0) {.noconv.} =
info "Received signal: " & $sig debug "Cleaning up..."
exitSignalled = true
proc shutdownCb(arg: pointer = nil) {.cdecl.} = if jclient != nil:
info "JACK server has shut down." discard jclient.clientClose
exitSignalled = true jclient = nil
quit QuitSuccess
addHandler(log) addHandler(log)
setErrorFunction(errorCb) setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr) jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr)
debug "JACK server status: " & $status debug "Server status: " & $status
if jclient == nil: if jclient == nil:
error getJackStatusErrorString(status) error getJackStatusErrorString(status)
quit 1 quit 1
when defined(windows): when defined(windows):
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
else: else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.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) discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
jclient.onShutdown(shutdownCb, nil) while true:
while not exitSignalled:
sleep(50) sleep(50)
cleanup() cleanup() # normally not reached