-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiditranspose_plugin.nim
More file actions
106 lines (80 loc) · 3.16 KB
/
miditranspose_plugin.nim
File metadata and controls
106 lines (80 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
## A simple MIDI transpose LV2 plugin
import std/math
#import std/strformat
#import std/strutils
import nymph/[atom, core, midi, urid, util]
import nymph/atom/util
const PluginUri = "urn:nymph:examples:miditranspose"
type
PluginPort {.pure.} = enum
Input, Output, Transposition
MidiTransposePlugin = object
input: ptr AtomSequence
output: ptr AtomSequence
transposition: ptr cfloat
map: ptr UridMap
midi_urid: Urid
MidiEvent = object
size: uint32
frames: int64
data: ptr UncheckedArray[byte]
proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble;
bundlePath: cstring; features: ptr UncheckedArray[ptr Lv2Feature]):
Lv2Handle {.cdecl.} =
try:
let plug: ptr MidiTransposePlugin = createShared(MidiTransposePlugin)
plug.map = cast[ptr UridMap](lv2FeaturesData(features, lv2UridMap))
if plug.map.isNil:
freeShared(plug)
return cast[Lv2Handle](nil)
plug.midi_urid = plug.map.map(plug.map.handle, lv2MidiMidiEvent)
return cast[Lv2Handle](plug)
except OutOfMemDefect:
return cast[Lv2Handle](nil)
proc connectPort(instance: Lv2Handle; port: cuint;
dataLocation: pointer) {.cdecl.} =
let plug = cast[ptr MidiTransposePlugin](instance)
case cast[PluginPort](port)
of PluginPort.Input:
plug.input = cast[ptr AtomSequence](dataLocation)
of PluginPort.Output:
plug.output = cast[ptr AtomSequence](dataLocation)
of PluginPort.Transposition:
plug.transposition = cast[ptr cfloat](dataLocation)
proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} =
let plug = cast[ptr MidiTransposePlugin](instance)
let outCapacity = plug.output.atom.size
atomSequenceClear(plug.output)
plug.output.atom.type = plug.input.atom.type
if not atomSequenceIsEmpty(plug.input):
#echo &"Event sequence size: {plug.input.atom.size}"
let noteOffset = clamp(floor(plug.transposition[] + 0.5), -12, 12).uint8
for ev in plug.input:
if ev.body.`type` == plug.midi_urid:
var msg = cast[ptr UncheckedArray[uint8]](atomContents(AtomEvent, ev))
#echo &"0x{toHex(msg[0], 2)} 0x{toHex(msg[1], 2)} 0x{toHex(msg[2], 2)}"
case midiGetMessageType(msg[]):
of midiMsgNoteOff, midiMsgNoteOn, midiMsgNotePressure:
msg[1] = clamp(msg[1] + noteOffset, 0, 127).uint8
else:
discard
discard atomSequenceAppendEvent(plug.output, outCapacity, ev)
proc cleanup(instance: Lv2Handle) {.cdecl.} =
freeShared(cast[ptr MidiTransposePlugin](instance))
proc NimMain() {.cdecl, importc.}
let descriptor = Lv2Descriptor(
uri: cstring(PluginUri),
instantiate: instantiate,
connectPort: connectPort,
activate: nil,
run: run,
deactivate: nil,
cleanup: cleanup,
extensionData: nil,
)
proc lv2Descriptor(index: cuint): ptr Lv2Descriptor {.
cdecl, dynlib, exportc: "lv2_descriptor".} =
if index == 0:
NimMain()
return addr(descriptor)
return nil