Compare commits

...

4 Commits

Author SHA1 Message Date
Christopher Arndt 1444154ca4 fix: set import search path for examples, so that they don't use the installed version
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-15 00:02:37 +02:00
Christopher Arndt 70c7782f5b feat: add passthru client example
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-15 00:01:39 +02:00
Christopher Arndt 03d7471656 fix: remove superfluous import and global var
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-15 00:01:00 +02:00
Christopher Arndt 9645542176 feat: implement converters for enums
Allows to pass enum values directly for cint/culong params

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
2024-05-14 23:47:13 +02:00
14 changed files with 131 additions and 44 deletions

View File

@ -58,10 +58,10 @@ proc processCb(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
outbuf[i] = inpbuf[i]
# Create JACK Client ptr
jackClient = clientOpen("passthru", NullOption.ord, status.addr)
jackClient = clientOpen("passthru", NullOption, status.addr)
# Register audio input and output ports
inpPort = jackClient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
outPort = jackClient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
inpPort = jackClient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput, 0)
outPort = jackClient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
# Set JACK callbacks
jackClient.onShutdown(shutdownCb)
discard jackClient.setProcessCallback(processCb, nil)

View File

@ -11,7 +11,7 @@ proc errorCb(msg: cstring) {.cdecl.} =
addHandler(log)
setErrorFunction(errorCb)
var jclient = clientOpen("jacket_info", NullOption.ord, status.addr)
var jclient = clientOpen("jacket_info", NullOption, status.addr)
debug "JACK server status: " & $status
if jclient == nil:

View File

@ -14,7 +14,7 @@ proc errorCb(msg: cstring) {.cdecl.} =
addHandler(log)
setErrorFunction(errorCb)
let jclient = clientOpen("jacket_property", NullOption.ord, status.addr)
let jclient = clientOpen("jacket_property", NullOption, status.addr)
debug "JACK server status: " & $status
if jclient == nil:

View File

@ -52,7 +52,7 @@ proc main() =
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_midi_print", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_midi_print", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -74,7 +74,7 @@ proc main() =
jclient.onShutdown(shutdownCb)
# Create output port
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput.ord, 0)
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput, 0)
# Activate JACK client ...
if jclient.activate() == 0:

View File

@ -110,7 +110,7 @@ proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
proc main() =
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_midi_print", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_midi_print", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -138,7 +138,7 @@ proc main() =
jclient.onShutdown(shutdownCb)
# Create output port
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput.ord, 0)
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput, 0)
# Activate JACK client ...
if jclient.activate() == 0:

View File

@ -83,7 +83,7 @@ proc main() =
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_midi_print", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_midi_print", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -110,7 +110,7 @@ proc main() =
jclient.onShutdown(shutdownCb)
# Create output port
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput.ord, 0)
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput, 0)
# Activate JACK client ...
if jclient.activate() == 0:

View File

@ -4,14 +4,13 @@
# nimble install threading
#
import std/[isolation, logging, os, strformat]
import std/[logging, os, strformat]
import threading/channels
import signal
import jacket
var
jclient: ClientP
event: MidiEvent
midiPort: PortP
midiEventChan: Chan[MidiEvent]
midiEventPrinter: Thread[void]
@ -32,8 +31,8 @@ proc cleanup() =
debug "Stopping MIDI event printer thread..."
# Receiving an invalid event causes receiving thread to wake up and
# break its endless loop
event.size = 0
midiEventChan.send(event)
let event = MidiEvent(size: 0)
discard midiEventChan.trySend(event)
midiEventPrinter.joinThread()
@ -73,22 +72,21 @@ proc midiEventPrinterProc() =
stdout.flushFile()
proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
var event: MidiEvent
let inbuf = portGetBuffer(midiPort, nFrames)
let count = midiGetEventCount(inbuf)
for i in 0..<count:
if midiEventGet(event.addr, inbuf, i.uint32) == 0:
midiEventChan.send(event)
# trySend does not work: https://github.com/nim-lang/threading/issues/30
#if not midiEventChan.trySend(event):
# warn "MIDI event channel overflow!"
if not midiEventChan.trySend(event):
warn "MIDI event channel overflow!"
proc main() =
addHandler(log)
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_midi_print", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_midi_print", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -115,7 +113,7 @@ proc main() =
jclient.onShutdown(shutdownCb)
# Create output port
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput.ord, 0)
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput, 0)
# Activate JACK client ...
if jclient.activate() == 0:

View File

