Compare commits

..

No commits in common. "227ab3f45dd29c0083383143b88410fb8a0d5cea" and "f3a6b660d68c4cb3372014fc4d1046e600422ced" have entirely different histories.

9 changed files with 27 additions and 186 deletions

View File

@ -66,12 +66,12 @@ proc main() =
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
# Register JACK callbacks # Register JACK callbacks
if jclient.setProcessCallback(processCb) != 0: if jclient.setProcessCallback(processCb, nil) != 0:
error "Could not set JACK process callback function." error "Could not set JACK process callback function."
cleanup() cleanup()
quit QuitFailure quit QuitFailure
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb, nil)
# 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.ord, 0)

View File

@ -1,156 +0,0 @@
# This requires either nim 1.9+ or --threads:on:
import std/[locks, logging, os, strformat]
import signal
import jacket
var
jclient: ClientP
event: MidiEvent
midiPort: PortP
rb: RingbufferP
midiEventPrinter: Thread[void]
status: cint
exitSignalled: bool = false
exitLoop: bool = false
overruns: uint = 0
dataReady: Cond
dataReadyLock: Lock
let rbSize = 128
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
addHandler(log)
proc cleanup() =
debug "Cleaning up..."
if jclient != nil:
debug "Deactivating JACK client..."
discard jclient.deactivate()
if midiEventPrinter.running:
debug "Stopping MIDI event printer thread..."
exitLoop = true
if dataReadyLock.tryAcquire():
dataReady.signal()
dataReadyLock.release()
debug "Joining MIDI event printer thread..."
midiEventPrinter.joinThread()
debug "Joined."
if jclient != nil:
debug "Closing JACK client..."
discard jclient.clientClose()
jclient = nil
debug fmt"Buffer overruns: {overruns}"
debug "Bye."
proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by
# default. Pass ``lvlAll`` when creating the logger to enable them.
debug "JACK error: " & $msg
proc signalCb(sig: cint) {.noconv.} =
info "Received signal: " & $sig
exitSignalled = true
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
info "JACK server has shut down."
exitSignalled = true
proc midiEventPrinterProc() {.thread.} =
var recvBuf: array[4, uint8]
dataReadyLock.acquire()
while not exitLoop:
while not exitLoop and ringbufferReadSpace(rb) >= 4:
var read = cast[int](ringbufferRead(rb, cast[cstring](recvBuf.addr), 4))
if recvBuf[0] <= 3:
for i in 0..<recvBuf[0].int:
stdout.write(fmt"{recvBuf[i+1]:#02X} ")
stdout.write("\n")
stdout.flushFile()
dataReady.wait(dataReadyLock)
dataReadyLock.release()
proc processCb*(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
var msgBuf: array[4, uint8]
let inbuf = portGetBuffer(midiPort, nFrames)
let count = midiGetEventCount(inbuf)
for i in 0..<count:
if midiEventGet(event.addr, inbuf, i.uint32) == 0:
msgBuf[0] = event.size.uint8
if event.size <= 3:
for b in 0..<event.size:
msgBuf[b+1] = event.buffer[b]
var written = cast[int](ringbufferWrite(rb, cast[cstring](msgBuf.addr), 4))
if written < 4:
inc(overruns)
else:
if dataReadyLock.tryAcquire():
dataReady.signal()
dataReadyLock.release()
proc main() =
# Create JACK client
setErrorFunction(errorCb)
jclient = clientOpen("jacket_midi_print", NoStartServer.ord or UseExactName.ord, status.addr)
debug "JACK server status: " & $status
if jclient == nil:
error getJackStatusErrorString(status)
quit QuitFailure
# 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)
# Set up a thread, which receives MIDI events from process callback via a
# ringbuffer and prints them without danger of blocking the process callback
rb = ringbufferCreate(rbSize.csize_t)
debug fmt"Created MIDI ringbuffer of {rbSize} bytes size."
createThread(midiEventPrinter, midiEventPrinterProc)
# Register JACK callbacks
if jclient.setProcessCallback(processCb) != 0:
error "Could not set JACK process callback function."
cleanup()
quit QuitFailure
jclient.onShutdown(shutdownCb)
# Create output port
midiPort = jclient.portRegister("midi_in", JACK_DEFAULT_MIDI_TYPE, PortIsInput.ord, 0)
# Activate JACK client ...
if jclient.activate() == 0:
debug "JACK client activated."
# ... and keep running until a signal is received
while not exitSignalled:
sleep(200)
cleanup()
debug "Freeing ringbuffer memory."
ringbufferFree(rb)
when isMainModule:
main()

View File

@ -102,12 +102,12 @@ proc main() =
createThread(midiEventPrinter, midiEventPrinterProc) createThread(midiEventPrinter, midiEventPrinterProc)
# Register JACK callbacks # Register JACK callbacks
if jclient.setProcessCallback(processCb) != 0: if jclient.setProcessCallback(processCb, nil) != 0:
error "Could not set JACK process callback function." error "Could not set JACK process callback function."
cleanup() cleanup()
quit QuitFailure quit QuitFailure
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb, nil)
# 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.ord, 0)

