-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_roundtrip_test.nim
146 lines (130 loc) · 4.47 KB
/
memory_roundtrip_test.nim
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import src/nimplugin
import std/[tables]
var params*: seq[Parameter] = @[]
# params.add(newBoolParameter("bool", true, uint32(0)))
## if just one bool, saved buffer should be
## 0 * 4 (uint32) * 4 (four of them) (version)
## 0 0 0 0 (tree, uint32)
## 0 0 0 0 0 0 0 18 (tree length)
## 0 0 0 1 (parameter, uint32)
## 0 0 0 0 0 0 0 6 (param length, uint64 since it's not part of the data block)
## 0 0 0 0 (param id, uint32)
## 2 (bool)
## 255 (true)
# params.add(newIntParameter("int", -1, 53, 31, uint32(1)))
# params.add(newFloatParameter("float", 0.0, 1.0, 0.33333333, uint32(2), 10))
# for i in 1 .. 10:
# if i mod 3 == 0:
# params.add(newIntParameter($i & "int", -i, i, i, uint32(i)))
# else:
# params.add(newFloatParameter($i & "float", 0.0, float(i), 0.3333 + 0.01 * float(i), uint32(i), 10))
params.add newFloatParameter(
"Level",
-48,
24,
0,
0'u32,
smLerp,
100,
)
params.add newFloatParameter(
"Flip",
0,
1,
0,
1'u32,
smLerp,
100,
)
params.add newFloatParameter(
"Rotate",
-1,
1,
0,
2'u32,
smLerp,
100,
)
var id_map* = params.id_table()
var save_handlers: Table[uint32, proc (plugin: ptr Plugin, data: ptr UncheckedArray[byte], data_length: uint64, offset: uint64): void]
save_handlers[0'u32] = nim_plug_load_handle_tree
save_handlers[1'u32] = nim_plug_load_handle_parameter
var plug: ptr Plugin = cast[ptr Plugin](alloc0(Plugin.sizeof))
plug.params = params
plug.id_map = id_map
plug.save_handlers = save_handlers
plug.dsp_param_data = @[]
plug.ui_param_data = @[]
for i in 0 ..< len(plug.params):
plug.dsp_param_data.add(ParameterValue(
param: plug.params[i],
kind: plug.params[i].kind
))
plug.ui_param_data.add(ParameterValue(
param: plug.params[i],
kind: plug.params[i].kind
))
for i in 0 ..< len(plug.params):
var p = plug.params[i]
case p.kind:
of pkFloat:
var remapped = if p.f_remap != nil:
p.f_remap(p.f_default)
else:
p.f_default
plug.dsp_param_data[i].f_raw_value = p.f_default
plug.dsp_param_data[i].f_value = remapped
plug.ui_param_data[i].f_raw_value = p.f_default
plug.ui_param_data[i].f_value = remapped
of pkInt:
var remapped = if p.i_remap != nil:
p.i_remap(p.i_default)
else:
p.i_default
plug.dsp_param_data[i].i_raw_value = p.i_default
plug.dsp_param_data[i].i_value = remapped
plug.ui_param_data[i].i_raw_value = p.i_default
plug.ui_param_data[i].i_value = remapped
of pkBool:
plug.dsp_param_data[i].b_value = plug.params[i].b_default
plug.ui_param_data[i].b_value = plug.params[i].b_default
var buf_size = nim_plug_save_param_tree_size(plug) + 16 # version
echo(buf_size)
var buffer: ptr UncheckedArray[byte] = cast[ptr UncheckedArray[byte]](alloc0(buf_size))
nim_plug_save_main(plug, buffer)
var plug_read: ptr Plugin = cast[ptr Plugin](alloc0(Plugin.sizeof))
plug_read.params = params
plug_read.id_map = id_map
plug_read.save_handlers = save_handlers
plug_read.dsp_param_data = @[]
plug_read.ui_param_data = @[]
for i in 0 ..< len(plug_read.params):
plug_read.dsp_param_data.add(ParameterValue(
param: plug_read.params[i],
kind: plug_read.params[i].kind
))
plug_read.ui_param_data.add(ParameterValue(
param: plug_read.params[i],
kind: plug_read.params[i].kind
))
discard nim_plug_load_main(plug_read, buffer, buf_size)
for i in 0 ..< len(plug.params):
echo "--"
case plug.params[i].kind:
of pkFloat:
echo plug.ui_param_data[i].f_raw_value
echo plug_read.ui_param_data[i].f_raw_value
echo (plug.ui_param_data[i].f_raw_value - plug_read.ui_param_data[i].f_raw_value) * 1000
of pkInt:
echo plug.ui_param_data[i].i_raw_value
echo plug_read.ui_param_data[i].i_raw_value
echo plug.ui_param_data[i].i_raw_value - plug_read.ui_param_data[i].i_raw_value
of pkBool:
echo plug.ui_param_data[i].b_value
echo plug_read.ui_param_data[i].b_value
echo plug.ui_param_data[i].b_value == plug_read.ui_param_data[i].b_value
echo "--"
for i in 0 ..< buf_size:
stdout.write(buffer[i])
stdout.write(" ")
# echo(read_as[uint64](buffer, 20))