Compare commits
3 Commits
f3a6b660d6
...
227ab3f45d
Author | SHA1 | Date |
---|---|---|
Christopher Arndt | 227ab3f45d | |
Christopher Arndt | 8964ad7052 | |
Christopher Arndt | 61b0405f3e |
|
@ -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, nil) != 0:
|
if jclient.setProcessCallback(processCb) != 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, nil)
|
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.ord, 0)
|
||||||
|
|
|
@ -0,0 +1,156 @@
|
||||||
|
# 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()
|
|
@ -102,12 +102,12 @@ proc main() =
|
||||||
createThread(midiEventPrinter, midiEventPrinterProc)
|
createThread(midiEventPrinter, midiEventPrinterProc)
|
||||||
|
|
||||||
# Register JACK callbacks
|
# Register JACK callbacks
|
||||||
if jclient.setProcessCallback(processCb, nil) != 0:
|
if jclient.setProcessCallback(processCb) != 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, nil)
|
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.ord, 0)
|
||||||
|
|
|
@ -107,12 +107,12 @@ proc main() =
|
||||||
createThread(midiEventPrinter, midiEventPrinterProc)
|
createThread(midiEventPrinter, midiEventPrinterProc)
|
||||||
|
|
||||||
# Register JACK callbacks
|
# Register JACK callbacks
|
||||||
if jclient.setProcessCallback(processCb, nil) != 0:
|
if jclient.setProcessCallback(processCb) != 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, nil)
|
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.ord, 0)
|
||||||
|
|
|
@ -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, nil) != 0:
|
if jclient.setPortConnectCallback(portConnected) != 0:
|
||||||
error "Error: could not set JACK port connection callback."
|
error "Error: could not set JACK port connection callback."
|
||||||
|
|
||||||
jclient.onShutdown(shutdownCb, nil)
|
jclient.onShutdown(shutdownCb)
|
||||||
|
|
||||||
if jclient.activate() == 0:
|
if jclient.activate() == 0:
|
||||||
while not exitSignalled:
|
while not exitSignalled:
|
||||||
|
|
|
@ -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, nil)
|
jclient.onShutdown(shutdownCb)
|
||||||
|
|
||||||
while not exitSignalled:
|
while not exitSignalled:
|
||||||
sleep(50)
|
sleep(50)
|
||||||
|
|
|
@ -98,7 +98,7 @@ if jclient.setProcessCallback(processCb, osc.addr) != 0:
|
||||||
cleanup()
|
cleanup()
|
||||||
quit QuitFailure
|
quit QuitFailure
|
||||||
|
|
||||||
jclient.onShutdown(shutdownCb, nil)
|
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.ord, 0)
|
||||||
|
|
|
@ -328,51 +328,51 @@ void jack_internal_client_close (const char *client_name)
|
||||||
|
|
||||||
# ------------------------------- Callbacks -------------------------------
|
# ------------------------------- Callbacks -------------------------------
|
||||||
|
|
||||||
proc setProcessThread*(client: ClientP; threadCallback: ThreadCallback; arg: pointer): cint {.
|
proc setProcessThread*(client: ClientP; threadCallback: ThreadCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_process_thread".}
|
importc: "jack_set_process_thread".}
|
||||||
|
|
||||||
proc setThreadInitCallback*(client: ClientP; threadInitCallback: ThreadInitCallback; arg: pointer): cint {.
|
proc setThreadInitCallback*(client: ClientP; threadInitCallback: ThreadInitCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_thread_init_callback".}
|
importc: "jack_set_thread_init_callback".}
|
||||||
|
|
||||||
proc onShutdown*(client: ClientP; shutdownCallback: ShutdownCallback; arg: pointer) {.
|
proc onShutdown*(client: ClientP; shutdownCallback: ShutdownCallback; arg: pointer = nil) {.
|
||||||
importc: "jack_on_shutdown".}
|
importc: "jack_on_shutdown".}
|
||||||
|
|
||||||
proc onInfoShutdown*(client: ClientP; shutdownCallback: InfoShutdownCallback; arg: pointer) {.
|
proc onInfoShutdown*(client: ClientP; shutdownCallback: InfoShutdownCallback; arg: pointer = nil) {.
|
||||||
importc: "jack_on_info_shutdown".}
|
importc: "jack_on_info_shutdown".}
|
||||||
|
|
||||||
proc setProcessCallback*(client: ClientP; processCallback: ProcessCallback; arg: pointer): cint {.
|
proc setProcessCallback*(client: ClientP; processCallback: ProcessCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_process_callback".}
|
importc: "jack_set_process_callback".}
|
||||||
|
|
||||||
proc setFreewheelCallback*(client: ClientP; freewheelCallback: FreewheelCallback; arg: pointer): cint {.
|
proc setFreewheelCallback*(client: ClientP; freewheelCallback: FreewheelCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_freewheel_callback".}
|
importc: "jack_set_freewheel_callback".}
|
||||||
|
|
||||||
proc setBufferSizeCallback*(client: ClientP; bufsizeCallback: BufferSizeCallback; arg: pointer): cint {.
|
proc setBufferSizeCallback*(client: ClientP; bufsizeCallback: BufferSizeCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_buffer_size_callback".}
|
importc: "jack_set_buffer_size_callback".}
|
||||||
|
|
||||||
proc setSampleRateCallback*(client: ClientP; srateCallback: SampleRateCallback; arg: pointer): cint {.
|
proc setSampleRateCallback*(client: ClientP; srateCallback: SampleRateCallback; arg: pointer = nil): 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): cint {.
|
arg: pointer = nil): 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): cint {.
|
arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_port_registration_callback".}
|
importc: "jack_set_port_registration_callback".}
|
||||||
|
|
||||||
proc setPortConnectCallback*(client: ClientP; connectCallback: PortConnectCallback; arg: pointer): cint {.
|
proc setPortConnectCallback*(client: ClientP; connectCallback: PortConnectCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_port_connect_callback".}
|
importc: "jack_set_port_connect_callback".}
|
||||||
|
|
||||||
proc setPortRenameCallback*(client: ClientP; renameCallback: PortRenameCallback; arg: pointer): cint {.
|
proc setPortRenameCallback*(client: ClientP; renameCallback: PortRenameCallback; arg: pointer = nil): 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): cint {.
|
proc setXrunCallback*(client: ClientP; xrunCallback: XRunCallback; arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_xrun_callback".}
|
importc: "jack_set_xrun_callback".}
|
||||||
|
|
||||||
proc setLatencyCallback*(client: ClientP; latencyCallback: LatencyCallback; arg: pointer): cint {.
|
proc setLatencyCallback*(client: ClientP; latencyCallback: LatencyCallback; arg: pointer = nil): 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): cint {.
|
proc setSyncCallback*(client: ClientP; syncCallback: SyncCallback; arg: pointer = nil): 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): cint {.
|
arg: pointer = nil): 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): cint {.
|
proc setPropertyChangeCallback*(client: ClientP, callback: PropertyChangeCallback, arg: pointer = nil): cint {.
|
||||||
importc: "jack_set_property_change_callback".}
|
importc: "jack_set_property_change_callback".}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
*
|
||||||
|
!/**/
|
||||||
|
!*.*
|
Loading…
Reference in New Issue