feat: implement converters for enums

Allows to pass enum values directly for cint/culong params

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-05-14 23:47:13 +02:00
parent 0bfb23b6ff
commit 9645542176
12 changed files with 45 additions and 40 deletions

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ proc main() =
# Create JACK client # Create JACK client
setErrorFunction(errorCb) 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 debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
@ -74,7 +74,7 @@ proc main() =
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb)
# Create output port # 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 ... # Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:

View File

@ -110,7 +110,7 @@ proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
proc main() = proc main() =
# Create JACK client # Create JACK client
setErrorFunction(errorCb) 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 debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
@ -138,7 +138,7 @@ proc main() =
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb)
# Create output port # 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 ... # Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:

View File

@ -83,7 +83,7 @@ proc main() =
# Create JACK client # Create JACK client
setErrorFunction(errorCb) 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 debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
@ -110,7 +110,7 @@ proc main() =
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb)
# Create output port # 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 ... # Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:

View File

@ -73,22 +73,21 @@ proc midiEventPrinterProc() =
stdout.flushFile() stdout.flushFile()
proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} = proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
var event: MidiEvent
let inbuf = portGetBuffer(midiPort, nFrames) let inbuf = portGetBuffer(midiPort, nFrames)
let count = midiGetEventCount(inbuf) let count = midiGetEventCount(inbuf)
for i in 0..<count: for i in 0..<count:
if midiEventGet(event.addr, inbuf, i.uint32) == 0: if midiEventGet(event.addr, inbuf, i.uint32) == 0:
midiEventChan.send(event) if not midiEventChan.trySend(event):
# trySend does not work: https://github.com/nim-lang/threading/issues/30 warn "MIDI event channel overflow!"
#if not midiEventChan.trySend(event):
# warn "MIDI event channel overflow!"
proc main() = proc main() =
addHandler(log) addHandler(log)
# Create JACK client # Create JACK client
setErrorFunction(errorCb) 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 debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
@ -115,7 +114,7 @@ proc main() =
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb)
# Create output port # 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 ... # Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:

View File

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

View File

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

View File

@ -74,7 +74,7 @@ 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_sine", NoStartServer or UseExactName, status.addr)
debug "JACK server status: " & $status debug "JACK server status: " & $status
if jclient == nil: if jclient == nil:
@ -101,7 +101,7 @@ if jclient.setProcessCallback(processCb, osc.addr) != 0:
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb)
# Create output port # 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 ... # Activate JACK client ...
if jclient.activate() == 0: if jclient.activate() == 0:

View File

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

View File

@ -232,6 +232,12 @@ type
PropertyChangeCallback* = proc (subject: Uuid, key: cstring, change: PropertyChange, arg: pointer) {.cdecl.} 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 ------------------------------ # ----------------------------- Version info ------------------------------
# void jack_get_version(int *major_ptr, int *minor_ptr, int *micro_ptr, int *proto_ptr) # 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 = proc getJackStatusErrorString*(status: cint): string =
# Get JACK error status as string. # Get JACK error status as string.
if status == Success.ord: if status == Success:
return "" return ""
if status == Failure.ord: if status == Failure:
# Only include this generic message if no other error status is set # Only include this generic message if no other error status is set
result = "Overall operation failed" 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") 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") 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") 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") 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") 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") 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") result.add("\nUnable to load internal client")
if (status and InitFailure.ord) > 0: if (status and InitFailure) > 0:
result.add("\nUnable to initialize client") result.add("\nUnable to initialize client")
if (status and ShmFailure.ord) > 0: if (status and ShmFailure) > 0:
result.add("\nUnable to access shared memory") 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") result.add("\nClient's protocol version does not match")
if (status and BackendError.ord) > 0: if (status and BackendError) > 0:
result.add("\nBackend Error") result.add("\nBackend Error")
if (status and ClientZombie.ord) > 0: if (status and ClientZombie) > 0:
result.add("\nClient is being shutdown against its will") result.add("\nClient is being shutdown against its will")