@ -0,0 +1,82 @@
## Simple JACK client, which just passes audio through
## from its single (mono) input to its single (mono) output.
import std/[logging, os]
import signal
import jacket
var
jclient: ClientP
status: cint
exitSignalled: bool = false
inpPort, outPort: PortP
log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
type JackBufferP = ptr UncheckedArray[DefaultAudioSample]
proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by
# default. Compile in non-release mode or pass ``lvlDebug`` or lower
# when creating the logger above to enable them.
debug "JACK error: " & $msg
proc cleanup(sig: cint = 0) =
debug "Cleaning up..."
if jclient != nil:
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
proc signalCb(sig: cint) {.noconv.} =
debug "Received signal: " & $sig
exitSignalled = true
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
warn "JACK server has shut down."
exitSignalled = true
proc processCb(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
var inpbuf = cast[JackBufferP](portGetBuffer(inpPort, nFrames))
var outbuf = cast[JackBufferP](portGetBuffer(outPort, nFrames))
# copy samples from input to output buffer
for i in 0 ..< nFrames:
outbuf[i] = inpbuf[i]
addHandler(log)
# Create JACK Client ptr
setErrorFunction(errorCb)
jclient = clientOpen("passthru", NullOption, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
error getJackStatusErrorString(status)
quit 1
# Register audio input and output ports
inpPort = jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput, 0)
outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
# Register JACK callbacks
jclient.onShutdown(shutdownCb)
if jclient.setProcessCallback(processCb, nil) != 0:
error "Could not set JACK process callback function."
cleanup()
quit QuitFailure
# Handle signals
when defined(windows):
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM)
else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
# Activate JACK client ...
if jclient.activate() == 0:
# ... and keep running until a signal is received
while not exitSignalled:
sleep(50)
# Deactivate client and close server connection
cleanup()

View File

@ -45,7 +45,7 @@ proc portConnected(portA: PortId; portB: PortId; connect: cint; arg: pointer) {.
addHandler(log)
setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_connect_cb", NoStartServer.ord, status.addr)
jclient = clientOpen("jacket_port_connect_cb", NoStartServer, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -57,8 +57,8 @@ when defined(windows):
else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
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("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput, 0)
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
if jclient.setPortConnectCallback(portConnected) != 0:
error "Error: could not set JACK port connection callback."

View File

@ -28,7 +28,7 @@ proc shutdownCb(arg: pointer = nil) {.cdecl.} =
addHandler(log)
setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_register", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_port_register", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -40,8 +40,8 @@ when defined(windows):
else:
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
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("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput, 0)
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
jclient.onShutdown(shutdownCb)

View File

@ -74,7 +74,7 @@ addHandler(log)
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_sine", NoStartServer.ord or UseExactName.ord, status.addr)
jclient = clientOpen("jacket_sine", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
@ -101,7 +101,7 @@ if jclient.setProcessCallback(processCb, osc.addr) != 0:
jclient.onShutdown(shutdownCb)
# Create output port
outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput, 0)
# Activate JACK client ...
if jclient.activate() == 0:

View File

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

1
examples/nim.cfg Normal file
View File

@ -0,0 +1 @@
--path:"../src/"

View File

@ -232,6 +232,12 @@ type
PropertyChangeCallback* = proc (subject: Uuid, key: cstring, change: PropertyChange, arg: pointer) {.cdecl.}
# ------------------------- Converters for Enums --------------------------
converter jackIntEnumToCInt*[T: JackOptions | JackStatus | LatencyCallbackMode |
PositionBits | TransportState | PropertyChange](x: T): cint = x.ord.cint
converter portFlagsToCULong*(x: PortFlags): culong = x.ord.culong
# ----------------------------- Version info ------------------------------
# void jack_get_version(int *major_ptr, int *minor_ptr, int *micro_ptr, int *proto_ptr)
@ -739,33 +745,33 @@ proc setInfoFunction*(infoCallback: InfoCallback) {.importc: "jack_set_info_func
proc getJackStatusErrorString*(status: cint): string =
# Get JACK error status as string.
if status == Success.ord:
if status == Success:
return ""
if status == Failure.ord:
if status == Failure:
# Only include this generic message if no other error status is set
result = "Overall operation failed"
if (status and InvalidOption.ord) > 0:
if (status and InvalidOption) > 0:
result.add("\nThe operation contained an invalid and unsupported option")
if (status and NameNotUnique.ord) > 0:
if (status and NameNotUnique) > 0:
result.add("\nThe desired client name was not unique")
if (status and ServerStarted.ord) > 0:
if (status and ServerStarted) > 0:
result.add("\nThe JACK server was started as a result of this operation")
if (status and ServerFailed.ord) > 0:
if (status and ServerFailed) > 0:
result.add("\nUnable to connect to the JACK server")
if (status and ServerError.ord) > 0:
if (status and ServerError) > 0:
result.add("\nCommunication error with the JACK server")
if (status and NoSuchClient.ord) > 0:
if (status and NoSuchClient) > 0:
result.add("\nRequested client does not exist")
if (status and LoadFailure.ord) > 0:
if (status and LoadFailure) > 0:
result.add("\nUnable to load internal client")
if (status and InitFailure.ord) > 0:
if (status and InitFailure) > 0:
result.add("\nUnable to initialize client")
if (status and ShmFailure.ord) > 0:
if (status and ShmFailure) > 0:
result.add("\nUnable to access shared memory")
if (status and VersionError.ord) > 0:
if (status and VersionError) > 0:
result.add("\nClient's protocol version does not match")
if (status and BackendError.ord) > 0:
if (status and BackendError) > 0:
result.add("\nBackend Error")
if (status and ClientZombie.ord) > 0:
if (status and ClientZombie) > 0:
result.add("\nClient is being shutdown against its will")