2022-10-10 03:48:36 +02:00
|
|
|
import std/[logging, math, os]
|
|
|
|
import signal
|
|
|
|
import jacket
|
|
|
|
|
2023-05-22 05:13:31 +02:00
|
|
|
var jclient: ClientP
|
|
|
|
var outPort: PortP
|
2022-10-10 03:48:36 +02:00
|
|
|
var status: cint
|
2022-10-10 20:35:00 +02:00
|
|
|
var exitSignalled: bool = false
|
2022-10-10 03:48:36 +02:00
|
|
|
var log = newConsoleLogger(when defined(release): lvlInfo else: lvlDebug)
|
|
|
|
|
|
|
|
type
|
2023-05-22 05:13:31 +02:00
|
|
|
Sample = DefaultAudioSample
|
|
|
|
JackBufferP = ptr UncheckedArray[Sample]
|
2022-10-10 03:48:36 +02:00
|
|
|
const
|
|
|
|
tableSize = 4096
|
|
|
|
sineFreq = 440.0
|
|
|
|
twoPi = Pi * 2.0
|
|
|
|
|
|
|
|
type
|
|
|
|
SineOsc = object
|
|
|
|
waveform: array[0..tableSize, float]
|
|
|
|
phase: float
|
|
|
|
idxInc: float
|
2023-05-22 05:13:31 +02:00
|
|
|
SineOscP = ref SineOsc
|
2022-10-10 03:48:36 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-22 05:13:31 +02:00
|
|
|
proc tick(osc: SineOscP): float =
|
2022-10-10 03:48:36 +02:00
|
|
|
result = osc.waveform[int(osc.phase)]
|
|
|
|
osc.phase += osc.idxInc;
|
|
|
|
|
|
|
|
if osc.phase >= tableSize:
|
|
|
|
osc.phase -= tableSize
|
|
|
|
|
2022-10-10 20:35:00 +02:00
|
|
|
proc cleanup() =
|
2022-10-10 03:48:36 +02:00
|
|
|
debug "Cleaning up..."
|
|
|
|
if jclient != nil:
|
|
|
|
discard jclient.deactivate()
|
|
|
|
discard jclient.clientClose()
|
|
|
|
jclient = nil
|
|
|
|
|
2022-10-10 20:35:00 +02:00
|
|
|
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
|
2022-10-10 03:48:36 +02:00
|
|
|
|
|
|
|
proc shutdownCb(arg: pointer = nil) {.cdecl.} =
|
2022-10-10 20:35:00 +02:00
|
|
|
info "JACK server has shut down."
|
|
|
|
exitSignalled = true
|
2022-10-10 03:48:36 +02:00
|
|
|
|
2023-05-22 05:13:31 +02:00
|
|
|
proc processCb(nFrames: NFrames, arg: pointer): cint {.cdecl.} =
|
|
|
|
var outbuf = cast[JackBufferP](portGetBuffer(outPort, nFrames))
|
|
|
|
let osc = cast[SineOscP](arg)
|
2022-10-10 03:48:36 +02:00
|
|
|
|
|
|
|
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)
|
2022-10-10 21:01:21 +02:00
|
|
|
quit QuitFailure
|
2022-10-10 03:48:36 +02:00
|
|
|
|
2022-10-10 19:45:13 +02:00
|
|
|
# Create sine oscillator
|
2022-10-10 21:01:21 +02:00
|
|
|
let sampleRate = jclient.getSampleRate.float
|
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):
|
2022-10-10 20:35:00 +02:00
|
|
|
setSignalProc(signalCb, SIGABRT, SIGINT, SIGTERM)
|
2022-10-10 03:48:36 +02:00
|
|
|
else:
|
2022-10-10 20:35:00 +02:00
|
|
|
setSignalProc(signalCb, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
|
2022-10-10 03:48:36 +02:00
|
|
|
|
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()
|
2022-10-10 21:01:21 +02:00
|
|
|
quit QuitFailure
|
2022-10-10 03:48:36 +02:00
|
|
|
|
2023-06-06 02:40:02 +02:00
|
|
|
jclient.onShutdown(shutdownCb)
|
2022-10-10 03:48:36 +02:00
|
|
|
|
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 20:35:00 +02:00
|
|
|
while not exitSignalled:
|
2022-10-10 03:48:36 +02:00
|
|
|
sleep(50)
|
|
|
|
|
2022-10-10 20:35:00 +02:00
|
|
|
cleanup()
|