2022-10-10 03:48:36 +02:00
|
|
|
import std/[logging, math, os]
|
|
|
|
import signal
|
|
|
|
import jacket
|
|
|
|
|
|
|
|
var jclient: ClientTPtr
|
2022-10-10 19:45:13 +02:00
|
|
|
var outPort: PortTPtr
|
2022-10-10 03:48:36 +02:00
|
|
|
var status: cint
|
|
|
|
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
|
|
|
|
|
|
|
|
type
|
|
|
|
SampleT = DefaultAudioSampleT
|
|
|
|
JackBufferPtr = ptr UncheckedArray[SampleT]
|
|
|
|
const
|
|
|
|
tableSize = 4096
|
|
|
|
sineFreq = 440.0
|
|
|
|
twoPi = Pi * 2.0
|
|
|
|
|
|
|
|
type
|
|
|
|
SineOsc = object
|
|
|
|
waveform: array[0..tableSize, float]
|
|
|
|
phase: float
|
|
|
|
idxInc: float
|
|
|
|
SineOscPtr = ref SineOsc
|
|
|
|
|
|
|
|
proc initSineOsc(sr: float, freq: float): SineOsc =
|
|
|
|
let phsInc = twoPi / tableSize
|
|
|
|
var phase = 0.0
|
|
|
|
|
|
|
|
for i in 0 ..< tableSize:
|
|
|
|
result.waveform[i] = sin(phase)
|
|
|
|
phase += phsInc
|
|
|
|
|
|
|
|
result.phase = 0.0
|
|
|
|
result.idxInc = tableSize / sr * freq
|
|
|
|
|
|
|
|
proc tick(osc: SineOscPtr): float =
|
|
|
|
result = osc.waveform[int(osc.phase)]
|
|
|
|
osc.phase += osc.idxInc;
|
|
|
|
|
|
|
|
if osc.phase >= tableSize:
|
|
|
|
osc.phase -= tableSize
|
|
|
|
|
|
|
|
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 cleanup(sig: cint = 0) {.noconv.} =
|
|
|
|
debug "Cleaning up..."
|
|
|
|
|
|
|
|
if jclient != nil:
|
|
|
|
discard jclient.deactivate()
|
|
|
|
discard jclient.clientClose()
|
|
|
|
jclient = nil
|
|
|
|
|
|
|
|
quit QuitSuccess
|
|
|
|
|
|
|
|
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
|
2022-10-10 19:45:13 +02:00
|
|
|
debug "JACK server has shut down"
|
2022-10-10 03:48:36 +02:00
|
|
|
cleanup()
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
proc processCb(nFrames: NFramesT, arg: pointer): cint {.cdecl.} =
|
|
|
|
var outbuf = cast[JackBufferPtr](portGetBuffer(outPort, nFrames))
|
2022-10-10 03:48:36 +02:00
|
|
|
let osc = cast[SineOscPtr](arg)
|
|
|
|
|
|
|
|
for i in 0 ..< nFrames:
|
|
|
|
outbuf[i] = osc.tick() * 0.2
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
addHandler(log)
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Create JACK client
|
2022-10-10 03:48:36 +02:00
|
|
|
setErrorFunction(errorCb)
|
2022-10-10 19:45:13 +02:00
|
|
|
jclient = clientOpen("jacket_sine", NoStartServer.ord or UseExactName.ord, status.addr)
|
|
|
|
debug "JACK server status: " & $status
|
2022-10-10 03:48:36 +02:00
|
|
|
|
|
|
|
if jclient == nil:
|
|
|
|
error getJackStatusErrorString(status)
|
|
|
|
quit 1
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Create sine oscillator
|
2022-10-10 03:48:36 +02:00
|
|
|
let sampleRate =(float) jclient.getSampleRate()
|
2022-10-10 19:45:13 +02:00
|
|
|
debug "JACK sample rate: " & $sampleRate
|
2022-10-10 03:48:36 +02:00
|
|
|
var osc = initSineOsc(sampleRate, sineFreq)
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Set up signal handlers to clean up on exit
|
2022-10-10 03:48:36 +02:00
|
|
|
when defined(windows):
|
|
|
|
setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
|
|
|
|
else:
|
|
|
|
setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Register JACK callbacks
|
|
|
|
if jclient.setProcessCallback(processCb, osc.addr) != 0:
|
|
|
|
error "Could not set JACK process callback function."
|
2022-10-10 03:48:36 +02:00
|
|
|
cleanup()
|
|
|
|
|
|
|
|
jclient.onShutdown(shutdownCb, nil)
|
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Create output port
|
|
|
|
outPort = jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
2022-10-10 03:48:36 +02:00
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Activate JACK client ...
|
2022-10-10 03:48:36 +02:00
|
|
|
if jclient.activate() == 0:
|
2022-10-10 19:45:13 +02:00
|
|
|
# ... and keep running until a signal is received
|
2022-10-10 03:48:36 +02:00
|
|
|
while true:
|
|
|
|
sleep(50)
|
|
|
|
|
|
|
|
cleanup() # normally not reached
|