nymph/examples/amp.nim

83 lines
1.8 KiB
Nim

import std/math
import nymph
const PluginUri = "urn:nymph:examples:amp"
type
CSample = cfloat
CSamples = UncheckedArray[CSample]
PortIndex = enum
INPUT = 0,
OUTPUT = 1,
GAIN = 2
Amp = object
input: ptr CSamples
output: ptr CSamples
gain: ptr cfloat
template db2coeff(db: cfloat): cfloat =
pow(10.0, db / 20.0)
proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble;
bundlePath: cstring; features: ptr ptr Lv2Feature):
Lv2Handle {.cdecl.} =
return createShared(Amp)
proc connectPort(instance: Lv2Handle; port: cuint;
dataLocation: pointer) {.cdecl.} =
let amp = cast[ptr Amp](instance)
case cast[PortIndex](port)
of INPUT:
amp.input = cast[ptr CSamples](dataLocation)
of OUTPUT:
amp.output = cast[ptr CSamples](dataLocation)
of GAIN:
amp.gain = cast[ptr cfloat](dataLocation)
proc activate(instance: Lv2Handle) {.cdecl.} =
discard
proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} =
let gain = cast[ptr Amp](instance)
for pos in 0 ..< nSamples:
gain.output[pos] = gain.input[pos] * db2coeff(gain.gain[])
proc deactivate(instance: Lv2Handle) {.cdecl.} =
discard
proc cleanup(instance: Lv2Handle) {.cdecl.} =
freeShared(cast[ptr Amp](instance))
proc extensionData(uri: cstring): pointer {.cdecl.} =
return nil
proc NimMain() {.cdecl, importc.}
proc lv2Descriptor(index: cuint): ptr Lv2Descriptor {.
cdecl, exportc, dynlib, extern: "lv2_descriptor".} =
NimMain()
if index == 0:
result = createShared(Lv2Descriptor)
result.uri = cstring(PluginUri)
result.instantiate = instantiate
result.connectPort = connectPort
result.activate = activate
result.run = run
result.deactivate = deactivate
result.cleanup = cleanup
result.extensionData = extensionData