View File

@ -107,12 +107,12 @@ proc main() =
createThread(midiEventPrinter, midiEventPrinterProc) createThread(midiEventPrinter, midiEventPrinterProc)
# Register JACK callbacks # Register JACK callbacks
if jclient.setProcessCallback(processCb) != 0: if jclient.setProcessCallback(processCb, nil) != 0:
error "Could not set JACK process callback function." error "Could not set JACK process callback function."
cleanup() cleanup()
quit QuitFailure quit QuitFailure
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb, nil)
# 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.ord, 0)

View File

@ -60,10 +60,10 @@ else:
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) != 0: if jclient.setPortConnectCallback(portConnected, nil) != 0:
error "Error: could not set JACK port connection callback." error "Error: could not set JACK port connection callback."
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb, nil)
if jclient.activate() == 0: if jclient.activate() == 0:
while not exitSignalled: while not exitSignalled:

View File

@ -43,7 +43,7 @@ else:
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) jclient.onShutdown(shutdownCb, nil)
while not exitSignalled: while not exitSignalled:
sleep(50) sleep(50)

View File

@ -98,7 +98,7 @@ if jclient.setProcessCallback(processCb, osc.addr) != 0:
cleanup() cleanup()
quit QuitFailure quit QuitFailure
jclient.onShutdown(shutdownCb) jclient.onShutdown(shutdownCb, nil)
# 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.ord, 0)

View File

