Skip to content

Commit 68ffd68

Browse files
committed
use cookie for parameter pointer caching
1 parent 9246e49 commit 68ffd68

File tree

3 files changed

+37
-41
lines changed

3 files changed

+37
-41
lines changed

src/clap/extensions/parameters.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ pub fn makeParameters(comptime Plugin: type) *const c.clap_plugin_params_t {
2929
return false;
3030

3131
const state = clap.State.fromClap(clap_plugin);
32-
const param = state.plugin.parameters.?.slice[index].*;
32+
const param = state.plugin.parameters.?.slice[index];
3333

34-
switch (param) {
35-
inline else => |p| {
34+
switch (param.*) {
35+
inline else => |*p| {
3636
info.?.* = .{
3737
.id = index,
38-
.default_value = @TypeOf(p).toFloat(p.options.default),
39-
.min_value = @TypeOf(p).toFloat(p.options.min),
40-
.max_value = @TypeOf(p).toFloat(p.options.max),
38+
.default_value = @TypeOf(p.*).toFloat(p.options.default),
39+
.min_value = @TypeOf(p.*).toFloat(p.options.min),
40+
.max_value = @TypeOf(p.*).toFloat(p.options.max),
4141
.flags = 0,
42+
.cookie = p,
4243
};
4344

4445
if (p.options.automatable)
@@ -130,7 +131,7 @@ pub fn makeParameters(comptime Plugin: type) *const c.clap_plugin_params_t {
130131
pub fn flush(clap_plugin: [*c]const c.clap_plugin_t, in: [*c]const c.clap_input_events_t, _: [*c]const c.clap_output_events_t) callconv(.c) void {
131132
for (0..in.?.*.size.?(in)) |i| {
132133
const event = in.?.*.get.?(in, @intCast(i)).?;
133-
clap.processEvent(Plugin, clap_plugin, event);
134+
clap.State.fromClap(clap_plugin).handleEvent(event);
134135
}
135136
}
136137
};

src/clap/extensions/state.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub const state = c.clap_plugin_state{
2626
};
2727

2828
// TODO: make Writer and Reader buffered
29-
// TODO: move msgpack serialization to core
3029

3130
pub const Writer = struct {
3231
clap_stream: *const c.clap_ostream,

src/clap/root.zig

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,34 @@ pub const State = struct {
147147
null,
148148
);
149149
}
150-
};
151150

152-
pub fn processEvent(comptime Plugin: type, clap_plugin: *const c.clap_plugin_t, event: *const c.clap_event_header_t) void {
153-
const state = State.fromClap(clap_plugin);
154-
switch (event.type) {
155-
c.CLAP_EVENT_PARAM_VALUE => {
156-
std.debug.assert(@hasDecl(Plugin, "Parameters"));
157-
158-
const value_event: *const c.clap_event_param_value = @ptrCast(@alignCast(event));
159-
const param = state.plugin.parameters.?.slice[value_event.param_id];
160-
161-
switch (param.*) {
162-
inline else => |*p| {
163-
const value = @TypeOf(p.*).fromFloat(value_event.value);
164-
p.set(value);
165-
zigplug.log.debug("parameter '{s}' = {any}", .{ p.options.name, value });
166-
},
151+
// TODO: handle param_mod
152+
pub fn handleParamEvent(self: *State, event: *const c.clap_event_param_value) void {
153+
const param: *zigplug.Parameter = blk: {
154+
if (event.cookie) |ptr|
155+
break :blk @ptrCast(@alignCast(ptr))
156+
else {
157+
@branchHint(.unlikely);
158+
break :blk self.plugin.parameters.?.slice[event.param_id];
167159
}
168-
},
169-
else => {},
160+
};
161+
162+
switch (param.*) {
163+
inline else => |*p| {
164+
const value = @TypeOf(p.*).fromFloat(event.value);
165+
p.set(value);
166+
zigplug.log.debug("parameter '{s}' = {any}", .{ p.options.name, value });
167+
},
168+
}
170169
}
171-
}
170+
171+
pub fn handleEvent(self: *State, event: *const c.clap_event_header_t) void {
172+
switch (event.type) {
173+
c.CLAP_EVENT_PARAM_VALUE => self.handleParamEvent(@ptrCast(@alignCast(event))),
174+
else => {},
175+
}
176+
}
177+
};
172178

173179
fn ClapPlugin(comptime Plugin: type) type {
174180
return extern struct {
@@ -226,15 +232,11 @@ fn ClapPlugin(comptime Plugin: type) type {
226232

227233
const event_count = clap_process.*.in_events.*.size.?(clap_process.*.in_events);
228234

229-
var event_i: u32 = 0;
230-
231-
while (event_i < event_count) {
232-
const event = clap_process.*.in_events.*.get.?(clap_process.*.in_events, event_i);
233-
event_i += 1;
235+
for (0..event_count) |i| {
236+
const event = clap_process.*.in_events.*.get.?(clap_process.*.in_events, @intCast(i));
234237
switch (event.*.type) {
235238
c.CLAP_EVENT_PARAM_VALUE => {
236239
const value_event: *const c.clap_event_param_value = @ptrCast(@alignCast(event));
237-
const param = state.plugin.parameters.?.slice[value_event.param_id];
238240

239241
if (comptime Plugin.meta.sample_accurate_automation) {
240242
end = value_event.header.time;
@@ -248,15 +250,9 @@ fn ClapPlugin(comptime Plugin: type) type {
248250
end = samples;
249251
}
250252

251-
switch (param.*) {
252-
inline else => |*p| {
253-
const value = @TypeOf(p.*).fromFloat(value_event.value);
254-
p.set(value);
255-
zigplug.log.debug("parameter '{s}' = {any} at {}", .{ p.options.name, value, value_event.header.time });
256-
},
257-
}
253+
state.handleParamEvent(value_event);
258254
},
259-
else => continue,
255+
else => state.handleEvent(event),
260256
}
261257
}
262258

0 commit comments

Comments
 (0)