@ -328,51 +328,51 @@ void jack_internal_client_close (const char *client_name)
# ------------------------------- Callbacks ------------------------------- # ------------------------------- Callbacks -------------------------------
proc setProcessThread*(client: ClientP; threadCallback: ThreadCallback; arg: pointer = nil): cint {. proc setProcessThread*(client: ClientP; threadCallback: ThreadCallback; arg: pointer): cint {.
importc: "jack_set_process_thread".} importc: "jack_set_process_thread".}
proc setThreadInitCallback*(client: ClientP; threadInitCallback: ThreadInitCallback; arg: pointer = nil): cint {. proc setThreadInitCallback*(client: ClientP; threadInitCallback: ThreadInitCallback; arg: pointer): cint {.
importc: "jack_set_thread_init_callback".} importc: "jack_set_thread_init_callback".}
proc onShutdown*(client: ClientP; shutdownCallback: ShutdownCallback; arg: pointer = nil) {. proc onShutdown*(client: ClientP; shutdownCallback: ShutdownCallback; arg: pointer) {.
importc: "jack_on_shutdown".} importc: "jack_on_shutdown".}
proc onInfoShutdown*(client: ClientP; shutdownCallback: InfoShutdownCallback; arg: pointer = nil) {. proc onInfoShutdown*(client: ClientP; shutdownCallback: InfoShutdownCallback; arg: pointer) {.
importc: "jack_on_info_shutdown".} importc: "jack_on_info_shutdown".}
proc setProcessCallback*(client: ClientP; processCallback: ProcessCallback; arg: pointer = nil): cint {. proc setProcessCallback*(client: ClientP; processCallback: ProcessCallback; arg: pointer): cint {.
importc: "jack_set_process_callback".} importc: "jack_set_process_callback".}
proc setFreewheelCallback*(client: ClientP; freewheelCallback: FreewheelCallback; arg: pointer = nil): cint {. proc setFreewheelCallback*(client: ClientP; freewheelCallback: FreewheelCallback; arg: pointer): cint {.
importc: "jack_set_freewheel_callback".} importc: "jack_set_freewheel_callback".}
proc setBufferSizeCallback*(client: ClientP; bufsizeCallback: BufferSizeCallback; arg: pointer = nil): cint {. proc setBufferSizeCallback*(client: ClientP; bufsizeCallback: BufferSizeCallback; arg: pointer): cint {.
importc: "jack_set_buffer_size_callback".} importc: "jack_set_buffer_size_callback".}
proc setSampleRateCallback*(client: ClientP; srateCallback: SampleRateCallback; arg: pointer = nil): cint {. proc setSampleRateCallback*(client: ClientP; srateCallback: SampleRateCallback; arg: pointer): cint {.
importc: "jack_set_sample_rate_callback".} importc: "jack_set_sample_rate_callback".}
proc setClientRegistrationCallback*(client: ClientP; registrationCallback: ClientRegistrationCallback; proc setClientRegistrationCallback*(client: ClientP; registrationCallback: ClientRegistrationCallback;
arg: pointer = nil): cint {. arg: pointer): cint {.
importc: "jack_set_client_registration_callback".} importc: "jack_set_client_registration_callback".}
proc setPortRegistrationCallback*(client: ClientP; registrationCallback: PortRegistrationCallback; proc setPortRegistrationCallback*(client: ClientP; registrationCallback: PortRegistrationCallback;
arg: pointer = nil): cint {. arg: pointer): cint {.
importc: "jack_set_port_registration_callback".} importc: "jack_set_port_registration_callback".}
proc setPortConnectCallback*(client: ClientP; connectCallback: PortConnectCallback; arg: pointer = nil): cint {. proc setPortConnectCallback*(client: ClientP; connectCallback: PortConnectCallback; arg: pointer): cint {.
importc: "jack_set_port_connect_callback".} importc: "jack_set_port_connect_callback".}
proc setPortRenameCallback*(client: ClientP; renameCallback: PortRenameCallback; arg: pointer = nil): cint {. proc setPortRenameCallback*(client: ClientP; renameCallback: PortRenameCallback; arg: pointer): cint {.
importc: "jack_set_port_rename_callback".} importc: "jack_set_port_rename_callback".}
proc setGraphOrderCallback*(client: ClientP; graphCallback: GraphOrderCallback; a3: pointer): cint {. proc setGraphOrderCallback*(client: ClientP; graphCallback: GraphOrderCallback; a3: pointer): cint {.
importc: "jack_set_graph_order_callback".} importc: "jack_set_graph_order_callback".}
proc setXrunCallback*(client: ClientP; xrunCallback: XRunCallback; arg: pointer = nil): cint {. proc setXrunCallback*(client: ClientP; xrunCallback: XRunCallback; arg: pointer): cint {.
importc: "jack_set_xrun_callback".} importc: "jack_set_xrun_callback".}
proc setLatencyCallback*(client: ClientP; latencyCallback: LatencyCallback; arg: pointer = nil): cint {. proc setLatencyCallback*(client: ClientP; latencyCallback: LatencyCallback; arg: pointer): cint {.
importc: "jack_set_latency_callback".} importc: "jack_set_latency_callback".}
@ -617,7 +617,7 @@ proc getTime*(): Time {.importc: "jack_get_time".}
proc releaseTimebase*(client: ClientP): cint {.importc: "jack_release_timebase".} proc releaseTimebase*(client: ClientP): cint {.importc: "jack_release_timebase".}
# int jack_set_sync_callback (jack_client_t *client, JackSyncCallback sync_callback, void *arg) # int jack_set_sync_callback (jack_client_t *client, JackSyncCallback sync_callback, void *arg)
proc setSyncCallback*(client: ClientP; syncCallback: SyncCallback; arg: pointer = nil): cint {. proc setSyncCallback*(client: ClientP; syncCallback: SyncCallback; arg: pointer): cint {.
importc: "jack_set_sync_callback".} importc: "jack_set_sync_callback".}
# int jack_set_sync_timeout (jack_client_t *client, jack_time_t timeout) # int jack_set_sync_timeout (jack_client_t *client, jack_time_t timeout)
@ -628,7 +628,7 @@ proc setSyncTimeout*(client: ClientP; timeout: Time): cint {.importc: "jack_set_
# JackTimebaseCallback timebase_callback, # JackTimebaseCallback timebase_callback,
# void *arg) # void *arg)
proc setTimebaseCallback*(client: ClientP; conditional: cint; timebaseCallback: TimebaseCallback; proc setTimebaseCallback*(client: ClientP; conditional: cint; timebaseCallback: TimebaseCallback;
arg: pointer = nil): cint {. arg: pointer): cint {.
importc: "jack_set_timebase_callback".} importc: "jack_set_timebase_callback".}
# int jack_transport_locate (jack_client_t *client, jack_nframes_t frame) # int jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
@ -722,7 +722,7 @@ proc removeProperties*(client: ClientP, subject: Uuid): cint {.importc: "jack_re
proc removeAllProperties*(client: ClientP): cint {.importc: "jack_remove_all_properties".} proc removeAllProperties*(client: ClientP): cint {.importc: "jack_remove_all_properties".}
# int jack_set_property_change_callback (jack_client_t* client, JackPropertyChangeCallback callback, void* arg) # int jack_set_property_change_callback (jack_client_t* client, JackPropertyChangeCallback callback, void* arg)
proc setPropertyChangeCallback*(client: ClientP, callback: PropertyChangeCallback, arg: pointer = nil): cint {. proc setPropertyChangeCallback*(client: ClientP, callback: PropertyChangeCallback, arg: pointer): cint {.
importc: "jack_set_property_change_callback".} importc: "jack_set_property_change_callback".}

3
tests/.gitignore vendored
View File

@ -1,3 +0,0 @@
*
!/**/
!*.*